Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview These exercises will allow you to have some more practice with the idea of classes. These exercises will ask you to take a skeleton

Overview

These exercises will allow you to have some more practice with the idea of classes. These exercises will ask you to take a skeleton of a class and implement it. The work you do on this class will carry over directly into the next project assignment.

Objectives

  • Practice with programming fundamentals
    • Review of various Java fundamentals (branching, loops, variables, methods, etc.)
    • Practice with Java classes and Object Oriented Programming

Exercise Description

For this project you will need to implement a class to store and retrieve information about products stored in inventory. A skeleton for this class is provided below, as well as a simple test class that lightly tests a few aspects of your implementation:

  • Product.javaimage text in transcribed
  • TestProduct.javaimage text in transcribed

Create a project folder named OOPReporting and import these two classes into that project folder. Take a look at the Product class - it needs to store a number of different elements. For this lab you need to modify this class so that it can correctly store:

  • A product name
  • A product type
  • A price per unit for the product (in pennies)
  • A list of user ratings for the product

(That last one might be a bit tricky to think about - what you want is not a single rating for the product, but a list of all of the ratings that all users have given in. So instead of it just being a "3 star product" you would store a list of all of the ratings - [ 4, 2, 5, 1, 3, 5, 1]. The final rating for the product is the average of all of the user ratings, which can change as new users add new ratings to the product over time.)

To get the test program to function properly, you will need to not just complete the methods used by the test program, you will also need to define new private member variables to store the information that the setters are storing and the getters are reading. You will also need to define a no-argument constructor that assigns default values for a new product.

Once you get the test code working, follow the Submission Instructions below and then read the next Project description - you can start working on the next project immediately if you have time left in the lab.

Product.java

/**

* A simple class for holding product information.

*

* @author PUT YOUR NAME HERE

* @version PUT THE DATE HERE

*/

public class Product {

// Private member variables go here - you will need to add them yourself.

/**

* Product constructor - creates a default product with no name or type and

* a price of 0.

*/

public Product() {

}

/**

* sets the name of the product object

*

* @param name

* - new name for the product

*/

public void setName(String name) {

}

/**

* returns the name of the product

*

* @return the name of the product

*/

public String getName() {

return "";

}

/**

* sets the type of the product

*

* @param type

* - the type of the product

*/

public void setType(String type) {

}

/**

* returns the type of the product

*

* @return - the product type

*/

public String getType() {

return "";

}

/**

* sets the price of the product in cents. A $10 product will have a price

* of 1000.

*

* @param price

* - the price of the product

*/

public void setPrice(int price) {

}

/**

* returns the price of the product in cents.

*

* @return the price of the product

*/

public int getPrice() {

return 0;

}

/**

* sets the count of this product in our inventory.

*

* @param quantity

* - the number of this product in inventory

*/

public void setQuantity(int quantity) {

}

/**

* returns the count of this product in our inventory

*

* @return the number of this product in inventory

*/

public int getQuantity() {

return 0;

}

/**

* sets the inventory code for this product

*

* @param code

* - the new inventory code for the product

*/

public void setInventoryCode(String code) {

}

/**

* returns the inventory code for this product

*

* @return the inventory code of the product

*/

public String getInventoryCode() {

return "";

}

/**

* NOTE: Each individual rating is stored with the product, so you need to

* maintain a list of user ratings. This method should append a new rating

* to the end of that list

*

* @param rating

* - the new rating to add to this product

*/

public void addUserRating(int rating) {

}

/**

* NOTE: See note on addUserRating above. This method should be written to

* allow you to access an individual value from the list of user ratings

*

* @param index

* - the index of the rating we want to see

*

* @return the rating indexed by the value index

*/

public int getUserRating(int index) {

return 0;

}

/**

* NOTE: See note on addUserRating above. This method should be written to

* return the total number of ratings this product has associated with it

*

* @return the number of ratings associated with this product

*/

public int getUserRatingCount() {

return 0;

}

/**

* NOTE: see note on addUserRating above. This method should be written to

* compute the average user rating on demand from a stored list of ratings.

*

* @return the average rating for this product as a whole integer value (use

* integer math)

*/

public int getAvgUserRating() {

return 0;

}

}

TestProduct.java

/**

* A simple program to test some of the functionality of the Product class. Note

* that this is NOT meant to be exhaustive.

*

*

*

*/

public class TestProduct {

public static void main(String[] args) {

Product p = new Product();

System.out.println("TESTING NAME");

System.out.println("------------");

String trueName = "Product 01";

p.setName(trueName);

String testName = p.getName();

if (!trueName.equals(testName)) {

System.out.println("ERROR - problem with setName or getName");

System.out.println("Name should be: " + trueName);

System.out.println("Name returned: " + testName);

}

System.out.println();

System.out.println("TESTING TYPE");

System.out.println("------------");

String trueType = "MyType";

p.setName(trueType);

String testType = p.getType();

if (!trueType.equals(testType)) {

System.out.println("ERROR - problem with setType or getType");

System.out.println("Type should be: " + trueType);

System.out.println("Type returned: " + testType);

}

System.out.println();

System.out.println("TESTING PRICE");

System.out.println("-------------");

int truePrice = 3000;

p.setPrice(truePrice);

int testPrice = p.getPrice();

if (truePrice != testPrice) {

System.out.println("ERROR - problem with setPrice or getPrice");

System.out.println("Price should be: " + truePrice);

System.out.println("Price returned: " + testPrice);

}

System.out.println();

System.out.println("TESTING PRICE");

System.out.println("-------------");

int rating0 = 3;

int rating1 = 4;

p.addUserRating(rating0);

p.addUserRating(rating1);

int testRating1 = p.getUserRating(1);

if (testRating1 != rating1) {

System.out.println(

"ERROR - problem with addUserRating or getUserRating");

System.out.println("Rating should be: " + rating1);

System.out.println("Rating returned: " + testRating1);

}

}

Transcribed image text

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Experts Guide To SQL

Authors: Frank Lusardi

1st Edition

0070390029, 978-0070390027

More Books

Students also viewed these Databases questions

Question

Briefly describe the MFEP.

Answered: 1 week ago