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

Objectives . Practice problem solving . Practice writing Java .Practice with sel

ID: 3585893 • Letter: O

Question


Objectives . Practice problem solving . Practice writing Java .Practice with selection; console input, output, and using printf() Problem Statement Given the nam ) of two different pizzas, determine which is the better value (lower cost per square inch). The program should prompt the user for the name, price, and diameter for one pizza; then ne name, price, and size of a second pizza. Then the program prints the name and price per square inch of the better value. If the two pizzas are of the same value, then the program should display a message to that effect. Example run of the program: (user input is in blue) First pizza name: Dominos cheese First pizza price: 10.99 First pizza diameter: 16 Second pizza name: Imos meat Second pizza price: 8.99 Second pizza diameter: 14 Doninos chese ts a better value (0.1/q.in.) than tnos neat 659/s, ) Notes: The name of the pizza could have a space Prices should be entered as dollars and cents (no dollar sign) Diameters are integer values The price should be displayed to the penny with a dollar sign (not arbitrary number of digits) Eaualitv should be compared to the penny .

Explanation / Answer

Given below is the code for the question. I am not sure how the value is computed in getValue().

The given code computes by calculating hte area of the pizza as pi * radius * radius . Then to get value it divides price / area .

But the screenshot given in the answer seems to be calculating the value using some other formula. If you can find out and let me know the formula used to get the value, I can modify it according. Only the getValue() method will need to be modified. Rest of the program will remain same. Post a comment to let me know the formula used in getValue, so I can help you by changing the code accordingly

Pizza.java

public class Pizza {

   private String name;

   private double price;

   private int diameter;

   public Pizza()

   {

       name = "none";

       price = 0;

       diameter = 0;

   }

   public String getName() {

       return name;

   }

   public void setName(String name) {

       this.name = name;

   }

   public double getPrice() {

       return price;

   }

   public void setPrice(double price) {

       this.price = price;

   }

   public int getDiameter() {

       return diameter;

   }

   public void setDiameter(int diameter) {

       this.diameter = diameter;

   }

  

   public double getValue()

   {

       //compute the area and then the value

       double radius = diameter / 2.0;

       double area = Math.PI * radius * radius;

       double value = price / area;

       return value;

   }

}

PizzaValueApp.java

import java.util.Scanner;

public class PizzaValueApp {

   public static void main(String[] args) {

       Scanner keybd = new Scanner(System.in);

       String name;

       double price;

       int diameter;

       //create 2 pizza objects

       Pizza p1 = new Pizza();

       Pizza p2 = new Pizza();

      

      

       //get and set 1st pizza details

       System.out.print("First pizza name: ");

       name = keybd.nextLine();

       p1.setName(name);

      

       System.out.print("First pizza price: ");

       price = keybd.nextDouble();

       p1.setPrice(price);

      

       System.out.print("First pizza diameter: ");

       diameter = keybd.nextInt();

       p1.setDiameter(diameter);

      

      

       //get and set 2nd pizza details

       System.out.print(" Second pizza name: ");

       keybd.nextLine(); //get rid of the newline befroe using nextLine() ,other wise empty line will be received

       name = keybd.nextLine();

       p2.setName(name);

      

       System.out.print("Second pizza price: ");

       price = keybd.nextDouble();

       p2.setPrice(price);

      

       System.out.print("Second pizza diameter: ");

       diameter = keybd.nextInt();

       p2.setDiameter(diameter);

      

       if(p1.getValue() < p2.getValue())

           System.out.printf(" %s is a better value ($%.2f /sq.in.) than %s ($%.2f /sq.in.)",

                   p1.getName(), p1.getValue(), p2.getName(), p2.getValue());

       else if(p2.getValue() < p1.getValue())

           System.out.printf(" %s is a better value ($%.2f /sq.in.) than %s ($%.2f /sq.in.)",

                   p2.getName(), p2.getValue(), p1.getName(), p1.getValue());

       else

           System.out.println("Both the pizzas are of same value");

   }

}

output

First pizza name: Dominos Cheesse

First pizza price: 10.99

First pizza diameter: 16

Second pizza name: Imos meat

Second pizza price: 8.99

Second pizza diameter: 14

Dominos Cheesse is a better value ($0.05 /sq.in.) than Imos meat ($0.06 /sq.in.)