Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions