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

Write a complete Java program in a source file to be named Assignment2.java. The

ID: 3786466 • Letter: W

Question

Write a complete Java program in a source file to be named Assignment2.java. The program prints out the following menu to a user, using ' ' (tabs) and ' ' (newline), and print or println methods of System.out: Welcome to our Smoothie Drink Shop! How many large size smoothie drinks would you like to buy? and read in the number. Then prompt to a user: How many medium size smoothie drinks would you like to buy? and read in the number. Then prompt to a user How many small size smoothie drinks would you like to buy? and read in the number. Then the program computes and prints the following: The total cost for the large size drinks The total cost for the medium size drinks The total cost for the small size drinks The total cost for all drinks The total number of drinks The average cost per drink for this purchase Use the NumberFormat class to format dollar amounts to be printed. The NumberFormat class is defined in the java.text package.

Explanation / Answer

import java.text.DecimalFormat;
import java.util.Scanner;


public class Assignment2 {
   public static void main(String[] args)
   {
       System.out.println("Welcome to our Smoothie Drink Shop!");
       System.out.println("--------------------------------");
      
       System.out.println("Price (Size):");
       System.out.println("Large (40oz): $8.50");
       System.out.println("Medium (30oz): $6.50");
       System.out.println("Small (20oz): $4.50");
      
       Scanner sc = new Scanner(System.in);
      
       int large = 0;
       System.out.print("How many large size smoothie drinks would you like to buy? : ");
       large = Integer.parseInt(sc.nextLine());
      
       int medium = 0;
       System.out.print("How many medium size smoothie drinks would you like to buy? : ");
       medium = Integer.parseInt(sc.nextLine());
      
       int small = 0;
       System.out.print("How many small size smoothie drinks would you like to buy? : ");
       small = Integer.parseInt(sc.nextLine());
       sc.close();
       DecimalFormat df = new DecimalFormat("#.00");
      
       double largeCost = large*8.50;
       double mediumCost = medium*6.50;
       double smallCost = small*4.50;
      
       System.out.printf("The total cost for large size drinks: $%.2f ", largeCost);
       System.out.printf("The total cost for medium size drinks: $%.2f ", mediumCost);
       System.out.printf("The total cost for small size drinks: $%.2f ", smallCost);
      
       double totalCost = largeCost + mediumCost + smallCost;
       int totalDrinks = large+small+medium;
       double average = totalCost/totalDrinks;
      
       System.out.printf("The total cost for all drinks: $%.2f ", totalCost);
       System.out.printf("The total number of drinks: $%d ", totalDrinks);
       System.out.printf("The average cost per drink for this purchase: $%.2f ", average);
   }
}

/*

Sample run

Welcome to our Smoothie Drink Shop!
--------------------------------
Price   (Size):
Large   (40oz):   $8.50
Medium   (30oz):   $6.50
Small   (20oz):   $4.50
How many large size smoothie drinks would you like to buy? : 10
How many medium size smoothie drinks would you like to buy? : 10
How many small size smoothie drinks would you like to buy? : 10
The total cost for large size drinks:        $85.00
The total cost for medium size drinks:       $65.00
The total cost for small size drinks:        $45.00
The total cost for all drinks:            $195.00
The total number of drinks:            $30
The average cost per drink for this purchase:    $6.50

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote