Question: **IN JAVA you'll be implementing a simple weekly calendar, where appointments are represented by actors. Since your actors represent weekly appointments, they are associated with

**IN JAVA

you'll be implementing a simple weekly calendar, where appointments are represented by actors. Since your actors represent weekly appointments, they are associated with both a day of the week, and with a time (an hour of the day).

We can encode days of the week using the numbers 0-6, which can then be used as positions within a fixed-size array. If our calendar is going to show a period of 10 hours, we can also encode the hours using numbers 0-9, which can also be used as array positions. We can then access any appointment using both numbers together: a day number and an hour number.

To use two separate numbers (two distinct dimensions) to access items in an array, it is possible to use an array that has two numeric coordinates instead of one--a two dimensional array (or 2D array). In Java, a two dimensional array type uses two pairs of square brackets instead of one.

Add features to Appointment

Edit Appointment and give it two fields: an int representing the hour of the day at which the appointment occurs, and a string representing a description of the appointment. We will use military time to denote hours, so 0 represents midnight, 8 is 8:00am, 20 is 8:00pm, 23 is 11:00pm, etc..

Modify the constructor so that it takes two parameters: one int parameter for the time, followed by a string representing the description.

Write an accessor method called getDescription() that returns the description of an appointment.

Write an accessor method called getHour() that returns the int-valued hour at which the appointment occurs.

Fill in the corresponding test class

By modifying the constructor for Appointment, its test class will no longer compile. Decide on the test fixture you wish to use for your Appointment class (that is, the initial conditions in which all tests are performed), and set it up.

Add tests for the accessor methods you have written (one test each is enough).

Compile and run your tests. Fix any errors.

Add mutator methods

Add a mutator method called setDescription() that allows an appointment's description to be changed. This mutator should take a single string argument.

Add a mutator method called setHour() that allows an appointment's time to be changed. This mutator should take a single int argument.

Write additional tests for these mutator methods (again, one test each is enough), compile and run them, and fix any errors.

Extend the Appointment class to handle more readable times

We would like for our appointment class to be a bit more friendly. People usually do not use military hour numbers to refer to times. It might be easier to create appointments if one could enter a time like "10pm" or "9am". Let's do this by adding a new mutator called setTime(). This mutator should take a single string parameter that consists of an hour number followed by "am" or "pm". It will set the field inside the appointment by examining the characters of the string. First, create a skeleton of this method that does nothing.

Brainstorm tests for this new mutator. Write them in your test class, then compile and run them (they should fail at this point).

Implement this mutator method. First, you can use the substring() method provided by the String class to extract a new string containing all but the last two characters (i.e., just the digits). Then use the Integer.parseInt() static method to convert the digits into the corresponding int numeric value:

String digitsOnly = theTime.substring(/* what goes here? */); int hour = Integer.parseInt(digitsOnly) ; 

Compile your implementation, run all tests, and fix errors.

Now you can write a second constructor. Add a new constructor to the class that takes two string parameters: first, a string representing the time, (non-military style), and then a string representing the appointment description. This new constructor can call the mutator method you just created to do part of its work.

Write, compile, and run new tests for your new constructor. Now you have two different ways you can create appointment objects: either by specifying the time as an int value, or by specifying it using a more human-readable string.

Part 2: The Weekly Calendar

In the second part of this lab assignment, you will work on completing the WeeklyCalendar class to manage appointments. Internally, your calendar should store appointments in a two-dimensional array, (sort of like a grid), where the first dimension corresponds to days of the week and the second dimension corresponds to hours of the day. This simplistic model can only store one appointment for each hour.

As you can see from the background of the world, the calendar has an image that is 8 by 11 cells in size, where the first row and first column are used as labels. We'll only be storing (and showing) 10 hours' worth of appointments, from 8am to 5pm. In addition to holding appointments in a two-dimensional array internally for easy access by day number or hour, your calendar will also position each actor in the world so that it appears in the correct spot on the grid.

As you saw in the Appointment class, we will treat hours of the day as integer values from 0 to 23 (even though we'll only be using values 8-17). Similarly, we will treat days of the week as integer values from 0 to 6, with 0 denoting Sunday, 1 denoting Monday, ..., and 6 denoting Saturday.

Procedure

Provide a better string representation for Appointment

We would like our appointments to print out in a more readable form like this:

11am: CS1114 

Assume we would like the toString() method to produce a string representation like this for an appointment. Write test cases to check that toString() produces this format. These test cases will fail until you correctly implement your own version of toString(), but write the test cases first.

Add a toString() method returning a String value to your class. Implement it so that it produces the desired output format. Since this method will be overriding an inherited method from the Object class, precede the method header line with the @Overrideannotation.

An @Override annotation consists of just that single word on a line by itself in between your method's Javadoc and the beginning of the method declaration. The @Override annotation instructs the compiler to check that the method signature matches a method signature in the base class, double-checking that you have declared it correctly.

Compile your code and run your tests until no more errors remain.

Edit the WeeklyCalendar

Edit the calendar class and give it one field: a 2D array of Appointment objects (the type should be Appointment[][]).

Modify the constructor to initialize your array by creating a new 2D array object of the correct size (7x10).

Remember that in Java, a two-dimensional array is actually a one-dimensional array that contains other one-dimensional arrays as its elements. For example, each "day" of your calendar is really a one-dimensional array of Appointment objects, while the overall calendar is a one-dimensional array of days.

The individual Appointment references in the array for each day should be left null, to indicate that there is no appointment at that location in the calendar.

Add mutator and accessor methods

Add a mutator method called addAppointment() that takes an integer representing a day (0-6) and a corresponding Appointment object.

Adding an appointment first requires determining where it belongs in the array. The day number can be used as-is when indexing into the array, but the hour number cannot, since the array has 10 hour positions (numbered zero through 9) to represent the times from 8am to 5pm (8 to 17, as simple ints). Therefore, remember to use the appointment's hour number minus 8 to index into the array. For example, adding a 9am appointment on day 3 (Wednesday) would set the array value at location [3][1].

When adding an appointment, if the day parameter is invalid or the appointment's hour number is out of the 8-17 range, the method should simply do nothing. Otherwise, you should first check for an existing appointment at the same spot, and remove it from the world since you'll be overwriting it. Then you can update the array.

Finally, since the appointment is also an actor, don't forget to add() it to the world at the correct grid position after you've placed it in the array. Add it at an x-coordinate of (day + 1), and a y-coordinate of (hour - 7), in order to leave room for the labels in the first row and first column of the world background.

Add an accessor method called getAppointment() that takes an integer representing a day (0-6) and a second integer representing an hour (0-23). It should return the corresponding appointment, if any (again, don't forget to subtract 8 from the hour when looking in the array), or null if there is no appointment at that time. It should also return null if either the day or time parameter is invalid. For example, retrieving the 9am appointment on day three will access position [3][1] in the array.

Complete the test class

Edit the test class for WeeklyCalendar, decide on your test fixture and set it up.

Add tests for the mutator and accessor that you have written.

Compile and run your tests. Fix any errors.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!