Tuesday, October 18, 2011

Java Equivalent to C# Assignment 3


package Income;
import java.util.Scanner;

public class NetIncomeCalculator {

 /**
  * This program calculates gross pay
  * and net pay given deductions in 
  * Social Security, medicare and a bus
  * pass. It uses else if statments
  * to determine the rate of deduductions
  * This is the equivalent of
  * Assignment three in C# in ITC 110
  * Steve Conger 10/18/2011
  */
 
 //declaring constants
 //final double SOCIALSECURITY = .09;
 //final double MEDICARE=.03;
 final int BUSPASS=25;
   
 public static void main(String[] args) {
  //declare and instantiate the class
  //to load it (main being static is
  //already loaded into memory
  NetIncomeCalculator netcalc = new NetIncomeCalculator();
  //call the starting method
  netcalc.Display();
 }
 
 private void Display()
 {
  //get input
  //the scanner is an object to read from the console
  Scanner reader = new Scanner(System.in);
  System.out.println("Enter the number of hours worked");
  double hours = reader.nextDouble();
  System.out.println("Enter your rate per hour");
  double rate=reader.nextDouble();
  
  //call the methods to do the calculations
  double grossPay=CalcIncome(hours, rate);
  double socialSec = CalcSocialSecurity(grossPay);
  double medicare = CalcMedicare(grossPay);
  double netPay=CalcNetPay(grossPay, socialSec, medicare);
  
  //outputs
  System.out.println("Your gross income is " + grossPay);
  System.out.println("Your Social Security deduction is " + socialSec);
  System.out.println("Your Medicare deduction is " + medicare);
  System.out.println("Your Bus pass deduction is " + BUSPASS/4);
  System.out.println("Your net pay is " + netPay);
 }
 
 private double CalcIncome(double hrs, double rt)
 {
  double gross=0;
  //check to see if the hours are more than forty
  //if they are pay time and a half for overtime
  //if not just pay the rate of pay
  if (hrs > 40)
  {
   gross=rt * (40 + ((hrs-40)*1.5));
  }
  else
  {
   gross = rt * hrs;
  }
  return gross;
 }
 
 private double CalcSocialSecurity(double gPay)
 {
  double ss =0;
  if (gPay >= 5000)
   ss=.4;
  else if (gPay >= 4000 )
   ss=.35;
  else if (gPay >=3000)
   ss=.25;
  else if (gPay >=2000)
   ss=.2;
  else if (gPay > 1000)
   ss=.12;
  else
   ss=.09;
  
  return gPay * ss;
 }
 
 private double CalcMedicare(double gPay)
 {
  double med=0;
  if (gPay >=5000)
   med=.2;
  else if (gPay >= 2000)
   med=.1;
  else
   med=.05;
  return gPay * med;
 }
 
 private double CalcNetPay(double gPay, double ss, double med)
 {
  return gPay - (ss + med + (BUSPASS/4));
 }

}

No comments:

Post a Comment