/** * File name: WageCalculatorApplet.java * Author: Hongyan Wang. Modified by David Wills to applet * * Description: The program accepts an employee's pay rate and number of hours * worked in a week, and calculates the wage the employee earned for that week. * * Others: The program has minimum error checking. It doesn't check the validity * of number of hours worked. The program uses JOptionPane for input. */ import java.text.*; // to use DecimalFormat import javax.swing.*; import java.awt.Graphics; public class WageCalculatorApplet extends JApplet { // Constants private static final double MAX_HOURS = 40.0; // Maximum normal work hours private static final double OVERTIME = 1.5; // Overtime pay rate factor double wages; // Wages earned public void init () { double payRate; // Employee's pay rate double hours; // Hours worked String input; // Get info. input = JOptionPane.showInputDialog ("Enter pay rate: "); payRate = Double.parseDouble( input ); input = JOptionPane.showInputDialog ("Enter hours worked: "); hours = Double.parseDouble( input ); // Calculate wage based on pay rate and hours worked if (hours > MAX_HOURS) // Is there overtime? wages = (MAX_HOURS * payRate) + // Yes (hours - MAX_HOURS) * payRate * OVERTIME; else wages = hours * payRate; // No } public void paint ( Graphics g ) { super.paint( g ); // Display the result DecimalFormat d2 = new DecimalFormat ("0.00"); g.drawString ("The wage should be " + d2.format (wages), 100, 100); } }