2.3 Example: Definition of the class Book - version
1
/*
+-------------------+
| Book |
+-------------------+
| String title |
| String authorName |
| int price |
| int year |
+-------------------+
*/
// to represent a book
class Book {
String title;
String authorName;
int price;
int year;
Book(String title, String authorName, int price, int year) {
this.title = title;
this.authorName = authorName;
this.price = price;
this.year = year;
}
}
/* Interactions:
Examples of the use of the constructor for the class Book
Book b1 = new Book("HtDP", "Matthias", 60, 2001);
Book b2 = new Book("Beach Music", "Conroy", 20, 1996);
Examples of the use of the field selectors for the class Book
b1.title -- expected: "HtDP"
b1.authorName -- expected: "Matthias"
b1.price -- expected: 60
b1.year -- expected: 2001
b2.title -- expected: "Beach Music"
b2.authorName -- expected: "Pat Conroy"
b2.price -- expected: 20
b2.year -- expected: 1996
*/