Question: Add a user-defined exception that can be thrown by one of the methods as part of the validation or error checking. The main method should
Add a user-defined exception that can be thrown by one of the methods as part of the validation or error checking. The main method should then create an instance of the class and call the method in such a way that the exception is thrown (e.g. invalid input or state of the system).
Test:
/* Decompiler 4ms, total 104ms, lines 27 */
package Week7;
public class Employee {
String empName;
double salary;
Employee(String empName, double salary) {
this.empName = empName;
this.salary = salary;
}
String getName() {
return this.empName;
}
double getSalary() {
return this.salary;
}
public double calcMonth(double salary) {
return salary/12;
}
public int calcMonth(int salary) {
return salary/12;
}
public String toString() {
String output = "";
output = output + "Name: " + this.getName() + " ";
output = output + "Salary: $" + this.getSalary() + " ";
return output;
}
}
Teahcer:
/* Decompiler 5ms, total 28ms, lines 43 */
package Week7;
import java.util.Random;
public class Teacher extends Employee {
private int students;
Teacher(String empName, double salary, int students) {
super(empName, salary);
if (students < 0) {
System.out.println("teacher cannot have less than 0 students");
} else {
this.students = students;
}
}
public int getStudents() {
return this.students;
}
public void setStudents(int students) {
if (students < 0) {
System.out.println("teacher cannot have less than 0 students");
} else {
this.students = students;
}
}
//overloading
public double calcMonth(double salary) {
return salary/12;
}
//overloading
public int calcMonth(int salary) {
return salary/12;
}
public float getClassAverage() {
Random ran = new Random();
return (float)(ran.nextInt(101) + 20);
}
public float getClassAverage(int customMax) {
Random ran = new Random();
return (float)(ran.nextInt(customMax - 20 + 1) + 20);
}
public String toString() {
return super.toString() + "Students: $" + this.students + " ";
}
}
Employee:
/* Decompiler 4ms, total 104ms, lines 27 */
package Week7;
public class Employee {
String empName;
double salary;
Employee(String empName, double salary) {
this.empName = empName;
this.salary = salary;
}
String getName() {
return this.empName;
}
double getSalary() {
return this.salary;
}
public double calcMonth(double salary) {
return salary/12;
}
public int calcMonth(int salary) {
return salary/12;
}
public String toString() {
String output = "";
output = output + "Name: " + this.getName() + " ";
output = output + "Salary: $" + this.getSalary() + " ";
return output;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
