Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,152 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,665 people online right now. Registration is fast and FREE... Join Now!




Pay Check

 
Reply to this topicStart new topic

> Pay Check, run through on how to make a more accurate pay program taking into con

Rating  4
computerfox
*****



post 12 Aug, 2009 - 06:12 AM
Post #1


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:

CODE


import java.util.*;



Next we need to set up the class:

CODE

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:

CODE


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:

CODE


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?

CODE


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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

shelleymagnezi
*



post 15 Oct, 2009 - 07:00 PM
Post #2
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
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 04:43PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month