This assignment should use JOptionPane to do input and output . Write a program
ID: 3785158 • Letter: T
Question
This assignment should use JOptionPane to do input and output. Write a program that reads the subtotal and the gratuity rate, and computes the gratuity and total. 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. Please make sure to implement correct rounding of the subtotal and tip amounts to avoid fractional cents. For testing purpose consider subtotal of $23.77 and a gratuity rate of 19%. Correct result would be tip amount of $4.52 (or $4.51 depending on rounding policy) but not $4.5163 and the total would be $28.29 (or $28.28 depending on rounding policy) but not $28.2863
Explanation / Answer
package com.chegg.gratuity;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class GratuityTest {
static String tip;
static String subtotal;
static double total;
public static void main(String[] args) {
JFrame parent = new JFrame();
subtotal = JOptionPane.showInputDialog("Enter Amount :");
double amount = Double.parseDouble(subtotal);
tip = JOptionPane.showInputDialog("Enter tip persentage :");
double tipAmount = Double.parseDouble(tip);
double gratuity = (amount*tipAmount)/100;
double roundGratuity = Math.round(gratuity*100.0)/100.0;
JOptionPane.showMessageDialog(parent, "Graduity is ");
JOptionPane.showMessageDialog(null, roundGratuity);
total = amount + gratuity;
double roundTotal =Math.round(total*100.0)/100.0;
JOptionPane.showMessageDialog(parent, "Total is ");
JOptionPane.showMessageDialog(null, roundTotal);
}
}