This is java. Design and implement a class called Book that contains instance da
ID: 3774456 • Letter: T
Question
This is java.
Design and implement a class called Book that contains instance data for the title, author, publisher, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted , multi-line description of the book.
Books.txt
Create a books.txt file that contains the following dta
Java Software Solutions
Lewis & Loftus
Addison-Welsey
2012
Faces in Time
Lewis Aldeman
Megalodon Entertainment
2010
Purple Cow
Seth Godin
Portfolio
2002
Create a driver class called BookShelf, whose main method does the following,1) using a while loop- until all data is read from the file: Scan data in from book.txt. create instance of book (from the data scanned in), add book to an ArrayList.2)Using a while loop: Index each element of the ArrayList and print to screen.
Explanation / Answer
BookShelf.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class BookShelf {
public static void main(String arg[]) throws FileNotFoundException{
File file = new File("D:\books.txt");
if(file.exists()){
Scanner scan = new Scanner(file);
ArrayList<Books> list = new ArrayList<Books>();
while(scan.hasNextLine()){
list.add(new Books(scan.nextLine(),scan.nextLine(),scan.nextLine(),Integer.parseInt(scan.nextLine())));
}
System.out.println("Books details: ");
System.out.println();
int i =0;
while(i<list.size()){
System.out.println(list.get(i));
i++;
}
}
else{
System.out.println("File does not exist");
}
}
}
Books.java
public class Books {
private String title, author, publisher;
private int copyrightDate;
public Books(String title, String author, String publisher, int copyrightDate){
this.title = title;
this.author = author;
this.publisher = publisher;
this.copyrightDate = copyrightDate;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getCopyrightDate() {
return copyrightDate;
}
public void setCopyrightDate(int copyrightDate) {
this.copyrightDate = copyrightDate;
}
public String toString(){
return "Title: "+getTitle()+" "+"Author: "+getAuthor()+" "+"Publisher: "+getPublisher()+" "+"Copyright Date: "+getCopyrightDate()+" ";
}
}
Output:
Books details:
Title: Java Software Solutions
Author: Lewis & Loftus
Publisher: Addison-Welsey
Copyright Date: 2012
Title: Faces in Time
Author: Lewis Aldeman
Publisher: Megalodon Entertainment
Copyright Date: 2010
Title: Purple Cow
Author: Seth Godin
Publisher: Portfolio
Copyright Date: 2002
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.