Learning Objectives

  • Abstract Class
  • Dynamic Binding
  • Reinforce Concepts from Inheritance and ExceptionsIntroduction

    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.

      Set Up

      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.

      Introduction to HashMap

      • In computing, a hash table (such as a Java HashMap) is a data structure used to implement an associative structure that maps keys to values.
      • A hash table uses a hash function to compute an index into an array of buckets or slots, from which the correct value can be found.
      • In a well-dimensioned hash table, the average cost (number of instructions) for each lookup is independent of the number of elements stored in the table.
      • Many hash table designs also allow arbitrary insertions and deletions of key-value pairs, at constant average cost per operation.
      • In some situations, hash tables turn out to be more efficient than search trees or any other table lookup structure. For this reason, they are widely used in many kinds of computer software, particularly for associative arrays, database indexing, caches, and sets.
      • Here is the Java documentation for HashMap: http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.htmlCreating a HashMap
        • To Create a HashMap in java, we first need to import the required header files:import java.util.HashMap;import java.util.Map;
          • To Create a HashMap in java, we use the HashMap constructor. HashMap is a generic class, which means that we must indicate the types of the keys and values it will use. For a map from String to Integer we use the constructor as follows:Map<String,Integer> map = new HashMap<String,Integer>();
            • The basic working of a HashMap used to track integer counts is:
              1. Check if the element is present in the map.
              2. If the element is present, increment its corresponding count.
              3. If the element is not present, add the word to the map with its corresponding count.
              4. To look for an element in the map, use the method: get()
              5. For example, if we are looking for the word “the”:Map<String,Integer> map = new HashMap<String,Integer>();String word = “the”;Integer count = map.get(word);
              6. If the count in the above example is null, it simply means that the word doesn’t exist in the map.
              7. To add an element to the map, use the library function: put():map.put(word, count);
              8. The arguments passed to the function 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.Description of the given Java files & Implementation Steps

                In the beginning, define two classes for your exception handling.

                • ArrayElementException.java
                • IllegalDivisionByZero.java

                  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).Teacher.java

                    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.Student.java

                    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.TeachingAssistantStudent.java

                    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.Scenario.java

                    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.Sample Outputs

                    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: 20CourseNameCourseGradeCoursesAssisting HoursCS180 10CS252 10Student: John Doe (1 Happy Ave)Division by zero!Course CS180 added with the grade 97Course CS252 added with the grade 68CourseNameCourseGradeCS18097CS25268Average is: 82.5Teacher: William Birck (8 Sunset Way)CS177 added.CS180 added.Course Already in list!Courses teaching this semester: CS177CS180CS177 removed.CS180 removed.Course not found!Courses teaching this semester: Course Details for AcademicPerson number 0:CourseNameCourseGradeCoursesAssisting HoursCS180 10CS252 10Course Details for AcademicPerson number 1:CourseNameCourseGradeCS18097CS25268Course Details for AcademicPerson number 2:Courses teaching this semester: