I am taking an IT course and need help desperately. I have referred to several different resources and am having a hard time grasping OOP. I came across this site and thought I should give it a try. Please bear with me I am new to the site. I am not looking for anyone to give me the answers. I am looking for direction and help to gain a better understanding so that I can proceed. This assignment has already been graded and I was told that my program compile and run fine, but my employee class does not work. I also needed to add a method to the employee class to calculate the weekly pay. I do not understand how to get the employee class to work. Should they be in two separate files (Employee.java and Payroll3.java) or could I have put them in one file? I am also confused when it comes to adding methods. Can someone please help me? I would greatly appreciate any assistance.

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

public class Payroll3
{
     // main method starts execution of program execution
     public static void main( String args[ ] )
     {
          boolean stop = false;
          while ( !stop )   
          { 
          // create Scanner to obtain input from command window
          Scanner input = new Scanner( System.in );

          System.out.print ( "Enter employee's first and last name or stop to exit program:" );
          String empName = input.nextLine(); // read employee's name input by user

          if ( empName.equals ( "stop" ) )
          {
               System.out.println( "Goodbye. Program Terminated." );
               stop = true; }

           else
           {
              double hourlyRate; // hourly rate 
              double weeklyHours; // amount of hours worked for the week
              double weeklyPay; // product of hourlyRate and weeklyHours

              System.out.print( "Enter employee's hourly rate: " ); // prompt
              hourlyRate = input.nextDouble ();

              while ( hourlyRate < 0 )
              {
                  System.out.print( "Invalid entry. Enter a positive number: " ); // prompt
                  hourlyRate = input.nextDouble(); // read hourly rate input by user
              }
                  System.out.print( "Enter number of hours employee worked: " ); // prompt
                  weeklyHours = input.nextDouble(); // read number of hours worked by the employee input by user
                  while ( weeklyHours < 0 )
                  {
                       System.out.print( "Invalid entry. Enter a positive number:" ); // prompt
                       weeklyHours = input.nextDouble();
                  }
                  weeklyPay = hourlyRate * weeklyHours; // multiply numbers

                  System.out.println( "Employee:" + empName ); // display employee's name
                  System.out.printf( "Weekly pay is $" + weeklyPay ); // display weekly total
                  System.out.println();
               }
          }

     } // end method main

} // end class Payroll3

This is my employee class

public class Employee 
{
     public static void main( String args[ ] )
     {
     private String empName;
     private double hourlyRate;
     private double weeklyHours;
 
     // constructor 
     public Employee( String name, double rate, double hours )
     {
         empName = name;
         hourlyRate = rate;
         weeklyHours = hours;     
     } // end constructor

     public void setEmpName ( String name )
     {     
          empName = name;
     }

     public String getEmpName()
     {
         return empName;
     }  

     public void setHourlyRate ( double rate )
     {     
          hourlyRate = rate;
     }

     public double getHourlyRate()
     {
         return hourlyRate;
     }      

     public void setWeeklyHours ( double hours )
     {     
          weeklyHours = hours;
     }

     public double getWeeklyHours()
     {
         return weeklyHours;
     }     

     // method to calculate weekly pay
     public double calculateWeeklyPay;
     {
         double weeklyPay = hourlyRate * weeklyHours;
      }
    }
} // end class Employee

Recommended Answers

All 4 Replies

I believe the problem with your Employee class is because you have the constructors and methods defined in the main method. It doesnt appear to me that you need to have a main method in that specific class.

I believe the problem with your Employee class is because you have the constructors and methods defined in the main method. It doesnt appear to me that you need to have a main method in that specific class.

Are you saying that I have my code backwards. Should my constructors and methods be in my employee class instead? I went back and removed the main method in the employee class, but am now getting an error message which says, "Exception in thread "main" java.lang.NoSuchMethodError: main". Here is my updated employee class.

public class Employee 
{
     private String empName;
     private double hourlyRate;
     private double weeklyHours;
 
     // constructor 

     public Employee( String name, double rate, double hours )
     {
         empName = name;
         hourlyRate = rate;
         weeklyHours = hours;     
     } // end constructor

     public void setEmpName ( String name )
     {     
          empName = name;
     }

     public String getEmpName()
     {
         return empName;
     }  

     public void setHourlyRate ( double rate )
     {     
          hourlyRate = rate;
     }

     public double getHourlyRate()
     {
         return hourlyRate;
     }      

     public void setWeeklyHours ( double hours )
     {     
          weeklyHours = hours;
     }

     public double getWeeklyHours()
     {
         return weeklyHours;
     }     

     // method to calculate weekly pay
     public double calculateWeeklyPay;
     {
         double weeklyPay = hourlyRate * weeklyHours;
     }
    
} // end class Employee

I'm having trouble seeing where you are using the Employee class in your Payroll3 class. I can't see where you are calling the methods or constructing objects from the class. The main method is where you call methods and put them to use, but not where you write the method itself.

I'm having trouble seeing where you are using the Employee class in your Payroll3 class. I can't see where you are calling the methods or constructing objects from the class. The main method is where you call methods and put them to use, but not where you write the method itself.

Hello,
The program compiles and run, but the new class (Employee) is not being used. I'm confused on how to get this to work. I also had to add a method to my Employee class to calculate the weekly pay which I think I did. I made the change and it still compiled and ran successfully. The updated Employee class is below with the method.

public class Employee 
{
     private String empName;
     private double hourlyRate;
     private double weeklyHours;
 
     // constructor 

     public Employee( String name, double rate, double hours )
     {
         empName = name;
         hourlyRate = rate;
         weeklyHours = hours;     
     } // end constructor

     public void setEmpName ( String name )
     {     
          empName = name;
     }

     public String getEmpName()
     {
         return empName;
     }  

     public void setHourlyRate ( double rate )
     {     
          hourlyRate = rate;
     }

     public double getHourlyRate()
     {
         return hourlyRate;
     }      

     public void setWeeklyHours ( double hours )
     {     
          weeklyHours = hours;
     }

     public double getWeeklyHours()
     {
         return weeklyHours;
     }     

     // method to calculate weekly pay
     public double getWeeklyPay()
     {
         return hourlyRate * weeklyHours;
     }
    
} // end class Employee
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.