7 Replies - 1439 Views - Last Post: 30 January 2010 - 02:58 PM Rate Topic: -----

#1 spark69  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 30-January 10

Help understanding these errors

Post icon  Posted 30 January 2010 - 01:14 PM

I have to create a new employee class with a name and salary, and then a certain percentage raise. I have written the following code and tester, but I get errors.

Employee.java

public class Employee
{
	private String name;
	private double salary = 0;
	/**
	 * @param args
	 * 
	 *  
	 */
	public static void main(String[] args, Object name, double salary)
	{
		/**
		 * Constructs an employee class.
		 * @param employeeName the employee name
		 * @param currentSalary the employee salary
		 */
		public Employee(String employeeName, double currentSalary)
		{
			name = employeeName;
			salary = currentSalary;
		}
		
		/** 
		 * Gets the employee name
		 * @return name the employee name
		 */
		public String getName()
		{
			return name;
		}
		
		/**
		 * Gets the employee salary.
		 * @return salary the employee salary
		 */
		public double getSalary()
		{
			return salary;
		}
		
		/**Raises the salary by a given percentage.
		 * @param percent the percentage of the raise
		 */
		public void raiseSalary(double percent)
		{
			Salary = salary * (1 + (percent / 100));
		}
		
	}

}



EmployeeTester.java
public class EmployeeTest
{

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		Employee harry = new Employee("Harry Hacker", 50000);
		harry.raiseSalary(10);
		
		System.out.println(harry.getName());
		System.out.println("Expected: Harry Hacker 50000");
		
		System.out.println(harry.getSalary());
		System.out.println("Expected: 50000");
		
		System.out.println(harry.getSalary());
		System.out.println("Expected: 55000");
		
		
	}

}



And here is the errors
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The constructor Employee(String, int) is undefined
The method raiseSalary(int) is undefined for the type Employee
The method getName() is undefined for the type Employee
The method getSalary() is undefined for the type Employee
The method getSalary() is undefined for the type Employee

at EmployeeTest.main(EmployeeTest.java:10)

I am taking java for school, but I am not a programmer. I have never written code and so some of the info in my book misses me by a mile. It seems to me that I have defined the methods in the class, but....


Please help.

Neil

This post has been edited by spark69: 30 January 2010 - 01:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help understanding these errors

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,390
  • Joined: 27-December 08

Re: Help understanding these errors

Posted 30 January 2010 - 01:19 PM

Please, :code:

From glancing over your Employee class, it looks like you tried to define the methods in question inside the main() method in your Employee class. Remember, you can't define a method within a method.
Was This Post Helpful? 0
  • +
  • -

#3 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Help understanding these errors

Posted 30 January 2010 - 01:22 PM

Make sure to use your code tags correctly :code:


edit:ninja'd ;)

And yes, macosxnerd is right about not being able to define methods within the main method.

In addition, you don't need a main method in your Employee class unless that's what you want to run. The main method is where the program executes, so only your EmpoyeeTest class needs to have it

This post has been edited by erik.price: 30 January 2010 - 01:22 PM

Was This Post Helpful? 0
  • +
  • -

#4 spark69  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 30-January 10

Re: Help understanding these errors

Posted 30 January 2010 - 01:41 PM

View Postmacosxnerd101, on 30 Jan, 2010 - 12:19 PM, said:

Please, :code:

From glancing over your Employee class, it looks like you tried to define the methods in question inside the main() method in your Employee class. Remember, you can't define a method within a method.


So in my work, what is the main method? I believe the methods I'm defining are the items like getName, getSalary, getRaise, correct?

Neil
Was This Post Helpful? 0
  • +
  • -

#5 anonymouscodder  Icon User is offline

  • member icon

Reputation: 126
  • View blog
  • Posts: 710
  • Joined: 01-January 10

Re: Help understanding these errors

Posted 30 January 2010 - 02:18 PM

View Postspark69, on 30 Jan, 2010 - 05:41 PM, said:

So in my work, what is the main method? I believe the methods I'm defining are the items like getName, getSalary, getRaise, correct?

Neil


But the methods getName, getSalary and getRaise are defined inside a main method.

Instead of:
public class Employee
{
	private String name;
	private double salary = 0;
	/**
	 * @param args
	 *
	 *  
	 */
	public static void main(String[] args, Object name, double salary)
	{
		/**
		 * Constructs an employee class.
		 * @param employeeName the employee name
		 * @param currentSalary the employee salary
		 */
		public Employee(String employeeName, double currentSalary)
		{
			name = employeeName;
			salary = currentSalary;
		}
	   
		/**
		 * Gets the employee name
		 * @return name the employee name
		 */
		public String getName()
		{
			return name;
		}
	   
		/**
		 * Gets the employee salary.
		 * @return salary the employee salary
		 */
		public double getSalary()
		{
			return salary;
		}
	   
		/**Raises the salary by a given percentage.
		 * @param percent the percentage of the raise
		 */
		public void raiseSalary(double percent)
		{
			Salary = salary * (1 + (percent / 100));
		}
	   
	}

}


The correct would be:
public class Employee
{
	private String name;
	private double salary = 0;
	
	  /**
		 * Constructs an employee class.
		 * @param employeeName the employee name
		 * @param currentSalary the employee salary
		 */
		public Employee(String employeeName, double currentSalary)
		{
			name = employeeName;
			salary = currentSalary;
		}
	   
		/**
		 * Gets the employee name
		 * @return name the employee name
		 */
		public String getName()
		{
			return name;
		}
	   
		/**
		 * Gets the employee salary.
		 * @return salary the employee salary
		 */
		public double getSalary()
		{
			return salary;
		}
	   
		/**Raises the salary by a given percentage.
		 * @param percent the percentage of the raise
		 */
		public void raiseSalary(double percent)
		{
			Salary = salary * (1 + (percent / 100));
	}

}

Was This Post Helpful? 0
  • +
  • -

#6 spark69  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 30-January 10

Re: Help understanding these errors

Posted 30 January 2010 - 02:36 PM

So in Java, the line of code

public static void main(String[] args)

is a main method? And that line needs to never be in a class, like when I need to create a class Employee or Student, but only appear in the tester, or the code that is actually running?

Neil
Was This Post Helpful? 0
  • +
  • -

#7 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: Help understanding these errors

Posted 30 January 2010 - 02:48 PM

View Postspark69, on 30 Jan, 2010 - 08:36 PM, said:

So in Java, the line of code

public static void main(String[] args)

is a main method? And that line needs to never be in a class, like when I need to create a class Employee or Student, but only appear in the tester, or the code that is actually running?

Neil


Yes, it is the main method. It must be in a class. More than likely in the class that you call the "tester". Here, the problem was that you were putting method within the main, that is methods within another method, which is not correct.

public class Employee {
  // Constructor

  // Getters, setters

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



When running java Employee, the JVM actually executes Employee.main(args), passing the arguments as an array.
Was This Post Helpful? 0
  • +
  • -

#8 spark69  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 30-January 10

Re: Help understanding these errors

Posted 30 January 2010 - 02:58 PM

That's more clear. Thanks for the help. As I said, I'm not a programmer, and my professor won't answer email, or only cryptically and the textbook is not much clearer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1