CS 1713, Fall 2003
Project 1 Solution

Here is one possible solution to Project 1. This is just to show one way to do Project 1. Below, I used DecimalFormat instead of the NumberFormat that you were supposed to use. Notice that this program is an artificial exercise; of course a more realistic program would need flexibility and ease of use.


package employeepkg;
import java.text.DecimalFormat; // for the DecimalFormat class, to get cents
/**
 * Title: Employee Package
 * Description: Information about employees
 * Copyright: Copyright (c) 2003
 * Company: UTSA
 * @author Neal R. Wagner
 * @version 1.0
 */
// Employee: a single employee
public class Employee {
   // Constant
   private final double WITHHOLDING = 11.1; // percent withholding;

   // The data (fields):
   private String myName;  // employee's name
   private String mySsn;   // employees's SSN
   private int myWithholdingCode;  // a number from 1 to 3 from W2 form
   private double myRate;  // how much they get paid per hour
   private int myWeeklyHours; // how many hours they work a week

   // The constructors:
   public Employee(String name, String ssn) { // other field are defaulted here
      myName = name; mySsn = ssn;
      // defaults
      myWithholdingCode = 1; myRate = 7.50; myWeeklyHours = 20;
   }
   public Employee(String name, String ssn,
            int withhold, double rate, int hours) {
      myName = name; mySsn = ssn;
      myWithholdingCode = withhold; myRate = rate; myWeeklyHours = hours;
   }
   // for two digits to right of decimal
   DecimalFormat twoDigits = new DecimalFormat("#.00");

   // The accessors:
   public String getName() { return myName; }
   public String getSsn() { return mySsn; }
   public double getWithholdingCode() { return myWithholdingCode; }
   public double getWeeklyGross() { return myWeeklyHours * myRate; }
   public double getWeeklyNet() {
       double tempGross = getWeeklyGross();
       return tempGross - deductionspercent()*tempGross/100.0;
   }
   public double getAnnualGross() { return getWeeklyGross() * 52; }
   public double getAnnualNet() { return getWeeklyNet() * 52; }
   public String toString() {
      return "Name: " + myName +
             "\n   SSN: " + mySsn +
             "\n   Witholding Code: " + myWithholdingCode +
             "\n   Rate: " + twoDigits.format(myRate) +
             "\n   Hours: " + myWeeklyHours +
             "\n   Weekly Gross: " + twoDigits.format(getWeeklyGross()) +
             "\n   Weekly Net: " + twoDigits.format(getWeeklyNet()) +
             "\n   Annual Gross: " + twoDigits.format(getAnnualGross()) +
             "\n   Annual Net: " + twoDigits.format(getAnnualNet()) + "\n";
   }
   private double deductionspercent() { return WITHHOLDING * myWithholdingCode; }
       //returns the WITHHOLDING percent * WithholdingCode

   // The modifiers (if they change their mind):
   public void setWithholdingCode(int withholdingcode) {
      myWithholdingCode = withholdingcode;
   }
   public void setRate(double price) { myRate = price; }
   public void setWeeklyHours(int hours) { myWeeklyHours = hours; }

}

package employeepkg; public class EmployeeMain { public static void main(String args[]) { // local names for the data members in Employee class String name, ssn; int withholdingCode; double rate; int weeklyHours; // read in all fields System.out.println("Enter Name, SSN, Witholding Code, Rate, and Weekly Hours\n" + " (separated by newlines):"); name = Keyboard.readString(); ssn = Keyboard.readString(); withholdingCode = Keyboard.readInt(); rate = Keyboard.readDouble(); weeklyHours = Keyboard.readInt(); // create an Employee class Employee employee1 = new Employee(name, ssn, withholdingCode, rate, weeklyHours); // print results System.out.println(employee1); // create 2nd Employee, using only name and SSN System.out.println("Enter Name and SSN (separated by newlines):"); name = Keyboard.readString(); ssn = Keyboard.readString(); Employee employee2 = new Employee(name, ssn); System.out.println(employee2); // change code for first employee to 3 employee1.setWithholdingCode(3); System.out.println(employee1); // change rate for second employee to 9.00 employee2.setRate(9.00); System.out.println(employee2); // change hours for second employee to 50 employee2.setWeeklyHours(50); System.out.println(employee2); } }
RUN (user input in boldface): Enter Name, SSN, Witholding Code, Rate, and Weekly Hours (separated by newlines): Neal R. Wagner 500-40-4000 1 5.00 35 Name: Neal R. Wagner SSN: 500-40-4000 Witholding Code: 1 Rate: 5.00 Hours: 35 Weekly Gross: 175.00 Weekly Net: 155.58 Annual Gross: 9100.00 Annual Net: 8089.90 Enter Name and SSN (separated by newlines): Mark Doderer 400-50-5000 Name: Mark Doderer SSN: 400-50-5000 Witholding Code: 1 Rate: 7.50 Hours: 20 Weekly Gross: 150.00 Weekly Net: 133.35 Annual Gross: 7800.00 Annual Net: 6934.20 Name: Neal R. Wagner SSN: 500-40-4000 Witholding Code: 3 Rate: 5.00 Hours: 35 Weekly Gross: 175.00 Weekly Net: 116.73 Annual Gross: 9100.00 Annual Net: 6069.70 Name: Mark Doderer SSN: 400-50-5000 Witholding Code: 1 Rate: 9.00 Hours: 20 Weekly Gross: 180.00 Weekly Net: 160.02 Annual Gross: 9360.00 Annual Net: 8321.04 Name: Mark Doderer SSN: 400-50-5000 Witholding Code: 1 Rate: 9.00 Hours: 50 Weekly Gross: 450.00 Weekly Net: 400.05 Annual Gross: 23400.00 Annual Net: 20802.60