4B Loops & Methods & this


Classwork


Create a class Month with the instance variables needed to store the name of the month, the number of days per month, and an int for the day the month begins on (0 for Sunday, 1 for monday, etc). It should have an accessor and mutator for each, and a default and a parameterized constructor. Make the default month a February that begins on Sunday, and chain the constructors together.

Also add two Month instance variables nextMonth and prevMonth.  In the mutator for nextMonth, make sure that if a month becomes my nextMonth, I become its prevMonth.

Add a method makeNext(name, days) which creates and sets a new month to be this month's nextMonth with the given name and number of days.  Figure out the new month's first day based on this month's first day and number of days.  (e.g., if the month starts on 0 and is 30 days long, the next month starts on 2 (hint: there are 7 days in a week).   At the end, return the new month.

To the Calendar class, add a toString() which creates and returns the calendar as one big String.  (hint: += for Strings is your friend)

Examples:

September
30
0

Output:
--------------------
	September

 S  M  T  W  T  F  S
--------------------
 1  2  3  4  5  6  7 
 8  9 10 11 12 13 14 
15 16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 
--------------------

January
31
4

Output:
--------------------
	January

 S  M  T  W  T  F  S
--------------------
             1  2  3 
 4  5  6  7  8  9 10 
11 12 13 14 15 16 17 
18 19 20 21 22 23 24 
25 26 27 28 29 30 31 
--------------------

You may do the month names, lines, and list of weekday caps using String literals. The rest should be created using loops/conditionals. I will not give full credit for using separate loops for each week.

Note that if the month starts on Thursday, the date 1 is lined up with the Thursday column, with leading spaces to move it over.

Note that the dates are lined up in columns so that the 1-digit dates line up with the 1's digit of the 2-digit dates.  

Do not mess up the values in any instance variables while doing this!

Add a method String chainprint(), which if the month's next is null, reutrns just the month's toString, and otherwise, returns the month's toString + a call to the next's chainprint() (Yes, we are being recursive.)

Part B

In a class with a main, create a year of calendars by creating a january and then using makeNext on each month.  (We dont have arrays yet, so create 12 separate variables. 

Make sure that you can chainprint the whole and partial year.


Extra Credit

The following code

	// outer loop -- keep going for 10 rows, from 0 to 9
	for (i = 0; i < 10; i++) {
		// inner loop -- print stars across row
		// start off at 10-i = 10-0 = 10 stars on row 0
		// end up with 10-i = 10-9 = 1 star on row 9
		for (j = i; j < 10; j++) {
			System.out.print("* "); // print one star+space
		}
		System.out.println(); // new line at end of row
	}
prints this:
* * * * * * * * * * 
* * * * * * * * * 
* * * * * * * * 
* * * * * * * 
* * * * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

Here are two patterns. +25 EC for writing code in a main program to produce each pattern. For credit, your program must use loops to generate the patterns; no credit will be given for simply printing the lines literally.

PATTERN 1
*                                     * 
* *                                 * * 
* * *                             * * * 
* * * *                         * * * * 
* * * * *                     * * * * * 
* * * * * *                 * * * * * * 
* * * * * * *             * * * * * * * 
* * * * * * * *         * * * * * * * * 
* * * * * * * * *     * * * * * * * * * 
* * * * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * * * * 
* * * * * * * * *     * * * * * * * * * 
* * * * * * * *         * * * * * * * * 
* * * * * * *             * * * * * * * 
* * * * * *                 * * * * * * 
* * * * *                     * * * * * 
* * * *                         * * * * 
* * *                             * * * 
* *                                 * * 
*                                     * 

PATTERN 2

                  * * 
                * * * * 
              * * * * * * 
            * * * * * * * * 
          * * * * * * * * * * 
        * * * * * * * * * * * * 
      * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * 
  * * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * * * * * 
  * * * * * * * * * * * * * * * * * * 
    * * * * * * * * * * * * * * * * 
      * * * * * * * * * * * * * * 
        * * * * * * * * * * * * 
          * * * * * * * * * * 
            * * * * * * * * 
              * * * * * * 
                * * * * 
                  * * 

(hint: more than one for loop could be involved in drawing one horizontal line in the pattern)


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. By default, documents should be length 0.

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, in the mutator for currentDoc, make sure that when a CompanyDoc becomes an employee's currentDoc, the employee becomes the author of the document as well.

In Employee, add a method swapDocs( which takes another Employee and has them swap their currentDocs.

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:

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)

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

If they create a document, check whether the current employee has a current document.  If so, ask if they want to replace it.  If so, ask for the title, create that document, and make it the current employee's current document.  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 it out.

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

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.

You can add as many other methods as you like for tasks like creating an Employee, but keep the mechanics of the menu looping all in the menu method.  Use labeled break statements to facilitate quitting the program from the inner menus and returning to the main menu from the innermost menu. Do not use breaks otherwise (aside from case statements) without adding comments to explain why they are appropriate.