there is acomple erorr as below in the current code: this is code as below: impo
ID: 3742133 • Letter: T
Question
there is acomple erorr as below in the current code:
this is code as 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));
}
}
}
this code already answerd and l used NeatBean ID 8.2 and l complie but there is an error as the instruction below:
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.
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).
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”.
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.”
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.
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.
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.
104 for (int í=A . length-1; 1>0; 1--){ System. out.println (Item name:+this.name System. out.println (Price of item+this.price if ( this. name. toLowerCase ( ) . egulas peas " isPeasPresenttrue 108 109 110sum+ A[i].getPrice ) 112 113 114 115 | if(13PeasPresent) // in order for (int i-0: iExplanation / Answer
Hi,
Ans 1. you were using "this" keyword to call the variables which are not visible by TestItem class. As "this" is used to refer current object but here TestItem and item are two totally different Classes, That's why there were these errors on print statements of reverse function. Instead of using "this" keyword we can use the getter functions of Item which can give you the correct Item name and Item Price for that particular Item.
Ans 2. For the method named equals in Item class, it is showing a warning that you have not used this method, however, this won't affect your code in any way.
Here's the correct and working code :
package sample;
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() {
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();
// sc.close();
}
public void show() {
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);
}
}
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--) {
//Your were using this.name but name is not by visible by TestItem
// as it is a instance variable of Item Class, That's why there was
// these errors on print statements. Instead of using this keyword
//You can simply use getter functions of Item class. As I have done here.
System.out.println("Item name : " + A[i].getName());
System.out.println("Price of item : " + A[i].getPrice());
if (A[i].getName().toLowerCase().equals("peas")) {
isPeasPresent = true;
}
sum += A[i].getPrice();
}
if (isPeasPresent) {
// in order
for (int i = 0; i < A.length; i++) {
System.out.println("Item name : " + A[i].getName());
System.out.println("Price of item : " + A[i].getPrice());
}
System.out.println(sum / A.length);
// in reverse order
for (int i = A.length - 1; i > 0; i--) {
System.out.println("Item name : " + A[i].getName());
System.out.println("Price of item : " + A[i].getPrice());
}
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++) {
//Use equals() to compare string Content as == just compare their reference addresses
if (B[i].getName().toUpperCase().equals("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<Item> 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));
}
sc.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.