Read through the code of the class Activity, noting it has three instance variables, name, date and time, which are of type String. Note that the date should consist of six characters in the format "ddmmyy" and the time should consist of four characters in the format "hhmm". Also note the constructor and methods of the class and what they do.

a.

i. An instance of WhatsOn is used to hold data about a number of activities. For example:

Key Value
1 Activity object with name "wash car", date "010117" (1 January 2017) and time "0900" (9 am).
2 Activity object with name "go shopping", date "020117" (2 January 2017) and time "1000" (10 am).
3 Activity object with name "return sale items", date "010117" (1 January 2017) and time "1000" (10 am).
4 Activity object with name "back to work", date "040117" (4 January 2017) and time "0900" (9 am).

Note that the order of the keys in the map is unimportant and simply represents the order in which the entries were added.

Declare an additional private instance variable activities in your WhatsOn class, capable of referencing a map where the key is an integer and the value is an Activity object, as in the example above.

The class WhatsOn has a variable nextId of type int. This variable will used to keep track of the number that will be used as the key for the next entry added to the map.

ii.Next amend the provided zero-argument constructor so that activities is assigned a suitable empty map object when a new instance of WhatsOn is created. Set today to "010117" and nextId to 1.

iii. Write a public instance method for the WhatsOn class called addActivity(), which takes three arguments as in the following header:

public void addActivity(String aName, String aDate, String aTime) and returns no value.

The method should create an instance of Activity and then add it to the map referenced by the current value of nextId as the key.

The value of nextId should then be incremented.

b. Write a public instance method for the WhatsOn class called whatsOnToday, which takes no arguments and returns no value. For each Activity in the collection referenced by activities, the method should determine whether or not the date is today's date. If so, the data for that activity should be printed to the standard output, with the details of each activity on a separate line. If no activities are found for today, then Nothing on today should be output.

The output from whatsOnToday after the four activities specified above have been added should be:

Today you have wash car at 0900
Today you have return sale items at 1000

c. Now turn to the Activity class.

Two activities with the same name, date and time should be considered as the same. So we need to override the equals() method inherited from Object. Whenever we override the inherited equals() we also need to provide a hashCode() method compatible with the redefined equals(). If we don't do this, objects of the class wont work correctly in some kinds of collection.

Write an equals() method to override that inherited from Object, which returns true if the names, dates and times for two Activity objects are the same, and false otherwise.

Write a hashCode() method to override that inherited from Object, which returns the number of characters in the name of an Activity object.

d. Return to the WhatsOn class. Write a method with the header:

public String removeActivity(String thisName, String thisDate, String thisTime)

This method should determine whether or not the activity with a name, date and time that matches the arguments is one of the values in the map referenced by activities. If so, the key-value pair with this value should be removed from the map. The returned value should be "Failed to remove" if the activity was not one of the values in the activities collection, and Removed successfully if it was.

i. Now look again at your constructor and the provided variables today and nextId. Note that both these variables are defined as private static. Explain why the keyword static has been used, and discuss whether or not this was a good choice in both cases.

ii. Now look again at the decision to implement this scenario using a map. Was this necessary? Is there a better solution using one of the other parts of the Collections Framework? Discuss this and justify your answer.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.