Methods Homework


Part A

Create a class CompanyDoc,  representing an official document used by a company. Give it a String title and an int length.

 Throughout the class, use the this. notation rather than bare instance variables and method calls.  

Add a default constructor. 

Add a parameterized constructor that takes a value for title. Use this( appropriately to call one constructor from another.


Add a toString(), which returns a string with the title, and the length in parentheses. So it might look like:

Employment Turnover Report (16342) 

Add a method longer( with return type boolean, which takes a CompanyDoc as parameter.

doc1.longer(doc2) returns true if doc1's length is greater than doc2's length, false otherwise.

Part B
Create a class Employee  with an instance variable for name and instance variable currentDoc of type CompanyDoc.

To CompanyDoc, also add an instance variable author of type Employee.

In Employee, have a method takeOwnership which takes a CompanyDoc.  This becomes the employee's currentDoc, the employee becomes the author of the document as well.  Have another method orphanDocument which removes (both directions) of this relationship if the employee has a currentDoc.

In Employee, add a method swapDocs which takes another Employee. In the method, swap their currentDocs  (think about what to do if one/both don't currently have documents).

Part C

Create a class DocRepository with one instance variable of type Employee, currentEmployee

Add a method menu that presents a menu-driven interface as follows (you can add as many methods to do this as you like ):

Main menu -- user can either create an employee or end the program

If they create an employee, ask for the name, create an employee with that name (henceforth, the current employee) , and go to Employee Menu.  (Do this even if a current employee was created previously; just replace the old one.)

Employee menu -- user can create a document, edit document, view document,  or return to the main menu.

If they ask to create a document, what we want to do is ask for the title, create that document, and make it the current employee's current document.  First, however, check whether the current employee already has a current document.  If not, just go ahead.  If they have one, first ask if they want to replace it, and only do so if they say yes.  After this, return to the Employee Menu

If they edit the document, first check if the current employee has a document.  If not, complain and return to the Employee menu.  If so, go to the Document menu.

If they view the document, simply print the current document if there is one.

Document menu -- user can add words, remove words, or return to the Employee menu.

If the user adds words, ask how many, and add that to the length of the current document.

If the user removes words, check that there are at least that many words in the current document.  If so, subtract that from the length, otherwise complain and return to the Document menu.

In a separate main, create a DocRepository and have it start its menu.


[EC+15]

In another class with a main called SongSwitch in your project, do problem 5.29 from the text for printing a cumulative song using loops and switch statements.  However, instead of 12 Days of Xmas, do some other similar song, like Old Lady Who Swallowed the Fly, Hole in the Bottom of the Sea, or  I Had a Shoggoth (or one you make up yourself) with at least 8 verses.   No credit will be given if you don't take advantage of the fall-through of the switch statement.