Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java In the next six questions, write a complete class called Book (described be

ID: 3745920 • Letter: J

Question

Java

In the next six questions, write a complete class called Book (described below).

A book is described by a title, author, ISBN code (see below), and publication year.

For full credit, follow good principles of class design and encapsulation.

I recommend writing the class in your development environment to make sure it compiles and then copying the code into the appropriate question

ISBN code is a unique code assigned to a book. Codes might contain numbers, letters, or dashes. For example, the code for our textbook is 978-0133807806.

Label the quesions in the code as well, thank you.

1: Write the class header and the instance data variables for the Book class.

2: Write a constructor for the Book class. The constructor should initialize the instance data variables using parameters.

3: Write accessor and mutator (getter and setter) methods. Include validity checking where appropriate.

4: Write a toString method to return a nicely formatted, multi-line description of the book.

5: Write one additional complete method that would make sense to in the Book class. (If you want, you can use a simple println statement as the method body, as shown in the lecture videos.)

6: Write code that would go inside of a main method.

The code should create three book objects and invoke one method on each object. Also include a statement to print a text representation of one of the books to the console.

Explanation / Answer

Book.java

public class Book {
//Declaring instance variables
private String ISBN;
private String title;
private String author;
private int year;

//Zero argumented constructor
public Book() {

}

//Parameterized constructor
public Book(String iSBN, String title, String author, int year) {
ISBN = iSBN;
this.title = title;
this.author = author;
this.year = year;
}

// getters and setters
public String getISBN() {
return ISBN;
}

public void setISBN(String iSBN) {
ISBN = iSBN;
}

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 int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Book [ISBN=" + ISBN + ", title=" + title + ", author=" + author +
", year=" + year + "]";
}

//This method will display the book info
public void displayBookInfo() {
System.out.println("ISBN=" + ISBN + ", title=" + title + ", author=" + author +
", year=" + year);
}

}

________________

Test.java

public class Test {

public static void main(String[] args) {

//Creating 3 instances of Book class
Book b1 = new Book("978-0133807806", "Computer Architecture", "Mike James", 1996);
Book b2 = new Book("978-0133807454", "C++ Programming", "M.k.Goel", 2001);
Book b3 = new Book("978-1234545677", "Project Engineering", "Subhash Roy", 2012);

//Displaying the output
System.out.println("Book#1 Info");
System.out.println("Author Name:" + b1.getAuthor());
System.out.println(b1);

System.out.println(" Book#2 Info");
System.out.println("Publication Year:" + b2.getYear());
System.out.println(b2);

System.out.println(" Book#3 Info");
System.out.println("ISBN:" + b3.getISBN());
b3.displayBookInfo();

}

}

___________________

Output:

Book#1 Info
Author Name:Mike James
Book [ISBN=978-0133807806, title=Computer Architecture, author=Mike James, year=1996]

Book#2 Info
Publication Year:2001
Book [ISBN=978-0133807454, title=C++ Programming, author=M.k.Goel, year=2001]

Book#3 Info
ISBN:978-1234545677
ISBN=978-1234545677, title=Project Engineering, author=Subhash Roy, year=2012

___________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote