[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

writing exception class; cannot compile Employee class

Posted on 2005-04-14
22
Medium Priority
?
170 Views
Last Modified: 2013-11-23
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());
}
}
}
0
Comment
Question by:eing1
  • 13
  • 9
22 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 13788645
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
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13788658
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
0
 

Author Comment

by:eing1
ID: 13791164
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.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 16

Expert Comment

by:suprapto45
ID: 13791405
Hi,

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

Regards
Dave
0
 

Author Comment

by:eing1
ID: 13795739
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;
0
 

Author Comment

by:eing1
ID: 13795770
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
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13795998
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
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13796006
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
0
 

Author Comment

by:eing1
ID: 13796104
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?
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13796111
Hi,

Give me few minutes.

Regards
Dave
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13796120
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

0
 

Author Comment

by:eing1
ID: 13796148
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

0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13796156
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
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13796160
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
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13796166
Hi,

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

Good luck

Regards
Dave
0
 

Author Comment

by:eing1
ID: 13796171
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
0
 

Author Comment

by:eing1
ID: 13796203
how do I go about finding out what the complete structure is??
0
 

Author Comment

by:eing1
ID: 13796207
I'll check back @ 11:30pm thank you again for taking the time to help me on this
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13796710
Hi,

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

Best Regards
Dave
0
 
LVL 16

Accepted Solution

by:
suprapto45 earned 1500 total points
ID: 13796750
Okay,

What you can do now is that to modify your Employee and EmployeeMain file. Please provide with the full path of your class. For example, in your Employee class, if you want to use the SalaryTooLowException, you need to provide the full path such as week3.SalaryTooLowException.

Example.

public class Employee {

private String employeeID;
private int salary;

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


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

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

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


Good luck

Best Regards
Dave
0
 

Author Comment

by:eing1
ID: 13798583
I did what you suggested and the error I get is :

package week3 does not exist
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13801024
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
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question