Question: I need to output the code below on BlueJ java complier, but the output is differnt than neat bean could you snap the output for
I need to output the code below on BlueJ java complier, but the output is differnt than neat bean could you snap the output for screen shot as code 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() {
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
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();
}
}
please good screen shoot and what is the differnce between BlueJ complier and NeatBean
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
