Test
public class book { //class name need to be captalized
private String title;
private int id;
public book(){ //need input title
String title = this.title; //this.title should be on the left, and we would set that equal to the input title
int id = this.id;
}
public String getTitle(){
return title;
}
public int getId(){
return id;
}
public String toString(){
return ; //getTitle() - directly put getters' names here
}
public int getCount(){
int count = 0;
int i;
for (i=0; i<book.length; i++){
count++;
}
return count;
}
public static void main(String[] args){
book book1 = new book("Scarlet Letter"); //Book <- class name
book book2 = new book("Animal Farm");
book1.getId();
book1.getTitle();
book2.getId();
book2.getTitle(); //in test method, we need to use System.out.print
String[] library = {"Scarlet Letter", "Animal Farm"};
library.getCount();
}
}
book.main(null);
//revise to make code run
public class Book {
private String title;
private int id;
public Book(String title, int id){
this.title = title;
this.id = id;
}
public String getTitle(){
return title;
}
public int getId(){
return id;
}
public String toString(){
return getTitle() + getId();
}
public static int getCount(String[] arr){
int count = 0;
int i;
for (i=0; i<arr.length; i++){
count++;
}
return count;
}
public static int getCount(Book[] arr){
int count = 0;
int i;
for (i=0; i<arr.length; i++){
count++;
}
return count;
}
public static void main(String[] args){
Book book1 = new Book("Scarlet Letter", 12345);
Book book2 = new Book("Animal Farm", 24680);
System.out.println(book1.getTitle());
System.out.println(book2.getTitle());
System.out.println(book1.getId());
System.out.println(book2.getId());
String[] library = {"Scarlet Letter", "Animal Farm"};
Book[] library2 = {book1, book2};
System.out.println(getCount(library));
System.out.println(getCount(library2));
}
}
Book.main(null);
Part 1 (Friday)
class Book (Part 1) Close Book
- Define 1 argument constructor for title,
- Define toString method for id and title.
- Generate unique id for each object
- Create a public getter that has Book Count
- Define tester method that initializes at least 2 books, outputs id and title, and provides a count of books in library.
//sample tester method code
System.out.println("Libary Book Count: " + Book.getBookCount()); // Notice how this method exist and works before any books are created
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("Libary Book Count: " + Book.getBookCount());
//revised part 1 to use static
public class Book {
private String title;
private int id;
private static int SID = 0; //static (not an instance variable) - a class variable
private long enterTime;
public Book(String title){ //one argument constructor
this.title = title;
this.id = ++SID;
this.enterTime = System.nanoTime(); //add a time when book entered the library using nanoTime()
}
public String getTitle(){
return title;
}
public int getID(){
return id;
}
public long getTime(){
return enterTime;
}
public String toString(){
return getTitle() + getID();
}
public static int getCount(){
return SID;
}
public static void main(String[] args){
System.out.println(Book.getCount()); //class method can be used without creating objects
Book book1 = new Book("Scarlet Letter");
System.out.println(Book.getCount()); //1 book created - would return 1
Book book2 = new Book("Animal Farm");
System.out.println("book 1 title: " + book1.getTitle());
System.out.println("book 2 title: " + book2.getTitle());
System.out.println("book 1 id: " + book1.getID()); //book 1 -> id:1
System.out.println("book 2 id: " + book2.getID()); //book 2 -> id:2
System.out.println("book count: " + Book.getCount()); //static class method (Book.method();)
}
}
Book.main(null);
Part 2
extended Classes (Part 2) Try to use alternate forms of loops and techniques for construction.
- Ensure Novel and Textbook run the Constructor from Book.
- Create instance variables unique to Novel has Author, Textbook has publishing company. New items are not required by Constructor.
- Make Getters and Setters for all new items. You can add your own.
- Add a time when book entered the library. This should be same for Parent and Subclasses.
- Make sure there are getters and setters for items as needed. For instance, be able to set items not required by constructor.
- Define tester method to test all items.
//Alternate for of initializing static data. You can use this to construct objects and get familiar with 2D arrays (Win, Win)
// 2D array initialization
String [][] books = {
{ "e=MC^2 a Biography",
"Pan Books (January 1, 2001)"}, // row 0
{ "The Practice of Programming",
"Addison-Wesley Professional Computing" } // row 1
};
//subclass Novel
public class Novel extends Book { //extends Book class
private String author;
//private long enterTime;
//(moved to Book class)
public Novel(String title){
super(title); //call superclass constructor with parameter title using super()
this.author = author;
//this.enterTime = System.nanoTime(); //add a time when book entered the library using nanoTime()
//(moved to Book class)
}
//new getters and setters
public String getAuthor(){
return author;
}
public void setAuthor(String str){
this.author = str;
}
//toString method
public String toString() {
return super.toString() + "Author: " + getAuthor(); //use Book class toString method through super.toString()
}
public static void main(String[] args){
Novel novel1 = new Novel("novel1");
novel1.setAuthor("people1"); //set author name
System.out.println("novel 1 author: " + novel1.getAuthor());
Novel novel2 = new Novel("novel2");
novel2.setAuthor("people2");
System.out.println("novel 2 author: " + novel2.getAuthor());
System.out.println("novel 1 title: " + novel1.getTitle()); //can also use superclass method
System.out.println("novel 1 enter time: " + novel1.getTime() + " nanoseconds"); //get enter time
System.out.println("novel 2 title: " + novel2.getTitle());
System.out.println("novel 2 enter time: " + novel2.getTime() + " nanoseconds"); //but is different novel having different time?
}
}
Novel.main(null);
public class Textbook extends Book { //extends Book class
private String company;
//private long enterTime;
//(moved to Book class)
public Textbook(String title){
super(title); //call superclass constructor with parameter title using super()
this.company = company;
//this.enterTime = System.nanoTime(); //add a time when book entered the library using nanoTime()
//(moved to Book class)
}
//new getters and setters
public String getCompany(){
return company;
}
public void setCompany(String str){
this.company = str;
}
//toString method
public String toString() {
return super.toString() + getCompany(); //use Book class toString method through super.toString()
}
public static void main(String[] args){
Textbook textbook1 = new Textbook("textbook");
textbook1.setCompany("company1"); //set author name
System.out.println("company 1 name: " + textbook1.getCompany());
}
}
Textbook.main(null);
Part 3
Simulation (Part 3)
- Build a Tester Method that does a Simulation.
- Define a default method in Book that returns "current shelfLife" from date/time of construction. (Hint, think about capturing time in sorts)
- Define shelfLife expiration methods as needed in TextBook and Novel.
- A TextBook has a fixed shelf life based on the date/time of constructor. (Hint, simulation 3 yrs as 3000 nanoseconds)
- A Novel has a computed shelf life of expiration, the simulation should extend shelf life if a certain number of return stamps where placed in the book. (Hint, 3 return stamps renews for an extra year)
- Use a sleep in Java to assist with simulation
- Make a method that looks at book in library and determines if they need to come of the shelf, try to have title and on/off status in output.
// To have a library, you will likely need a Data Structure. Remember this could not be part of an Object, but could still be part of the Class.
ArrayList<Book> library = new ArrayList<Book>();
public class Book {
private String title;
private int id;
private static int SID = 0; //static (not an instance variable) - a class variable
private long currentShelfLife;
public Book(String title){ //one argument constructor
this.title = title;
this.id = SID + 1;
SID++;
this.currentShelfLife = System.nanoTime(); //add a time when book entered the library using nanoTime()
}
public String getTitle(){
return title;
}
public int getID(){
return id;
}
public long getCurrentShelfLife(){
return currentShelfLife;
}
public String toString(){
return getTitle() + getID() + getCurrentShelfLife();
}
public static int getCount(){
return SID;
}
public int extendShelfLife(){
}
public static void main(String[] args){
}
}
Book.main(null);
//broke - remove book from library
//extend shelflife()
//nano.Time - shelflife
//Thread.sleep() - some time to decrease power
//random generate a num between 0-5; 3 checkouts renews for an extra year/full power
//if (shelflife <= 0)
//broke