Java Lab 2 β€” Inheritance

Remember: the goal of these labs is to give you experience with Java. Labs are not graded, but you will receive feedback, and your participation is mandatory.

All exercises are due by midnight Friday. See below for the turn-in procedure.

Goal

This lab is divided into two sections:

  • In the first section you will write a java program that keeps a list of students and teachers, sorts this person’s list by name and prints it.
  • In the second section you will add to this program the possibility to keep track of courses, each course will have a teacher and many students. You will add a class that tries to find a schedule so that no teacher has to teach two courses the same day, and no student has to follow two courses the same day.

Part I: People

In the following exercises, we will create the following class hierarchy:

![Person hierarchy](images/person-hierarchy.png)

For each exercise, you will probably want to write a main method in a separate class to test the classes you are implementing as you go.

Exercise 1: Class Person

Write an abstract class Person that implements the above methods. A person should have a given name and a family name. It should also be possible to print a representation of the person.

Exercise 2: Classes Teacher and Student

Write the Teacher and Student classes that inherit from Person. In addition to a name, Students should have a current year in school. Printing a Teacher or a Student should show this additional information as well.

Exercise 3: Class People

Write a class People that inherits from the java.util.ArrayList<Person> class. This class will keep the list of students and teachers. It will also be capable of printing a list of all of the people.

Sorting out people

Now we want to sort people so that we can print them in alphabetical order. To achieve this, we will use the method java.util.Collections.sort() that we saw in class the other day. This method requires that objects be Comparable (by satisfying the) Comparable interface.

Exercise 4: Interface Comparable<Person>

We want people to be ordered by the their names, family name first. If two people have the same family name, the two would be ordered by their given names.

Exercise 5: Sorting People

Add a method sort() to the class People.

Part II: Course scheduler

In this part, we will use your solutions from Part 1 to create the following class hierarchy:

![Course hierarchy](images/course-hierarchy.png)

Exercise 6: Courses

Create a class Course. Each course has a single Teacher and many students.

  • Think about how to add students to the course. What do you need to do that make that possible?
  • Each course is either scheduled or not scheduled for a specific day of the week, which we can represent by an int.
  • The setDay(int) method schedules a course for a given day of the week.
  • The method compatible checks if two courses can co-exist at the same time (that is, they do not share any People in common).

Next, we are going to write a class Schedule that tries to find a compatible schedule for a list of courses.

Exercice 7: Exceptions

Create a new exception class called IncompatibleSchedule.

Exercice 8: Schedule

Write the class Schedule as shown above, but only implement the method print(). The method print should print all the Courses in Schedule, and the day they are scheduled, or not scheduled if they are not scheduled.

Exercice 9: checkSchedule

Implement the method checkSchedule. This method takes a Course:

  • if all the others courses scheduled the same day are compatible, it does nothing.
  • if one of them is not compatible, it throws exception IncompatibleSchedule.

Exercice 10: updateSchedule

Implement the method updateSchedule, using the method checkSchedule. This method tries to find a schedule for all the Courses. If this schedule is found it returns true; if no compatible schedule exists, it throws IncompatibleSchedule.

  • To implement this method you will use a backtracking algorithm.
    1. If all the courses are scheduled, return true.
    2. Otherwise, if there still are unscheduled courses, take one of them.
    3. For each of the available days:
      • Try to schedule if for day d.
      • call updateSchedule() recursively to try to schedule the other courses.
      • if there is a schedule incompatibility, try another day.
    4. if there is no possible day on which to schedule the course, throw IncompatibleSchedule.

Exercice 11: Test

Add some students, teachers and courses and test the scheduling algorithm. To avoid adding a lot of courses, you can set daysInSchoolWeek to a small value during testing.

Hand-in

  1. Select your projects in Eclipse.
  2. Right-click and select Export...
  3. Choose General > Archive File.
  4. Be sure to un-check the .classpath and .project files.
  5. Export the project to an archive named your-last-name-lab2
  6. Upload the archive (zip) to the lab2 submission site.

Creative Commons License Assignment by Pablo de Oliveira Castro, licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.