Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm very confused on how to go about this. I was given this sample code to start off, this is Java btw: public class Item

I'm very confused on how to go about this. I was given this sample code to start off, this is Java btw:

public class Item {

private String name; private double price; public static final double TOLERANCE = 0.0000001; public Item(String name,double price) { this.name = name; this.price = price; } public Item() { this("",0.0); } public Item(Item other) { this.name = other.name; this.price = other.price; } public String getName() { return name; } public double getPrice() { return price; } public void setName(String name) { this.name = name; } public void setPrice(double price) { this.price = price; } public void input() { //write code here } public void show() { //write code here }

public String toString() { return "Item: " + name + " Price: " + price; } public boolean equals(Object other) { if(other == null) return false; else if(getClass() != other.getClass()) return false; else { Item otherItem = (Item)other; return(name.equals(otherItem.name) && equivalent(price, otherItem.price)); } }

1. Using the Item class provided to you for download, write a method called input that prompts the user for the items name and price and accepts the appropriate input from the console. Write a method called show that outputs the item information to console. Test your methods by creating a class called TestItem that contains a main method. Your main method should create a default Item object, gets input from the user using the input method of the class, and then outputs the item to the console using the show method.

2. Add a static method to the TestItem class that averages an array of type double and returns the answer as a double. Create an array of type Item that collects the input for 3 items in a loop and calculates the average price. Output the items and average price to the user from the console in the appropriate format (money).

3. Change your Java program so that it tests to see if one of the items is named Peas (not case sensitive) Your program should only output the average that is calculated if one of the items is named Peas, otherwise output no average output.

4. Change your Java program so that it will accept a practically unlimited number of items. To accomplish this you may use an array larger than would be practical. Use 100 for array size if you choose this option. Optionally you may store your items in an ArrayList instead of an array. In either case, for the input let any negative input be your sentinel value for price. Do not include this negative value in determining your average. You will continue to use the feature from step 3, testing if one of the items is named Peas, otherwise output no average output.

5. Add a static method to your Java program in the TestItem class that outputs Welcome to Tri-C in a pop-up message box before the items and prices are output. You should use the showMessageDialog method of Javas JOptionPane class for this task.

6. Add a static method to your TestItem class that takes an array of items and outputs them in reverse order of input. Output your items in both forward and reverse and print the average if one of the items is named Peas in your test runs.

7. Change your Java program so that after it produces its output, it asks users if they would like to process another group of items. The program should terminate when the user enters no (not case sensitive). Otherwise it should continue processing names and prices.

8. Change your Java program so that it will check for duplicate items input and produce an error message that allows the user to re-enter that item.

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_2

Step: 3

blur-text-image_3

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

Database Systems Design Implementation And Management

Authors: Carlos Coronel, Steven Morris

14th Edition

978-0357673034

More Books

Students also viewed these Databases questions

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago