Question: Take a look at the Product class - it needs to store a number of different elements. you need to modify this class so that

Take a look at the Product class - it needs to store a number of different elements. you need to modify this class so that it can correctly store:

  • A product name
  • A product type
  • An inventory code as a String
  • A price per unit for the product (in pennies)

This first part is a simplified version of the Product class you worked on last week, so you can reuse your work there in this week's project.

Once you have that working, you need to code up the following methods:

  • toString
  • equals
  • hashCode

The toString method should display the title, the inventory code, the type and the price per unit as comma-separated values in parentheses. For example, the following product:

Name = "Casablanca" Inventory Code = "C0000007" Type = "DVD" Price = "995"

Should be return the following String from toString():

(Casablanca, C0000007, DVD, 995)

The equals() method should return true if two Product objects have the same name, inventory code, type and price (false if any of these are different).

The hashCode() method should return the same number for two Product objects that are equal, and preferably a different number for two Product objects that are different. (HINT: Use the hashCode values for the component pieces of the Product class to come up with a hashCode value - see the slides from this week for an example).

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!