In: Computer Science
Consider a class Book that contains information about a Book. The class should has the following attributes:
• The title of the book
• The author of the book
• Year of publication (e.g. 1994)
• The number of people that have rated this book as a 1 (Terrible)
• The number of people that have rated this book as a 2 (Bad)
• The number of people that have rated this book as a 3 (OK)
• The number of people that have rated this book as a 4 (Good)
• The number of people that have rated this book as a 5 (Great)
Implement the class with accessors and mutators for the title of the book, author of the book and year of publication. Write a method addRating that takes an integer as an input parameter. The method should verify that the parameter is a number between 1 and 5, and if so, increment by one the number of people rating the book that matches the input parameter. For example, if 3 is the input parameter, then the number of people that rated the book as a 3 should be incremented by one. Write another method, getAverage, that returns the average value for all the book ratings.
Test the class by writing a main method that creates at least two Book objects, adds at least five ratings for each book, and output the title of the book, author of the book, year of publication, and average rating for each book object.
Provide the UML class diagram for the Book class.
Note that the question does not require the use of arrays.
The program should be well-structured and should have a reasonable set of methods in addition to the main method. It should use a good coding style, proper indentation, meaningful identifier names and appropriate comments throughout.
The program should also include a method (eg, UserInfo( )) to output your student details (name, referenceID) at the start of program results.
Devise suitable test data (in addition to the above example) to test your program.
Book.java
public class Book {
private String title, author;
private int yearOfPublication;
private int nRating1, nRating2, nRating3, nRating4, nRating5;
public Book()
{
this.title = "";
this.author = "";
this.yearOfPublication = 0;
this.nRating1 = 0;
this.nRating2 = 0;
this.nRating3 = 0;
this.nRating4 = 0;
this.nRating5 = 0;
}
public Book(String title, String author, int
yearOfPublication)
{
this.title = title;
this.author = author;
this.yearOfPublication = yearOfPublication;
this.nRating1 = 0;
this.nRating2 = 0;
this.nRating3 = 0;
this.nRating4 = 0;
this.nRating5 = 0;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getYearOfPublication() {
return yearOfPublication;
}
public int getnRating1() {
return nRating1;
}
public int getnRating2() {
return nRating2;
}
public int getnRating3() {
return nRating3;
}
public int getnRating4() {
return nRating4;
}
public int getnRating5() {
return nRating5;
}
public void addRating(int rating)
{
switch(rating)
{
case 1:
this.nRating1++;
break;
case 2:
this.nRating2++;
break;
case 3:
this.nRating3++;
break;
case 4:
this.nRating4++;
break;
case 5:
this.nRating5++;
break;
default:
System.out.println("Please enter a number between 1 and 5");
}
}
public double getAverage()
{
return((double)(this.nRating1 + this.nRating2 + this.nRating3 +
this.nRating4 + this.nRating5) / 5);
}
@Override
public String toString()
{
return("Title: " + this.title + ", Author: " + this.author + ",
Year of publication: " + this.yearOfPublication +
"\nNumber of people who rated 1: " + this.nRating1 +
"\nNumber of people who rated 2: " + this.nRating2 +
"\nNumber of people who rated 3: " + this.nRating3 +
"\nNumber of people who rated 4: " + this.nRating4 +
"\nNumber of people who rated 5: " + this.nRating5 +
"\nAverage rating: " + this.getAverage());
}
}
Test.java (Main class)
public class Test {
public static void main(String[]args)
{
UserInfo();
Book book1 = new Book("The Ambassadors", "Henry James",
1903);
book1.addRating(3);
book1.addRating(1);
book1.addRating(3);
book1.addRating(3);
book1.addRating(2);
book1.addRating(5);
book1.addRating(4);
book1.addRating(4);
book1.addRating(5);
book1.addRating(5);
book1.addRating(5);
book1.addRating(3);
book1.addRating(2);
book1.addRating(1);
book1.addRating(3);
Book book2 = new Book("Native Son", "Richard Wright", 1940);
book2.addRating(1);
book2.addRating(2);
book2.addRating(2);
book2.addRating(4);
book2.addRating(4);
book2.addRating(4);
book2.addRating(5);
book2.addRating(5);
book2.addRating(3);
book2.addRating(3);
book2.addRating(4);
book2.addRating(4);
System.out.println("***** BOOK 1 *****\n------------------");
System.out.println(book1.toString());
System.out.println("\n***** BOOK 2
*****\n------------------");
System.out.println(book2.toString());
}
public static void UserInfo()
{
System.out.println("STUDENT DETAILS:\n----------------\nStudent
Reference Id: 199780234"
+ "\nStudent Name: Joseph Gillbert\n");
}
}
******************************************************************* SCREENSHOT ********************************************************
UML Class Diagram for Book class: