Pop Quiz
public class Book {
protected String title; // The title of the book
public static int count = 0; // total number of Book objects created
public static double time; // The start time of the Book object in nanoseconds
// Constructor for the Book class
public Book(String title) {
this.title = title; // Set the title of the book
this.time = System.nanoTime(); // Set the time when the book is created
count ++; // Number of books goes up each time a new book is added
}
// Getter for String representation of the Book object
public String toString() {
return "Title of Book: " + title; // Return the title of the book
}
// getter for the current count of Book objects
public int getBookCount() {
return count; // Return the count of Book objects
}
// Method to get the elapsed time since the creation of the Book object
public double shelfLife() {
return (System.nanoTime() - time); // Calculates the time since creation
}
public boolean expired() {
return (this.shelfLife() > 5 * 100000000); // Check if the elapsed time is greater than 5 seconds
}
// Tester Method
public static void main(String[] args) {
// Create an array with two Books
Book[] books1 = {
new Book("Starships"),
new Book("Asteroids"),
};
// Print the title and id of each Book
for (Book i : books1) {
System.out.println(i); // Print the title of the book
System.out.println("ID: " + i.hashCode()); // Print the id of the object
System.out.println("Shelf Life: " + i.shelfLife());
}
System.out.println();
System.out.println("Book Count: " + Book.count); // Print the total count of Book objects
}
}
Book.main(null);
class Novel extends Book { // extends to the parent class
private String author;
private int checkouts;
public Novel (String title, String author) {
super(title); // grabs the title from the Book class
this.author = author; // sets the author of the class
this.checkouts = 0; // sets the number of checkouts to 0
}
// Method that gets the author
public String getAuthor() {
return this.author;
}
// setter for the author
public void setAuthor(String author) {
this.author = author;
}
// setter for the time after the checkout
public void checkout() {
checkouts ++;
this.time = System.nanoTime();
}
// getter for the number of checkouts
public int getCheckouts() {
return this.checkouts;
}
// Tester class
public static void main(String[] args){
// creates an array for the novels
Novel[] novels = {
new Novel("The Awakening", "Steven Robbers"),
new Novel("Green Leaves", "Tyler Boulder"),
};
for (Novel i : novels) { // iterates throughout the novels in the array
System.out.println(i);
System.out.println("ID: " + i.hashCode());
System.out.println("Shelf Life: " + i.shelfLife()); // prints the time on the shelf
}
}
}
Novel.main(null);
public class Textbook extends Book { // extends fromt the Book class
private String company; // creates string for the publishing company
Textbook(String title, String company) {
super(title); // extends to the title from the Book class
this.company = company;
}
// getter for the company
public String getCompany() {
return this.company;
}
// setter for the company
public void setCompany(String Company) {
this.company = company;
}
// Tester method
public static void main(String[] args){
// creates array for textbooks
Textbook[] textbooks1 = {
// adds to the array
new Textbook("Calculus AB", "Bernabeo Maths"),
new Textbook("Calculus BC", "Krenz Addition Co."),
};
for (Textbook i : textbooks1) { // iterates through array
System.out.println(i);
System.out.println("ID: " + i.hashCode());
System.out.println("Shelf Life: " + i.shelfLife());
}
}
}
Textbook.main(null);