Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ICS 108 Object-Oriented Programming Lab # 10 Polymorphism Objectives: In this lab, the following topic will be covered: Polymorphism Task Given below the classes Lab10,

ICS 108 Object-Oriented Programming

Lab # 10 Polymorphism

Objectives:

In this lab, the following topic will be covered:

Polymorphism

Task

Given below the classes Lab10, Person, Employee, and Student, complete the following program to produce the output provided.

Note:

Write the missing methods only.

Do not modify the classes Person, Employee, Student.

Do not change the main method.

Put each class in a separate file

Here is a sample run of the program:

List of Employees

Name Phone ID Salary

Saad 0563428255 200003 16000

Salem 0501331845 200001 9000

avg salary of = 12500.0

List of Students

Name Phone ID GPA

Reem 0564448202 20000 3.6

Hasan 0594448202 20002 2.8

avg GPA of students = 3.2

// file Lab10.java

import java.util.ArrayList; public class Lab10 { public static void main(String[] args) { ArrayList personList = createPersonList(); //1. This method prints the employees only

printEmployees(personList); //2. This method finds the average salary of employees

double avgSalary = avgSalary(personList); System.out.println("avg salary of = " + avgSalary); //3. This method prints the students only

printStudents(personList); //4. This method finds the average GPA of students

double avgGpa = avgGpa(personList); System.out.println("avg GPA of students = " + avgGpa); } private static ArrayList createPersonList() { ArrayList personList = new ArrayList<>(); personList.add(new Employee("Saad", "0563428255", 200003, 16000)); personList.add(new Student("Reem", "0564448202", 20000, 3.6)); personList.add(new Employee("Salem", "0501331845", 200001, 9000)); personList.add(new Student("Hasan", "0594448202", 20002, 2.8)); return personList; } }

// file Person.java

public class Person { private String name, phone; public Person(String name, String phone) { this.name = name; this.phone = phone; } @Override public String toString() { return name + "\t" + phone; } }

// file Employee.java

public class Employee extends Person{ private int sid; private int salary; public Employee(String name, String phone, int sid, int salary) { super(name, phone); this.sid = sid; this.salary = salary; } public int getSalary() { return salary; } @Override public String toString() { return super.toString() + "\t" + sid + "\t" + salary; } }

// file Student.java

public class Student extends Person{ private int sid; private double gpa; public Student(String name, String phone, int sid, double gpa) { super(name, phone); this.sid = sid; this.gpa = gpa; } public double getGpa() { return gpa; } @Override public String toString() { return super.toString() + "\t" + sid + "\t" + gpa; } }

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

Building Database Driven Catalogs

Authors: Sherif Danish

1st Edition

0070153078, 978-0070153073

More Books

Students also viewed these Databases questions