Test

4/20 test

  • toc:true - badges: true
  • comments: true
  • categories: [jupyter]
public class Book {
    private String title;
    private int id; 
    private static int count;
    private long firstShelved;

    public Book() {}

    // 1. 1 argument constructor
    public Book(String title) {
        this.title = title;

        // 3. generate id
        this.id = (int) (Math.random()*1000);

        // 4. time when book enters library
        this.firstShelved = System.currentTimeMillis();
    }

    // 4. public get count
    public static int getCount(){
        return count;
    }

    public long getShelfLife() {
        return System.currentTimeMillis() - this.firstShelved;
    }

    public boolean isExpired() {
        if(this.getShelfLife()/1000 > 3) {
            return true;
        }
        return false;
    }

    // 2. toString
    public String toString() {
        return ("id = " + this.id + ", title = " + this.title + ", shelf life = " + this.getShelfLife() + ", expired = " + this.isExpired());
    }

    // 2. tester method
    public static void main(String[] args) {
        // 5. generate 2 books, output id and title, count of books in library
        Book book1 = new Book("The Grapes of Wrath");
        Book book2 = new Book("East of Eden");
        
        Book[] library = {book1, book2};

        for(int i = 0; i < library.length; i++) {
            System.out.println(library[i].toString());
            count++;
        }

        System.out.println(getCount());
    }

}

Book.main(null);
id = 59, title = The Grapes of Wrath, shelf life = 1, expired = false
id = 473, title = East of Eden, shelf life = 16, expired = false
2

Question 2

extended Classes (Part 2) Try to use alternate forms of loops and techniques for construction.

  1. Ensure Novel and Textbook run the Constructor from Book.
  2. Create instance variables unique to Novel has Author, Textbook has publishing company. New items are not required by Constructor.
  3. Make Getters and Setters for all new items. You can add your own.
  4. Add a time when book entered the library. This should be same for Parent and Subclasses.
  5. Make sure there are getters and setters for items as needed. For instance, be able to set items not required by constructor.
  6. Define tester method to test all items.
public class Novel extends Book {
    // unique instance variables
    private String title;
    private String author;

    public Novel(String title) {
        // 1. run constructor form book
        super(title);
    }

    // 3. getters and setters for new items
    public String getAuthor() {
        return this.author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public static void main(String[] args) {
        Novel test = new Novel("Harry Potter");
        test.setAuthor("JK Rowling");

        // add time to shelf life

        try {
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
          } 
        
        System.out.println(test.toString() + ", author = " + test.getAuthor());

    }
}

public class Textbook extends Book {
    private String publishingCompany;

    public Textbook(String title) {
        super(title);
    }

    public String getPublishingCompany() {
        return this.publishingCompany;
    }

    public void setPublishingCompany(String publishingCompany) {
        this.publishingCompany = publishingCompany;
    }

    public static void main(String[] args) {
        Textbook test = new Textbook("Calc");
        test.setPublishingCompany("Newton Calc Company");

        // add time to shelf life

        try {
            Thread.sleep(5000);
          } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
          } 
        
        System.out.println(test.toString() + ", company = " + test.getPublishingCompany());
    }
}

Novel.main(null);
Textbook.main(null);
id = 571, title = Harry Potter, shelf life = 1000, expired = false, author = JK Rowling
id = 223, title = Calc, shelf life = 5000, expired = true, company = Newton Calc Company