Below are two BlueJ java files. The first file is my main, called PizzaValueApp,
ID: 3589066 • Letter: B
Question
Below are two BlueJ java files. The first file is my main, called PizzaValueApp, and the second is Pizza.java. The program compares values of pizzas ($/Area) based on user input. Everything compiles and my user prompts work, but the program always says the pizzas are of equal value, so if-else statements must be wrong somehow. I am stuck and barely know what I am doing anyhow. Please help. Thanks
*Also, I have pictures of what is expected and sample output but I don't know how to upload them
import java.util.Scanner;
public class PizzaValueApp
{
public static void main(String [] args)
{
Scanner keyboard = new Scanner(System.in);
String name = "empty";
int diameter= 0;
double price = 0;
Pizza pza1 = new Pizza(); //creates new Pizza class objects
Pizza pza2 = new Pizza();
//next 3 chunks prompt user input and set keyed in values
System.out.print("First pizza name: ");
name = keyboard.nextLine(); //allows spaces in name input
pza1.setName(name);
System.out.print("First pizza price: ");
price = keyboard.nextDouble();
pza1.setPrice(price);
System.out.print("First pizza diameter: ");
price = keyboard.nextInt();
pza1.setDiameter(diameter);
//next 3 chunks get pizza 2 keyboard input and set values
System.out.print("Second pizza name: ");
keyboard.nextLine();
name = keyboard.nextLine();
pza2.setName(name);
System.out.print("Second pizza price: ");
price = keyboard.nextDouble();
pza2.setPrice(price);
System.out.print("Second pizza diameter: ");
price = keyboard.nextInt();
pza2.setDiameter(diameter);
if(pza1.getValue() < pza2.getValue()) //the lower number is actually the greater value
{
System.out.printf("%n %s is a better value ($%.2f/sq.in.) than %s ($%.2f/sq.in.)", pza1.getName(), pza1.getValue(), pza2.getName(), pza2.getValue());
}
else if(pza2.getValue() < pza1.getValue())
{
System.out.printf("%n %s is a better value ($%.2f/sq.in.) than %s ($%.2f/sq.in.)", pza2.getName(), pza2.getValue(), pza1.getName(), pza1.getValue());
}
else //when both ifs are false, the only option left is for the values to be equal
{
System.out.println("Both the pizzas are of the same value");
}
}
}
//DIFFERENT FILE BELOW
public class Pizza
{
private String name; //restaurant name and toppings
private double price; //pizza price
private int diameter; //diameter of pizza
public Pizza()
{
name = "empty";
diameter = 0;
price = 0;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getDiameter()
{
return diameter;
}
public void setDiameter(int diameter)
{
this.diameter= diameter;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
public double getValue() //finds area and pizza value ($ per area)
{
final double Const = Math.PI/4; //this is a constant for the area formula
double area = Const * diameter * diameter; //alternative cirlce area formula, A = pi/4 * d^2
double value = price/area;
return value;
}
}
Explanation / Answer
import java.util.Scanner;
public class PizzaValueApp {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String name = "empty";
int diameter = 0;
double price = 0;
Pizza pza1 = new Pizza(); // creates new Pizza class objects
Pizza pza2 = new Pizza();
// next 3 chunks prompt user input and set keyed in values
System.out.print("First pizza name: ");
name = keyboard.nextLine(); // allows spaces in name input
pza1.setName(name);
System.out.print("First pizza price: ");
price = keyboard.nextDouble();
pza1.setPrice(price);
System.out.print("First pizza diameter: ");
diameter = keyboard.nextInt();
pza1.setDiameter(diameter);
// next 3 chunks get pizza 2 keyboard input and set values
System.out.print("Second pizza name: ");
keyboard.nextLine();
name = keyboard.nextLine();
pza2.setName(name);
System.out.print("Second pizza price: ");
price = keyboard.nextDouble();
pza2.setPrice(price);
System.out.print("Second pizza diameter: ");
diameter = keyboard.nextInt();
pza2.setDiameter(diameter);
if (pza1.getValue() < pza2.getValue()) // the lower number is actually
// the greater value
{
System.out
.printf("%n %s is a better value ($%.2f/sq.in.) than %s ($%.2f/sq.in.)",
pza1.getName(), pza1.getValue(), pza2.getName(),
pza2.getValue());
}
else if (pza2.getValue() < pza1.getValue()) {
System.out
.printf("%n %s is a better value ($%.2f/sq.in.) than %s ($%.2f/sq.in.)",
pza2.getName(), pza2.getValue(), pza1.getName(),
pza1.getValue());
} else // when both ifs are false, the only option left is for the
// values to be equal
{
System.out.println("Both the pizzas are of the same value");
}
}
}
public class Pizza {
private String name; // restaurant name and toppings
private double price; // pizza price
private int diameter; // diameter of pizza
public Pizza() {
name = "empty";
diameter = 0;
price = 0;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDiameter() {
return diameter;
}
public void setDiameter(int diameter) {
this.diameter = diameter;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getValue() // finds area and pizza value ($ per area)
{
final double Const = Math.PI / 4; // this is a constant for the area
// formula
double area = Const * diameter * diameter; // alternative cirlce area
// formula, A = pi/4 * d^2
double value = price / area;
return value;
}
}
OUTPUT:
First pizza name: Pizza1
First pizza price: 322
First pizza diameter: 4
Second pizza name: Pizza2
Second pizza price: 433
Second pizza diameter: 6
Pizza2 is a better value ($15.31/sq.in.) than Pizza1 ($25.62/sq.in.)
NOTE:
Just i changed the code at getting diameter from key board
System.out.print("First pizza diameter: ");
diameter = keyboard.nextInt();
pza1.setDiameter(diameter);
And
System.out.print("Second pizza diameter: ");
diameter = keyboard.nextInt();
pza2.setDiameter(diameter);
please check the code
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.