Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a superclass Appointment and subclasses Onetime, Daily, and Monthly. A

ID: 3755731 • Letter: I

Question

Implement a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has a description (for example, “see the dentist”) and a date (use int's to store the date). Write a method occursOn(int year, int month, int day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. (Think about what you need to check for the other types of appointments.)

Additionally, you will need to implement a class called Calendar. This will contain an ArrayList of Appointment objects (The list is an instance variable of the Calendar class). You will need to provide the following methods for your Calendar class.

A no argument constructor

public Calendar(){...}

This constructor will initialize your ArrayList instance variable to an empty list.

/**

* A method to add an appointment to the calendar

* @param apt – the appointment object to add to the calendar.

*/

public void add(Appointment apt) {…}

   /**

   * A method to remove an appointment from the calendar.

   * This method uses the occursOn() method from the public

   * interface for the Appointment class. Therefore, if parameters

   * are entered that occur after a start date for a given Daily      

   * appointment

   * the Daily appointment will be removed as well. (Because occursOn()    * willreturn true in this case). This is a limitation we will

   * accept for now.

   * @param year - the year of the appointment to remove

   * @param month - the month of the appointment to remove

   * @param day - the day of the appointment to remove

   */

   public void remove(int year, int month, int day) {

       //this method needs to iterate over your list of appointments

       //and remove elements who's occursOn() method return true

       //when passed the parameters above.

}

   /**

   * Method to return a string representation of this Calendar object.

   * Overrides the Object method toString (see page 448 in text).

   * (also see page 453 Special Topic 9.6)

   * @return a String representation of the Calendar object.

   */

   public String toString() {

       String ret = "";

       //this method needs to iterate over your list of appointments

       //and construct the return string

       //make sure to put each appointment on its own line

       //by using “ ”

       return ret;

   }

  

Note that you also need to create a toString method for your Appointment class. (Your subclasses will inherit this version of the method).

Make sure to add accessors to your Appointment class for getYear, getMonth, getDay, and getDescription.

Tester file (AppointmentDemo.java) is below and posted under that is some sample output from a working project using the AppointmentDemo.java file.

AppointmentDemo.java

/**

* Demonstration of the Calendar and Appointment classes

*/

public class AppointmentDemo {

   public static void main(String[] args) {

       Calendar calendar = new Calendar();

       //create some appointments and add them to our calendar

       //note the method calls here imply that

       //your Appointment class will need to have a 4 argument constructor

       //that accepts year, month, day, and description

       //the first call is year:2000, month: 8, day: 13

       calendar.add(new Daily(2000, 8, 13, "Brush your teeth."));

       calendar.add(new Monthly(2003, 5, 20, "Visit grandma."));

       calendar.add(new Onetime(2004, 11, 2, "Dentist appointment."));

       calendar.add(new Onetime(2004, 10, 31, "Trick or Treat."));

       calendar.add(new Monthly(2004, 11, 2, "Dentist appointment."));

       calendar.add(new Onetime(2004, 11, 2, "Dentist appointment."));

       //note here we can simply use + calendar because we have

       //implemented the toString() method

       System.out.println("Before removal of appointment " + " " + calendar);

       calendar.remove(2004, 11, 2);

       //note that the daily appointment is removed because it occurs on

       //11/2/2004 (as well as many other days).

       System.out.println("After removal of 11/2/2004 " + " " + calendar);

      

   }

}


Example Output of Tester File:

Before removal of appointment

Daily[Brush your teeth. Date: 8/13/2000]

Monthly[Visit grandma. Date: 5/20/2003]

Onetime[Dentist appointment. Date: 11/2/2004]

Onetime[Trick or Treat. Date: 10/31/2004]

Monthly[Dentist appointment. Date: 11/2/2004]

Onetime[Dentist appointment. Date: 11/2/2004]

After removal of 11/2/2004

Monthly[Visit grandma. Date: 5/20/2003]

Onetime[Trick or Treat. Date: 10/31/2004]

Explanation / Answer

PROGRAM:

//Appointment.java

// import the java package

import java.util.ArrayList;

// Create a class of Appointment

/**

A class to keep track of an appointment.

*/

public abstract class Appointment

{

// Declare the description variable

protected String description;

// declare the variable day, month , year

protected int day, month, year;

/**

Constructs an appointment without a description.

*/

public Appointment(String desc, int Day, int Month, int Year)

{

// variables

description = desc;

day = Day;

month = Month;

year = Year;

}

// to get the description

public String getDescription()

{

return description;

}

/**

Sets the description of this appointment.

@param description the text description of the appointment

*/

public void setDescription(String description)

{

this.description = description;

}

// get the day method

public int getDay()

{

return day;

}

// set the day

public void setDay(int day)

{

this.day = day;

}

// get month method

public int getMonth()

{

return month;

}

// setMonth method

public void setMonth(int month) {

this.month = month;

}

// getyear method

public int getYear()

{

return year;

}

// setYear method

public void setYear(int year)

{

this.year = year;

}

/**

Determines if this appointment occurs on the given date.

@param year the year

@param month the month

@param day the day

@return true if the appointment occurs on the given date.

*/

public boolean occursOn(int day, int month, int year)

{

// check out the condition

if(getDay() == day && getMonth() == month && getYear() == year)

return true;

else

return false;

}

// convert string into string for appointment

public String toString()

{

// return the description

return description +" on "+ day +"/"+month+"/" +year ;

}

}

//AppointmentInheritance.java

// import the java package

import java.util.ArrayList;

/**

Demonstration of the appointment classes

*/

public class AppointmentInheritance

{

//Define an array of Appointments here (globally)

ArrayList<Appointment> appointments;

// constructor of Appointments

public AppointmentInheritance()

{

// create a array list c=to contain appointment

appointments = new ArrayList<Appointment>();

}

// make appointment method

public void makeAppointment(Appointment a)

{

appointments.add(a);

}

// create a method of check appointments

public void checkAppointments()

{  

for(Appointment a : appointments)

System.out.println(a.toString());

}

  

// a method to display all the appointments

public void show(int day, int month, int year)

{

// appointment

for(Appointment a : appointments)

if(a.occursOn(day, month, year))

System.out.println(a.toString());

}

}

//TestAppointment.java

// import the required package

import java.util.Scanner;

// Create the class of TestAppointment

public class TestAppointment

{

// create a main method

public static void main(String[] args)

{

// create an object of AppointmentInheritance

AppointmentInheritance myAppts = new AppointmentInheritance();

// declare the already exist data into appointment book

myAppts.makeAppointment(new Monthly("Visit grandma",10,12,2018));

myAppts.makeAppointment(new Daily("Brush your teeth",18,10,2017));

myAppts. makeAppointment(new OneTime("Dentist appointment", 19, 11, 2017));

System.out.println("********The appointments in the book are***** ");

myAppts.checkAppointments();

// scan the scanner

Scanner scanner = new Scanner(System.in);

// declare the string

String ans, desc;

// declare the variable

int type, day, month, year;

// appointment

Appointment app;

// checkout the condition by the while loop

while(true)

{

// ask from the user

System.out.println("Please press yes/no to add "

+ "an appointment? y/n: ");

ans = scanner.next();

// when yes

if(!ans.equalsIgnoreCase("y"))

break;

else

{

while(true)

{

//

System.out.println("1. One time Appointment");

System.out.println("2. Daily Appointment");

System.out.println("3. Monthly Appointment");

System.out.println("Please make a selection of "

+ "appointment type: ");

type = scanner.nextInt();

// when choice is not between 1 and 3

if(type>=1 && type<=3)

break;

}

// new line

scanner.nextLine();

// Ask from the user to enter day, month, year

System.out.println("Enter the description: ");

desc = scanner.nextLine();

System.out.println("Enter the day: ");

day = scanner.nextInt();

System.out.println("Enter the month: ");

month = scanner.nextInt();

System.out.println("Enter the year: ");

year = scanner.nextInt();

if(type == 1)

app = new OneTime(desc, day, month, year);

else if(type == 2)

app = new Daily(desc, day, month, year);

else

app = new Monthly(desc, day, month, year);

  

myAppts.makeAppointment(app);

}

}

// display the message for prompt the input by user.

System.out.println(" *****The appointments in the book are**** ");

myAppts.checkAppointments();

System.out.println(" Enter date to show appointments for the date");

System.out.println("Enter the day: ");

day = scanner.nextInt();

System.out.println("Enter the month: ");

month = scanner.nextInt();

System.out.println("Enter the year: ");

year = scanner.nextInt();

myAppts.show(day, month, year);

}

}

// Daily.java

/**

Daily appointment.

*/

// class Daily

public class Daily extends Appointment

{

/**

Constructs a Daily Appointment

@param description the text description of the appointment

*/

public Daily(String desc, int day, int month, int year) {

super(desc, day, month, year);

}

/**

Determines if this appointment occurs on the given date.

@param year the year

@param month the month

@param day the day

@return true if the appointment occurs on the given date.

*/

public boolean occursOn(int day, int month, int year)

{

return true;

//this is the polymorphic method that will return true always,

//since this is a daily appt.

}

/**

Converts appointment to string description.

*/

public String toString()

{

return "[Daily] "+description;

}

}

//Monthly.java

/**

Monthly appointment.

*/

// create a class Monthly

public class Monthly extends Appointment

{

/**

Initializes appointment for a given date.

@param day the day of the month

@param description the text description of the appointment

*/

// constructor of monthly

public Monthly(String desc, int day, int month, int year)

{

super(desc, day, month, year);

//call the superclass method to initialize description

//initialize the day instance variable

}

/**

Determines if this appointment occurs on the given date.

@param year the year

@param month the month

@param day the day

@return true if the appointment occurs on the given date.

*/

public boolean occursOn(int day, int month, int year)

{

if(getDay() == day)

return true;

else

return false;

//will return true if the day passed in the parameter

//matches the day instance variable

}

/**

Converts appointment to string description.

*/

public String toString()

{

// return the string

return "[Monthly] "+ description + " on day "+day+" of the month";

}

}

//Onetime.java

/**

A onetime appointment.

*/

// create a class of OneTime

public class OneTime extends Appointment

{

/**

Initializes appointment for a given date.

@param year the year

@param month the month

@param day the day

@param description the text description of the appointment

*/

// constructor

public OneTime(String desc, int day, int month, int year)

{

//initialize description by calling a superclass method

//initialize all the instance variables with the

//parameters passed to this constructor

super(desc, day, month, year);

}

/**

Converts appointment to string description.

*/

public String toString()

{

return "[OneTime] "+super.toString();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote