Problem 1 For this problem, you will be writing a Diary class that can record we
ID: 3871148 • Letter: P
Question
Problem 1
For this problem, you will be writing a Diary class that can record weekly appointments, like class meeting times, project meetings, lab sessions, etc. To do this, the Diary class maintains a 2-D array of time entries where rows represent the day of the week, and columns represent the hour of the day. We will only be storing 10 hours' worth of appointments, from 8am to 5pm. Before we can develop the Diary class, we need an Appointment class that is a blueprint for the various appointments that will be recorded in the Diary. Appointment Class (skeleton provided)
Features: 1. Include two fields: an integer field 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.
2. Write a constructor that takes two parameters: one integer parameter for the time, followed by a string representing the description.
3. Write accessor methods called getDescription() and getHour() that return the description of an appointment and the hour at which that appointment occurs respectively.
4. Include mutator methods called setDescription() and setHour() that allow an appointment's description and time to be changed respectively. These mutators should each take a single argument.
5. Since we 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". To do this, add a new mutator called setTime() . This mutator should take a single string parameter that consists of an hour number followed by “am” or “pm” (no spaces between the hour number and “am” or “pm”). It will set the field inside the appointment by examining the characters of the string. Hint: Use the substring() provided by the String class to extract a new string containing only the digits and then use the Integer.parseInt() static method to convert the digits into the corresponding integer value.
6. Write a second 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.
7. Add a toString() method returning a String value. This method is included to print the appointments in a more readable format as given below: 3pm: CS 211
Diary Class (skeleton provided)
Features: 1. Include one field: a 2D array of Appointment objects (the type should be Appointment[][] ).
2. Add a constructor to initialize your array by creating a new 2D array of the correct size. (Remember that we are only recording appointments that occur between 8:00 am – 5:00 pm on any day of the week. Your array dimensions should take this into account).
3. Add a mutator method called addAppointment() that takes an integer representing a day and a corresponding Appointment object. When adding an appointment, if the day parameter is invalid or the appointment's hour number is out of the 8 am – 5 pm range, the method should simply do nothing.
4. Add an accessor method called getAppointment() that takes an integer representing a day and a second integer representing an hour. It should return the corresponding appointment, if any, or null if there is no appointment at that time. It should also return null if either the day or time parameter is invalid.
Problem 2
A microwave control panel has four buttons: one for increasing the time by 30 seconds, one for switching between power levels 1 and 2, a reset button, and a start button. Implement a class that simulates the microwave, with a method for each button. The method for the start button should print a message “Cooking for ... seconds at level ...”. Include instance variables for time and power level. The skeleton for the Microwave class is provided. Complete the methods.
Explanation / Answer
Appointment.java
public class Appointment {
//Declaring instance variables
private int hr;
private String description;
//Parameterized constructor
public Appointment(int hr, String description) {
super();
if (hr >= 8 && hr <= 17) {
this.hr = hr;
this.description = description;
}
}
//Overloaded parameterized constructor
public Appointment(String time, String description) {
super();
setTime(time);
this.description = description;
}
//getters and setters
public int getHr() {
return hr;
}
public void setHr(int hr) {
this.hr = hr;
}
public void setTime(String time) {
String amOrPm;
//System.out.println(time.length());
if (time.length() == 3) {
amOrPm = time.substring(1, time.length());
// System.out.println(amOrPm);
if (amOrPm.equalsIgnoreCase("AM")) {
this.hr = Integer.parseInt(time.substring(0, 1));
} else if (amOrPm.equalsIgnoreCase("PM")) {
//System.out.println(time.substring(0,1));
this.hr = 12 + Integer.parseInt(time.substring(0, 1));
}
} else {
amOrPm = time.substring(2, time.length());
if (amOrPm.equalsIgnoreCase("PM"))
this.hr = 12 + (Integer.parseInt(time.substring(0)));
}
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
if (hr < 12)
return "hr =" + hr + " am , Description=" + description;
else if (hr == 12)
return "hr =" + hr + " Pm , Description=" + description;
else
return "hr =" + (hr - 12) + " Pm ,Description=" + description;
}
}
__________________
Dairy.java
public class Dairy {
// Creating an Appointment 2-D array reference
Appointment app[][];
// Zero argumented constructor
public Dairy() {
app = new Appointment[7][10];
}
// This method will add the appointment to the Dairy
void addAppointment(int day, Appointment app1) {
if ((day >= 1 && day <= 7)) {
app[day - 1][app1.getHr() - 8] = app1;
}
}
// This method will get the appointment from the Dairy
Appointment getAppointment(int day, int hour) {
if ((day >= 1 && day <= 7) && hour >= 8 && hour <= 17) {
return app[day - 1][hour - 8];
} else {
return null;
}
}
// This method will return Appointment 2-D array
public Appointment[][] getApp() {
return app;
}
}
_______________________
Test.java
public class Test {
public static void main(String[] args) {
//Creating an weekdays array
String days[] = {
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
};
//Creating an Appointment objects by passing time and Description
Appointment a1 = new Appointment(12, "Project Meetings");
Appointment a2 = new Appointment(17, "Lab Sessions");
//Creating an Appointment objects by passing time in string format and Description
Appointment a3 = new Appointment("1Pm", "Class Meetings");
Appointment a4 = new Appointment("5Pm", "Lunch");
//Creating an Dairy class object
Dairy d = new Dairy();
//Adding the appointments to the Dairy on some days between 1-7
d.addAppointment(5, a1);
d.addAppointment(2, a2);
d.addAppointment(1, a3);
d.addAppointment(7, a4);
System.out.println("---- Displaying Appointments ----");
//Displaying the appointments on particular days by calling getter method
System.out.println("On " + days[5] + " " + d.getAppointment(5, 12).toString());
System.out.println("On " + days[2] + " " + d.getAppointment(2, 17).toString());
System.out.println("---- Displaying all Appointments in the dairy----");
//Displaying all appointments on particular days by calling getter method
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 10; j++) {
if (d.getApp()[i][j] != null)
System.out.println("On " + days[i] + " at " + d.getApp()[i][j].toString());
}
}
}
}
____________________
---- Displaying Appointments ----
On Saturday hr =12 Pm , Description=Project Meetings
On Wednesday hr =5 Pm ,Description=Lab Sessions
---- Displaying all Appointments in the dairy----
On Monday at hr =1 Pm ,Description=Class Meetings
On Tuesday at hr =5 Pm ,Description=Lab Sessions
On Friday at hr =12 Pm , Description=Project Meetings
On Sunday at hr =5 Pm ,Description=Lunch
_________________
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.