CMSC203, Assignment 2 Summer 2018 Concepts Utilized in this Project UML diagrams
ID: 3908908 • Letter: C
Question
CMSC203, Assignment 2
Summer 2018
Concepts Utilized in this Project
UML diagrams
Menu driven program
Java fundamentals, including decision structures, loops
Constructors, Overloaded constructors
toString method
Formatting output with DecimalFormat
Random Class
Java objects & classes
Overview
Write an application that simulates adding new books to the Amazon database.
The Application repeatedly asks the information of a new book and once the information is gathered, it will be displayed to the user. Note that this application does not save the information of the book.
Information of the book includes its title, author, the number of ratings, the sum of all the rating (the rating given to a book is 1 or 2 or 3 or 4), its price and whether the book has a hardcover or not.
When adding a new book, the user can choose to provide only the book title and its author. The rest of the information will be set to default values by the program: Price to random number between 1 and 10, the number of ratings and the sum of ratings to 0 and no hardcover for the book.
The user can also choose to enter all the book’s information.
A book can have 0 or more than one rating. Rating given to a book is a number between 1 (lowest) and 4 (highest). The number of ratings and the total will be saved in the book’s information.
After information is entered for the book, it will be displayed to the user. The program will also display a recommendation information for the book based on its average rating: If the average rating is between 3 and 4, the book is strongly recommended, between 2 and 3 is recommended, between 1 and 2, not recommended and if there is no rating then no information is available for recommendation.
User Operation
Following is a Sample of program run:
Specifications & Requirements
Design and implement a Java application to satisfy the specified high level requirements from the Overview section. The system is required to do the following:
Data Element Class - Book
Create a class with the given information (fields) in the overview section.
Create a constructor that takes book’s title and author and creates a book instance with the provided information. The number of ratings, Total rating will be set to zero, price is set to random number between 1 and 10 and the book has not hardcover.
Create another constructor that takes the information for the title, author, price and whether the book has hardcover or not and creates a book instance with the given information. The number of ratings, Total rating will be set to zero.
A method addRating that takes the rating for the book and adds it to the total rating as well as incrementing the number of ratings for this book.
A method called findAvgRating that returns the average rating for this book or 0 if there is no rating for the book.
A method called bookRecommendation that returns a string based on the average rating of the book. The book is “strongly recommended” for average rating between 3 and 4, “Recommended” for average rating between 2 and 3(exclusive) , “Not Recommended” for average rating between 1 and 2 (exclusive) , and if there is no rating return “No Information Is Available For Recommendation”.
A toString method that returns the string representation of a Book object: title, author, number of ratings, average rating, price and book recommendation.
Add any necessary getter or setter methods.
Driver Class - Amazon
This is the driver class for Book that contains a main method.
Create a method getInput() that returns a Book object. This method allows the user to enter book information, uses the information to construct a Book object and returns it.
This class contains a main method which continues asking the book information as long as user chooses to enter more information and displays the information of the book to the user. Refer to the program sample run for more clarification.
Add any necessary methods to modularize your code.
Deliverables / Submissions:
Submit a compressed file containing the follow (see below):The Java application (it must compile and run correctly); Javadoc files in a directory; a write-up as specified below.Be sure to review the provided project rubric to understand project expectations.
UML diagram of the Book class
Any assumptions that you are making for this project
In three or more paragraphs, highlights of your learning experience
Each student must submit two compressed (.zip) files to the assignment’s folder on Blackboard for this project with the following contents:
LastNameFirstName_Assignment3_Complete.zip, a compressed file in the zip format, with the following:
Write up (Word document)
UML Diagram (Word or jpg document)
Javadoc (directory)
File1.html (example)
File2.html (example)
src (directory)
File1.java (example)
File2.java (example)
LastNameFirstName_Assignment3_Moss.zip, a compressed file containing one or more Java files:
File1.java (example)
File2.java (example)
This folder should contain Java source files only
Grading Rubric
CMSC 203 Assignment #2
Name _____________________________
Overview:
There are two parts to the rubric. First, the project is graded on whether it compiles, whether it runs without errors, and whether it satisfies the specifications. These points add up to 100. Second, the score is decremented if various additional requirements are not met, e.g., no UML diagram, uses constructs that are not allowed, etc.
PROGRAMMING (100 pts)
Compiles 40 pts _____
Accuracy
Passes private instructor tests 15 pts _____
Execution: runs without errors (either run-time or logic errors) 45 pts _____
Possible Sub-total 100 pts _____
REQUIREMENTS (Subtracts from Programming total)
Documentation:
Documentation within source code is missing or incorrect - 5 pts _____
Description of what class does is missing
Author’s Name, @author, is missing
Methods not commented properly using Javadoc @param, @return
UML diagram is not provided - 5 pts _____
Test cases are not provided - 5 pts _____
Lessons-learned paragraphs are not provided - 5 pts _____
Programming Style:
Incorrect use of indentation, statements, structures - 4 pts _____
User interface
Not clear to user how data is to be entered; UI does not follow requirements - 10 pts _____
Output is easy to understand - 4 pts _____
Design:
Does not use classes specified (Book and Amazon classes) - 6 pts _____
Classes do not have the functionality specified - 5 pts _____
No Existence of 2 class constructors as explained in overview section -4 pts _____
No check for invalid book rating -1 pts _____
No Loop for entering more book information -2 pts _____
No Loop for entering more than one rating for the book -2 pts _____
Average rating does not round to 2 decimal places -1 pts _____
Price is not generated randomly when the price is not provided -1 pts _____
Possible decrements: -60 pts _____
Possible total grade: 100 pts _____
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// Book.java
import java.util.Random;
public class Book {
// attributes
private String title;
private String author;
private int numRatings;
private int totalRating;
private double price;
private boolean hasHardcover;
/**
* constructor with arguments to initialize title and author
*/
public Book(String title, String author) {
this.title = title;
this.author = author;
// default values for other fields
numRatings = 0;
totalRating = 0;
price = new Random().nextInt(10) + 1;// a random number between 1-10
hasHardcover = false;
}
/**
* constructor with arguments to initialize title , author, price and
* hasHardcover fields
*/
public Book(String title, String author, double price, boolean hasHardcover) {
this.title = title;
this.author = author;
this.price = price;
this.hasHardcover = hasHardcover;
// default values
numRatings = 0;
totalRating = 0;
}
/**
* method to add a rating
*/
public void addRating(int rating) {
// validating rating
if (rating >= 1 && rating <= 4) {
// adding to total rating
totalRating += rating;
numRatings++;
}
}
/**
* method to find the average rating
*
* @return the average rating, 0 if no ratings available
*/
public double findAvgRating() {
if (numRatings == 0) {
return 0;
} else {
// finding average rating
double avg = (double) totalRating / numRatings;
return avg;
}
}
/**
* @return a String containing info about the recommendation status
*/
public String bookRecommendation() {
double avgrating = findAvgRating();
if (avgrating == 0) {
return "No Information Is Available For Recommendation";
} else if (avgrating >= 3 && avgrating <= 4) {
return "Strongly recommended";
} else if (avgrating >= 2 && avgrating < 3) {
return "Recommended";
} else {
return "Not Recommended";
}
}
/**
* returns a String containing all book values
*/
public String toString() {
String data = "Title: " + title + " Author: " + author
+ " Number of ratings: " + numRatings;
// rounding avrerage rating into two decimal places
data += String.format(" Average rating: %.2f", findAvgRating());
data += " Price: $" + price + " Recommendation: "
+ bookRecommendation();
return data;
}
// getters and setters
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 getNumRatings() {
return numRatings;
}
public void setNumRatings(int numRatings) {
this.numRatings = numRatings;
}
public int getTotalRating() {
return totalRating;
}
public void setTotalRating(int totalRating) {
this.totalRating = totalRating;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean hasHardcover() {
return hasHardcover;
}
public void setHardcover(boolean hasHardcover) {
this.hasHardcover = hasHardcover;
}
}
// Amazon.java
import java.util.Scanner;
public class Amazon {
// scanner to read user input
private static Scanner scanner = new Scanner(System.in);
/**
* method to let the user enter details about a book, and return a Book
* object created using the entered values
*/
public static Book getInput() {
/**
* getting title and author
*/
System.out.print("Enter book title: ");
String title = scanner.nextLine();
System.out.print("Enter author: ");
String author = scanner.nextLine();
/**
* creating a book
*/
Book book = new Book(title, author);
/**
* asking if user wants to skip entering remaining info
*/
System.out.print("Do you want to skip the other info? (y/n): ");
String choice = scanner.nextLine();
if (choice.equalsIgnoreCase("y")) {
// returning created book
return book;
}
/**
* getting remaining info
*/
System.out.print("Enter price: ");
double price = Double.parseDouble(scanner.nextLine());
book.setPrice(price);
System.out.print("Has hard cover? (y/n): ");
if (scanner.nextLine().equalsIgnoreCase("y")) {
book.setHardcover(true);
}
choice = "y";
/**
* looping until user finish entering all ratings he want
*/
while (choice.equalsIgnoreCase("y")) {
System.out.print("Enter a rating (1-4): ");
int rating = Integer.parseInt(scanner.nextLine());
if (rating >= 1 && rating <= 4) {
book.addRating(rating);
System.out.println("Added!");
} else {
System.out.println("Invalid rating!");
}
System.out.print("want to add more rating? (y/n): ");
choice = scanner.nextLine();
}
return book;
}
public static void main(String[] args) {
// defining an array of books
Book books[] = new Book[100];
// current count of books
int booksCount = 0;
boolean quit = false;
/**
* looping until user wishes to quit
*/
while (!quit) {
/**
* prompting and creating a book, adding to the array, and asking if
* user wants to add more
*/
Book b = getInput();
books[booksCount] = b;
booksCount++;
System.out.println("A book added");
System.out.print("Want to add more books? (y/n): ");
if (!scanner.nextLine().equalsIgnoreCase("y")) {
quit = true;
}
}
/**
* Displaying all entered books
*/
System.out.println(" Added Books: ");
for (int i = 0; i < booksCount; i++) {
System.out.println(books[i]);
System.out.println();
}
}
}
/*OUTPUT*/
Enter book title: Hello
Enter author: Mark
Do you want to skip the other info? (y/n): y
A book added
Want to add more books? (y/n): y
Enter book title: Hey man
Enter author: Oliver
Do you want to skip the other info? (y/n): n
Enter price: 7.55
Has hard cover? (y/n): y
Enter a rating (1-4): 4
Added!
want to add more rating? (y/n): y
Enter a rating (1-4): 3
Added!
want to add more rating? (y/n): y
Enter a rating (1-4): 3
Added!
want to add more rating? (y/n): n
A book added
Want to add more books? (y/n): y
Enter book title: My dear
Enter author: Laurel
Do you want to skip the other info? (y/n): n
Enter price: 7
Has hard cover? (y/n): y
Enter a rating (1-4): 0
Invalid rating!
want to add more rating? (y/n): y
Enter a rating (1-4): 1
Added!
want to add more rating? (y/n): n
A book added
Want to add more books? (y/n): n
Added Books:
Title: Hello
Author: Mark
Number of ratings: 0
Average rating: 0.00
Price: $4.0
Recommendation: No Information Is Available For Recommendation
Title: Hey man
Author: Oliver
Number of ratings: 3
Average rating: 3.33
Price: $7.55
Recommendation: Strongly recommended
Title: My dear
Author: Laurel
Number of ratings: 1
Average rating: 1.00
Price: $7.0
Recommendation: Not Recommended