In this lab, you are going to learn how to use abstract classes and how dynamic binding is applied. You are given an abstract class called AcademicPerson
, which has one abstract method that needs to be implemented in any class that extends AcademicPerson class. If a class that extends AcademicPerson
does not implement the abstract method, then the derived class will have to be abstract itself.
The skeleton code of the following classes are given to you:
Teacher
, which extends AcademicPerson
Student
, which extends AcademicPerson
TeachingAssistantStudent
, which extends Student
Scenario
You need to implement the requested methods in each of the above classes. The constructor and all instance variables are given to you, you don’t need to change them.
Before coding, you need to set up your working directory. Open the terminal and go into your cs180 directory. Inside the cs180 directory, create the lab13 directory for this lab assignment.
For example, assume you are in your home directory (/homes/your_login):
% mkdir -p ~/cs180/lab13 % cd ~/cs180/lab13
Download skeleton. Unzip the skeleton zip file under ~/cs180/lab13 and open the downloaded files with DrJava or Eclipse (preferred). If you use Eclipse, switch workspace to ~/cs180, and create a Project named lab13 under ~/cs180.
HashMap
) is a data structure used to implement an associative structure that maps keys to values.HashMap
: http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.htmlimport java.util.HashMap; import java.util.Map;
HashMap
used to track integer counts is:get()
null
, it simply means that the word doesn’t exist in the map.put()
:
map.put(word, count);
put
are the word (String
) and count (int
) which is the total number of times the word is seen. To fetch the count of a given word, use the get()
function.In the beginning, define two classes for your exception handling.
The following description shows the functionality of the given classes.
Instance variables:
name
(name of the person)address
(address of the person)numCourses
(number of courses taken so far)courses
(course codes)public String toString() |
---|
This method returns the name and address of the person. |
public abstract void printCourses(); |
---|
This is the definition of the abstract method that needs to be implemented in any class that extends the AcademicPerson class (and is not abstract itself). |
You need to implement the following methods:
public void addCourse(String course) |
---|
It adds a course into the list of courses(located in the parent) for the teacher. This method throws “ArrayElementException” when the course that is being added to the list already exists in it. |
public void removeCourse(String course) |
---|
This method removes a course from the list of courses(located in the parent) for the teacher. This method throws “ArrayElementException” when the course that is being removed is not in the list. |
public void printCourses() |
---|
This method prints all of the courses in the list, one in each line. |
In this class there is a variable called grades which contains the grades of the courses that the student has taken.
You need to implement the following methods:
public void addCourseGrade(String course, int grade) |
---|
It adds a course into the list of courses (located in the parent class) for the Student. Grades is a list that is defined in this class; you need to add the grade of this course to this list. A student can take the same course several times. If a course that already exists in the list is given as input to this method, you need to compare the input grade with the one that is saved in the Grades list; the highest grade is saved in the Grades list. |
public void getAverageGrade() |
---|
This method prints the student’s average grade for all the courses. The method throws “IllegalDivisionByZero” exception, when there are no courses in the list. |
public void printCourses() |
---|
The method prints all the courses with the corresponding grades, one in each line. |
In this class the list of the courses in the parent class is used for the courses that the TA is taking as student; for the courses that he/she is teaching, a HashMap
named courseHour
has been defined.
You need to implement the following methods:
public void addCourseHour(String course, int hours) |
---|
It adds a course into the HashMap with the key of course and hours as the value. This method throws “ArrayElementException” when the course that is being added to the HashMap already exists in it. |
public void printCourses() |
---|
The method should first print the courses, along with the grades, that the TA is taking as a student. It should then print out the courses with the corresponding hours that the TA is assisting, one in each line. |
In this class, there is a main method that is given to you for testing. You are free to change it since it is not being tested.
In this class you need to implement only one method.
public void printCourses(AcademicPerson[] ap) |
---|
This method takes a list of type AcademicPerson and prints out the details of their courses. |
If your implementation is correct, you’ll have the following output when you run the main method:
Student: Sara Smith (100 State Ave) Also serving as Teaching Assistant. CS180 added for TA. CS252 added for TA. Course Already in list! Total TA hours: 20 CourseName CourseGrade CoursesAssisting Hours CS180 10 CS252 10 Student: John Doe (1 Happy Ave) Division by zero! Course CS180 added with the grade 97 Course CS252 added with the grade 68 CourseName CourseGrade CS180 97 CS252 68 Average is: 82.5 Teacher: William Birck (8 Sunset Way) CS177 added. CS180 added. Course Already in list! Courses teaching this semester: CS177 CS180 CS177 removed. CS180 removed. Course not found! Courses teaching this semester: Course Details for AcademicPerson number 0: CourseName CourseGrade CoursesAssisting Hours CS180 10 CS252 10 Course Details for AcademicPerson number 1: CourseName CourseGrade CS180 97 CS252 68 Course Details for AcademicPerson number 2: Courses teaching this semester:
Are you overwhelmed by an intense schedule and facing difficulties completing this assignment? We at GrandHomework know how to assist students in the most effective and cheap way possible. To be sure of this, place an order and enjoy the best grades that you deserve!
Post Homework