3 Replies - 1011 Views - Last Post: 20 January 2013 - 04:14 AM Rate Topic: -----

#1 treyriggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-January 13

Error: Could not find or load main class Employee.EmployeeTest

Posted 19 January 2013 - 11:29 PM

package Employee;
public class Employee {

	String name;
	String department;

	double hourlyRate;

	Employee(String name, String department, double hourlyRate) {
		this.name = name;
		this.department = department;
		this.hourlyRate = hourlyRate;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public void setHourlyRate(double hourlyRate) {
		this.hourlyRate = hourlyRate;
	}

	public String getNameAndDepartment() {
		return name + " " + department;
	}

	double weeklyPay(int numOfHourWorked) {
		if (numOfHourWorked < 40) {
			return (numOfHourWorked * hourlyRate);
		} else
			return (40 * hourlyRate);

	}
}


package Employee;
class UnionEmployee extends Employee {

	double dues;

	UnionEmployee(String name, String department, double hourlyRate, double dues) {
		super(name, department, hourlyRate);
		this.dues = dues;
	}

	public void setDues(double dues) {
		this.dues = dues;
	}

	double weeklyPay(int numOfHourWorked) {
		if (numOfHourWorked <= 40) {
			return (super.weeklyPay(numOfHourWorked));
		} else
			return ((super.weeklyPay(40) + ((numOfHourWorked - 40) * hourlyRate * 1.5)) - dues);
	}
}


package Employee;
class CommissionEmployee extends Employee {

	double commisionRate;
	double salesAmount;

	CommissionEmployee(String name, String department, double hourlyRate) {
		super(name, department, hourlyRate);
	}

	public void setCommisionRate(double commisionRate) {
		this.commisionRate = commisionRate;
	}

	public void setSalesAmount(double salesAmount) {
		this.salesAmount = salesAmount;
	}

	double weeklyPay(int numOfHourWorked) {
		return (super.weeklyPay(numOfHourWorked) + (commisionRate * salesAmount));
	}

}


package Employee;
class TestEmployee {
	public void main(String[] args) {
	
	
	}
	UnionEmployee emp1 = new UnionEmployee("John Henry", "Maintenance", 15.00, 0.08);
	CommissionEmployee emp2 = new CommissionEmployee("Elizabeth Thompson", "Human Resources", 17.00);
	Employee emp3 = new Employee("Jordan Bullock", "Operations", 13.50);
	
	
	
	void display(Employee emp, int numOfHourWorked) 
	{
		System.out.println("Name and department :" + emp1 + emp.getNameAndDepartment());
		System.out.println("Weekly pay of employee :"
				+ emp1 + emp.weeklyPay(numOfHourWorked));
	}

	void display(UnionEmployee uEmp, CommissionEmployee cEmp,
			int numOfHourWorked)
	{
		System.out.println("Weekly Pay for UnionEmployee"
				+  emp1 + uEmp.weeklyPay(numOfHourWorked));
		System.out.println("Weekly Pay for UnionEmployee"
				+ emp1 + cEmp.weeklyPay(numOfHourWorked));
	}



}



This is the Error I get when I compile and Run:

Error: Could not find or load main class Employee.EmployeeTest

Is This A Good Question/Topic? 0
  • +

Replies To: Error: Could not find or load main class Employee.EmployeeTest

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4426
  • View blog
  • Posts: 12,293
  • Joined: 18-April 07

Re: Error: Could not find or load main class Employee.EmployeeTest

Posted 20 January 2013 - 01:18 AM

main is static. It has to be static so that when a program starts it can find main() without needing to create the instance of an object.

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



Being that you don't have static it is being seen as an instance method. There it isn't seen by the class loader and returns that error.

P.S. Your main function is not doing anything, so don't expect it to create objects or run functions. You have to do something in your main function.

:)

This post has been edited by Martyr2: 20 January 2013 - 01:19 AM

Was This Post Helpful? 1
  • +
  • -

#3 treyriggins  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-January 13

Re: Error: Could not find or load main class Employee.EmployeeTest

Posted 20 January 2013 - 01:24 AM

Thank you for helping me,

So should I move the objects i created inside the main function because I have tried that as well and i get errors inside the class. I am really confused on this test class method been working on it all day. Do you have any more suggestions that may be of some help?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Error: Could not find or load main class Employee.EmployeeTest

Posted 20 January 2013 - 04:14 AM

I assume you mean that you've tried a main() method that looks like:
public static void main(String[] args)
{
    UnionEmployee emp1 = new UnionEmployee("John Henry", "Maintenance", 15.00, 0.08);
    CommissionEmployee emp2 = new CommissionEmployee("Elizabeth Thompson", "Human Resources", 17.00);
    Employee emp3 = new Employee("Jordan Bullock", "Operations", 13.50);
}

And I can see how that will cause errors, because the variable emp1 is used in the two other methods in that class, but those methods don't know what emp1 is. emp1 was declared and initialized in the main() method, so the variable emp1 is a "local variable" to the main() method, unknown outside main().

The fix that occurs first to me is: change emp1 from a main() local variable to a class variable, but then I wonder if the display() methods require emp1. Each of those methods has their own local Employee variables, emp and uEmp, so I'm thinking it's the methods that need modifying: remove emp1 entirely.

Since the main() method I suggested above doesn't call either of those methods, we'll need to add those calls to main():
public static void main(String[] args)
{
    UnionEmployee emp1 = new UnionEmployee("John Henry", "Maintenance", 15.00, 0.08);
    CommissionEmployee emp2 = new CommissionEmployee("Elizabeth Thompson", "Human Resources", 17.00);
    Employee emp3 = new Employee("Jordan Bullock", "Operations", 13.50);
    
    display( emp3, 40 );
    display( emp1, emp2, 35 );
}

Then, since a static method main() is calling those methods, they will also have to be static:
static void display(Employee emp, int numOfHourWorked) 
{
    System.out.println( "Name and department :" + emp.getNameAndDepartment() );
    System.out.println( "Weekly pay of employee :"
            + emp.weeklyPay(numOfHourWorked));
}

static void display(UnionEmployee uEmp, CommissionEmployee cEmp,
        int numOfHourWorked)
{
    System.out.println("Weekly Pay for UnionEmployee"
            +  uEmp.weeklyPay(numOfHourWorked));
    System.out.println("Weekly Pay for UnionEmployee"
            + cEmp.weeklyPay(numOfHourWorked));
}

There are other logic errors to correct, but hopefully you can see how to do those now that it's running.

Keep coding!
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1