818 views
 owned this note
<style> h3 { border-bottom: 1px solid #ccc; } section { margin-bottom: 2rem; padding: 0em 1em; padding-bottom: 1em; border-radius: 4px; background-color: #f7f7f7; border: 1px solid #ccc; } summary { font-weight: bolder; } summary:hover { text-decoration: underline; } .todo { color: #ff00ff; border: 2px dashed #ff00ff; padding: 0em 1em; border-radius: 5px; //display: none; // UNCOMMENT TO HIDE TODOs } </style> # Homework 1B-from-FP (Spring 2025) **Due: Sunday, February 2 at 11:59pm** :::info **Collaboration Policy:** _You may collaborate as much as you want on this assignment_ (this is more flexible than the normal course policy). The goal is to get everyone up to speed on Java. You are required to turn this in, but it will be weighted lightly in final grades. That said, we strongly encourage you to actively try writing these on your own while collaborating, as future assignments will assume you can handle these sorts of problems independently. ::: **Need help?** Find us, as well as questions and answers from other students, on [EdStem](https://edstem.org/us/courses/69789/discussion)--**be sure to check our FAQ/reading list post before posting, since this contains helpful tips that may already answer your question!** Also, here’s our [office hours schedule](https://brown-csci0200.github.io/calendars.html). ## Learning Objectives In this assignment, you will: - Practice using interfaces as types - Practice sharing common fields and methods across classes - Practice creating association and interface/inheritance diagrams to present problem designs ### Stencil Code and Assignment Setup Using **[this GitHub Classroom link](https://classroom.github.com/a/03IdaVqi)**, accept the assignment and create a repository for your code. **Be sure to follow the [Java Stencil setup guide](https://docs.cs200.io/s/java-stencil-setup-guide)** for instructions on how to do this. This assignment requires that you have IntelliJ installed and set up for this course. For details on the setup process, please see the [IntelliJ Setup Guide](https://docs.cs200.io/s/intellij-guide). If you’re having trouble, the guide includes a [Common Bugs/FAQ](https://docs.cs200.io/s/intellij-guide#Common-BugsFAQ) section which might help. ### Style Expectations While you may have seen references to the course style guide, for this first assignment **we’ll only be expecting the following style requirements**: - Method and variable names are in `camelCase` - Class names are in `UpperCamelCase` - Basic Javadocs for all methods and classes (look [here](https://docs.cs200.io/java-style-guide#Documentation-Comments) for examples) ## Testing Make sure to test your code iteratively as you work! That means writing tests for your methods _before_ you write the methods. Testing and good code design make up a lot of your grade for this homework. Put your testing in the file `Homework1BTest.java`. For this and all assignments, you should test _all_ of the methods that we have asked you to write. **This includes testing all exceptions mentioned in the problem statements.** :::warning <details><summary><b><i>Warning:</i> Extraneous Testing</b></summary> When writing your test suite, you should test _only_ the methods we have asked you to write in this handout. Do not reference fields or methods that are not required by the assignment in your tests, or the autograder may fail. ::: </details> ## Creating An Association/Interface/Inheritance Diagram Association/interface/inheritance diagrams represent the structure of your project, including interfaces, abstract classes, and classes. Here is a [guide to the notation in these diagrams](https://docs.cs200.io/containment-association-interface-inheritance-guide). ### Task 1 **Answer the questions (in the dropdown below) about this diagram that we are developing in lecture.** :::info <details><summary><b><i>Details</i></b></summary> ![The inheritance-containment diagram for animals](https://imgur.com/MJ84e3m.png) - Which class names can be used with the “new” keyword? - What are the fields and methods in a `FruitFly`? - What are the fields and methods in a `Shark`? - Imagine that we wanted to add a method named `likesFruit` to `IAnimal`, which returns true for all `FruitFly`s and those `Boas` whose `eats` field is `"fruit"`. Which classes would need to be edited to provide this method? ::: </details> **Type your answers in a block comment at the bottom of your** `Homework1BTest.java` **file.** ## Creating Your Own Association/Inheritance/Interface Diagram Kathi has decided that she would like to pursue her other passion: cookies! She’s just opened the “Kathi Kafé,” which she’ll operate on the weekends. Kathi will need baristas, waiters, and bakers. These people will be her employees. Each employee has an hourly wage. Kathi is, of course, the boss of the Kathi Kafé, so she needs to be represented as well. However, she is not an employee (she’s putting all her profits back into the business). Kathi, the baristas, and the bakers need access to the supply closet. Kathi and the waiters need to be able to open the cash register. ### Task 2 **Develop an association/inheritance/interface diagram for this scenario.** :::info <details><summary><b><i>Details</i></b></summary> You can draw this on paper or with a drawing tool. There’s no one correct answer to this question, but there are “better designed” answers. Your diagram should include both association relationships and any inheritance/interface relationships. Refer to the [diagram guide](https://docs.cs200.io/containment-association-interface-inheritance-guide) to see how to represent these relationships. **You are NOT writing code for this question.** Along with the code files you submit to Gradescope, upload a png (picture) or a PDF with your diagram, named `HierarchyDiagram.png` or `HierarchyDiagram.pdf`. ::: </details> Save this in a document called `HierarchyDiagram.pdf`. # [Programming Problems](https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExeWppcG43NjBoNDhxeWZscXFxc2t5N2gzb2Q2Y2dxY21lZHZoMG9hbiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9cw/2LBbHKWzEMRvtnNJHu/giphy.gif) ## Adding Grades and TAs In Homework 1A, we began designing a C\@B-like system, and made classes to represent courses, faculty, and students. With this homework, we will add more features, such as: - Different kinds of grades - TAs (students who can also help with grading) :::info **Important prerequisites**: You should start with your `Student`, `Faculty`, and `Course` files from HW1A. To use them, copy `Student.java`, `Faculty.java`, and `Course.java` from the `sol` folder in your HW1A project into the `sol` folder for this project.   <br /> If you prefer, we have provided completed versions of these files you can use instead. Go to **[this folder](https://canvas.brown.edu/courses/1097657/files/folder/hw01a-sol-files)** on Canvas to download individual files (or the zip containing them) and put them in your `hw1-17b` -`sol` fodler. If this link doesn’t work, go to our course [Canvas](https://canvas.brown.edu) page and click on “Files” to see them). ::: </details> ### :rotating_light: Super important notes :rotating_light: For this assignment, you will be creating several classes with fields. To be consistent with our autograder, you must follow a few conventions when doing this (for just this assignment): - **Make all fields `public`**: When you declare fields in your classes, they should be `public`. That is, if you were creating a field called `length`, you should declare it as `public int length`, similar to what we did in class. - **Naming**: Name your fields with **exactly** the same names as listed in this handout. Our autograder will try and access fields with these names--if you don't use them, there will be errors! ## Adding Grades As we build out our classes for managing courses, we will need to represent course grades. ### Task 3 **Create a class called `GradeReport` that has three fields: `forStudent`, `forCourse` and `grade`.** The constructor should take parameters for each of these three fields as inputs (in order as stated). For now, let `grade` be an `int`. In the next task, we’ll learn about another way to represent grades. :::info <details><summary><b><i>Enums:</i> types with a few discrete variants</b></summary> Instead of using built-in types like `String`, we can use `enum` types. An `enum` is a special data type used to represent a group of **related constants**. They are types that we define. An example could be four directions: Up, Down, Left, and Right. For this, we can define a `Direction` enum. To create a new `enum` in IntelliJ, you can click New > Java Class, and then select `enum` from the list on the menu that appears. ![](https://lh5.googleusercontent.com/6g8XYj29n0ThqqpF-ZUIijyUcbmdI1NSoTfz3XR0fZHY5_O4soaTDTwvWtauAHE_GhbJ62tXoy71jM-G7kRg61iIV2U4EyJg7YbS0UXS5yxdLXAxCxD9oy_rbf0PyYmdsaQfQrq2c7W3AWjMV0jJ9fE) We define enums using the `enum` keyword (in place of `class` or `interface`). List the variants, separated by commas. These variants should be in UPPER_CASE_LETTERS. ```java public enum Direction { UP, DOWN, LEFT, RIGHT; } ``` To access a specific `enum` variant, use dot syntax. Enums are a special type of class, so we can use the name of an enum anywhere that we would use the name of a class. ```java Direction myFavoriteDirection = Direction.LEFT; Direction yourFavoriteDirection = Direction.RIGHT; if (myFavoriteDirection != yourFavoriteDirection) { System.err.println("We have a friendly disagreement."); } ``` Switch statements are commonly used when working with `enum`s. Here is an example that gets the `String` representation of a `Direction` called `myFavoriteDirection`. Notice that we use the unqualified name of each variant in switch statements (e.g. `UP` instead of `Direction.UP`). This example could be done using an `if`-`else if`-`else` tree, but a switch statement is more appropriate since we are comparing against all possible `Direction`s. ```java String stringRepresentation; switch (myFavoriteDirection) { case UP: stringRepresentation = "UP"; break; case DOWN: stringRepresentation = "DOWN"; break; case LEFT: stringRepresentation = "LEFT"; break; case RIGHT: stringRepresentation = "RIGHT"; break; default: break; } ``` ::: </details> ## Creating Grade Classes Grades come in one of two formats: - Letter grades can be one of `A`, `B`, `C`, `NC`. These can be represented using `enum`s. - S/NC grades are captured with two booleans: `pass` for whether the student passed, and `passDistinction` for whether the student has passed with distinction. The `passDistinction` boolean should be `true` only if the boolean representing whether the student passed is also `true`. ### Task 4-A **Define classes for `LetterGrade` and `SNCGrade`, with the components indicated above (each class in separate files).** :::info <details><summary><b><i>Details</i></b></summary> The constructor for `LetterGrade` should take in a `Letter` enum with constants for `A`, `B`, `C`, or `NC` and store that enum constant in its `grade` field. The constructor for `SNCGrade` should take in a `SNC` enum with three constants: `SDIST`, `S`, or `NC`. You should use this enum to set the fields `pass` and `passDistinction`. Remember that the `Letter` and `SNC` enums should be defined in their own files, not inside of the `LetterGrade` or `SNCGrade` classes. See [the section above](https://docs.cs200.io/_7-_K3iLRja4WviBh8BxAA?view#Task-3) for guidance on creating new enums in IntelliJ. ::: </details> ### Task 4-B **Define an interface `IGrade` that requires an `isPassing` method that takes no inputs and returns a `boolean`.** Both of the grade classes (`LetterGrade` and `SNCGrade`) should implement this interface. See the lecture 3 notes for an example of an interface.  ### Task 4-C **For each grade class, write 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 4-D **Change the type of `grade` in `GradeReport` from `int` to `IGrade`.** ## Adding TAs In Homework 1A, we had students and faculty, but we omitted your invaluable TAs. TAs are interesting because they are students, but they have some of the same permissions that faculty do. We want to add them to our grading system. ### Task 5-A **Create a new class `TA`, in its own file.** TAs should be students, but with an additional field called `assisting` that is of type `Course`. Create a constructor for the `TA` class that takes the TA’s name, the two `Course`s that they are taking as students, and the `Course` that they are assisting on. TAs shouldn’t be able to assist with a class they’re taking, so raise a `IllegalArgumentException` with the message `"Cannot take and assist the same course"` if this is the case. :::info <details><summary><b><i>Study Question</i></b></summary> Why did we make <code>TA</code> a subclass of <code>Student</code> rather than <code>Faculty</code>, given that TAs have elements of each of the other roles? ::: </details> TAs, like faculty, should be able to look up grades for courses on which they are assisting. ### Task 5-B **Write a method called `viewGrade` in both the `Faculty` and `TA` class that takes in a `GradeReport` and returns the grade in the report as a `String`.** This method should not be available for students who are not TAs. If a Faculty member or TA tries to view a grade for a course they are not teaching/assisting, throw a `RuntimeException` with the message `"Access denied"`. :::info <details><summary><b><i>Note</i></b>: Runtime Exceptions</summary> To throw a <code>RuntimeException</code> in Java use the syntax: throw new RuntimeException("\<error message>") ::: </details> :::success <details><summary><b><i>Hint</i></b>: <code>toString()</code></summary> You will want to write a <code>toString()</code> method for each class that represents a grade. For letter grades your <code>toString()</code> method should return one of <code>"A"</code>, <code>"B"</code>, <code>"C"</code>, or <code>"NC"</code>. For S/NC grades, the method should return one of <code>"NC"</code>, <code>"S"</code>, or <code>"S (with distinction)"</code>. The method signature should be: <code>@Override public String toString()</code>. ::: </details> ## Sharing Name Fields ### Task 6-A At this point, each of the `Faculty`, `Student`, and `TA` classes have fields for a person’s name. **Create a new abstract superclass (parent class) called `Person` that shares the `name` field across these three classes.** :::info <details><summary><b><i>Study Question</i></b></summary> Why did we make the <code>Person</code> class abstract? What does the <code>abstract</code> annotation allow or prohibit regarding the <code>Person</code> class? ::: </details> ## Handing In Once you’ve cloned the github classroom repo (from the top of the handout), the following files should be in the `sol` directory. You can move the files you wrote last week into this directory. We’ve also provided files on Canvas (go to the "Files" section and look for a folder for Homework 1A) if you would like to use our implementation. - `Course.java` containing public class `Course` - `Faculty.java` containing public class `Faculty` - `Student.java` containing public class `Student` - `Homework1BTest.java` containing public class `Homework1BTest` - `AutograderCompatibility.java` containing public class `AutograderCompatibility` After completing this assignment, you should be ready to turn in the following solution files: - `Course.java` containing public class `Course` - `Faculty.java` containing public class `Faculty` - `Student.java` containing public class `Student` - `Homework1BTest.java` containing public class `Homework1BTest` and your answers to the written questions about the `Animals` diagram. - `Person.java` containing public abstract class `Person` - `TA.java` containing public class `TA` - `GradeReport.java` containing public class `GradeReport` - `IGrade.java` containing public interface `IGrade` - `Letter.java` containing public enum `Letter` - `LetterGrade.java` containing public class `LetterGrade` - `SNC.java` containing public enum `SNC` - `SNCGrade.java` containing public class `SNCGrade` - `HierarchyDiagram.pdf` containing the hierarchy diagram :::info **_Note:_** You may also include other classes, interfaces, and abstract classes. ::: 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. :::info <details><summary><b><i>Note</i>:</b> Autograder Compatibility</summary> There should be a class in the stencil code named <code>AutograderCompatibility</code>. Using this class is required to ensure that your submission is working correctly with the autograder. After we get past homework 1, you will lose points if your code does not work with the autograder (so you might want to use this file to help check for errors). If you get an error involving this file when you try to run your code, the problem is with your code, not this file. You should not edit the <code>AutograderCompatibility</code> file.  If Gradescope gives you the message <i>“Could not run your code (0.0/0.0),"</i> uncomment the main method of <code>AutograderCompatibility</code> and check that it compiles and runs. If the Gradescope autograder still doesn’t work, come to hours or post on <a href="https://edstem.org/us/courses/32789/discussion">Ed</a> for help. ::: </details> ## FAQ **Do we have to test all the methods in the stencil?** No, you only need to test the methods you write. **For examples of tests, see the** [**Homework 1A FAQ**](https://docs.cs200.io/s/KqYkTK-x2#FAQ)**. If you have other questions, feel free to post on Ed or come to hours.** *** _Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CSCI0200 document by filling out the_ [_anonymous feedback form_](https://docs.google.com/forms/d/e/1FAIpQLSfe9cDwpzv7xCbbevbrAZacuB4MB8yxer8jiK0SK-CQH3RXuQ/viewform?usp=header)_._