This program is a simple calculator that computes a weekly salary based upon daily values for overtime and regular hours after entering the hourly salary.
I am trying to make this program run from a few methods and I'm running into errors left and right:
import java.util.Scanner; //Takes user input import java.text.*; //import for the decimal format public class Mod_of_3_4 { //Constant Vars int employeeId; int regHours; int overHours; double wage; double totalPay; //Day 1 Var int regHours1; int overHours1; //Day 2 Var int regHours2; int overHours2; //Day 3 Var int regHours3; int overHours3; //Day 4 Var int regHours4; int overHours4; //Day 5 Var int regHours5; int overHours5; public static void main(String[] args) { //Methods inputID(); inputWage(); inputDays(); calcPay(); calcTotal(); printResults(); } //Employee ID Number public int inputID() { System.out.println("Enter Employee ID: "); Scanner scan = new Scanner(System.in); int employeeId = scan.nextInt(); } //Hourly Wage public double inputWage() { System.out.println("Enter the Hourly Wage: "); Scanner scan3 = new Scanner(System.in); double wage = scan3.nextDouble(); } //Input Daily Hours public int inputDays() { //Day 1 System.out.println("Monday: Enter the Number of Regular Hours: "); Scanner scan1 = new Scanner(System.in); int regHours1 = scan1.nextInt(); System.out.println("Monday: Enter the Number of Over Time Hours: "); Scanner scan2 = new Scanner(System.in); int overHours1 = scan2.nextInt(); //Day 2 System.out.println("Tuesday: Enter the Number of Regular Hours: "); Scanner scan4 = new Scanner(System.in); int regHours2 = scan4.nextInt(); System.out.println("Tuesday: Enter the Number of Over Time Hours: "); Scanner scan5 = new Scanner(System.in); int overHours2 = scan5.nextInt(); //Day 3 System.out.println("Wednesday: Enter the Number of Regular Hours: "); Scanner scan6 = new Scanner(System.in); int regHours3 = scan6.nextInt(); System.out.println("Wednesday: Enter the Number of Over Time Hours: "); Scanner scan7 = new Scanner(System.in); int overHours3 = scan7.nextInt(); //Day 4 System.out.println("Thursday: Enter the Number of Regular Hours: "); Scanner scan8 = new Scanner(System.in); int regHours4 = scan8.nextInt(); System.out.println("Thursday: Enter the Number of Over Time Hours: "); Scanner scan9 = new Scanner(System.in); int overHours4 = scan9.nextInt(); //Day 5 System.out.println("Friday: Enter the Number of Regular Hours: "); Scanner scan10 = new Scanner(System.in); int regHours5 = scan10.nextInt(); System.out.println("Friday: Enter the Number of Over Time Hours: "); Scanner scan11 = new Scanner(System.in); int overHours5 = scan11.nextInt(); } //Reformat and Calculate public int calcPay() { //Decimal Format DecimalFormat df = new DecimalFormat("#.##"); //Calculations int regHours = (regHours1 + regHours2 + regHours3 + regHours4 + regHours5); int overHours = (overHours1 + overHours2 + overHours3 + overHours4 + overHours5); } public double calcTotal() { double totalPay = (regHours * wage) + (overHours * (wage * 1.5)); } //Print Results public void printResults() { System.out.println("Total pay for the week is: $"+df.format(totalPay)); System.out.println("For Employee #" + employeeId); } }
I'm getting the error messages:
I've tried to fix the errors and I'm only getting more! Also, is there a way for me to simplify this code by using loops? I am new to Java, so any help would be much appreciated!
--------------------Configuration: Mod of 3.4 - JDK version 1.7.0_03 <Default> - <Default>--------------------
C:\Users\Gooon\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:41: error: non-static method inputID() cannot be referenced from a static context
inputID();
^
C:\Users\City\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:42: error: non-static method inputWage() cannot be referenced from a static context
inputWage();
^
C:\Users\Central\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:43: error: non-static method inputDays() cannot be referenced from a static context
inputDays();
^
C:\Users\New\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:44: error: non-static method calcPay() cannot be referenced from a static context
calcPay();
^
C:\Users\Mexico\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:45: error: non-static method calcTotal() cannot be referenced from a static context
calcTotal();
^
C:\Users\Is\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:46: error: non-static method printResults() cannot be referenced from a static context
printResults();
^
C:\Users\Hot\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:130: error: cannot find symbol
System.out.println("Total pay for the week is: $"+df.format(totalPay));
^
symbol: variable df
location: class Mod_of_3_4
7 errors
Process completed.
--------------------------------------------------------------------
This is the final, finished version of the file:
import java.text.*; //allows for the formatting of the decimal import java.util.Scanner; //for user input public class Modification_of_3_4 { public static void main(String[] args) { setAllVariables(); //sets the variables via user input with the scanner System.out.println(totalPay()); //The print result from totalpay() below } private static void setAllVariables() { getEmployeeName(); getHourWage(); setRegHours(); setOverHours(); //Calculates the total pay if (regHours + overHours > 168) { System.err.println("The Total Hours Must be " + "Less Than 168"); System.exit(0); } else { total = ((wage * regHours) + ((wage * 1.5) * overHours)); } } //Variables Listed Below will be used in the program private static void getEmployeeName() { System.out.println("Enter Employee's Name: "); Scanner scan = new Scanner(System.in); employeeName = scan.next(); } //Allows input of Employee's Name private static void getHourWage() { System.out.println("Enter the Hourly Wage: "); Scanner scan1 = new Scanner(System.in); wage = scan1.nextDouble(); } //Allows input of Hourly Wage private static void setRegHours() { System.out.println("Enter the Number of Regular Hours: "); Scanner scan2 = new Scanner(System.in); regHours = scan2.nextDouble(); if (regHours > 168) { System.err.println("The Max Hours Must be " + "Less Than 168"); System.exit(0); } } //Allows input of the # of Regular Hours private static void setOverHours() { System.out.println("Enter the Number of Overtime Hours: "); Scanner scan3 = new Scanner(System.in); overHours = scan3.nextDouble(); if (overHours > 168) { System.err.println("The Max Hours Must be " + "Less Than 168"); System.exit(0); } //Allows input of the # of Overtime Hours } public static String totalPay() { DecimalFormat df = new DecimalFormat("#.##"); String result = "For " + employeeName + "," + " the Total Pay is $" +df.format(total); return result; } //Formats, then prints the string //Variables private static String employeeName; //employee's name private static double wage; //hourly wage private static double regHours; //regular hours private static double overHours; //overtime hours private static double total; //total pay }