Im very confused on how to go about this I was given this sa
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 item’s 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 Java’s 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.
Solution
1.
import java.util.Scanner;
public class Item {
Scanner read=new Scanner(System.in);
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
try
{
System.out.println(\"Enter the name of item\");
setName(read.next());
System.out.println(\"Enter the price of item\");
setPrice(read.nextInt());
}
catch(Exception e)
{
System.out.println(\"pls enter proper name or price!!\");
}
}
public void show()
{
//write code here
System.out.println(\"Name: \"+getName());
System.out.println(\"Price: \"+getPrice());
}
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));
}
}
private boolean equivalent(double price2, double price3) {
// TODO Auto-generated method stub
if(price2==price3)
{
return true;
}
else
{
return false;
}
}}
//TestItem class
public class TestItem
{
public static void main(String args[])
{
Item a=new Item();
a.input();
a.show();
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
2.
public class TestItem
{
public static void main(String args[])
{ Item[] itemlist=new Item[3];
double [] Prices=new double[3];
for(int i=0;i<3;i++)
{
itemlist[i]=new Item();
}
try{
for(int i=0;i<3;i++)
{
itemlist[i].input();
}
}
catch(Exception e)
{
System.out.println(\"pls enter proper name or price \");
System.exit(0); //terminates program on error
}
for(int i=0;i<3;i++)
{
Prices[i]=itemlist[i].getPrice();
}
for(int i=0;i<3;i++)
{
itemlist[i].show();
}
System.out.println(\"average price: \"+averagePrice(Prices));
}
public static double averagePrice(double[] Prices)
{double sum=0;
for(int i=0;i<Prices.length;i++)
{
sum=sum+Prices[i];
}
double average=sum/Prices.length;
return average;
}
}




