Answered step by step
Verified Expert Solution
Question
1 Approved Answer
by using JAVA Exercise: Consider a superclass PurchaseItem which models customers purchases. This class has: two private instance variables name (String) and unitPrice (double). One
by using JAVA Exercise:
Consider a superclass PurchaseItem which models customers purchases. This class has:
- two private instance variables name (String) and unitPrice (double).
- One constructor to initialize the instance variables.
- A default constructor to initialize name to no item, and unitPrice to 0. use this()
- A method getPrice that returns the unitPrice.
- Accessor and mutator methods.
- A print method to return the name of the item followed by @ symbol, then the unitPrice.
Consider two subclasses WeighedItem and CountedItem.
WeighedItem has an additional instance variable weight (double) in Kg while CountedItem has an additional variable quantity (int) both private.
- Write an appropriate constructor for each of the classes making use of the constructor of the superclass in defining those of the subclasses.
- Override getPrice method that returns the price of the purchasedItem based on its unit price and weight (WeighedItem), or quantity (CountedItem). Make use of getPrice of the superclass
- Override also toString method for each class making use of the toString method of the superclass in defining those of the subclasses.
toString should return something that can be printed on the receipt.
For example
Banana @ 3.00 1.37Kg 4.11 SR (in case of WeighedItem class)
Pens @ 4.5 10 units 45 SR (in case of CountedItem class)
Write an application class where you construct objects from the two subclasses and print them on the screen.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started