Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 25 of 25

Thread: Help with Methods

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Question Help with Methods

    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:




    --------------------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.
    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!


    --------------------------------------------------------------------
    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
    }
    Last edited by Blasfemmy; August 20th, 2014 at 02:33 PM.


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Help with Methods

    Read this line carefully from your errors:

    error: non-static method inputID() cannot be referenced from a static context

    Now, look at your main method and then the methods you called. It has to do with their declaration. The line I posted from your error line tells you everything you need to know.
    Last edited by bankston13; October 21st, 2012 at 04:56 PM.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Methods

    I'm still really confused, I attempted to declare the statement correctly but am just running into more errors

    public static void main(String[] args) {
     
    //Methods
    		public static inputID(int);
    		public static inputWage(double);
    		public static inputDays(int);
    		public static calcPay(int);
    		public static calcTotal(double);
    		public static printResults(double);
     
    	}

    I'm getting 30 errors now...

    --------------------Configuration: Mod of 3.4 - JDK version 1.7.0_03 <Default> - <Default>--------------------
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:41: error: illegal start of expression
    		public static inputID(int);
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:41: error: illegal start of expression
    		public static inputID(int);
    		       ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:41: error: ';' expected
    		public static inputID(int);
    		                     ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:41: error: not a statement
    		public static inputID(int);
    		                      ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:41: error: ';' expected
    		public static inputID(int);
    		                         ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:42: error: illegal start of expression
    		public static inputWage(double);
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:42: error: illegal start of expression
    		public static inputWage(double);
    		       ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:42: error: ';' expected
    		public static inputWage(double);
    		                       ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:42: error: not a statement
    		public static inputWage(double);
    		                        ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:42: error: ';' expected
    		public static inputWage(double);
    		                              ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:43: error: illegal start of expression
    		public static inputDays(int);
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:43: error: illegal start of expression
    		public static inputDays(int);
    		       ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:43: error: ';' expected
    		public static inputDays(int);
    		                       ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:43: error: not a statement
    		public static inputDays(int);
    		                        ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:43: error: ';' expected
    		public static inputDays(int);
    		                           ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:44: error: illegal start of expression
    		public static calcPay(int);
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:44: error: illegal start of expression
    		public static calcPay(int);
    		       ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:44: error: ';' expected
    		public static calcPay(int);
    		                     ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:44: error: not a statement
    		public static calcPay(int);
    		                      ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:44: error: ';' expected
    		public static calcPay(int);
    		                         ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:45: error: illegal start of expression
    		public static calcTotal(double);
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:45: error: illegal start of expression
    		public static calcTotal(double);
    		       ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:45: error: ';' expected
    		public static calcTotal(double);
    		                       ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:45: error: not a statement
    		public static calcTotal(double);
    		                        ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:45: error: ';' expected
    		public static calcTotal(double);
    		                              ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:46: error: illegal start of expression
    		public static printResults(double);
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:46: error: illegal start of expression
    		public static printResults(double);
    		       ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:46: error: ';' expected
    		public static printResults(double);
    		                          ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:46: error: not a statement
    		public static printResults(double);
    		                           ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:46: error: ';' expected
    		public static printResults(double);
    		                                 ^
    30 errors
     
    Process completed.

    I guess I'm just declaring the statement completely wrong? I tried to search Google for solutions to no avail... I don't even know what to search for besides the error message.

  4. #4
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Methods

    i'm getting less errors (18) with this version, but I don't know if I am even close to being on the right track.

    public static void main(String[] args) {
     
    //Methods
    		public void inputID();
    		public void inputWage();
    		public void inputDays();
    		public void calcPay();
    		public void calcTotal();
    		public void printResults();
     
    	}

    --------------------Configuration: Mod of 3.4 - JDK version 1.7.0_03 <Default> - <Default>--------------------
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:41: error: illegal start of expression
    		public void inputID();
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:41: error: illegal start of expression
    		public void inputID();
    		       ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:41: error: ';' expected
    		public void inputID();
    		                   ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:42: error: illegal start of expression
    		public void inputWage();
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:42: error: illegal start of expression
    		public void inputWage();
    		       ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:42: error: ';' expected
    		public void inputWage();
    		                     ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:43: error: illegal start of expression
    		public void inputDays();
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:43: error: illegal start of expression
    		public void inputDays();
    		       ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:43: error: ';' expected
    		public void inputDays();
    		                     ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:44: error: illegal start of expression
    		public void calcPay();
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:44: error: illegal start of expression
    		public void calcPay();
    		       ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:44: error: ';' expected
    		public void calcPay();
    		                   ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:45: error: illegal start of expression
    		public void calcTotal();
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:45: error: illegal start of expression
    		public void calcTotal();
    		       ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:45: error: ';' expected
    		public void calcTotal();
    		                     ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:46: error: illegal start of expression
    		public void printResults();
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:46: error: illegal start of expression
    		public void printResults();
    		       ^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:46: error: ';' expected
    		public void printResults();
    		                        ^
    18 errors
     
    Process completed.

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Help with Methods

    You're almost on the right track. You declared the methods in your main method correctly the first time around, so thats good. The fix you made in your second post is wrong. The reason I bolded the static and non-static keywords in my earlier post is because thats what your problem is. Your main method is always declared like this:

    public static void main(String[] args){
     
    }

    Your main method is a static method, and a non-static method can't be referenced by it. To fix your code, you just have to add one word to each of your method declarations that aren't the main method. Keep in mind that this portion of code doesn't work:

    public static void main(String[] args) {
     
    //Methods
    		public void inputID();
    		public void inputWage();
    		public void inputDays();
    		public void calcPay();
    		public void calcTotal();
    		public void printResults();
     
    	}

    You can't set a method to void or any other state in the main when you call it. That can only be done in the method declartation.

  6. The Following User Says Thank You to bankston13 For This Useful Post:

    Blasfemmy (October 22nd, 2012)

  7. #6
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Methods

    I tried to set the method headers lower in the code as "static" to make it so that it is universally static, thus removing the errors?:

    public static void main(String[] args) {
     
    //Methods
    		public inputID();
    		public inputWage();
    		public inputDays();
    		public calcPay();
    		public calcTotal();
    		public printResults();
     
    	}
     
    //Employee ID Number	
    	public static int inputID()...
    //Hourly Wage
    	public static double inputWage()...
    //Input Daily Hours
    	public static int inputDays()...
    //Reformat and Calculate
    	public static int calcPay()...
            public static double calcTotal()...
    //Print Results
    	public static double printResults()...

    But it is returning errors all over the place again ><

    --------------------Configuration: Mod of 3.4 - JDK version 1.7.0_03 <Default> - <Default>--------------------
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:41: error: illegal start of expression
    		public inputID();
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:42: error: illegal start of expression
    		public inputWage();
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:43: error: illegal start of expression
    		public inputDays();
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:44: error: illegal start of expression
    		public calcPay();
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:45: error: illegal start of expression
    		public calcTotal();
    		^
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:46: error: illegal start of expression
    		public printResults();
    		^
    6 errors
     
    Process completed.

    How can I properly start this expression?
    Last edited by Blasfemmy; October 21st, 2012 at 09:16 PM.

  8. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with Methods

    Did you read the above help?
    Did you understand it? If not ask for clarification. Don't go off and make random changes to your code without a clear understanding. You have placed all your other methods (not that they can really be called methods) INSIDE the main method. This is a no-no.
    Improving the world one idiot at a time!

  9. The Following User Says Thank You to Junky For This Useful Post:

    Blasfemmy (October 22nd, 2012)

  10. #8
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Methods

    Okay, so I messed around with the code a bit and now i've got it down to only one error!

    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
    		public int inputID();
    		public double inputWage();
    		public int inputDays();
    		public int calcPay();
    		public double calcTotal();
    		public [B]RETURN TYPE HERE[/B] printResults();
     
     
     
    //Employee ID Number	
    	public static int inputID() {
     
    		System.out.println("Enter Employee ID: ");
    		Scanner scan = new Scanner(System.in);
    		int employeeId = scan.nextInt();
     
    	}
    //Hourly Wage
    	public static double inputWage() {
     
    		System.out.println("Enter the Hourly Wage: ");
    		Scanner scan3 = new Scanner(System.in);
    		double wage = scan3.nextDouble();
     
    	}
    //Input Daily Hours
    	public static 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 static int calcPay() {
     
    	//Decimal Format
    		DecimalFormat df = new DecimalFormat("#.##"); 
     
    	//Calculations		
    		int regHours = (regHours1 + regHours2 + regHours3 + regHours4 + regHours5);
    		int overHours = (overHours1 + overHours2 + overHours3 + overHours4 + overHours5);
    			}	
    	public static double calcTotal() {
    		double totalPay = (regHours * wage) + (overHours * (wage * 1.5));
    	}
    //Print Results
    	public static [B]RETURN TYPE HERE[/B] printResults() { /[B]/this has to match the declared method return type?[/B]
     
    		System.out.println("Total pay for the week is: $"+df.format(totalPay));
    		System.out.println("For Employee #" + employeeId);
    	}
    }

    With the error:
    --------------------Configuration: Mod of 3.4 - JDK version 1.7.0_03 <Default> - <Default>--------------------
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:46: error: invalid method declaration; return type required
    public printResults();
    ^
    1 error

    Process completed.
    What return type should I use for this? I tried to put a double in and it returned 29 errors. Is this a logical error in the code, or am I using the wrong return type? Thanks for all the help thus far!
    Last edited by Blasfemmy; October 22nd, 2012 at 09:57 AM.

  11. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with Methods

    There are multiple errors throughout the code.
    Take a break from code writing and focus on some research.
    Search keywords:
    Java methods return types


    Note how the samples work. If the method says it returns a double, it returns a double. If it says it returns a String, it returns a String.

    Example:
    // A method in a class that returns an int
    public static int sum(int a, int b) {
       int result = a + b;
       return result;
    }
     
    //A method in a class that returns a String
    public String toString {
        String result = "There are " + numOfParts + " parts at " + warehouseName + ".";
        return result;
    }
     
    //A method in a class that returns nothing
    public static void main(String[] args) {
        System.out.println(this.toString());
    }
    Note that methods with a return type of anything but void (the NO return, return type), has a return statement of the same type.

  12. #10
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Methods

    I'm still confused on how this works >< I can't take a break from code writing, this is a project due tomorrow, and I've been trying to figure this out for almost a week now. Looking at the posted code above, it doesn't look like that is what I am doing. I simplified the code a lot so that it isn't such a huge file and it does less than before. I'm not getting these errors as before, but am getting zero, no matter what I input.

     
    import java.util.Scanner; //Takes user input
    import java.text.*;  //Decimal Format Import
     
    public class Mod_of_3_4 {
     
    	public static int employeeId;
    	public static double regHours;
    	public static double overHours;
    	public static double wage;
    	public static double totalPay;
     
     
    public static void main(String[] args) {
     
    	}
     
      //Methods
    		inputID();
    		inputWage();
    		inputDays();
    		calcPay();
    		printResults();
     
    //Employee ID Number	
    	public static int inputID() {
     
    		System.out.println("Enter Employee ID: ");
    		Scanner scan = new Scanner(System.in);
    		int employeeId = scan.nextInt();
     		return;
    	}
    //Hourly Wage
    	public static double inputWage() {
     
    		System.out.println("Enter the Hourly Wage: ");
    		Scanner scan3 = new Scanner(System.in);
    		double wage = scan3.nextDouble();
    		return;
    	}
    //Input Daily Hours
    	public static double inputDays() {
     
    		System.out.println("Enter the Number of Regular Hours: ");
    		Scanner scan1 = new Scanner(System.in);
    		double regHours = scan1.nextDouble();
     
    		System.out.println("Enter the Number of Overtime Hours: ");
    		Scanner scan2 = new Scanner(System.in);
    		double overHours = scan2.nextDouble();
    		return;
    	}		
    //Reformat and Calculate
    	public static double calcPay() {
    		double totalPay = (regHours * wage) + (overHours * (wage * 1.5));
    		return;
    	}
    //Print Results
    	public static double printResults() {
    		DecimalFormat df = new DecimalFormat("#.##"); 
     		System.out.println("Total pay for the week is: $"+df.format(totalPay));
    		System.out.println("For Employee #" + employeeId);
    		return;
    	}
    }

    Build Output With User Input:

    --------------------Configuration: Mod of 3.4 - JDK version 1.7.0_03 <Default> - <Default>--------------------
    Enter Employee ID: 
    1234
    Enter the Hourly Wage: 
    17.25
    Enter the Number of Regular Hours: 
    36.5
    Enter the Number of Overtime Hours: 
    17.75
    Total pay for the week is: $0
    For Employee #0
     
    Process completed.

    Does this problem have to do with the return types?

  13. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with Methods

    Well the code was updated to include return statements. But. What do they return.
    Look at my examples. Notice I declared a variable of the same type as the method specifies, and named this variable result.
    At the end this variable containing a value is returned by the return statement.

    The code you currently posted is the same thing as placing two quarters in a box and saying "I have a box of money". No one has a clue how much money a box of money is. You have to specify what you put in the box.
    "I have $0.50"
    Now that has meaning.

    Saying:
    return;
    returns no value. The print out is zero because that is the value in the variable when it is printed.




    Does this problem have to do with the return types?
    The fact that the methods return nothing? yes.
    The fact that the instance variables have the value 0 when printed? no
    The reason for that is declaring new variables in the methods, setting the values in the method variables, and exiting the method without setting the value in the instance variables.

    When you type:
    double d;
    you create a variable named d of type double.
    If there is a variable named d in a higher scope, it is hidden in the shadow of the new variable d.

    So when the code says:
    double wage = scan3.nextDouble();
    inside the input wage method, this variable named wage hides the other variable you have named wage. This local wage, gets the value set, then the method exits and the value goes away forever. The wage variable as an instance variable was never touched or even seen.

  14. The Following User Says Thank You to jps For This Useful Post:

    Blasfemmy (October 22nd, 2012)

  15. #12
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Methods

    Okay, that made a lot of sense! How can I set it so it actually returns a value? Would I have to declare a new variable and set what value is returned equal to that variable?

    Here is my attempt:

    import java.util.Scanner; //Takes user input
    import java.text.*;  //Decimal Format Import
     
    public class Mod_of_3_4 {
     
    	public static int employeeId;
    	public static double regHours;
    	public static double overHours;
    	public static double wage;
    	public static double totalPay;
     
     
    public static void main(String[] args) {
     
       //Methods
    		inputID();
    		inputWage();
    		inputDays();
    		calcPay();
    		printResults();
    	}
     
     
    //Employee ID Number	
    	public static int inputID() {
     
    		System.out.println("Enter Employee ID: ");
    		Scanner scan = new Scanner(System.in);
    		int employeeId = scan.nextInt();
    		int result = employeeId;
     		return result;
    	}
    //Hourly Wage
    	public static double inputWage() {
     
    		System.out.println("Enter the Hourly Wage: ");
    		Scanner scan3 = new Scanner(System.in);
    		double wage = scan3.nextDouble();
    		double result = wage;
     		return result;
    	}
    //Input Daily Hours
    	public static double inputDays() {
     
    		System.out.println("Enter the Number of Regular Hours: ");
    		Scanner scan1 = new Scanner(System.in);
    		double regHours = scan1.nextDouble();
     
    		System.out.println("Enter the Number of Overtime Hours: ");
    		Scanner scan2 = new Scanner(System.in);
    		double overHours = scan2.nextDouble();
    		double result = overHours;
     		return result;
    	}		
    //Reformat and Calculate
    	public static double calcPay() {
    		double totalPay = (regHours * wage) + (overHours * (wage * 1.5));
    		double result = totalPay;
     		return result;
    	}
    //Print Results
    	public static double printResults() {
    		DecimalFormat df = new DecimalFormat("#.##"); 
     		String result = ("For Employee #" + employeeId + ", the Total Pay for the Week is $" + df.format(totalPay)));
     		return result;
    	}
    }

    With the error:


    --------------------Configuration: Mod of 3.4 - JDK version 1.7.0_03 <Default> - <Default>--------------------
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:72: error: ';' expected
    String result = ("For Employee #" + employeeId + ", the Total Pay for the Week is $" + df.format(totalPay)));
    ^
    1 error

    Process completed.
    Note: In the actual build messages window, the "^" is underneath the ";" at the end of the statement.

    To me, this sounds like a logical error in the formatting of the decimal place, right? After testing it, it seems as though it is still not returning any sort of value...
    Last edited by Blasfemmy; October 22nd, 2012 at 03:22 PM.

  16. #13
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Methods

    I tried again to get it to return a value, this time looking a lot closer at the example and still can't get a return value:

     
    import java.util.Scanner; //Takes user input
    import java.text.*;  //Decimal Format Import
     
    public class Mod_of_3_4 {
     
    	public static int employeeId;
    	public static double regHours;
    	public static double overHours;
    	public static double wage;
    	public static double totalPay;
     
     
    public static void main(String[] args) {
     
       //Methods
       		result = inputID();
       		result1 = inputWage();
       		result2 = inputDays();
       		result3 = inputOverDays(); 
       		result4 = calcPay();
       		result5 = printResults();
     
     
    		inputID();
    		inputWage();
    		inputDays();
    		inputOverDays();
    		calcPay();
    		printResults();
    	}
     
    //Employee ID Number	
    	public static int inputID() {
     
    		System.out.println("Enter Employee ID: ");
    		Scanner scan = new Scanner(System.in);
    		int employeeId = scan.nextInt();
    		int result = employeeId;
     		return result;
    	}
    //Hourly Wage
    	public static double inputWage() {
     
    		System.out.println("Enter the Hourly Wage: ");
    		Scanner scan3 = new Scanner(System.in);
    		double wage = scan3.nextDouble();
    		double result1 = wage;
     		return result1;
    	}
    //Input Daily Hours
    	public static double inputDays() {
     
    		System.out.println("Enter the Number of Regular Hours: ");
    		Scanner scan1 = new Scanner(System.in);
    		double regHours = scan1.nextDouble();
     		double result2 = regHours;
     		return result2;
     
    	}
     
     	public static double inputOverDays() {
     
    		System.out.println("Enter the Number of Overtime Hours: ");
    		Scanner scan2 = new Scanner(System.in);
    		double overHours = scan2.nextDouble();
    		double result3 = overHours();
     		return result3;
     
    	}		
    //Reformat and Calculate
    	public static double calcPay() {
    		double totalPay = (regHours * wage) + (overHours * (wage * 1.5));
    		double result4 = totalPay;
     		return result4;
    	}
    //Print Results
    	public static double printResults() {
    		DecimalFormat df = new DecimalFormat("#.##"); 
     		double result5 = ("For Employee #" + employeeId + ", the Total Pay for the Week is $" + df.format(totalPay)));
     		result5 = printResults();
     		return printResults();
    	}
    }

    With the one error message of

    --------------------Configuration: Mod of 3.4 - JDK version 1.7.0_03 <Default> - <Default>--------------------
    C:\Users\Andrew\Documents\JCreator Pro\MyProjects\Mod of 3.4\src\Mod_of_3_4.java:86: error: ';' expected
    double result5 = ("For Employee #" + employeeId + ", the Total Pay for the Week is $" + df.format(totalPay)));
    ^
    1 error

    Process completed.

  17. #14
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with Methods

    Comments and such left out to keep this post shorter.

    Your typical HelloWorld program.
    public class HelloWorld {
     
    	public static void main(String[] args) {
    		System.out.println("Hello World!");
    	}
    }
    A HelloWorld program showing how to use a local variable.
    public class HelloWorldVariables {
     
    	public static void main(String[] args) {
    		String helloWorld = "Hello World!";
    		System.out.println(helloWorld);
    	}
    }
    Notice the method in the following example claims to return a value that is of type String. It says so in the declaration:
    public static String helloWorld()
    right there where it says String.
    Look at the method how a variable of type String is created. Named result. Notice how the return statement says to return this thing that is of type String and has a value of "Hello World". Finally notice the use of the method name, in the same manner as we would use a variable name that is also of type String. Exactly as in the sample program above.
    public class HelloWorldMethods {
     
    	public static void main(String[] args) {
     
    		System.out.println(helloWorld());
    	}
     
    	/** Returns a String containing the awesome words of programming
    	 * @return Returns the string "Hello World"*/
    	public static String helloWorld() {
    		String result = "Hello World";
    		return result;
    	}
     
    }
    On this final example, note how the class variables are set in setVariables(). See how the variables do not have the type before the variable name, as compared to the methods in the code you posted where you put:
    double wage = .........
    When you type double wage = you declare a new variable of type double. Here the code is:
    hey = "Hello";
    Try this. Run the program below, then add the keyword String to the front of that line. So it becomes:
    String hey = "Hello";
    Then try to run the program again and compare the results to the previous run.
    public class HelloWorldMethodsAndVariables {
     
    	public static void main(String[] args) {
    		setVariables();
     
    		System.out.println(helloWorld());
    	}
     
    	/**This method is just to pretend something important happened, and the variables were set*/
    	private static void setVariables() {
    		hey = "Hello";
    		there = "World";
    	}
     
    	/** Returns a String containing the awesome words of programming
    	 * @return Returns a String made from variables in the class*/
    	public static String helloWorld() {
    		String result = hey + " " + there;
    		return result;
    	}
     
    	private static String hey;
    	private static String there;
    }

  18. #15
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with Methods

    I stopped for food during my last post, so I did not read your last post..

    look at this method:
    //Hourly Wage
    public static double inputWage() {

    System.out.println("Enter the Hourly Wage: ");
    Scanner scan3 = new Scanner(System.in);
    double wage = scan3.nextDouble();
    double result1 = wage;
    return result1;
    }
    Notice how you are setting the value to wage, (although incorrectly as described at the bottom of my previous post), you are also attempting to return this value from the method. You don't need to do both. Look at the final example in my previous post. See how the method setVariables gives the class variables a value, and does no return at all. Look at the helloWorld method, how it uses the return value. It says, go back to where my method name was typed in the code, and instead put the value of my return statement. That return statement is a variable. Ok swap that variable name with the value in the variable, and finally put THAT value in where the method name was, so that System.out.println can print it

  19. #16
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Methods

    Okay, the first time running it, it outputs "Hello World" as expected. The second time it outputs "null null", is this because the "String" is conflicting with the "void" in the header, thus making the whole value null? So the header defines what the return type will be for the rest of the code in it?

  20. #17
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with Methods

    Quote Originally Posted by Blasfemmy View Post
    Okay, the first time running it, it outputs "Hello World" as expected. The second time it outputs "null null", is this because the "String" is conflicting with the "void" in the header, thus making the whole value null? So the header defines what the return type will be for the rest of the code in it?
    Not exactly. When you add String to the line hey = "Hello", you are creating a whole new variable with the same name as a different variable. This newly created variable is deleted after the method body exits, and nothing happens to that 'other' variable with the same name. Without the keyword String in the way on that line, the variable getting set is the variable of the class that has that name.
    The reason why. When the compiler sees a variable name, the current scope is checked. Think of the scope as the inner-most {} which contain the line of code in question, as the current scope. So for the method setVariables() the scope starts at:
    setVariables() {
    and ends at:
    }
    located at the end of the method body. When a line of code inside those brackets has a variable name, the current scope is checked for a variable name. In the case with String hey = "Hello" , the variable is being declared and there is absolutely no way you could mean any other variable, because you are creating one and giving it a value. In the case of hey = "Hello" , without the String, the variable named hey does not exist within the scope of the method body. This is when the compiler looks up to the next set of brackets which contain these brackets. This is the next level of scope. In this case it happens to be the class { }.
    The class does have a variable named hey, so the compiler assumes this is the variable you wanted to put a value in, and that is what happens.

    When the String is there, the program prints null because the instance variables were never set and null is the value in the instance variable when it printed.

  21. #18
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with Methods

    Quote Originally Posted by Blasfemmy View Post
    So the header defines what the return type will be for the rest of the code in it?
    Yes. If by header you mean a line of code that looks like:
    public String toString()

    public static void main(String[] args) {
    void, needs no return statement

    public static String helloWorld() {
    String, must return a String

    scan3.nextDouble();
    Using a method that returns a double, the method might look like this:
    public double nextDouble()

  22. #19
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Methods

    I think I finally understand this concept! Here is what I have:

     
    import java.util.Scanner; 
     
    public class Modification_of_3_4 {
     
    	public static void main(String[] args) {
    		setVariables();
     
    		System.out.println(totalPay());
    	}
     
     
    	private static void setVariables() {
    		System.out.println("Enter Employee ID: ");
    		Scanner scan = new Scanner(System.in);
    		employeeId = scan.nextInt();
     
    		System.out.println("Enter the Hourly Wage: ");
    		Scanner scan1 = new Scanner(System.in);
    		wage = scan1.nextDouble();
     
    		System.out.println("Enter the Number of Regular Hours: ");
    		Scanner scan2 = new Scanner(System.in);
    		regHours = scan2.nextDouble();
     
    		System.out.println("Enter the Number of Overtime Hours: ");
    		Scanner scan3 = new Scanner(System.in);
    		overHours = scan3.nextDouble();
     
    		total = ((wage * regHours) + ((wage * 1.5) * overHours));
    	}
     
     
    	public static String totalPay() {
    		String result = "For Employee #" + employeeId + "," + " the Total Weekly Pay is $" + total;
    		return result;
    	}
     
    	private static int employeeId;
    	private static double wage;
    	private static double regHours;
    	private static double overHours;
    	private static double total;
    }

    It returns no errors, and I get a value that the output mathematically makes sense!

    --------------------Configuration: Modification of 3.4 - JDK version 1.7.0_03 <Default> - <Default>--------------------
    Enter Employee ID:
    1
    Enter the Hourly Wage:
    10
    Enter the Number of Regular Hours:
    10
    Enter the Number of Overtime Hours:
    10
    For Employee #1, the Total Weekly Pay is $250.0

    Process completed.
    Does the code look right?
    Last edited by Blasfemmy; October 22nd, 2012 at 05:23 PM.

  23. #20
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with Methods

    Well it looks like you are starting to understand how these basic parts work. Great.
    Now on to another important topic.

    Think of the code you started with, how it had all of these different methods. That is the better way to go. With the current code, consider what has to be done if the employee gets a raise. Or works another day. Each variable has to be set again because it is all in one method. Now that you can make methods pass values and set variables, break the code back up into methods that accomplish just one task.
    setWage
    setHoursWorked
    getOvertimeHours
    what ever you may end up with....
    so instead of calling a method named setVariables, just call each method that sets each variable.
    Although you could have a method named setAllVariables that just calls each necessary method to set all of the variables, and just call setAllVariables from main the way you do now. It does not seem necessary in this case.

  24. #21
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Methods

    Great idea!

    Is this what you were referring to? With this I am able to simply delete the variable listed under "setAllVariables" and it won't be included in the execution of the program.

    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() {
    		getEmployeeId();
    		getHourWage();
    		setRegHours();
    		setOverHours();
     
     
    		//Calculates the total pay
     
    		total = ((wage * regHours) + ((wage * 1.5) * overHours));
    	} //Variables Listed Below will be used in the program
     
     	private static void getEmployeeId() {
     
     
    		System.out.println("Enter Employee ID: ");
    		Scanner scan = new Scanner(System.in);
    		employeeId = scan.nextInt();
     
    		} //Allows input of Employee ID #
     
    	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();
     
    		} //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();
     
    		} //Allows input of the # of Overtime Hours
     
    	public static String totalPay() { 
    		DecimalFormat df = new DecimalFormat("#.##"); 
    		String result = "For Employee #" + employeeId + "," + " the Total Pay is $" +df.format(total);
    		return result;
    	} //Formats, then prints the string
     
     //Variables
    	private static int employeeId; //employee id number
    	private static double wage; //hourly wage
    	private static double regHours; //regular hours
    	private static double overHours; //overtime hours
    	private static double total; //total pay
    }

  25. #22
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Methods

    With this update, I was able to use If-Else statements to check to make sure that the user does not input more hours than there are in a week (168). At the moment, it exits the program. How would I make it bring it back a step? For example: Instead of having
    "How many Regular Hours: 999
    You cannot work more hours than there are in a week
    Process Completed"
    it would be:

    "How many Regular Hours: 999
    You cannot work more hours than there are in a week
    "How many Regular Hours: 40
    ..."

    Current Code:

    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() {
    		getEmployeeId();
    		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 getEmployeeId() {
     
     
    		System.out.println("Enter Employee ID: ");
    		Scanner scan = new Scanner(System.in);
    		employeeId = scan.nextInt();
     
    		} //Allows input of Employee ID #
     
    	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 Employee #" + employeeId + "," + " the Total Pay is $" +df.format(total);
    		return result;
     
    	} //Formats, then prints the string
     
     //Variables
    	private static int employeeId; //employee id number
    	private static double wage; //hourly wage
    	private static double regHours; //regular hours
    	private static double overHours; //overtime hours
    	private static double total; //total pay
    }
    Last edited by jps; October 22nd, 2012 at 06:47 PM. Reason: code=java tag

  26. #23
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with Methods

    Please use [code=java] in place of [code] when you post code. That is how to get the keyword highlighting in the code. It is so much easier to read. I edited the post above and changed it to look at the code this time.

    Think about the problem you have. Think about how to solve the problem.

    Write down steps to solve the problem.

    Organize the steps into some type of order and fill in missing details.

    Once you have a complete set of ideas on how to solve the problem start writing code to do what the steps say to do.




    Here is an example of what I mean.
    Problem:
    I need to get a message to mom.

    Solution:
    Call mom and give her the message.

    Steps:
    Get mom's number
    Dial mom's number
    Give her the message
    Wait for her to answer
    Say hi
    Find my phone

    Steps in some kind of order:
    Find my phone
    Get mom's number
    Dial mom's number
    Wait for her to answer
    Say hi
    Give her the message

    public class PhoneMomSomeMessage() {
       public static void main(String[] args) {
          if(findMyPhone()) {
             if(phone.hasNumber(momsNumber)) {
                phone.dial(momsNumber);
                waitForAnswer();
                openGreeting();
                relayImportantMessage();
             }
             else {
                takeStepsToFindMomsNumberAnotherWay();
             }
          }
          else {
             takeStepsToFindPhoneAnotherWay();
          }
       }
     
     
       /**Returns true if phone is found without getting up, false otherwise*/
       private boolean findMyPhone() {
          if(phoneIsOnDesk() || phoneIsInPocket) {
             return true;
          }
          return false;
       }
    }

    So the code eventually takes shape determined by the steps. Note that this does not show a sample of how to solve your problem, it shows a sample of how to write code that follows a plan. If you get stuck with your plan or code feel free to ask questions.

  27. The Following User Says Thank You to jps For This Useful Post:

    Blasfemmy (October 22nd, 2012)

  28. #24
    Member
    Join Date
    Oct 2012
    Posts
    35
    My Mood
    Fine
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Methods

    Sorry about not using the code=java. I think now that I have a better grip on the syntax for java, progressing forward will be a lot easier. For some reason this concept just wasn't clicking. Thanks for all of the help! I really appreciate you taking all of this time to break down this topic, and working with me until I understood it. When you break down the problem into steps, it seems as though finding a solution is a lot simpler than making things up as you go. Thanks again!

  29. #25
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Help with Methods

    I am glad it is working out.

Similar Threads

  1. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  2. Help with methods?
    By THeAnnonymous in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 09:27 AM
  3. I need help with my methods please?
    By THeAnnonymous in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 11th, 2011, 09:22 AM
  4. I need help with METHODS!!!
    By Slone in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 14th, 2010, 08:33 AM
  5. methods help
    By hockey87 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 9th, 2010, 11:57 PM