Hello, I'm fairly new to programming in java, and over the course of the months in learning and doing projects on it, have gotten used to a pattern of what every newbie programmer goes through: being overwhelmed doing a project I have no idea on what's its about, over-exhausting resources in looking for the answer in a commonly overlooked mistake, thinking everything is going right while actually writing code, then compiling the code and hitting a roadblock in one place where everything else is fine. That's how it feels again with this program, that I'm struggling to complete for class.
The end result of the program is supposed to calculate an employee's net pay, through how many hours worked and the pay rate of the individual. The program is to loop on repeat until prompted by the user to stop. But I'm trying to deal with the first two methods of in-taking the hours worked and pay rate in from the scanner, then sending it to the gross pay method to get a result first.
Everything else seems to be working, but the gross pay return value comes up twice. It seems to be because two variables were asked even though one was returned, but two needed to be sent to another method in order to get the gross pay result in the first place. I'm not sure how to only make it one passed variable and one return variable without repeating anything. this isn't even how the entire thing is supposed to be, I just need to make sure each component is working before putting the whole thing together and getting a project with too many errors to manage. Would gladly appreciate any advice to give on this probably simple coding mistake that was made.
The end result of the program is supposed to calculate an employee's net pay, through how many hours worked and the pay rate of the individual. The program is to loop on repeat until prompted by the user to stop. But I'm trying to deal with the first two methods of in-taking the hours worked and pay rate in from the scanner, then sending it to the gross pay method to get a result first.
Everything else seems to be working, but the gross pay return value comes up twice. It seems to be because two variables were asked even though one was returned, but two needed to be sent to another method in order to get the gross pay result in the first place. I'm not sure how to only make it one passed variable and one return variable without repeating anything. this isn't even how the entire thing is supposed to be, I just need to make sure each component is working before putting the whole thing together and getting a project with too many errors to manage. Would gladly appreciate any advice to give on this probably simple coding mistake that was made.
Code:
import java.util.Scanner;
/**
* Write a description of class GrossToNet here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class GrossToNet
{
public static void main()
{
Scanner scan = new Scanner(System.in);
while (true) {
System.out.print("Please enter number of hours worked. 0 to quit: ");
double hoursWorked = scan.nextDouble();
if (hoursWorked == 0) {
break;
}
System.out.print("Please enter the payrate. 0 to quit: ");
double payRate = scan.nextDouble();
if (payRate == 0) {
break;
}
double grossPay = payCalc(hoursWorked, payRate);
//1. call method for payCalc to take in varaibles - hoursWorked and payRate
System.out.println("Your gross pay is " + grossPay);
}
System.out.println("Gross Pay Calculator Program Ended");
}
public static double payCalc(double hoursIn, double payIn)
{
double gPay;
if (hoursIn <= 40)
{
gPay = (payIn * hoursIn);
System.out.println("Your gross pay is $" + gPay);
}
else
{
gPay = (40 * payIn) + ((hoursIn - 40) * (payIn * 1.5));
System.out.println("Your gross pay is $" + gPay);
}
return gPay;
}
}