Hello, I am a little stuck. I have grasped the concept of methods, understanding thier purpose but still unsure on how to code it. Below is part of my code I kindly need guidance on. I am trying to create a method which can hold roles and depending on which switch option is selected it will return the the amount. I have four employement roles which all have different payscales. I have commented the errors on the role method. IfI can correct this and make it work then I can complete this program :)

import java.util.Scanner;//allows input from the keyboard
import java.text.*;
class Wage_Method_2 // a class is a collection of static and non-static methods and variables/class name
{
    public static void main(String[]args)//The main method; starting point of the program.
    {
        Scanner input = new Scanner(System.in);//declaring and creating scanner; an inputstream / start of class
        DecimalFormat fmt = new DecimalFormat("0.00");
        //DecimalFormat Currency = new DecimalFormat("###,###,##0.00");


        double normHrs = 0, overHrs = 0, bonusHrs = 0, actualHrs = 0, cash=0, overPay =0, bonusPay = 0, grossWage = 0,vat23 = 0, netWage =0;
        String empName,nextEMP = "y", manager, superVisor, teamLead, general; // declaring variables

        System.out.println ("\t\t************************"); 
        System.out.println ("\t\tHUMAN RESOURCES: PAYROLL"); // displays title of program
        System.out.println ("\t\t************************"); 


        while (nextEMP.equalsIgnoreCase("y"))// this loops around the entire program to allow the user to repeat adding in emp hours
        {
            System.out.print("Please enter an employee's first name: "); // name of employee
            empName = input.next();//their name
            System.out.print("Please enter "+empName+"'s employment ID: "); // name of employee
            int job = input.nextInt();//their name
            System.out.print("Please enter their total hours worked: ");// hours they have worked
            actualHrs = input.nextDouble();// their hours

            double basicPay = rate (5.75)*actualHrs;

            System.out.println("Basic Pay: "+basicPay);

            if (nextEMP.equalsIgnoreCase("n")) // if "n" is entered...
            {
                System.out.println("Thank you, the hours have been successfully logged.");// polite exit message

            }   

    }// end of whileloop
    }// end of class

    public static double rate (double cash String jobrole)// when compiling these errors appear: error: ')' expected   and error: <identifier> expected

        {

        switch (jobrole)
        {
            case 1: 
            cash = 5.75;
            case 2:
            cash = 5.75*1.1;
            case 3:
            cash = 5.75*1.25;
            case 4:
            cash = 5.75*1.42;
            break;  
        }
        return (cash);

        }

}// end of program

Here's a clue...

Methods are ways to break up your program into pieces to allow managibility.

You may not know but this in itself is a method and can be used as a great example.

public static void main(String[] args) {

}

A class method is a single interaction point with an instance of the class. It should do only one thing, and do it well. There are methods to access state data, and methods to change/set them. The setter methods should be written to properly validate the changes the caller is asking for, and return or throw an appropriate error if the input is invalid. These validation rules can be simple, or very complex, depending upon the situations.

FWIW in your rate() example, there is a missing comma in the argument list between the two arguments. Also you don't have a default value for a default state where jobrole is invalid. In that case, it should either return a known invalid value (like -1) or it should throw an error. The latter (throw error) is more acceptible these days.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.