this question is answered but question 5 and 6 is missing the code below as: imp
ID: 3742047 • Letter: T
Question
this question is answered but question 5 and 6 is missing the code below as:
import javax.swing.JOptionPane;
import java.util.ArrayList;
import java.util.Scanner;
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()
{
// Code to be written by student
Scanner sc=new Scanner(System.in);
System.out.println("Enter Name of item : ");
this.name=sc.nextLine();
System.out.println("Enter price of "+this.name+" : ");
this.price=sc.nextDouble();
}
public void show()
{
// Code to be written by student
System.out.println("Item name : "+this.name);
System.out.println("Price of item : "+this.price);
}
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 static boolean equivalent(double a, double b)
{
return ( Math.abs(a - b) <= TOLERANCE );
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author VISHAL
*/
public class TestItem {
public static double Average(Item[]A)
{
double sum=0.0;
for(int i=0; i<A.length; i++){
sum+=A[i].getPrice();
}
return(sum/A.length);
}
public static void printWelcome() {
JOptionPane.showMessageDialog(null, "Welcome to Tri-C");
}
public static void main(String[]args)
{
//question 1
Item A=new Item();
A.input();
A.show();
//question 2
Item []B=new Item[3];
for(int i=0; i<3; i++)
{
B[i]=new Item();
B[i].input();
}
System.out.println("Average Price for all item : "+Average(B));
//question no 3
boolean flag=false;
for(int i=0; i<3; i++)
{
if( B[i].getName().toUpperCase()=="PEAS")
{
flag=true;
break;
}
}
if(flag!=true)
{
System.out.println("Average Price for all item : "+Average(B));
}
//question 4 and 7
ArrayList C=new ArrayList();
Scanner sc= new Scanner(System.in);
String choice;int count=0;
while(true)
{
Item D=new Item();
D.input();
C.add(D);
count++;
System.out.println("Want to enter more item y or n : ");
choice=sc.nextLine();
if(!choice.equalsIgnoreCase("y"))
break;
}
boolean flag1=false;
for(int i=0; i<count; i++)
{
String Comp=C.get(i).toString();
if( Comp =="PEAS")
{
flag=true;
break;
}
}
if(flag!=true)
{
System.out.println("Average Price for all item : "+Average(B));
}
}
}
the JOptionPane is not shown for question 5 and forward and reverise statiac method in question 6 how to add them to code is also not clear based on the information below:
1.Using the Item class provided to you for download, write a method called input in the Item class 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, get input from the user using the input method of the class, and then output the item to the console using the show method of the Item class.
2.Add a static method to the TestItem class that averages the prices from an array of type Item and returns the answer as a double. Create an array of type Item that collects the input for 3 items in a loop and uses your static method to calculate 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 it should 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 for the price of an Item be your sentinel value. 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.
Explanation / Answer
Question 5 & 6 are solved below
import javax.swing.JOptionPane;
import java.util.ArrayList;
import java.util.Scanner;
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()
{
// Code to be written by student
Scanner sc=new Scanner(System.in);
System.out.println("Enter Name of item : ");
//this.name=sc.nextLine();
System.out.println("Enter price of "+this.name+" : ");
//this.price=sc.nextDouble();
}
public void show()
{
// Code to be written by student
System.out.println("Item name : "+this.name);
System.out.println("Price of item : "+this.price);
}
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 static boolean equivalent(double a, double b)
{
return ( Math.abs(a - b) <= TOLERANCE );
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author VISHAL
*/
public class TestItem {
public static double Average(Item[]A)
{
double sum=0.0;
for(int i=0; i<A.length; i++){
sum+=A[i].getPrice();
}
return(sum/A.length);
}
public static void printWelcome() {
JOptionPane.showMessageDialog(null, "Welcome to Tri-C");
}
public static void printItemsinReverse(Item[]A) {
double sum=0.0;
boolean isPeasPresent = false;
for(int i=A.length-1; i>0; i--){
System.out.println("Item name : "+this.name);
System.out.println("Price of item : "+this.price);
if(this.name.toLowerCase().equlas("peas")){
isPeasPresent = true;
}
sum+=A[i].getPrice();
}
if(isPeasPresent) {
// in order
for(int i=0; i<A.length; i++){
System.out.println("Item name : "+this.name);
System.out.println("Price of item : "+this.price);
}
System.out.println(sum/A.length);
// in reverse order
for(int i=A.length-1; i>0; i--){
System.out.println("Item name : "+this.name);
System.out.println("Price of item : "+this.price);
}
System.out.println(sum/A.length);
}
}
public static void main(String[]args)
{
// question 5
printWelcome();
//question 1
Item A=new Item();
A.input();
A.show();
//question 2
Item []B=new Item[3];
for(int i=0; i<3; i++)
{
B[i]=new Item();
B[i].input();
}
System.out.println("Average Price for all item : "+Average(B));
//question no 3
boolean flag=false;
for(int i=0; i<3; i++)
{
if( B[i].getName().toUpperCase()=="PEAS")
{
flag=true;
break;
}
}
if(flag!=true)
{
System.out.println("Average Price for all item : "+Average(B));
}
// question 6
printItemsinReverse(B);
//question 4 and 7
ArrayList C=new ArrayList();
Scanner sc= new Scanner(System.in);
String choice;int count=0;
while(true)
{
Item D=new Item();
D.input();
C.add(D);
count++;
System.out.println("Want to enter more item y or n : ");
choice=sc.nextLine();
if(!choice.equalsIgnoreCase("y"))
break;
}
boolean flag1=false;
for(int i=0; i<count; i++)
{
String Comp=C.get(i).toString();
if( Comp =="PEAS")
{
flag=true;
break;
}
}
if(flag!=true)
{
System.out.println("Average Price for all item : "+Average(B));
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.