Link to home
Start Free TrialLog in
Avatar of eing1
eing1

asked on

writing exception class; cannot compile Employee class

objective: create two classes that extends: SalaryTooLowException and SalaryTooHighException. both classes should define one constructor which takes a String message

define the third class as Employee, has two private attributes: employeeID and salary
Employee should define one constructor which takes these two values as paramenter and throws the two exceptions.
the constructor will check value of salary parameter, if value > 200,000 throw SalaryTooHighException. if value < 10,000 throw SalaryTooLowException.
otherwise constructor will set the values for the object Override the toString() method to display the employeeID and salary.

Create a class called EmployeeMain which contains a main method.  The main method should try to instantiate three different Employee objects: one with a salary greater than 200,000, a second with a salary less than 10,000, and a third with a valid salary.  Catch the exceptions within the main method and print appropriate success or error messages.  Success messages should use Employee's toString() method.  Error messages should print the message from the exception.

here are my class :

package week3;

public class SalaryTooLowException extends Exception {
      public SalaryTooLowException (String toolow) {
            super(toolow);

}
}
------------------------------------------------------------------
package week3;

public class SalaryTooHighException extends Exception {
      public SalaryTooHighException (String toohigh) {
            super (toohigh);


}
}
--------------------------------------------------------------------------------

package week3;


public class Employee {

private String employeeID;
private int salary;

public Employee (String employeeID, int salary)
throws SalaryTooLowException, SalaryTooHighException {
 
      if (salary < 10000) {
throw new SalaryTooLowException (salary + "is too low.");
}


      else if (salary > 200000) {
throw new SalaryTooHighException (salary + "is too high.");
}

      else {

System.out.println("Success!!");
}

this.employeeID = employeeID;
this.salary = salary;
}


public String toString() {
return employeeID + salary ;
}
}
------------------------------------------------------------------------------
public class EmployeeMain {

try {
      
      Employee Employee = new Employee ("1", 100000);
            System.out.println ("Employee.toString()");

} catch (SalaryTooHighException e){
      e.printStackTrace();
      System.out.println (e.getMessage());
} catch (SalaryTooLowException e) {
      e.printStackTrace();
      System.out.println (e.getMessage());
}

try {
      Employee Employee2 = ("2", 400000);
            System.out.println ("Employee2.toString()");

} catch (SalaryTooHighException e){
      e.printStackTrace();
      System.out.println (e.getMessage());
} catch (SalaryTooLowException e) {
      e.printStackTrace();
      System.out.println (e.getMessage());
}

try {
      Employee Employee3 = ("3", 5000);
            System.out.println ("Employee3.toString()");

} catch (SalaryTooHighException e){
      e.printStackTrace();
      System.out.println (e.getMessage());
} catch (SalaryTooLowException e) {
      e.printStackTrace();
      System.out.println (e.getMessage());
}
}
}
Avatar of suprapto45
suprapto45
Flag of Singapore image

Hi,

I'll help you. Actually, you did a good programming but you only miss on the EmployeeMain class. You do not have any public static void main(String[] args).

Let me help you.

Regards
Dave
Hi,

Here is your problem.

1. Employee Employee = new Employee(...);
Following Java convention, the name of the instance should be with lower case. Employee emp = new Employee(..);

2. To print the .toString(), just use the instance variable, in this case, emp.toString();

3. Employee Employee2 = ("2", 400000);
Where is the new Employee and wrong Java convention.

Well, here is your completed EmployeeMain.java. The rest of the files are okay.
------------------------------------------------------

/*
 * Created on Apr 15, 2005
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.experts;

/**
 * @author user
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class EmployeeMain {
      public static void main(String[] args)
      {
            try
            {
                  Employee emp = new Employee ("1", 100000);
                  System.out.println (emp.toString());

            } catch (SalaryTooHighException e){
                   e.printStackTrace();
                   System.out.println (e.getMessage());
            } catch (SalaryTooLowException e) {
                   e.printStackTrace();
                   System.out.println (e.getMessage());
            }

            try {
                   Employee emp2 = new Employee("2", 400000);
                  System.out.println (emp2);

            } catch (SalaryTooHighException e){
                   e.printStackTrace();
                   System.out.println (e.getMessage());
            } catch (SalaryTooLowException e) {
                   e.printStackTrace();
                   System.out.println (e.getMessage());
            }

            try {
                   Employee emp3 =  new Employee("3", 5000);
                          System.out.println (emp3.toString());

            } catch (SalaryTooHighException e){
                   e.printStackTrace();
                   System.out.println (e.getMessage());
            } catch (SalaryTooLowException e) {
                   e.printStackTrace();
                   System.out.println (e.getMessage());
            }            
      }
}


Good luck and regards
Dave
Avatar of eing1
eing1

ASKER

What about the Employee Class I can't seem to get that to compile, I'll try the EmployeeMain class later today but can some you take a look at the Employee Class?? thanks for the response.
Hi,

The Employee class is okay with me. Can you tell me what your error is?

Regards
Dave
Avatar of eing1

ASKER

I get the following error when I try to compile my Employee classs:

cannot resolve symbol
symbol: class SalaryTooHighException
location: class week3.employee
throw new SalaryTooHighException (salary + " is too high.")

same type of message repeat for every location that has SalaryTooHighException and SalaryTooLowException

another error is

cannot return a value from method whose result type is void
return employeeID + salary;
Avatar of eing1

ASKER

This is the message I get when I try to compile my EmployeeMain class.

C:\J24WORK>javac SalaryTooLowException.java

C:\J24WORK>javac EmployeeMain.java
EmployeeMain.java:8: cannot resolve symbol
symbol  : class Employee
location: class week3.EmployeeMain
Employee emp = new Employee ("1", 100000);
^
EmployeeMain.java:8: cannot resolve symbol
symbol  : class Employee
location: class week3.EmployeeMain
Employee emp = new Employee ("1", 100000);
                   ^
EmployeeMain.java:11: cannot resolve symbol
symbol  : class SalaryTooHighException
location: class week3.EmployeeMain
} catch (SalaryTooHighException e){
            ^
EmployeeMain.java:14: cannot resolve symbol
symbol  : class SalaryTooLowException
location: class week3.EmployeeMain
} catch (SalaryTooLowException e) {
            ^
EmployeeMain.java:20: cannot resolve symbol
symbol  : class Employee
location: class week3.EmployeeMain
Employee emp2 = new Employee("2", 400000);
^
EmployeeMain.java:20: cannot resolve symbol
symbol  : class Employee
location: class week3.EmployeeMain
Employee emp2 = new Employee("2", 400000);
                    ^
EmployeeMain.java:23: cannot resolve symbol
symbol  : class SalaryTooHighException
location: class week3.EmployeeMain
} catch (SalaryTooHighException e){
            ^
EmployeeMain.java:26: cannot resolve symbol
symbol  : class SalaryTooLowException
location: class week3.EmployeeMain
} catch (SalaryTooLowException e) {
            ^
EmployeeMain.java:32: cannot resolve symbol
symbol  : class Employee
location: class week3.EmployeeMain
Employee emp3 = new Employee("3", 5000);
^
EmployeeMain.java:32: cannot resolve symbol
symbol  : class Employee
location: class week3.EmployeeMain
Employee emp3 = new Employee("3", 5000);
                    ^
EmployeeMain.java:35: cannot resolve symbol
symbol  : class SalaryTooHighException
location: class week3.EmployeeMain
} catch (SalaryTooHighException e){
            ^
EmployeeMain.java:38: cannot resolve symbol
symbol  : class SalaryTooLowException
location: class week3.EmployeeMain
} catch (SalaryTooLowException e) {
            ^
12 errors
Hi,

It seems that your EmployeeMain cannot access other files. Do you put them on the same folder or different folder? Can you send me your structure of your package?

Best Regards
Dave
Hi,

Another thing is that your Employee also cannot access SalaryTooHighException class. It is the same problem with the EmployeeMain class.

"cannot return a value from method whose result type is void
return employeeID + salary;"

It should not be an error because you do not return it as a void but instead a String.

public String toString() {
return employeeID + salary ;
}

Best Regards
Dave
Avatar of eing1

ASKER

I have all the files in the same folder. Can you try to compile all the files and see if you can get it to work?
Hi,

Give me few minutes.

Regards
Dave
Hi,

yes, I can get it work. The output is

------------------
Success!!
1100000
SalaryTooHighException: 400000is too high.
        at Employee.<init>(Employee.java:15)
        at EmployeeMain.main(EmployeeMain.java:19)
400000is too high.
SalaryTooLowException: 5000is too low.
        at Employee.<init>(Employee.java:10)
        at EmployeeMain.main(EmployeeMain.java:31)
5000is too low.
--------------------


Okay...what you can do, please post your Employee.java and EmployeeMain.java again and I will examine them. Also, please let me know how do you compile them.

Best Regards
Dave

Avatar of eing1

ASKER

YESSSSSS!! GREAT JOB

here are my EmployeeMain and Employee class

package week3;

public class EmployeeMain {
public static void main(String[] args)
{
try
{
Employee emp = new Employee ("1", 100000);
System.out.println (emp.toString());

} catch (SalaryTooHighException e){
e.printStackTrace();
System.out.println (e.getMessage());
} catch (SalaryTooLowException e) {
e.printStackTrace();
System.out.println (e.getMessage());
}

try {
Employee emp2 = new Employee("2", 400000);
System.out.println (emp2.toString());

} catch (SalaryTooHighException e){
e.printStackTrace();
System.out.println (e.getMessage());
} catch (SalaryTooLowException e) {
e.printStackTrace();
System.out.println (e.getMessage());
}

try {
Employee emp3 = new Employee("3", 5000);
System.out.println (emp3.toString());

} catch (SalaryTooHighException e){
e.printStackTrace();
System.out.println (e.getMessage());
} catch (SalaryTooLowException e) {
e.printStackTrace();
System.out.println (e.getMessage());
}
}
}
----------------------------------------------------------------------------------------------
package week3;


public class Employee {

private String employeeID;
private int salary;

public Employee (String employeeID, int salary)
throws SalaryTooLowException, SalaryTooHighException {
 
      if (salary < 10000) {
throw new SalaryTooLowException (salary + "is too low.");
}


      else if (salary > 200000) {
throw new SalaryTooHighException (salary + "is too high.");
}

      else {
this.employeeID = employeeID;
this.salary = salary;
System.out.println("Success!!");
}
}

public String toString() {
return employeeID + salary ;
}
}
-----------------------------------------------------------------------------------
This is the exact step I take when compiling the classes

1. cd \j24work
2. javac SalaryTooLowException.java
3. java SalaryTooLowException

4. javac SalaryTooHighException.java
5. java SalaryTooHighException

6. javac Employee.java
7. java Employee

8. javac EmployeeMain.java
9. java EmployeeMain

Hi,

If I were you, I will do this when compiling.

1. cd \week3 or cd \j24work\week3 -> this is your package name, isn't it? (anyway, I do not know).
2. javac SalaryTooLowException.java
3. javac SalaryTooHighException.java
4. javac Employee.java
5. javac EmployeeMain.java
6. java EmployeeMain

Try again please.... :)

Best Regards
Dave
HI,

Please also give me your complete structure such as

C:\jdk1.4.1\bin\j24work\week3 or anything that you use.

best Regards
Dave
Hi,

I got to go now. I'll be back in the next few hours time.

Good luck

Regards
Dave
Avatar of eing1

ASKER

I first had all of my files in the j24work folder, then I created a new folder : week3 and put all of my Employee, EmployeeMain, SalaryTooLooException and SalaryTooHighException in this folder

I did what you suggested
1. cd \week3 or cd \j24work\week3 -> this is your package name, isn't it? (anyway, I do not know).
2. javac SalaryTooLowException.java
3. javac SalaryTooHighException.java
4. javac Employee.java
5. javac EmployeeMain.java
6. java EmployeeMain


but I still get the same error as what I post above
Avatar of eing1

ASKER

how do I go about finding out what the complete structure is??
Avatar of eing1

ASKER

I'll check back @ 11:30pm thank you again for taking the time to help me on this
Hi,

I am back....please send me the latest errors that you have.

Best Regards
Dave
ASKER CERTIFIED SOLUTION
Avatar of suprapto45
suprapto45
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of eing1

ASKER

I did what you suggested and the error I get is :

package week3 does not exist
Hi,

Sorry for the delay. Normally, I am not that active on Sunday. This is what you can do. You need to find out where do you install your jdk. If it is in C:\jdk1.4.1, put all of your files in your bin folder within your jdk home such as C:\jdk1.4.1\bin. Remove all the package from your classes - package week3;. Then try compile all your classes again from your command prompt.

Let me know your update then :).

Regards
Dave