---
# System prepended metadata

title: 'Homework 1: Classes'
tags: [Homeworks]

---

---
tags: Homeworks
---
# Homework 1: Classes
### Due: Jan 29, 2021 (Anywhere on Earth)

## Learning Objectives
* Create a class hierarchy that makes proper use of interfaces (for variant types or contracts) and abstract classes (for sharing common code)
* Comment code effectively with Javadocs
* Develop a collection of examples and tests that exercise key behavioral requirements aspects of a problem
* Write programs that can compile and run against a provided interface
* Write programs that behave as expected based on a provided description or collection of test cases




By the end of this homework, you will be able to:
* design a class hierarchy for a real-world situation
## Assignment Link
Here is the [github classrooms link](https://classroom.github.com/a/KyIl4vAH)

If this is your first time downloading an assignment for the course, check out our [First Time Setup Guide](https://hackmd.io/6K9NmQ0fSjOxH7AYeIYGnw?view).

Please make sure to read [our gradescope submission guide](https://hackmd.io/jC-SEMn_RpiGqMH_EhsDMw) before handing in your assignment!

## How to Hand In <a name="handin"></a>
In this assignment, there are a few Study Questions. These are for practice, so you do not need to submit anything for these questions. We will post the answers to the Study Questions on Piazza.

In order to hand in your solutions to these problems, they must be stored in appropriately-named files with the appropriate package header in an appropriately-named directory.

The source code files should comprise the `src` package, and your solution code files, the `sol` package. We mean that all your solution code should have a line at the top saying `package sol`; and they should be in the `hw01-classes/sol/` directory. For this assignment there is no `src` code, but in the future, it will have `package src`; and be in `[homework-name]/src/`. 

After completing this assignment, the following solution files should be in your
`hw01-classes/sol/` directory:

* `Course.java` containing `public class Course`
* `Person.java` containing `public abstract class Person`
* `Employee.java` containing `public abstract class Employee`
* `Faculty.java` containing `public class Faculty`
* `TA.java` containing `public class TA`
* `Student.java` containing `public class Student`
* `GradeReport.java` containing `public class GradeReport`
* `LetterGrade.java` containing `public class LetterGrade`
* `SNCGrade.java` containing `public class SNCGrade`
* `Homwork1TestSuite.java` containing `public class Homework1TestSuite`
* you may also have other classes, interfaces, and abstract classes

To hand in your files, submit these files to the **Homework 1: Implementation** assignment on Gradescope. Once you have handed in your homework, you should receive an email, more or less immediately, confirming that fact. If you don’t receive this email, try handing in again, or ask the TAs what went wrong.


## Testing
Please make sure to test your code! Testing and good code design make up a lot of your grade for this homework. Put your testing in a file named `Homework1TestSuite.java`.

You should submit `Homework1TestSuite.java` to the **Homework 1: Testing** assignment on Gradescope. See [our gradescope submission guide](https://hackmd.io/jC-SEMn_RpiGqMH_EhsDMw) for an explanation on how we are grading your tests, and how you can use the Testing assignment on Gradescope to get a better understanding of the assignment.

**Note:** For this assignment, you do not have to include test cases for any exceptions that are thrown in the constructor of a class.

## The Hunt Begins...

After a long journey on the S.S.N.C, you finally step foot on Rhode Island's firm soil. As you step through the cold New England night, a scrap of parchment flutters through the breeze to your feet. You pick it up and read its contents aloud.

*My name, dear reader, you should already know*
*A bear lamped in blue, left not long ago*
*If treasure you seek*
*In grades you should peek*
*At the last will and testament of Blueno*

You heed the advice of the mysterious message and continue onwards towards Brown. What secrets lay hidden beneath Brown's unique grading system?
## Problem Setup

In an organization such as Brown, there seems to be some obvious hierarchy among people: there are full-time employees (including faculty, admins, facilities staff, etc), and there are students (including graduate and undergraduate). A hierarchy of classes seems a good way to capture the people at a university. But some subtle issues arise. For example, instructional staff will need the ability to store and view grades. Clearly, faculty are instructional staff, but so are TAs. TAs are students, however, and students in general shouldn’t be entering grades. This assignment uses the Faculty, Students, TAs, and grading as an example to help you understand the roles of interfaces, abstract classes, and types in Object Oriented design.

**Note:** We have you develop the code for this assignment in stages. You will turn in one set of files with your cumulative work on all tasks. You do *not* need to maintain versions of your code from each task separately.

**Note:** You will see several study questions in the handout. You are not required to submit answers to these, but you should have figured out answers to them, because they check how well you understand the concepts we are studying.


# Problems

## Courses

Start by creating a class name `Course` with fields for the department (a string), `courseNumber` (integer), and number of `credits` the course gives (a double that should be either .5, 1, or 1.5).

For this class only, make two constructors. The first should simply require all three fields as inputs. The second should take only the department and course number, setting the credits to 1 (by default). Since most courses are worth 1 credit, this makes it easier to create courses with default information. 

The first constructor should throw an `IllegalArgumentException` with the error message ``"Invalid credits"`` if the number of credits is not one of the expected values.

**Task:** Define the `Course` class as just described.


## Faculty and Students
Now create a simple class hierarchy for people at a university. Every `Person` is either a full-time `Employee` or a `Student`. To keep the assignment from getting tedious, the only full-time Employees we’ll capture are `Faculty`. We won’t distinguish between undergraduate and graduate students. In other words, we have the following hierarchy:
![](https://i.imgur.com/5x1ACiB.png)
Note that students in this hierarchy take only one course course at a time (because we haven’t yet learned how to do lists in Java). The course and grade fields should be initialized to null as part of declaring the field within the class. You can do this by writing something like: 

`Course taking = null;`

when you set up the field. *Fields that are initialized in this way do not need to be arguments to the constructor*. We initially set field values to null when we intend to set the value of a field sometime after the object has been created. In the first lecture on lists, we said not to use null to represent the empty list because the empty list is not a temporary value, but rather a useful subset of lists on which we need to define methods.

**Task:** Create a class hierarchy (classes, abstract classes, and interfaces, as needed) to capture the class hierarchy in the diagram. Include fields that capture the information in the diagram. Include constructors where appropriate.
- **Note:** Make sure that the `Person`, `Student`, `Employee`, and `Faculty` classes/interfaces/abstract classes are in your hiearchy. That is, if you make a class to represent students, you should name it `Student`, and not `Students`, or `SomeStudent`, or another name. Otherwise, your code will fail some of the autograder tests.

**Task:** Define a method `register` for students that takes a course and stores it in the `taking` field if the student is not already taking another course. Raise a `RuntimeException` with the message `“Cannot register for course”` if the student is already taking something else.

**Task:** Define a method `teach` for faculty that takes a course and stores it in the teaching field. Raise a `RuntimeException` with the message `“Cannot teach course”`if the professor is already teaching something else.

When testing, it might also be helpful to define a method `checkteach(Course c)`, which returns `true` if the Faculty is teaching that course, and `false` otherwise. 

**Study Question:** Since both faculty and students are involved with Courses (teaching or taking) and the two methods you just wrote seem similar, should we have a shared course field in either the Person class or in an abstract class over students and faculty?

**Study Question:** Why are we creating the `register` and `teach` methods, instead of just requiring courses to be passed in as part of the constructors for students and faculty?

## TAs
**Task:** Augment your work to also include TAs (the class name should `TA`). TAs are students, but they also have a field named `assisting` that stores which course the TA is currently helping on. Since TAs are only set up when they are known to be assisting courses, we can take the course that a TA is assisting as part of the constructor for a TA. The TA’s `register` method should not allow a `TA` to register for a course they are assisting in. Raise a `RuntimeException` with the message `“Already assisting or registered for this course”`if this is the case.

## Grading
Now, we want to extend our classes to support grading. Thinking ahead to when students might be enrolled in multiple courses, we want to store a grade along with the course that the grade was for.

**Task:** Create a class called `GradeReport`that has two fields: `forCourse` and `grade`. For now, let the grade be just an integer. The constructor should take both fields as inputs.

**Task:** Add a field `grades` to `Student`, which will hold a single `GradeReport`. This field should be initialized to null when creating Students. Now, we need to let faculty and TAs assign grades to students.

Now, we need to let faculty and TAs assign grades to students.

**Task:** Add a method called `giveGrade` to the `Student` class. This method should take the `Faculty` or `TA` who is giving the grade, the Course that the grade is for, and a numeric grade. The method should create a `GradeReport` and store it in the student’s grades field, as long as the student is taking the given course and the given Faculty/TA is working on the course. 

If either of these conditions fails, the method should throw a `RuntimeException` with the message `“Incorrect course”` If the conditions hold, however, return the newly created `GradeReport`.

Figuring out how to define `giveGrade` to take only faculty or TAs as the person assigning the grade is part of what we want you to think through for this assignment. You do **NOT** have a way to ask ”is this object from the `Faculty` class”: you have to find another way to set this up using what we have learned in the course so far. If you have seen Java and know `instanceOf`, you may **not** use it here, as that is actually not proper OO design (in general, not just for CS18). You are welcome to create additional methods in the class hierarchy if needed to write this in good
OO style. 

**Study Question:** What can go wrong if we use `Person` as the type of the grade-assigner in `giveGrade`?

## Enriching Grades
Our initial setup has simple numeric grades, but in reality there are many forms of grades: numeric grades, S/NC grades, letter grades, written performance reports, and so on. We therefore want to relax our notion of grades to support more options.

For this assignment, we will support two kinds of grades: letter grades (one of “A”, “B”, “C”, “NC”) and S/NC grades, which are captured with two booleans: one for whether the student passed, and another for whether the student has passed with distinction. The distinction boolean should only be true if the passed boolean is also true

**Task:** Define classes for `LetterGrade` and `SNCGrade`, with the components indicated above. 

The constructor for `LetterGrade` should take in a string `"A", "B", "C"`, or `"NC"` and store that string in its `grade` field. If the input string is any other string, you should throw a `RuntimeException` with the message `“Invalid grade”`.

The constructor for `SNCGrade` should take in a string with three possibilities: “SDIST”, “S”, or “NC”. You should use this string to set the fields `pass` and `passDistinction`. If the input string is any other string, you should throw a `RuntimeException` with the message `“Invalid grade”`.

Each grade class should have a method `isPassing`, which takes no input and returns a boolean indicating whether the grade reflects the student having passed. For letter grades, any grade other than “NC” is a passing grade.

**Task:** Modify `GradeReport` and `giveGrade` so that grades are now either letter or S/NC grades. We no longer want to support numeric grades (we just used those to get you started more easily).

**Task:** Add a method `creditsEarned` to the `Student` class, which takes no input and returns a double. If the student has a `GradeReport` and has earned a passing grade, this method returns the number of credits for the course. If the student has no `GradeReport`, you should throw a `RuntimeException` with the message `“No gradereport”`. If the student has a GradeReport but didn’t pass, the method should return 0.

Your challenge here is to write this in good OO style, meaning that you do not dig into fields further than calling methods on them. Create additional methods in your classes as needed to satisfy this requirement.

**Task:** Only professors are allowed to view students’ grades; furthermore, they can only view grades of students in their own courses. Add a method to the `Faculty` class called `viewGrade` that takes a `Student` and returns a string. 

If the student is in the professor’s class and has a `GradeReport` stored, the method returns a string indicating the grade earned. You should throw a `RuntimeException` with the message `“Not enrolled”`, if the student is not in the professor’s course, and you should throw a `RuntimeException` with the message `“Invalid report”`, if the student has no `GradeReport`.

**Hint:** You will want to add a method to each class that produces a string version of the grade. In Java, every class as a default method named `toString` that shows how to display that class as a string (by returning a String—note the capital letter). Write `toString` methods for each Grade class. For letter grades, just return the string value. For S/NC grades, return one of “NC”, “S”, or “S*” (for S with distinction).

## The Hunt Continues...
Whilst going through the grade reports, you find a loose sheet of paper hidden among the stack. 
![](https://i.imgur.com/yu037pH.png)

It dawns on you that Blueno may have left more than just their ghost behind. This might be the first clue to finding Blueno's lost treasure! It might be wise to save the above image. The subsequent clues may not be as easy to find...

---

Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CS 18 document by filling out our [anonymous feedback form](https://forms.gle/ym89rAZyBk8Hg3LNA)







