Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Storeltem class that represents an item at a store. Storeltem class has two private fields: name as a string, price as a

Write a Storeltem class that represents an item at a store. Storeltem class has two private fields: name as a

Write a Storeltem class that represents an item at a store. Storeltem class has two private fields: name as a string, price as a double. Here's a sample code that appears in a class other than StoreItem: StoreItem stItem1= new StoreItem("Coffee Maker", 100.0); System.out.println(stItem1.getDiscounted Price (10)); // prints 90.0 stIteml.setPrice (140.0); System.out.println(stItem1.getDiscountedPrice (10)); // prints 126.0 try { stItem1.setPrice(-10); . } System.out.println(stItem1.getPrice()); // should not reach this line catch (Exception e) { System.out.println("Negative price rejected"); // this line reached } System.out.println(stItem1.getPrice()); // prints 140.0, price was not updated try { StoreItem stItem2 new StoreItem("Flannel Sheet Set", -49.99); System.out.println(stItem2.getPrice()); // should not reach this line } catch (Exception e) { System.out.println("Negative price rejected in constructor"); // this line reached } Write the complete class definition of Storeltem class which will produce results as shown in the example above. Specifically, write a constructor with 2 parameters with initial values for the two fields: name and price. The constructor should validate that the price is not negative. If so, it should throw an IllegalArgumentException public accessor and public mutator for the price private field. The mutator should validate that new price is not negative. If so, it should throw an IllegalArgumentException get DiscountedPrice method with one integer parameter giving discount percent value. This method returns the discounted price of the store item for the given discount percent. Make sure there is no code duplication. Edit Format Table

Step by Step Solution

3.40 Rating (156 Votes )

There are 3 Steps involved in it

Step: 1

StoreItemjava public class StoreItem fields private String name private double price twoargument con... 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

Java An Introduction To Problem Solving And Programming

Authors: Walter Savitch

8th Edition

0134462033, 978-0134462035

More Books

Students also viewed these Programming questions

Question

Under what circumstances should derived associations be used?

Answered: 1 week ago