Question: Use the Java hierarchy you posted in Week 3 (corrected based on any feedback you may have received) add one overriding method and one overloading.

Use the Java hierarchy you posted in Week 3 (corrected based on any feedback you may have received) add one overriding method and one overloading. The main method should create an instance of the class and demonstrate the correct functionality of the overriding and overloading methods.

//TEST

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

Teacher teacher = new Teacher("Professor Smith",147_099.6,30);

System.out.println(teacher.toString());

System.out.println("Class Average: "+teacher.getClassAverage());

}

}

//Teacher

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");

return;

}

this.students = students;

}

public int getStudents(){

return students;

}

public void setStudents(int students){

if(students<0){

System.out.println("teacher cannot have less than 0 students");

return;

}

this.students = students;

}

public float getClassAverage(){

// pick a random grade between 20 and 120

Random ran = new Random();

return ran.nextInt(101)+20;

}

public String toString() {

return super.toString()+"Students: $" + this.students + " ";

}

}

//Employee

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 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!