How do I write this with java doc? Book.java public class Book { private String
ID: 3863309 • Letter: H
Question
How do I write this with java doc?
Book.java
public class Book {
private String title;
private String author;
private int numberOfRatings;
private int totalRating;
private float price;
private boolean hasHardCover;
public Book(String title, String author){
this.title = title;
this .author = author;
numberOfRatings = 0;
totalRating = 0;
price = (float) (Math.random()*10);
hasHardCover = false;
}
public Book (String title, String author, float price, boolean hasHardCover){
this.title = title;
this .author = author;
numberOfRatings = 0;
totalRating = 0;
this.price = price;
this.hasHardCover = hasHardCover;
}
public void addRating(int rating){
totalRating += rating;
numberOfRatings ++;
}
public float findAvgRating() {
if (numberOfRatings == 0)
return 0;
return ((float)totalRating)/numberOfRatings;
}
public String bookRecommendation(){
float avgRating = findAvgRating();
if (avgRating>=3 && avgRating<=4)
return "Strongly Recomended";
if (avgRating >= 2 && avgRating <3)
return "Recommended";
else if (avgRating >=1 && avgRating<2)
return "Not Recommended";
else if (avgRating == 0)
return "No Information Is Available For Recommendation";
return "UNEXPECTED DAATA FOUND";
}
@Override
public String toString(){
return "Book title: " + title
+ " Book author:" + author
+ " Number of ratings: " + numberOfRatings
+ " Avg rating: " + findAvgRating()
+ " Price: " + price
+ " " + bookRecommendation();
}
}
Driver.java
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.showMessageDialog;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Sam
*/
public class Driver {
public Book getInput() {
Book b1;
String title =JOptionPane.showInputDialog("Title");
String author =JOptionPane.showInputDialog("Author");
String choice =JOptionPane.showInputDialog("Do you want to add more info");
if (choice.startsWith("y")){
String price =JOptionPane.showInputDialog("Price");
String hasHardCover =JOptionPane.showInputDialog("Has hard cover?");
b = new Book(title, author, Float.parseFloat(price), hasHardCover.equalsIgnoreCase("yes"));
}
else
b1 = new Book(title, author);
choice =JOptionPane.showInputDialog("Do you want to add rating info");
while (choice.startsWith("y")){
int rating =Integer.parseInt(JOptionPane.showInputDialog("add rating"));
if(rating>=1 && rating <= 4)
b1.addRating(rating);
else
JOptionPane.showMessageDialog(null, "Invalid rating", "Error", JOptionPane.ERROR_MESSAGE);
choice =JOptionPane.showInputDialog("Do you want to add rating info");
}
return b1;
}
public static void main(String[] args) {
Book b = new Driver().getInput();
JOptionPane.showMessageDialog(null, b.toString(), "details", JOptionPane.INFORMATION_MESSAGE);
}
}
Explanation / Answer
Please find below the program code with java doc
PROGRAM CODE:
Book.java
package GUI;
/**
*
* @author Sam
* @version 0.1
*
* Representation of a book with title, author, number of ratings received,
* total ratings, price and whether the book has hard cover or not
*/
public class Book {
private String title;
private String author;
private int numberOfRatings;
private int totalRating;
private float price;
private boolean hasHardCover;
/**
* constructor of the class that initiates the title and author with the value in parameters.
* The remaining properties are assigned default values
*
* @param title - represents the title of the book
* @param author - represents the author of the book
*/
public Book(String title, String author){
this.title = title;
this .author = author;
numberOfRatings = 0;
totalRating = 0;
price = (float) (Math.random()*10);
hasHardCover = false;
}
/**
* Constructor of the class with values such as title, author, price and hard cover indicator
* @param title - string - represents the title of the book
* @param author - string - represents the author of the book
* @param price - float - represents the price of the book
* @param hasHardCover - boolean - indicates whether the book has hard cover or not
*/
public Book (String title, String author, float price, boolean hasHardCover){
this.title = title;
this .author = author;
numberOfRatings = 0;
totalRating = 0;
this.price = price;
this.hasHardCover = hasHardCover;
}
/**
* method that increases the number of ratings count and also calculates the rating total
* @param rating - int - represents the rating given for the book by a single entity
*/
public void addRating(int rating){
totalRating += rating;
numberOfRatings ++;
}
/**
* returns 0 if the number of ratings is zero else returns the average of ratings
* @return average rating value - float
*/
public float findAvgRating() {
if (numberOfRatings == 0)
return 0;
return ((float)totalRating)/numberOfRatings;
}
/**
* returns a string value describing whether the book is recommended or not
* @return string - recommendation comment
*/
public String bookRecommendation(){
float avgRating = findAvgRating();
if (avgRating>=3 && avgRating<=4)
return "Strongly Recomended";
if (avgRating >= 2 && avgRating <3)
return "Recommended";
else if (avgRating >=1 && avgRating<2)
return "Not Recommended";
else if (avgRating == 0)
return "No Information Is Available For Recommendation";
return "UNEXPECTED DAATA FOUND";
}
/**
* returns the entire information of the book in a single string
*/
@Override
public String toString(){
return "Book title: " + title
+ " Book author:" + author
+ " Number of ratings: " + numberOfRatings
+ " Avg rating: " + findAvgRating()
+ " Price: " + price
+ " " + bookRecommendation();
}
}
Driver.java
package GUI;
import javax.swing.JOptionPane;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Driver class that tests the Book class by collecting information from the user.
* @author Sam
*/
public class Driver {
public Book getInput() {
Book b1;
String title =JOptionPane.showInputDialog("Title");
String author =JOptionPane.showInputDialog("Author");
String choice =JOptionPane.showInputDialog("Do you want to add more info");
if (choice.startsWith("y")){
String price =JOptionPane.showInputDialog("Price");
String hasHardCover =JOptionPane.showInputDialog("Has hard cover?");
b1 = new Book(title, author, Float.parseFloat(price), hasHardCover.equalsIgnoreCase("yes"));
}
else
b1 = new Book(title, author);
choice =JOptionPane.showInputDialog("Do you want to add rating info");
while (choice.startsWith("y")){
int rating =Integer.parseInt(JOptionPane.showInputDialog("add rating"));
if(rating>=1 && rating <= 4)
b1.addRating(rating);
else
JOptionPane.showMessageDialog(null, "Invalid rating", "Error", JOptionPane.ERROR_MESSAGE);
choice =JOptionPane.showInputDialog("Do you want to add rating info");
}
return b1;
}
public static void main(String[] args) {
Book b = new Driver().getInput();
JOptionPane.showMessageDialog(null, b.toString(), "details", JOptionPane.INFORMATION_MESSAGE);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.