Then write the code in the main method that reads the subtot

Then write the code in the main method that reads the subtotal and the gratuity rate, then computes the gratuity and total. Include exception handling (try-catch block) so that when a user enters a non-numeric value your code will respond effectively.

For example, if the user enters 10 for subtotal and 15% for gratuity rate, the program displays $1.5 as gratuity and 11.5 as total.

Example

Enter the subtotal and the gratuity rate 10 15

The gratuity is $1.5 and the total is $11.5

Solution

HI, Please find my program.

Pleaase let me know in case of any issue.

import java.util.Scanner;

public class GratuityCalc {

  

   public static void main(String[] args) {

      

       Scanner sc = new Scanner(System.in);

      

       try{

           // taking user input

           System.out.print(\"Enter the subtotal and the gratuity rate: \");

           double subtotal = sc.nextDouble();

           double gratuityRate = sc.nextDouble();

          

           double gratuity = subtotal*gratuityRate/100.0;

           double total = subtotal + gratuity;

                  

           System.out.println(\"Total: \"+total);

           System.out.println(\"Gratuity: \"+gratuity);

          

          

          

       }catch (Exception e) {

           System.out.println(\"Enter numeric values for subtotal and grtuity\");

       }

   }

}

/*

Sample run:

Enter the subtotal and the gratuity rate: 10 15

Total: 11.5

Gratuity: 1.5

*/

Then write the code in the main method that reads the subtotal and the gratuity rate, then computes the gratuity and total. Include exception handling (try-catc
Then write the code in the main method that reads the subtotal and the gratuity rate, then computes the gratuity and total. Include exception handling (try-catc

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site