Question

In: Computer Science

Consider a class Book that contains information about a Book. The class should has the following...

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.

Solutions

Expert Solution

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:


Related Solutions

Consider a class Movie that information about a movie. The class has the following attributes: • ...
Consider a class Movie that information about a movie. The class has the following attributes: • The movie name • The MPAA rating (for example, G, PG, PG-13, R) • The number of people that have rated this movie as a 1 (Terrible) • The number of people that have rated this movie as a 2 (Bad) • The number of people that have rated this movie as a 3 (OK) • The number of people that have rated this...
C++ Consider a class Movie that information about a movie. The class has the following attributes:...
C++ Consider a class Movie that information about a movie. The class has the following attributes: • The movie name • The MPAA rating (for example, G, PG, PG-13, R) • Array of size 5 called Ratings, each index will hold the following. [0] The number of people that have rated this movie as a 1 (Terrible) [1] The number of people that have rated this movie as a 2 (Bad) [2] The number of people that have rated this...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {   ...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {    private String title, author, edition;    private int numPages;    public Book { //part (a) } //default    public Book(String t, String a, String e, int np) { //part (b) }    public String getTitle() {…} //returns the title of this Book    public String getAuthor() {…} //returns the author of this Book    public String getEdition() {…} //returns the edition of this Book...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {   ...
Consider the following incomplete declaration of a Book class for 1a-1c. public class Book {    private String title, author, edition;    private int numPages;    public Book { //part (a) } //default        public Book(String t, String a, String e, int np) { //part (b) }    public String getTitle() {…} //returns the title of this Book    public String getAuthor() {…} //returns the author of this Book    public String getEdition() {…} //returns the edition of this...
. Create a class called Book to represent a book. A Book should include four pieces...
. Create a class called Book to represent a book. A Book should include four pieces of information as instance variables‐a book name, an ISBN number, an author name and a publisher. Your class should have a constructor that initializes the four instance variables. Provide a mutator method and accessor method (query method) for each instance variable. Inaddition, provide a method named getBookInfo that returns the description of the book as a String (the description should include all the information...
The tbl_employee contains information about the employee and its department, and it contains the following fields...
The tbl_employee contains information about the employee and its department, and it contains the following fields (it is possible to also include other missing fields): employee_id, employee_gender ,employee_salary, employee_annual_sallary, department_name, department_location, department head. Make use of the database normalization skills learned to normalize a database to the third normalization form. Organize the columns and tables to reduce data redundancy and arrange attributes based on the relationships. Provide a screen shot of the database design before the normalization. Create a heading...
To be done in Java Consider the partially complete book class given.    Make the following...
To be done in Java Consider the partially complete book class given.    Make the following additions to the book class. Add a getAuthor() and a getTitle() method to the book class. They should take no parameters and should return a String. Change the 3-parameter constructor for the class so that it correctly initializes the member variables. Write an accessor method called getPages(). Add a zero parameter getDetails() method to the Book class. It should assemble the information for a...
By using Python: a. Make a class called Book. The __init__() method for Book should store...
By using Python: a. Make a class called Book. The __init__() method for Book should store two attributes: a title and an author. Make a method called book_info() that prints these two pieces of information, and a method called book_available() that prints a message indicating that the book is available in the library. b. Create an instance called book1 from your class. Print the two attributes individually, and then call both methods. c. Create three different instances from the class,...
Overriding the equals Method File Player.java contains a class that holds information about an athlete: name,...
Overriding the equals Method File Player.java contains a class that holds information about an athlete: name, team, and uniform number. File ComparePlayers.java contains a skeletal program that uses the Player class to read in information about two baseball players and determine whether or not they are the same player. Fill in the missing code in ComparePlayers so that it reads in two players and prints “Same player” if they are the same, “Different players” if they are different. Use the...
Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT