Add an abstract class called Person to your project Add the following public pro
ID: 3874572 • Letter: A
Question
Add an abstract class called Person to your project
Add the following public properties to your Person class:
(String) name
(int) age
Add another class called Employee that inherits from Person and add the following public properties:
(double) salary
(int) yearsOfService
Add another class called Student that inherits from Person and add the following public properties:
(ArrayList) grades
(int) yearInSchool
(String) major
In the Student class create a method called getAverage() that will cycle through all grades in the array list and return the average. Note: if you want to know the number of elements in an array list it is: grades.size() This is equivalent to an array’s .length
Add another class of your choosing and include appropriate properties to the class
For all classes include:
A single workhorse constructor
toString() method
can someone tell me how to do this one?I totally have no idea.....
Explanation / Answer
package mypackage;
public abstract class Person {
public String name;
public int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
package mypackage;
public class Employee extends Person {
public double salary;
public int yearsOfService;
public Employee(String name, int age, double salary, int yearsOfService) {
super(name, age);
this.salary = salary;
this.yearsOfService = yearsOfService;
}
@Override
public String toString() {
return "Employee [salary=" + salary + ", yearsOfService=" + yearsOfService + "]";
}
}
package mypackage;
import java.util.ArrayList;
public class Student extends Person {
public ArrayList<Double> grades;
public int yearInSchool;
public String major;
public Student(String name, int age, ArrayList<Double> grades, int yearInSchool, String major) {
super(name, age);
this.grades = grades;
this.yearInSchool = yearInSchool;
this.major = major;
}
@Override
public String toString() {
return "Student [grades=" + grades + ", yearInSchool=" + yearInSchool + ", major=" + major + "]";
}
public void getAverage()
{
double avgGrade=0;
for(Double grade:grades)
{
avgGrade=avgGrade+grade;
}
avgGrade=avgGrade/grades.size();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.