Answer to Question #264240 in Java | JSP | JSF for Zach

Question #264240

Given the number of people attending a pizza party, output the number of needed pizzas and total cost. For the calculation, assume that people eat 2 slices on average and each pizza has 12 slices and costs $14.95.

Output each floating-point value with two digits after the decimal point, which can be achieved as follows:

System.out.printf("Cost: $%.2f\n", cost)


1
Expert's answer
2021-11-11T07:30:31-0500
import java.util.Scanner;

public class Main {

    private final static double ONE_PIZZA_COST = 14.95;
    private final static int PIZZA_SLICES_NUMBER = 12;
    private final static float ONE_PERSON_EAT = 2;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of people: ");
        String input = sc.nextLine();

        //check input format (only digits)
        if(!input.matches("\\d+")) {
            System.out.println("Wrong input format");
        } else {

            int peopleCount = Integer.parseInt(input);
            int pizzaCount = (int) Math.ceil(peopleCount * ONE_PERSON_EAT / PIZZA_SLICES_NUMBER);
            double cost = pizzaCount * ONE_PIZZA_COST;

            System.out.println("Number of pizzas: " + pizzaCount);
            System.out.printf("Cost: $%.2f\n", cost);
        }

        sc.close();
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS