Quiz
public class Book {
private static ArrayList<Book> library = new ArrayList<Book>();
private String title;
private int id;
private static int count;
public static long time;
public Book(String title) {
this.title = title;
int var = count;
this.id = count;
Book.count++;
startTime();
}
public void startTime(){
time = System.nanoTime();
}
public long shelfLife(){
return (System.nanoTime() - time);
}
public String toString() {
return id + ". " + title;
}
// public int getShelfLife(){
// return shelfLife;
// }
public static void main(String[] args) {
Book[] books = { // Use Array iniitialization to add book
new Book("Barron's Computer Science \"A\""), // Set a new Book object as array element.
new Book("Angels and Demons"),
new Book("Lion, Witch, and a Wardrobe")
};
for (Book book : books) { // Use Foreach syntax to iterate over array
System.out.println(book); // this is same as book.toString()
System.out.println("Shelf Life" + book.shelfLife());
}
System.out.println("Book count: " + Book.count);
// System.out.println(books[0].time);
// System.out.println("Shelf Life" + books[0].shelfLife());
}
}
Book.main(null);
public class Novel extends Book {
private String author;
private int numCheckouts;
public Novel(String title, String author){
super(title);
this.author = author;
startTime();
}
public String getAuthor(){
return author;
}
public void setAuthor(String author){
this.author = author;
}
public String toString(){
return super.toString() + " written by " + author;
}
public static void main(String[] args){
Novel[] novels = {
new Novel("The Great Gatsby", "F. Scott Fitzgerald"),
new Novel("Harry Potter", "J.K. Rowling"),
new Novel("Everything Everything", "Nicola Yoon")
};
for (Novel novel : novels){
System.out.println(novel);
System.out.println("Shelf Life: " + novel.shelfLife());
}
// System.out.println(novels[0].time);
// System.out.println("Shelf Life: " + novels[0].shelfLife());
}
}
public class Textbook extends Book {
private String pubCompany;
public Textbook(String title, String pubCompany){
super(title);
this.pubCompany = pubCompany;
startTime();
}
public String getPubCompany(){
return pubCompany;
}
public void setPubCompany (String pubCompany){
this.pubCompany = pubCompany;
}
public static void main(String[] args){
Textbook[] textbooks = {
new Textbook("The Great Gatsby", "F. Scott Fitzgerald"),
new Textbook("Harry Potter", "J.K. Rowling"),
new Textbook("Everything Everything", "Nicola Yoon")
};
for (Textbook textbook : textbooks){
System.out.println(textbook);
System.out.println("Shelf Life: " + textbook.shelfLife());
}
// System.out.println(textbooks[0].time);
// System.out.println("Shelf Life: " + textbooks[].shelfLife());
}
}
Novel.main(null);
Textbook.main(null);