Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m very confused on how to go about this. I was given this sample code to star

ID: 3888690 • Letter: I

Question

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.

Explanation / Answer

Below is your code. Let me know in comments if you have any issues: -

Item.java

import java.util.Scanner;

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() {

Scanner sc = new Scanner(System.in);

System.out.print("Please enter Item name: ");

this.name = sc.nextLine();

System.out.print("Please enter price: ");

this.price = Double.parseDouble(sc.next());

}

public void show() {

System.out.println(this);

}

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) && this.price == otherItem.price);

}

}

}

TestItem.java

import java.util.ArrayList;

import java.util.Scanner;

import javax.swing.JOptionPane;

public class TestItem {

public static void main(String[] args) {

printWelcome();

Item it = new Item();

it.input();

it.show();

Scanner in = new Scanner(System.in);

Item arr[] = new Item[3];

double price[] = new double[3];

Item item;

for (int i = 0; i < arr.length; i++) {

item = new Item();

item.input();

arr[i] = item;

price[i] = item.getPrice();

}

double avPrice = getAverage(price);

System.out.println("Items are: ");

for (int i = 0; i < arr.length; i++) {

System.out.println(arr[i]);

}

System.out.println();

System.out.println("Average price: $" + avPrice);

System.out.println("----------------Part 3 - Average if Peas ------------------");

printAverage(arr);

System.out.println("-----------------Part 4 -- ArrayList of Items ------------");

String option = "";

do {

ArrayList<Item> items = new ArrayList<>();

System.out.println("Please enter items. Enter -1 in price to stop entering....");

Item ite;

while (true) {

ite = new Item();

ite.input();

if (ite.getPrice() == new Double(-1)) {

break;

} else {

// -------------- Part 8 code added ------------------

if (items.contains(ite)) {

System.err.println("Duplicate item added. Please add a new one.");

} else

items.add(ite);

}

}

System.out.println("Item details are : ");

for (int i = 0; i < items.size(); i++) {

System.out.println(items.get(i));

}

System.out.println();

printAverage(items.toArray(new Item[items.size()]));

System.out.println("------------------------ Part 6 -------------------------");

printItemsReverse(items);

System.out.println("Do you want to process another set of items ? Yes/No");

option = in.next();

} while (!option.equalsIgnoreCase("No"));

}

public static void printWelcome() {

JOptionPane.showMessageDialog(null, "Welcome to Tri-C");

}

public static double getAverage(double arr[]) {

double sum = 0;

for (int i = 0; i < arr.length; i++) {

sum = sum + arr[i];

}

return sum / arr.length;

}

public static void printAverage(Item arr[]) {

boolean isPeas = false;

for (int i = 0; i < arr.length; i++) {

if (arr[i].getName().equalsIgnoreCase("Peas")) {

isPeas = true;

break;

}

}

if (!isPeas) {

System.out.println("No Average output");

} else {

double sum = 0;

for (int i = 0; i < arr.length; i++) {

sum = sum + arr[i].getPrice();

}

System.out.println("Average price : $" + sum / arr.length);

}

}

public static void printItemsReverse(ArrayList<Item> items) {

System.out.println("Item in forward order : ");

System.out.println("Item details are : ");

for (int i = 0; i < items.size(); i++) {

System.out.println(items.get(i));

}

System.out.println();

System.out.println("Item in reverse order : ");

System.out.println("Item details are : ");

for (int i = items.size() - 1; i >= 0; i--) {

System.out.println(items.get(i));

}

System.out.println();

printAverage(items.toArray(new Item[items.size()]));

}

}

Sample Run: -

Please enter Item name: NewGRov
Please enter price: 3232
Item: NewGRov Price: 3232.0
Please enter Item name: Wallah
Please enter price: 232
Please enter Item name: Qui
Please enter price: 323
Please enter Item name: Priv
Please enter price: 323.32
Items are:
Item: Wallah Price: 232.0
Item: Qui Price: 323.0
Item: Priv Price: 323.32

Average price: $292.7733333333333
----------------Part 3 - Average if Peas ------------------
No Average output
-----------------Part 4 -- ArrayList of Items ------------
Please enter items. Enter -1 in price to stop entering....
Please enter Item name: Noprice
Please enter price: 432.32
Please enter Item name: Peas
Please enter price: 3232
Please enter Item name: Flower
Please enter price: 323.3232
Please enter Item name: Norick
Please enter price: 32.32
Please enter Item name: ChocoChips
Please enter price: 32.32
Please enter Item name: Forcast
Please enter price: 323.32
Please enter Item name: NA
Please enter price: -1
Item details are :
Item: Noprice Price: 432.32
Item: Peas Price: 3232.0
Item: Flower Price: 323.3232
Item: Norick Price: 32.32
Item: ChocoChips Price: 32.32
Item: Forcast Price: 323.32

Average price : $729.2672000000001
------------------------ Part 6 -------------------------
Item in forward order :
Item details are :
Item: Noprice Price: 432.32
Item: Peas Price: 3232.0
Item: Flower Price: 323.3232
Item: Norick Price: 32.32
Item: ChocoChips Price: 32.32
Item: Forcast Price: 323.32

Item in reverse order :
Item details are :
Item: Forcast Price: 323.32
Item: ChocoChips Price: 32.32
Item: Norick Price: 32.32
Item: Flower Price: 323.3232
Item: Peas Price: 3232.0
Item: Noprice Price: 432.32

Average price : $729.2672000000001
Do you want to process another set of items ? Yes/No
No