Assignment 6: Circular Data; State Change
Goals: Practice working with circularly referential data, state change.
PARTNER CHANGE: We forgot to change the partners during the lab. Find a new partner, and email the instructor with your names. If you do not let me know by Sunday evening who is you new partner, I will assign you a partner.
Instructions
The names of the projects and some of the project files must be exactly the same as specified in the assignment. Failure to do so makes it impossible for the graders to run your submission and results in immediate loss of at least 50% of the homework credit.
Make sure you follow the style guidelines for code indentation.
You will submit this assignment by the deadline using the Web-CAT submission system.
With each homework you will also submit your log file named pairxxx.txt where you replace xxx with your pair number.
On top of every file you submit you will have the names of both partners, and the pair number. You loose at least two points for the failure to do so.
The .txt file will be the log of your work on this assignment. Each log entry will have data and time, who was present (one or both of the partners) and a short comment decribing what you were working on.
Due Date: Friday, October 19th, 9:59 pm.
Practice Problems
Work out these problems on your own. Save them in an electronic portfolio, so you can show them to your instructor, review them before the exam, use them as a reference when working on the homework assignments.
Work out the problem we have started in the lectures, dealing with a list of books, where each book can have multiple authors.
Define at least five examples of books, each with mulpilple authors, and some of the books having the same author in thei author list.
Design the method coAuthors That for a given author produces a list of all authors with whom this author has written a book.
Note: You may need several helper methods to solve this problem. Make sure you do it one step at a time.
Design the method bySameAuthor that determines whether oe book and another book have an author in common.
Pair Programming Assignment
Problem 1: Circular Data
In this problem we will model a university registrar system.
Start a Java project HW6-Registrar and define the classes and interfaces that implement the class diagram shown. Notice, that we will need to break the circularity of this class diagram.
+------------------------------------------------+
| +------------------+ |
| | | |
| v | v
| +------+ | +------+
| | ALoS |<------------+ | | ALoC |<-----------+
| +------+ | | +------+ |
| +------+ | | +------+ |
| / \ | | / \ |
| --- | | --- |
| | | | | |
| ---------------- | | --------------- |
| | | | | | | |
| +-------+ +---------------+ | | +-------+ +--------------+ |
| | MTLoS | | ConsLoS | | | | MTLoC | | ConsLoC | |
| +-------+ +---------------+ | | +-------+ +--------------+ |
| +-------+ +-| Student first | | | +-------+ +-| Course first | |
| | | ALoS rest |-+ | | | ALoC rest |-+
| | +---------------+ | | +--------------+
| | | |
| v | v
| +--------------+ | +-------------+
| | Student | | | Course |
| +--------------+ | +-------------+
| | String name | | | String name |
| | int id | | | int credits |
+----| ALoC courses | +-----| ALoS roster |
+--------------+ +-------------+
Define examples of at least three Students and eight Courses, with every student enrolled in at least two courses.
Design the method addCourse in the class Student that enrolls this student in the given course. Throw an exception if the student is already enrolled in the course, or if the student’s credits would be over 20 after the enrollment is completed.
Design the method dropCourse that drops this student from the given course. Throw an exception if the student is not enrolled in the given course, or if it would result in this student being registered for fewer than two courses after the drop is processed.
A student meets an interesting fellow student and wonders whether or not they might meet during one of the classes our student is enrolled in. Design the method canMeet that determines this student is taking a course that the other student is also enrolled in.
The registrar keeps a list of all students and a list of all courses. These are set up at the beginning of each quarter. Design the class Registrar to manage these lists and include your original examples in the registrar’s database.
Design the method register in the class Registrar that consumes the name of a student, the student’s id, and a course name, and enrolls the given student in the given course. The same restrictions on registration apply as before. Additionally, the method should throw an exception if any of the information given does not exist (no student with the given name and id, or no course with the given name).
Design the method withdraw in the class Registrar that consumes the name of a student, the student’s id, and a course name, and drops the given student from the given course. The same restrictions apply as above.
Note 1: Most of these methods require several helper methods. At least half of the grade for this homework will be assessing the design of these helpers – whether you truly follow the rule one task = one method.
Note 2: All of these methods (except canMeet), are defined only to affect a change in the registrar system. Design your tests carefully. At least one half of the grade for this homework will be assessing the design and implementation your tests for these methods.
Note 3: Did you notice that the above two criteria cover two halves of the grade for the homework? Well, there is some wiggle room, but we really want you to understand how important these two issues are (i.e., helpers and testing).
Problem 2: Designing Mutable Lists
Start a new project HW6-Lists for this problem and import the files from Lists.zip available from the assignment page. You should have three files: Node.java, LoS.java, and LinkedListExamples.java.
The example files show how to design a mutable list by having a special node (a sentinel) that marks the end of the list, so that the empty list has at least one entry, though this entry has no useful data. When we insert into the list (or remove items from it), we change the list.
The LoS and Node classes represent a collection of Strings organized as a list. Instead of having two different classes for the empty list and for nonempty list, we have a class that represents a Node in a list (similar to our Cons class that contained the data and a link to the next item), and a subclass that represents the end of the list (a sentinel). An LoS list always contains exactly one Node. If the list is empty, this Node is our special sentinel node. Each node then links to the the remaining items in the list, except for the las sentinel node that does not link to anything.
The following pictures illustrate the structure of an empty linked list and a linked list after we added three Strings, "abc", "def", and "ghi":
+------+ |
| LoS | |
+------+ +----------+ |
| node |->| Sentinel | |
+------+ +----------+ |
| "" | |
| null | |
+----------+ |
|
+------+ |
| LoS | |
+------+ +-------+ +-------+ +-------+ +----------+ |
| node |->| Node | +->| Node | +->| Node | +->| Sentinel | |
+------+ +-------+ | +-------+ | +-------+ | +----------+ |
| "abc" | | | "def" | | | "ghi" | | | "" | |
| next |-+ | next |-+ | next |-+ | null | |
+-------+ +-------+ +-------+ +----------+ |
Study the code and classes. Make sure you understand what is going on. Add at least one additional test to each test method defined in the LinkedListExamples class.
Add tests for the methods that are not tested.
Design the method removeNode for the LoS class that removes the Node that contains given String.
Design the method size that counts the number of nodes in a list LoS, not including the sentinel node.
Design the method toLowerCase that changes the String in every node to the same String, but with all lower case letters.
Note: The class String defines the method toLowerCase() that produces a new String with all upper case characters converted to the lower case.