Page 1 of 1

Pay Check run through on how to make a more accurate pay program taking into con Rate Topic: ****- 1 Votes

#1 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Posted 12 August 2009 - 07:12 AM

In this tutorial, we will take a look at how to make a more accurate pay program using the tax rates. Now I'm not sure if it's the same for every state, but I'm just letting you know, I will be working off of the New Jersey tax rates.

First as usual, we would need the imports:


import java.util.*;




Next we need to set up the class:

public class javaPay {	 //name it whatever you wish, but you have to name the .java file the same thing.




Now, we will be using a method to return the final pay:


public class javaPay {
	public static double finalPay(double netPay){
		
		double finalPay=0;
		
				//This section might vary for other states/countries, just be aware of this.
		if(netPay<=5000){
			finalPay=(netPay-(netPay*.08));
		}
		if(netPay>=5000.01&&netPay<=10000&&netPay>5000){
			finalPay=(netPay-(netPay*.10));
		}
		if(netPay>=10000.01&&netPay>10000){
			finalPay=(netPay-(netPay*.12));
		}
		
		return finalPay;
	}




Now we need the driver function which will run the program:


 public static void main (String args[]) {
		
		double netPay,totalPay,hours;					   //variables used to calculate pay
		Scanner fox=new Scanner(System.in);		 //input device since JAVA doesn't use cin>>
		
		System.out.print("Enter net pay: ");
		netPay=fox.nextDouble();
		System.out.print("Enter hours: ");
		hours=fox.nextDouble();
		netPay*=hours;										//calculates pay before taxes
		totalPay=finalPay(netPay);						//uses our method to calculate pay after taxes
		
		System.out.println("Before taxes: "+netPay);		//outputs final results
		System.out.println("After taxes: "+totalPay);




How will this look when we put the code together?


import java.util.*;

public class javaPay {
	public static double finalPay(double netPay){
		
		double finalPay=0;
		
		if(netPay<=5000){
			finalPay=(netPay-(netPay*.08));
		}
		if(netPay>=5000.01&&netPay<=10000&&netPay>5000){
			finalPay=(netPay-(netPay*.10));
		}
		if(netPay>=10000.01&&netPay>10000){
			finalPay=(netPay-(netPay*.12));
		}
		
		return finalPay;
	}

	public static void main (String args[]) {
		
		double netPay,totalPay,hours;
		Scanner fox=new Scanner(System.in);
		
		System.out.print("Enter net pay: ");
		netPay=fox.nextDouble();
		System.out.print("Enter hours: ");
		hours=fox.nextDouble();
		netPay*=hours;
		totalPay=finalPay(netPay);
		
		System.out.println("Before taxes: "+netPay);
		System.out.println("After taxes: "+totalPay);
	}
}



And if we try it out with the following values:

Pay per hour: 20
Hours: 60 (Total hours getting paid for, be it for one week or two)

Your should get:

Attached Image

I hope that was helpful and I hope you take a look at my other stuff.

Is This A Good Question/Topic? 0
  • +

Replies To: Pay Check

#2 shelleymagnezi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-October 09

Posted 15 October 2009 - 08:00 PM

That looks good. What interests me is how to get the pay to show up with two decimal points.
My professor referred us to the DecimalFormat subclass.
My book uses, for example %5.2f - 5 places before the decimal, 2 after floating point no.
Thanks
Was This Post Helpful? 0
  • +
  • -

#3 Cinderhaze  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-December 13

Posted 12 December 2013 - 03:04 AM

It would be interesting to have a follow-up where you extract out the interface or base class and create several different versions, specific to a few states using OOP. Nice start though!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1