Like many of the posts I see out here I have decided to learn Java. It is a bit more than I bargained for. From what I see every class in the world starts with a payroll program, mine is no different.
I have done ok with it up to this point, but I have been asked to take my current working program, and alter it.

import java.util.Scanner; //uses class Scanner


public class Payroll
{
public static void main(String[] args)
{
// create another Scanner to obtain name from command window
Scanner input2 = new Scanner(System.in);


char eName; // name of employee


// create Scanner to obtain input from command window
Scanner input = new Scanner(System.in);


double rate; // hourly rate of employee
double hours; // hours worked by employee


double result; // product of rate and hours


System.out.print("Enter Employee's Name or Enter STOP to Exit: "); // prompt for name
String name = input2.nextLine(); //name input from user


while (!(name).equalsIgnoreCase("STOP"))
{


System.out.print("Enter Employee's Hourly Pay Rate: "); // prompt for rate
rate = input.nextDouble(); // rate input from user.
while (rate <= 0)
{
System.out.print("Rate Must Be Greater Than Zero. Enter Rate: ");
rate = input.nextDouble(); // rate loop from user.
} // End while


System.out.print("Enter Hours Employee Worked This Week: "); // prompt for hours worked
hours = input.nextDouble(); // hours student worked this week.
while (hours <= 0)
{
System.out.print("Hours Worked Must Be Greater Than Zero. Enter Hours: ");
hours = input.nextDouble(); // Hours loop from user
} // End while


result = rate * hours; // figures students salary


System.out.print(name); // display name
System.out.printf("'s salary for the week is %c%.2f\n", '$', result); //display salary


System.out.print("Enter Employee's Name or Enter STOP to Exit: "); // internal loop prompt
name = input2.nextLine(); //name input from user


} // End While
System.out.print("Ending Program.");
}


}

I have been told I need two classes, a constructor to initialize the Employee object, and methods to compute weekly pay and such. I am confused and a bit lost at the moment.
This is what I have tried: (first class)

public class Payroll
{
public static void main(String[] args)
{
// create Employee object nextEmployee
// pass employee name to constructor
Employee nextEmployee = new Employee("stupid");


nextEmployee.displayMessage(); // display message
}
} // end class Payroll

and (second class)

import java.util.Scanner; //uses class Scanner


public class Employee
{
private String employeeName; // name of employee


// constructor inititalizes employeeName
public Employee(String eName)
{
employeeName = eName; // initializes employeeName
} // end constructor


// method to set the employee's name
public void setEmployeeName(String eName)


{
employeeName = eName; // store the employee name
} // end method setEmployeeName


// method to retrieve the course name
public String getEmployeeName()
{
return employeeName;
} // end method getEmployeeName


// display message
public void displayMessage()
{
// getEmployeeName gets the name of the employee
System.out.printf("Please Enter Employee's Name: \n%s!\n\n",
getEmployeeName() );
} //end method display message


}

The resulting program compiles, but will only work if I actually put something in the parenthesis of the first class. I still need the program to prompt and store whatever name is entered. I want to get the name part working and understood before I move on to the math, but I am completely stumped.
Can anyone offer any direction?

Recommended Answers

All 22 Replies

No way I can understand so much of code without code tags..

Not sure what a "code tag" is, but I have just about every other line documented.
Does not matter, I solved it myself.
Thanks for the reply. :o)

Code tag is the hash sign "#" in the toolbar when you create post or make answer to post in advanced view. This will paste [ c o d e] and [ / c o d e] in text area and you put the code you wish to post between these two tags. Also you may type these tags stright into message too.

ext thing, it will be nice if you can post solution to given problem for other people to see it

Ok, let me give this a try. I will put my first class between these code tags

public class Payroll
{
	public static void main(String[] args)	
	{
	// create Employee object nextEmployee
	// pass employee name to constructor
	Employee nextEmployee = new Employee();
	
	nextEmployee.displayMessage(); // display message
	}
} // end class Payroll

I used that class to call the class that does all the work. I will put it between these code tags.

mport java.util.Scanner; //uses class Scanner


public class Employee
{
	// Employee class has 3 fields
	public char eName;
	public double rate;
	public double hours; 
	public double result;
	
	// Employee class has one constructor
	public void Employee(char newEname)
		{ // initialize name of employee
		eName = newEname;
		}
	     
	// Employee class has four methods
	
		// get the employee name
		public String getEmployeeName(String eName)
		{	
		return eName;
		}
		
		// display message
		public void displayMessage()
		{
		Scanner newEname = new Scanner(System.in);
		System.out.print("Enter Name: ");
		String name = newEname.nextLine();
		
		while (!(name).equalsIgnoreCase("STOP"))
		
	{
				Scanner input = new Scanner(System.in);
		
		System.out.print("Enter Employee's Hourly Pay Rate: "); // prompt for rate
		rate = input.nextDouble(); // rate input from user.
		while (rate <= 0)
		{
			System.out.print("Rate Must Be Greater Than Zero. Enter Rate: ");
			rate = input.nextDouble(); // rate loop from user.
		} // End while
		
		System.out.print("Enter Hours Employee Worked This Week: "); // prompt for hours worked
		hours = input.nextDouble(); // hours student worked this week.
		while (hours <= 0)
		{
			System.out.print("Hours Worked Must Be Greater Than Zero. Enter Hours: ");
			hours = input.nextDouble(); // Hours loop from user
		} // End while
        
		result = rate * hours; // figures students salary
        
		System.out.print(name); // display name
		System.out.printf("'s salary for the week is %c%.2f\n", '$', result); //display salary
		
		System.out.print("Enter Employee's Name or Enter STOP to Exit: "); // internal loop prompt
		name = newEname.nextLine(); //name input from user
						
	} // End While
	System.out.print("Ending Program.");
		}
}

Proper use of code tags? Or should I place them throughout my code, perhaps at each method or something?

The only thing I am thinking about it cleaning up this code somewhat. Separating it in to more methods, using some set and get statements for rate, hours, and all that so I can place up in the main class. Is that good technique? Is it even possible without creating more sub classes?

Proper use of code tags? Or should I place them throughout my code, perhaps at each method or something?

The only thing I am thinking about it cleaning up this code somewhat. Separating it in to more methods, using some set and get statements for rate, hours, and all that so I can place up in the main class. Is that good technique? Is it even possible without creating more sub classes?

Yes, those code tags are fine :) You don't need to break out all of the methods, code tags just preserve the formatting so indentation and such remains visible and the blocks are easily seen.

You could certainly add methods to get and set other properties on your Employee, you don't need any subclasses for that. If you add getters and setters for those properties, make the variables private to the class.

Can someone explain this? It is my understand that a constructor has no return type, not even void. So in my employee class I removed the "void" from what I had labeled as a constructor, thinking in reality it was just another method
So it then looked like this:

public Employee(char newEname)
		{ // initialize name of employee
		eName = newEname;
		}

It still compiled fine so I thought I was good.
I went back to recomplie my main class just to be safe, and now it no longer works. I receive this message:

Payroll.java:7: cannot find symbol
symbol  : constructor Employee()
location: class Employee
   Employee nextEmployee = new Employee();

I do not understand. I have played with everything in this line of code I can think of, but with no void in the Employee class, the Payroll class will not run. I put void back in and everything is great.
???

Actually, the constructor does have a return type - it is an object instance of your class. That is it's entire purpose. Your first "constructor":

public void Employee(char newEname)
        { // initialize name of employee
        eName = newEname;
        }

wasn't really a constructor. It was a method called Employee. The reason your code worked previously is that when no constructor is specified at all, the compiler will provide a default no-argument constructor for the class. If you specify a constructor, as you did by removing the return type, you no longer had the default Employee() constructor and so it failed. If you want to create the employee with no parameter to the constructor, you will need to give it a default constructor like so:

public Employee(){
    // whatever you need to do to set up the object
}

Actually though, you probably want the main program to obtain the name of the employee (as you do in the displayMessage() method) and pass that name to your constructor with the name parameter. The name also should be a string, since char will only hold a single character.

Thank you for that explaination. You were 100% correct.
I appreciate you help.

Hey guys, I am trying something different to try and clean this up, but I must be missing something. I am trying to use set and get commands in place of what I have above, so I have changed (starting with employee name) my code to this:

import java.util.Scanner; //uses class Scanner


public class Employee
{
    // Employee class has 4 fields
    public char eName;
    public double rate;
    public double hours; 
    public double result;

    // Employee class has one constructor
    public Employee()

        { // initialize name of employee
        employeeName = "";
        }
        // sets values
       public void setEmployeeName(String eName)
       {
       employeeName = eName;
       }

        // get the employee name
        public String getEmployeeName()
        {   
        return (eName);
        }

        // display message
        public void displayMessage()
        {
        Scanner newEname = new Scanner(System.in);
        System.out.print("Enter Name: ");
        String name = newEname.nextLine();

        while (!(name).equalsIgnoreCase("STOP"))

    {
                Scanner input = new Scanner(System.in);

        System.out.print("Enter Employee's Hourly Pay Rate: "); // prompt for rate
        rate = input.nextDouble(); // rate input from user.
        while (rate <= 0)
        {
            System.out.print("Rate Must Be Greater Than Zero. Enter Rate: ");
            rate = input.nextDouble(); // rate loop from user.
        } // End while

        System.out.print("Enter Hours Employee Worked This Week: "); // prompt for hours worked
        hours = input.nextDouble(); // hours student worked this week.
        while (hours <= 0)
        {
            System.out.print("Hours Worked Must Be Greater Than Zero. Enter Hours: ");
            hours = input.nextDouble(); // Hours loop from user
        } // End while

        result = rate * hours; // figures students salary

        System.out.print(name); // display name
        System.out.printf("'s salary for the week is %c%.2f\n", '$', result); //display salary

        System.out.print("Enter Employee's Name or Enter STOP to Exit: "); // internal loop prompt
        name = newEname.nextLine(); //name input from user

    } // End While
    System.out.print("Ending Program.");
        }
}

Now I get the following 3 errors no matter what I try. I think something is wrong with my "employeeName, or eName, but I am at a loss as to what it is.
Any help would be appreciated.

Employee.java:15: cannot find symbol
symbol  : variable employeeName
location: class Employee
        employeeName = "";
                ^
Employee.java:19: cannot find symbol
symbol  : variable employeeName
location: class Employee
       employeeName = eName;
           ^
Employee.java:25: incompatible types
found   : char
required: java.lang.String
        return eName;
                       ^

You haven't declared employeeName yet in your class. You have

public char eName;

which should be changed to

public String employeeName;

Also, getEmployeeName() should return employeeName.

Once again you are dead on accurate. Why do I not have to also declare eName?
I am going to try and change the rest now and see how I do.

I already have a question about changing up my displaymessage(), but that is going to be my last step, I do not want to get ahead of myself.

How long have you been doing this? Just so I can get an idea of how long I have to go before I can hope to become somewhat competent.

I put in the other three; rate, hours, and result.
I had to comment out the first line in each one,

// 4 fields need to be set up
		// 1st is employee
		{ 
		employeeName = "";
		}
		// sets values
	   public void setEmployeeName(String eName)
	   {
	   employeeName = eName;
	   }
	   // get the employee name
		public String getEmployeeName()
		{	
		return (employeeName);
		}
		
		// 2nd is rate
		{ 
		//rate = "";
		}
		// sets values
	   public void setRate(double eRate)
	   {
	   rate = eRate;
	   }
	   // get the employee rate
		public double getRate()
		{	
		return (rate);
		}
	
		// 3rd is hours
		{ 
		//hours = "";
		}
		// sets values
	   public void setHours(double eHours)
	   {
	   hours = eHours;
	   }
	   // get the employee rate
		public double getHours()
		{	
		return (hours);
		}
		
		// 4th is result
		{ 
		//result = "";
		}
		// sets values
	   public void setResult(double eResult)
	   {
	   result = eResult;
	   }
	   // get the employee rate
		public double getResult()
		{	
		return (result);
		}

otherwise I got an incompatible type error. It wanted double in there somehwere, I do not know why or where. Why does it work with the line commented out?

I am getting "illegal start of expression" for this line

public void displayMessage();

I have checked all my braces, all my semi colons, everything looks right to me. What am I doing wrong?

import java.util.Scanner; //uses class Scanner

public class Payroll
{// begin class Payroll
	public static void main(String[] args)	
	{//begin method main
	
	// create Employee object nextEmployee
	// pass employee name to constructor
	Employee nextEmployee = new Employee();
	
	nextEmployee.displayMessage(); // display message
	
		
		public void displayMessage();
		{// begin display method
			Scanner newEname = new Scanner(System.in);
			System.out.print("Enter Name: ");
			String name = newEname.nextLine();
		
			while (!(name).equalsIgnoreCase("STOP"))
			{// begin main While
					Scanner input = new Scanner(System.in);
		
				System.out.print("Enter Employee's Hourly Pay Rate: "); // prompt for rate
				rate = input.nextDouble(); // rate input from user.
				while (rate <= 0)
				{// begin while
				System.out.print("Rate Must Be Greater Than Zero. Enter Rate: ");
				rate = input.nextDouble(); // rate loop from user.
				} // End while
		
				System.out.print("Enter Hours Employee Worked This Week: "); // prompt for hours worked
				hours = input.nextDouble(); // hours student worked this week.
				while (hours <= 0)
				{//begin while
				System.out.print("Hours Worked Must Be Greater Than Zero. Enter Hours: ");
				hours = input.nextDouble(); // Hours loop from user
				} // End while
        
				result = rate * hours; // figures students salary
        
				System.out.print(name); // display name
				System.out.printf("'s salary for the week is %c%.2f\n", '$', result); //display salary
		
				System.out.print("Enter Employee's Name or Enter STOP to Exit: "); // internal loop prompt
				name = newEname.nextLine(); //name input from user
						
			} // End main While
		System.out.print("Ending Program.");
	
		}// end display method
	}// end method main
} // end class Payroll

I put in the other three; rate, hours, and result.
I had to comment out the first line in each one,

// snipped

otherwise I got an incompatible type error. It wanted double in there somehwere, I do not know why or where. Why does it work with the line commented out?

You are trying to initialize numeric values with an empty string "". Initialize those to valid numeric values, such as

public double rate = 0.0;

You don't need to put those initializations inside braces (blocks), you can initialize when you declare them as I did above;

I am getting "illegal start of expression" for this line

public void displayMessage();

I have checked all my braces, all my semi colons, everything looks right to me. What am I doing wrong?

Here you have put a semicolon at the end of the method declaration, before the brace which begins the actual code. The semicolon ends a statement, so it interprets this as a single statement which makes no sense to it. Remove the semicolon so your method is properly defined.

How long have you been doing this? Just so I can get an idea of how long I have to go before I can hope to become somewhat competent.

I've been using Java about 4 years now on custom in-house software at work, plus another 8 years with different languages before that :)

No worries though, you certainly don't need that much time to become competent with Java. Just keep at it and it will come. As a software developer, you never stop learning - you just learn to learn more easily :) Eventually you become very familiar with the structural concepts and then it's merely a matter of figuring out how to incorporate the various APIs available to you and apply them to the task at hand.

Thank you for your patience, I do appreciate your help with this issue.
Concerning the ;, that is what I thought also, but when I remove it I then get two errors.
I still get "illegal start of expression" for that line, but I also get ";" expected at

} // end display method

If I take this whole block out and put it back in the Employee class everything works fine.
Is is possible that I have a method inside a method when I put it in Payroll?
Here is the whole class.

import java.util.Scanner; //uses class Scanner

public class Payroll
{// begin class Payroll
	public static void main(String[] args)	
	{//begin method main
	
	// create Employee object nextEmployee
	// pass employee name to constructor
	Employee nextEmployee = new Employee();
	
	nextEmployee.displayMessage(); // display message
	
		public void displayMessage()
		{// begin display method
			Scanner newEname = new Scanner(System.in);
			System.out.print("Enter Employee Name or STOP to Exit: ");
			String name = newEname.nextLine();
		
			while (!(name).equalsIgnoreCase("STOP"))
			{// begin main While
					Scanner input = new Scanner(System.in);
		
				System.out.print("Enter Employee's Hourly Pay Rate: "); // prompt for rate
				rate = input.nextDouble(); // rate input from user.
				while (rate <= 0)
				{// begin while
				System.out.print("Rate Must Be Greater Than Zero. Enter Rate: ");
				rate = input.nextDouble(); // rate loop from user.
				} // End while
		
				System.out.print("Enter Hours Employee Worked This Week: "); // prompt for hours worked
				hours = input.nextDouble(); // hours student worked this week.
				while (hours <= 0)
				{//begin while
				System.out.print("Hours Worked Must Be Greater Than Zero. Enter Hours: ");
				hours = input.nextDouble(); // Hours loop from user
				} // End while
        
				result = rate * hours; // figures students salary
        
				System.out.print(name); // display name
				System.out.printf("'s salary for the week is %c%.2f\n", '$', result); //display salary
		
				System.out.print("Enter Employee's Name or Enter STOP to Exit: "); // internal loop prompt
				name = newEname.nextLine(); //name input from user
						
			} // End main While
		System.out.print("Ending Program.");
	
		} // end display method
	}// end method main
} // end class Payroll

Yes, you're correct in your suspicion - you have the method displayMessage() declared inside your main method. You need to take that method declaration out, but leave the code to read from the Scanner and use your setXXX() methods to set the properties on the Employee object that you have created. Also, don't create a new Employee with your default constructor, but instead read in the name and create the Employee with the constructor that takes a name parameter :

Scanner newEname = new Scanner(System.in);
        System.out.print("Enter Employee Name or STOP to Exit: ");
        String name = newEname.nextLine();
        
        Employee thisEmployee = new Employee(name);

        while (!(name).equalsIgnoreCase("STOP"))
        {// begin main While
                        // you don't actually need a new Scanner here - you have one from above
                        Scanner input = new Scanner(System.in);

                System.out.print("Enter Employee's Hourly Pay Rate: "); // prompt for rate
                thisEmployee.setRate( input.nextDouble() ); // rate input from user.
... // the rest of your code

That is a good idea. I had not thought about it that way.
I am a little confused by the scanner and the setxxx() statements though. Maybe I just do not know how to set them up.
But the set statements you talk about will replace the scanner therefore I should not need that any longer?

How do I pull the statements from the other class where I set them up?
I have tried both

public void setRate(double eRate)
	   {
	   rate = eRate;
	   }

and

public double getRate()
		{	
		return (rate);
		}

written in to this class right before I use

thisEmployee.setRate(input.nextDouble() );

but they just produce more illegal start of expression errors.

Those methods stay in the Employee class. You use them to set the values of the properties you have defined for the Employee. They don't replace the Scanner. The Scanner allows you to collect the input from the user. That input is then used to set the property values with your set() methods. Perhaps it was confusing that I inlined the method that reads the Scanner input. This might be more clear:

Scanner input = new Scanner(System.in);

double inputRate = 0;
System.out.print("Enter Employee's Hourly Pay Rate: "); // prompt for rate
inputRate = input.nextDouble();
thisEmployee.setRate( inputRate ); // rate input

The setXX() methods allow you to set those values on the Employee that you are currently working with, so they are part of the Employee class definition.

Maybe coding in this fashion would be less confusing:

public class Employee
{
   private String name;

   public String getName()
   {
       return name;
   }

   public void setName(String name)
   {
       this.name = name;
   }
}

Thanks guys. I appreciate all the help.

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.