Monday, July 7, 2014

A java class example

Here is a class that calculates a simplified weekly payroll. Notice components that make up the class:

Fields: Fields are class level variables that describe the object. In this case we need the employee number, the rate of pay and the number of hours worked for the week.

Constants: Constants are values that cannot be changed in the course of the program. Java uses the word "final" to designate a constant. The naming convention for constants is to use all caps.

Sets and Gets: fields should be private. This is part of an object oriented principle called Encapsulation. You can use sets and gets to expose these private fields to outside classes. Sets allow another class to change the value of the underlying field. Gets allow another class to see the current value of a field. Not every field needs to be seen by other classes. You can also make a set without a get or visa-versa. You can also add validation to the sets if needed.

Public Methods: public methods are methods that can be accessed by other classes. You can also make private methods for things that are only internal to the class. Notice that each method does one thing. That makes troubleshooting and maintenance much easier.

Constructors: constructors are specialized methods whose purpose is to initialize or "construct" the class. Constructors can be overridden. This call has three constructors: an empty constructor that takes no parameters, a constructor that takes just the employee's number and a constructor that takes the employee number, the pay rate and the hours worked. You can have as many constructors as you want as long as each constructor has a different set of parameters. (It is important to note it is not the name of the parameter that matters, but the data type. You can only have one constructor that takes a single String parameter, even if they represent different Strings. So you can't do something like


public Pay(String employeeNumber){}
public Pay(String employeeName){}

They would not be seen as different by the complier.) Only one constructor is used for any instance of the class. Which constructor is used is determined by the parameters that are passed. In our example I used the last constructor that takes the employee number, pay rate and hours.

Here is the Pay.java class


package com.spconger.PayrollExample;

public class Pay {
 /*
  * This class represents a simplified
  * Payroll class. It calculates base pay,
  * overtime pay. total pay and net pay
  * with deductions for social security
  * federal withholding and union dues
  * it has a ToString method that returns
  * a complete pay stub
  * Steve Conger, 7/7/1014
  */
 //fields: class level variables that describe the object
 private String employeeNumber;
 private double payRate;
 private double hoursWorked;
 
 //declare a constant
 private final double UNIONDUES=.02;
 
 //get and set methods to allow access
 //to each field
 
 public String getEmployeeNumber() {
  return employeeNumber;
 }
 public void setEmployeeNumber(String employeeNumber) {
  this.employeeNumber = employeeNumber;
 }
 public double getPayRate() {
  return payRate;
 }
 public void setPayRate(double payRate) {
  this.payRate = payRate;
 }
 public double getHoursWorked() {
  return hoursWorked;
 }
 public void setHoursWorked(double hoursWorked) {
  this.hoursWorked = hoursWorked;
 }

 
 
 //public methods
 
 public double calculateBasePay(){
  double basePay = getHoursWorked() * getPayRate();
  return basePay;
 }
 
 public double calculateOvertimePay(){
  double overtime=(getHoursWorked()-40) * getPayRate()*1.5;
  return overtime;
 }
 
 public double calculateTotalPay(){
  return calculateBasePay() + calculateOvertimePay();
 }

 public double calculateSocialSecurity(){
  double ss=0;
  double ssPercent=0;
  
  double total = calculateTotalPay();
  if(total > 1500)
   ssPercent=.25;
  else if (total > 1000)
   ssPercent=.2;
  else if (total > 600)
   ssPercent=.15;
  else 
   ssPercent=.1;
  
   
  ss=total * ssPercent; 
    
  return ss;
 }
 
 public double calculateFederalWithholding(){
  double fw=0;
  double fwPercent=0;
  
  double total = calculateTotalPay();
  if(total > 1500)
   fwPercent=.1;
  else if (total > 1000)
   fwPercent=.08;
  else if (total > 600)
   fwPercent=.05;
  else 
   fwPercent=.01;
  
  fw=total * fwPercent;
  
  return fw;
 }
 
 public double calculateUnionDues(){
  return calculateTotalPay()*UNIONDUES;
 }
 
 public double calculateTotalDeductions(){
  return calculateSocialSecurity() + calculateFederalWithholding() 
    + calculateUnionDues();
 }
 
 public double calculateNetPay(){
  return calculateTotalPay()-calculateTotalDeductions();
 }
 
 
 public String toString(){
  
  String paystub= "Employee Number: "
    + getEmployeeNumber() 
    + "\n"
    + "Hours: "
    + getHoursWorked() 
    + "\n"
    + " Rate: "
    + getPayRate()
    + "\n"
    + " Base Pay: "
    + calculateBasePay()
    + "\n"
    + " Overtime: "
    + calculateOvertimePay()
    + "\n"
    + "Total Pay: " 
    + calculateTotalPay()
    + "\n"
    + "Social Security Detuction: "
    + calculateSocialSecurity()
    + "\n"
    + "Federal Witholding: "
    + calculateFederalWithholding()
    + "\n"
    + "Union dues: "
    + calculateUnionDues()
    + "\n"
    + "Total Deductions: "
    + calculateTotalDeductions()
    + "\n"
    + "Net Pay: "
    + calculateNetPay();
  
  return paystub;
  
    
 }
 //public constructors
 //empty Constructor
 public Pay(){
  
 }
 
 //Overridden constructor that takes employee number
 public Pay(String emp){
  setEmployeeNumber(emp);
 }
 
 //overridden constructor
 public Pay(String emp, double rate, double hours){
  setEmployeeNumber(emp);
  setPayRate(rate);
  setHoursWorked(hours);
 }

}


Here is Program.java


package com.spconger.PayrollExample;

public class Program {

 /**
  * @param args
  */
 public static void main(String[] args) {
  Program p=new Program();
  p.Display();

 }
 
 private void Display(){
  Pay pay = new Pay("345", 35.75, 45);
  System.out.println(pay.toString());
 }

}


Here is a picture of the results using the toString() method.

1 comment: