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: 3699070 • 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. 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. Then fill an array of Apointment objects with a mixture of appointments. Have the user enter date and print out all appointments that occurs on that date. Also, provide a tester class, AppintmentTester, for the above classes, in which create various types of appointments and test the expected outputs.

Explanation / Answer

Please find my implementation in Java.

//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