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

The Review class describes the review of a Book. It has following attributes: Th

ID: 638791 • Letter: T

Question

The Review class describes the review of a Book. It has following attributes:

The following constructor should be provided to initialize each attribute:
    public Review()

This constructor initializes numberOfReviews to 0 and sumOfRatings and average to 0.0.

The following method should be provided to increase the number of Reviews by 1 and the sum of Rating by adding the parameter rating. If the number of reviews is greater than 0, then its average (rating) needs to be computed. Otherwise, the average should be set to 0.0.
public void updateRating(double rating)

The following method must be defined:

public String toString()

The toString() method constructs a string of the following form:
Reviews: 0.00(0)

where 0.00 is the average (only two digits after the decimal point should be shown - use DecimalFormat class) and 0 within the parenthesis is the number of reviews. Note that the average and the number of reviews can be changed to some other values.

Book class

The Book class describes a book that a customer can give a review/rating. It must have the following attributes:

The following constructor should be provided to initialize each attribute:
public Book()
This constructor initializes all strings to "?", and instantiates an object of Review (i.e., call the constructor of the Review class to create an object of Review).

The following accessor methods should be provided to get the attributes:
public String getTitle()
public String getPublisher()
public Review getReview()

The following mutator methods should be provided to change the attributes:
public void setTitle(String aTitle)
public void setPublisher(String aPublisher)

The following mutator method should be provided to change the review. It should call updateRating method of the Review class:

public void addRating(double rate)

The following method must be defined:

public String toString()

The toString() method constructs a string of the following format:

Title: Introduction to Java Programming,
Publisher: Pearson,
Reviews: 0.00(0)

You can make use of the DecimalFormat class in java.text package to format the average having two digits after the a decimal point "0.00".

Attribute name Attribute type Description numberOfReviews int the number of Reviews sumOfRatings double the sum of all ratings average double the average of ratings

Explanation / Answer

Review.java

import java.text.DecimalFormat;

public class Review {

private int numberOfReviews;
private double sumOfRatings;
private double average;
DecimalFormat format = new DecimalFormat("0.00");
public Review() {

    numberOfReviews = 0;
    sumOfRatings = 0.0;
    average = 0.0;
}

public Review(int numRev, double sumRat, double average) {

    numberOfReviews = numRev;
    sumOfRatings = sumRat;
    this.average = average;
}

public void updateRating(double rating) {

    numberOfReviews ++;
    sumOfRatings = sumOfRatings + rating;
    if (numberOfReviews > 0) {
        average = sumOfRatings / numberOfReviews;
    }
}

public String toString()
{

    return "Reviews: " + numberOfReviews+ " Rating: " + format.format(average);
}
}

Book.java

import java.text.DecimalFormat;
public class Book {

private String title;
private String publisher;
private Review bookReview;
DecimalFormat format = new DecimalFormat("0.00");

public Book(String title, String publisher, Review bookRev) {

    this.title = title;
    this.publisher = publisher;
    bookReview = bookRev;
}

public Book() {

    title = "?";
    publisher = "?";
    //Review rev = new Review();
    bookReview=new Review();
}

public String getTitle() {
    return title;
}
public String getPublisher() {
    return publisher;
}
public Review getReview() {
    return bookReview;
}
public void setTitle(String aTitle) {
    title = aTitle;
}
public void setPublisher(String aPublisher) {
    publisher = aPublisher;
}
public void addRating(double rate) {
   
    bookReview.updateRating(rate);
}
public String toString() {
    return " " + "Title:" + " " + title + "," + " " +
            "Publisher:" + " " + publisher + "," + " " +
               bookReview + " ";
}
}

bookdetails.java

import java.util.Scanner;

public class bookdetails
{
public static void main (String[] args)
{
   // local variables, can be accessed anywhere from the main method
   char input1 = 'Z';
    String bookTitle;
   String bookPublisher;
   double rating;
   String line = new String();

   // instantiate a Book object
   Book book1 = new Book();

   printMenu();

   //Create a Scanner object to read user input
   Scanner scan = new Scanner(System.in);


   do // will ask for user input
    {
     System.out.println("Enter your choice?");
     line = scan.nextLine();

     if (line.length() == 1)
      {
       input1 = line.charAt(0);
       input1 = Character.toUpperCase(input1);

       // matches one of the case statement
       switch (input1)
        {
         case 'A':   //Add Book
           book1 = new Book();
           System.out.print("Please enter the book information: ");
           System.out.print("Enter a book title: ");
           bookTitle = scan.nextLine();
           book1.setTitle(bookTitle);

           System.out.print("Enter its publisher: ");
           bookPublisher = scan.nextLine();
           book1.setPublisher(bookPublisher);
           break;
         case 'D':   //Display Book
           System.out.print(book1);
           break;
         case 'Q':   //Quit
           break;
         case 'R':   //Add Rating
           System.out.print("Please give a rating of the book: ");
           rating = Double.parseDouble(scan.nextLine());
           book1.addRating(rating);
           break;
         case '?':   //Display Menu
           printMenu();
           break;
         default:
           System.out.print("Unknown action ");
           break;
        }
      }
     } while (input1 != 'Q' || line.length() != 1);
}


/** The method printMenu displays the menu to a user**/
public static void printMenu()
{
System.out.print("Choice Action " +
                    "------ ------ " +
                    "A Add Book " +
                    "D Display Book " +
                    "Q Quit " +
                    "R Give Rating " +
                    "? Display Help ");
}
}

Sample output:

Choice Action

------ ------

A Add Book

D Display Book

Q Quit

R Give Rating

? Display Help

Enter your choice?

a

Please enter the book information:

Enter a book title:

introduction to java

Enter its publisher:

pearson

Enter your choice?

r

Please give a rating of the book:

5

Enter your choice?

r

Please give a rating of the book:

4

Enter your choice?

d

Title: introduction to java,

Publisher: pearson,

Reviews: 2 Rating: 4.50

Enter your choice?

q

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