Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Program to set appointments in java import java.util.ArrayList; // Appointment.java: object class representing an appointment import java.util.Arrays; public class Appointment { private int startTime; private
Program to set appointments in java import java.util.ArrayList; // Appointment.java: object class representing an appointment import java.util.Arrays; public class Appointment { private int startTime; private int endTime; private String dayOfWeek; private int day; private String month; private int year; /* * creates Appointment object with data for all instance variables */ public Appointment(int theStartTime, int theEndTime, String theDayOfWeek, int theDay, String theMonth, int theYear) { startTime = theStartTime; endTime = theEndTime; if (!validTimes()) { String msg = "invalid start time ("+startTime+") and/or end time ("+endTime+")"; IllegalArgumentException e = new IllegalArgumentException(msg); throw e; } dayOfWeek = theDayOfWeek; if (!validDayOfWeek()) { String msg = "invalid day of week: " + dayOfWeek; IllegalArgumentException e = new IllegalArgumentException(msg); throw e; } day = theDay; month = theMonth; if (!validMonth()) { String msg = "invalid month: " + month; IllegalArgumentException e = new IllegalArgumentException(msg); throw e; } year = theYear; } /* * copy constructor creates Appointment object with data * from other Appointment object * note that we don't need to validate instance variables * since the other Appointment couldn't be created with * invalid data (see above constructor) */ public Appointment(Appointment other) { startTime = other.startTime; endTime = other.endTime; dayOfWeek = new String(other.dayOfWeek); day = other.day; month = new String(other.month); year = other.year; } /* * returns true if the startTime instance variable comes before * the endTime instance variable and false otherwise */ public boolean validTimes() { boolean result; if (startTime < endTime) { result = true; } else { result = false; } return result; } /* * returns true if the month instance variable is one of the months * January through December, and false otherwise * note using Arrays.asList() method to convert the MONTHS array to * a List, so that we can create the months ArrayList from it */ public boolean validMonth() { final String[] MONTHS = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; ArrayListmonths = new ArrayList (Arrays.asList(MONTHS)); return months.contains(month); } /* * returns true if the month instance variable is one of the months * January through December, and false otherwise * note using Arrays.asList() method to convert the DAYS_OF_WEEK array to * a List, so that we can create the daysOfWeek ArrayList from it */ public boolean validDayOfWeek() { final String[] DAYS_OF_WEEK = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; ArrayList daysOfWeek = new ArrayList (Arrays.asList(DAYS_OF_WEEK)); return daysOfWeek.contains(dayOfWeek); } /* * returns true if the dayOfWeek instance variable is one of the days * Sunday through Saturday, and false otherwise */ public String toString() { String result = ""; result = result + dayOfWeek; result = result + ", "; result = result + month; result = result + " "; result = result + day; result = result + ", "; result = result + year; result = result + ": "; result = result + startTime; result = result + " - "; result = result + endTime; return result; } } public class ApptTester { public static void main(String[] args) { // 1. array list of Appointments ArrayList apptBook = new ArrayList (); // 2. add an appointment for Monday, 9/21,2020, from 10:10 am until 11:00 am Appointment appt = new Appointment(1010, 1100, "Monday", 21, "September", 2020); apptBook.add(appt); System.out.println("The first appointment is " + appt); // 3. add an appointment for Wednesday, 9/23/2020, from 10:10 am until 11:00 am appt = new Appointment(1010, 1100, "Wednesday", 23, "September", 2020); apptBook.add(appt); // 4. add an appointment for Wednesday, 9/23/2020, from 3:35 pm until 4:50 pm appt = new Appointment(1535, 1650, "Wednesday", 23, "September", 2020); apptBook.add(appt); // 5. add an appointment for Friday, 9/25/2020, from 10:10 am until 11:00 am appt = new Appointment(1010, 1100, "Friday", 25, "September", 2020); apptBook.add(appt); System.out.println("The last appointment is " + appt); System.out.println(); System.out.println("The appointments for this week:"); // output the list of Appointments // uses calls to Appointment toString method to output the // Appointment objects in the list System.out.println(apptBook); } // end main method } // end ApptTetster class
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started