Link to home
Start Free TrialLog in
Avatar of RgrWalker
RgrWalker

asked on

Need help with a Java Program that will store an hourly Rate and compute gross and net pay

Needing help writing a Program that stores an hourly pay rate and hours worked.  It should also Compute Gross Pay (Hours times rate), Witholding Tax and Net Pay (Gross pay minus withholding tax).  Withholding Tax must be computed as a percentage of gross pay based on the following.

           Gross Pay                                                                       Withholding Percentage

Up to and including 300.00                                                                          10

300.01 and up                                                                                            12
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Please post code you've written so far
Avatar of RgrWalker
RgrWalker

ASKER

class EmployeePayroll
{
  public static void main(String[]arg)
  }
var withHoldingtax;
if( grossPay > 300 )
withHoldingTax = eval( parseFloat(grossPay) / 12 );
else
withHoldingTax = eval( parseFloat(grossPay) / 10 );
}
That looks like JavaScript - are you in the right TA?
if ur question is related to JavaScript ask moderators to move this question to

this thread https://www.experts-exchange.com/Web/Web_Languages/JavaScript/
If it's not JavaScript, we can't help you until you've produced a significant amount of work, as it's obviously schoolwork
I'm sure that I'm in the right TA, I'm not sure how to write it.  I started this morning writing this and using two books.  I received some help and figured out that I was in the Wrong TA wich at the time was JavaScript.  I need this to be a program and not a web page.  So, with that being said.  

How would I go about creating a java program that stores an hourly pay rate and hours worked.  I also need it to Compute Gross Pay (Hours times rate), Witholding Tax and Net Pay (Gross pay minus withholding tax).  Withholding Tax must be computed as a percentage of gross pay based on the following.

           Gross Pay                                                                       Withholding Percentage

Up to and including 300.00                                                                          10

300.01 and up                                                                                            12

public class EmployeePayroll
{
public static void main(String[] args)
{
 grossPay=hourlyRateOfPay*numberOfHoursWorked;
 netPay=grossPay-withHoldingTax;

System.out.println("Gross pay: "+grossPay);
System.out.println("Withholding tax: "+withHoldingTax);
System.out.println("Net pay: "+netPay);


Am I even on the right track with this much of it?
If  it is in Java

May be ur expecting some thing like this??

 class EmployeePayroll
{
public static void main(String[] args)
{
    double grossPay=0;
    double hourlyRateOfPay=0;
    double netPay=0;
    double withHoldingTax=0;
    double numberOfHoursWorked=0;
 grossPay=hourlyRateOfPay*numberOfHoursWorked;
 if(grossPay<=300.0)
 {
 withHoldingTax=grossPay*10.0/100.0;
 }
 else
 {
 withHoldingTax=grossPay*12.0/100.0;    
 }
 netPay=grossPay-withHoldingTax;

System.out.println("Gross pay: "+grossPay);
System.out.println("Withholding tax: "+withHoldingTax);
System.out.println("Net pay: "+netPay);
}
 }
Actual code you required is

grossPay=hourlyRateOfPay*numberOfHoursWorked;
 if(grossPay<=300.0)
 {
 withHoldingTax=grossPay*10.0/100.0;
 }
 else
 {
 withHoldingTax=grossPay*12.0/100.0;    
 }
 netPay=grossPay-withHoldingTax;

System.out.println("Gross pay: "+grossPay);
System.out.println("Withholding tax: "+withHoldingTax);
System.out.println("Net pay: "+netPay);

and numberOfHoursWorked & hourlyRateOfPay should come dynamically. Assuming you know how to get them
How is this storing the hourly pay rate and hours worked?
use System.in.read method
ASKER CERTIFIED SOLUTION
Avatar of sudhakar_koundinya
sudhakar_koundinya

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
May be it contains some errors.

You need to check ur self.
But code logic behind reading and computing is that
That was exactly what I was looking for, Thank you, It works great
Homework done then?
I'm having problems understanding some of this code.  Although it works I'm not exactly sure what it does.  Could someone please explain what this logic that resolves the code is?

I will post to the side of the code that I don't understand if that's ok.

class EmployeePayroll
{
public static void main(String[] args)
{
    double grossPay=0;
    double hourlyRateOfPay=0;
    double netPay=0;
    double withHoldingTax=0;
    double numberOfHoursWorked=0;
    System.err.print("Hourly Rate of Pay : ");
    hourlyRateOfPay=Double.parseDouble(readLine());
 
 System.err.print("No of hours worked : ");
 numberOfHoursWorked=Double.parseDouble(readLine());
 grossPay=hourlyRateOfPay*numberOfHoursWorked;
 if(grossPay<=300.0)
 {
 withHoldingTax=grossPay*10.0/100.0;
 }
 else
 {
 withHoldingTax=grossPay*12.0/100.0;    
 }
 netPay=grossPay-withHoldingTax;

System.out.println("Gross pay: "+grossPay);
System.out.println("Withholding tax: "+withHoldingTax);
System.out.println("Net pay: "+netPay);
}
public static String readLine()
{
 
 Basically from Here Down I am lost.  Could you explain to me line by line what each one is doing?


 StringBuffer sb=new StringBuffer();
    try
    {
       while(true)
       {
        char c=(char)System.in.read();
        if(c=='\r'||c=='\n' || c==-1 || c==255)
        {
            break;
        }
        sb.append(c);
       }
       
    }
    catch(Exception ex)
    {
       
    }
    return sb.toString();
}
 }
>>Basically from Here Down I am lost.  Could you explain to me line by line what each one is doing?

It reads charachter by character from Standard input sung Systme.in.read() method

these characters will be stored into some temperory buffer and this buffer will be returned as string to main program.

sb.append(c)  stores the characters

but before storing into that buffer, it checks for newline or Ctrl+Z charachter(normally treated as EOF-End of File Character)

 if(c=='\r'||c=='\n' || c==-1 || c==255)  this line checks for the EOF or newline.

Regards
Sudhakar
So let me see if I can get this right.

char c=(char)System.in.read(); Is telling it to read character by character?

StringBuffer sb=new StringBuffer();  Not sure about this one

if(c=='\r'||c=='\n' || c==-1 || c==255)  Checks the line for end of file or new line?

break;  This is telling it to go to the end of the program?

sb.append(c)  stores the characters, Where are they being stored?

and last but not least, What is the importance or why is this portion of the program needed?  Can't it be done with what is above all of this?

public static String readLine()
{
 StringBuffer sb=new StringBuffer();
    try
    {
       while(true)
       {
        char c=(char)System.in.read();
        if(c=='\r'||c=='\n' || c==-1 || c==255)
        {
            break;
        }
        sb.append(c);
       }
       
    }
    catch(Exception ex)
    {
       
    }
    return sb.toString();
}
 }
One more thing, What is System.err.print

What's the difference between System.err.print and System.out.print

Thanks
System.err.print  -> prints the output to Standard error device

 System.out.print prints the standard output device

If we use System.err.print we can't redirect the output to some file

I mean

If i try to do like this java myclass >output.txt
Nothing will be stored into output.txt

whereas System.out will do that.
>>StringBuffer sb=new StringBuffer();  Not sure about this one
This is temporary buffer that stores the characters that were read from console

>>if(c=='\r'||c=='\n' || c==-1 || c==255)  Checks the line for end of file or new line?
correction : Checks the character for end of file or new line?

>>break;  This is telling it to go to the end of the program?
Correction : This is telling to the program that the user has read the required characters. So that application will do some other executions

>>sb.append(c)  stores the characters, Where are they being stored?
This is temporary buffer-> This temporary buffer is created using this statement StringBuffer sb=new StringBuffer();

>> and last but not least, What is the importance or why is this portion of the program needed?  Can't it be done with what is  >> above all of this?

As u need to get the input  to the application from standard input device i.e console device, u need this


Regards
Sudhakar
>>catch(Exception ex)

There is a possibilty of getting runtime errors (I/O Errors) while reading the characters from standard input device. To supress that error

the code  block should    be placed between try and catch statements

try
{
        Your code block

}
catch(Exception ex)
{
{


>>    return sb.toString();

what ever the data u read from standard input device, that data will be collected in this temporary buffer and will be returned to main program as String

for example
numberOfHoursWorked=Double.parseDouble(readLine()); --> here u r reading the data from standard input device and gets the information  as double value using Double.parseDouble

Regards
Sudhakar

One more and I think I can figure this all out.

catch(Exception ex)
{
}
return sb.toString();
}


What is up with the catch(Exception ex)?  Why is that needed and why did you use it?

and

return sb.toString()  I'm afraid to guess here.
and Finally

What does this do?

public static String readLine()


Thank you so much for your help btw, I understand all of it now.
>>
public static String readLine()

That is method that will be called by ur main program

In java, A class consists of Data members and methods to perform it's tasks

main is a starting method of a class and main is using the readLine method, for it's further execution

regards
Sudhakar