-
Need your help again
Hi Guys,
I need a help in these one. Actually, I’m a newbie and I’ve been working with this problem. Please help. Thanks.
• Handle exceptions for getting sex description
a. Throw exception for undefined codes
Tip: throw a new Exception with the error description as parameter
b. Catch the exception and return the error description to the caller
Tip: extract the Exception message
• Handle exceptions for getting civil status description
a. Throw exception for undefined codes
b. Catch the exception and return the error description to the caller
• Handle exceptions for getting employee type description
a. Throw exception for undefined codes
b. Catch the exception and return the error description to the caller
• Handle exceptions for computing monthly salary
a. Throw exception for zero or negative salary
b. Catch the exception and propagate the exception to the calling method
Tip: declare throws clause in the method signature
• Catch the exception in the main() method and handle the exception by printing the stack trace before stopping the program execution
1. Set employee attributes to invalid test data to create the error situation
Thanks in advance!
Here's my attempted program.. don't know if this really correct:
public class EmployeeMain {
public static void main(String[] args) {
String name = "Vanessa Rose V. Castro"; //for name
String address = " Taguig City "; //for address
System.out.println("Name: " + name);
System.out.println("Address: " + address);
System.out.println ("Sex: " + EmployeeMain.getsexCode());
System.out.println("Civil Status Code: " + EmployeeMain.getcivilStatusCode());
System.out.println ("Employee Type: " + EmployeeMain.getemployeeStatusCode());
System.out.println("Birthdate: " + EmployeeMain.getbirthMonth() + " " + EmployeeMain.getbirthDay() + ", " + EmployeeMain.getbirthYear());
System.out.println ("Date Hired: " + EmployeeMain.gethiredMonth() + " " + EmployeeMain.gethiredDay() + ", " + EmployeeMain.gethiredYear());
System.out.println ("Monthly Salary: " + EmployeeMain.getmonthlySalary());
char sex = 'F'; //for sex
char civilStatus = 'S'; //for civil status
char employeeStatus = 'R'; //for employee status
int basicSalary = 10000;
int ot = 150;
double tax = basicSalary*0.10; // assume witholding tax is 10% of salary
double deductions = 500; //example: SSS, others
double monthlySalary = basicSalary + ot - tax - deductions;
// added for activity 1 run 2
int yyyy=1985, mm=01, dd=25; //birth date
String mmm = mm==1?"January":mm==2?"February":mm==3?"March":mm= =4?"April":mm==5?"May":mm==6?"June":
mm==7?"July":mm==8?"August":mm==9?"September":mm== 10?"October":mm==11?"November":mm==12?"December":" Unknown";
int yyyy1=2007, mm1=8, dd1=21; // hired date
String mmm1 = mm1==1?"January":mm1==2?"February":mm1==3?"March": mm1==4?"April":mm1==5?"May":mm1==6?"June":
mm1==7?"July":mm1==8?"August":mm1==9?"September":m m1==10?"October":mm1==11?"November":mm1==12?"Decem ber":"Unknown";
try {
sex = 'Z';
}
catch(Exception e){
System.out.println("Sex: " + e.getMessage());
}
try {
civilStatus = 'Y';
}
catch(Exception e){
System.out.println("Civil Status Code: " + e.getMessage());
}
try {
employeeStatus = 'X';
}
catch(Exception e){
System.out.println("Employee Type: " + e.getMessage());
}
try {
monthlySalary = basicSalary + ot - tax - deductions/0;
}
catch(Exception e){
System.out.println("Salary is less than or equal to zero!" + e.getMessage());
}
}
//declaring of methods as static
static String getsexCode () {
char sex = 'Z';
String sexCode = sex=='F'?"Female":sex=='M'?"Male":"Unknown Sex!";
return sexCode;
}
static String getcivilStatusCode () {
char civilStatus = 'Y';
String civilStatusCode = civilStatus=='S'?"Single":civilStatus=='M'?"Marrie d":civilStatus=='O'?"Others":"Unknown Civil Status!";
return civilStatusCode;
}
static String getemployeeStatusCode () {
char employeeStatus = 'X';
String employeeStatusCode = employeeStatus=='R'?"Regular":employeeStatus=='P'? "Probationary":employeeStatus=='C'?"Contractual":" Unknown Employee Type!";
return employeeStatusCode;
}
static double getmonthlySalary () {
int basicSalary = 10000;
int ot = 500;
double tax = basicSalary*0.10; // assume witholding tax is 10% of salary
double deductions = 500; //example: SSS, others
double monthlySalary = - tax - deductions + basicSalary + ot/0;
return monthlySalary;
}
//for birth date
static int getbirthYear () {
int yyyy=1985, mm=01, dd=25; //birth year
return yyyy;
}
static String getbirthMonth () {
int yyyy=1985, mm=01, dd=25; //birth month
String mmm = mm==1?"January":mm==2?"February":mm==3?"March":mm= =4?"April":mm==5?"May":mm==6?"June":
mm==7?"July":mm==8?"August":mm==9?"September":mm== 10?"October":mm==11?"November":mm==12?"December":" Unknown";
return mmm;
}
static int getbirthDay () {
int yyyy=1985, mm=01, dd=25; //birth date
return dd;
}
//for date hired
static int gethiredYear () {
int yyyy1=2007, mm1=8, dd1=21; //hired year
return yyyy1;
}
static String gethiredMonth () {
int yyyy1=2007, mm1=8, dd=21; //hired month
String mmm1 = mm1==1?"January":mm1==2?"February":mm1==3?"March": mm1==4?"April":mm1==05?"May":mm1==6?"June":
mm1==07?"July":mm1==8?"August":mm1==9?"September": mm1==10?"October":mm1==11?"November":mm1==12?"Dece mber":"Unknown";
return mmm1;
}
static int gethiredDay () {
int yyyy1=2007, mm1=8, dd1=21; //hired day
return dd1;
}
}
-
You can google the exceptions tutorials on the net....
don't be on a hurry,
sukatoa
-
Please use tags when you post some code here pal. It's easy to read.
Can you compile this code. There are few errors. Better to correct them first. Did you write this code by yourself? If not, when you copied(actually it's not a good habit) do it carefully.