Question

Write a Java program that implements a superclass Appointment and subclasses Onetime, Daily, and Monthly. An...

Write a Java program that implements a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has a description (for example, “see the dentist”) and a date. It writes a method occursOn (int year, int month, int day) that checks whether the appointmentoccurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. Then it will fill an array of Appointment objects with a mixture of appointments. When the user enters a date, it prints out all appointments that occur on that date. The appointment book program also gives the user the option to add new appointments. The user must specify the type of the appointment, the description, and the date. Also, improve the appointment book program of by letting the user save the appointment data to a file and reload the data from a file. The saving part makes a method save. Saves the type, description, and date to a file. The loading part determines the type of the appointment to be loaded, creates an object of that type, and then calls a load method to load the data.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

code:

//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*****\n");

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("\n*****The appointments in the book are****\n");

myAppts.checkAppointments();

System.out.println("\nEnter 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();

}

}

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Add a comment
Know the answer?
Add Answer to:
Write a Java program that implements a superclass Appointment and subclasses Onetime, Daily, and Monthly. An...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming language: JAVA Implement a superclass Appointment and subclasses OneTime, Daily, and Monthly. An appointment has...

    Programming language: JAVA 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, check whether the day of the month matches. Ask the user to enter a date to check (for example, 2006 10 5), and ask to user if they want...

  • (JAVA) In this assignment, you will implement a superclass Appointment and subclasses Onetime, Daily, and Monthly....

    (JAVA) In this assignment, you will 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 mon.th matches. In your application part, fill an array of Appointment objects with a mixture of appointments. Have the...

  • Help me with a python program : Implement a superclass appointment and subclasses Onetime, Daily and...

    Help me with a python program : 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(year, month, 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 a list of appointment objects with a mixture of appointments. Have the user enter a date and print...

  • *PYTHON* Implement a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has the date,...

    *PYTHON* Implement a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has the date, the month, the year, and the description of an appointment (e.g., “see the dentist”). Then, write a method occcursOn(year, month, day) that checks whether the appointment occurs on that date. In the test program, you should create a list of appointments using your subclasses of Onetime, Daily, and Monthly. Once you have those appointments available, allow the user to input day, month, and year...

  • 5.Implement a superclass Appointment and subclasses Onetime, Daily and Monthly. An appointment has a description (for...

    5.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(year,month,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 and the appointment date started before the date entered. Then, fill a list of Appointment objects with a mixture of appointments. Have the user enter a date and...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • Programming Assignment 6 Write a Java program that will implement a simple appointment book. The ...

    Programming Assignment 6 Write a Java program that will implement a simple appointment book. The program should have three classes: a Date class, an AppointmentBook class, and a Driver class. • You will use the Date class that is provided on Blackboard (provided in New Date Class example). • The AppointmentBook class should have the following: o A field for descriptions for the appointments (i.e. Doctor, Hair, etc.). This field should be an array of String objects. o A field...

  • CAN SOMEONE COMPLETE THIS CODE PLEASE THNK YOU!! Question - Write a program in c++ that...

    CAN SOMEONE COMPLETE THIS CODE PLEASE THNK YOU!! Question - Write a program in c++ that keeps an appointment book. Make a class Appointment that stores a description of the appointment, the appointment day, the starting time, and the ending time. Your program should keep the appointments in a sorted vector. Users can add appointments and print out all appointments for a given day. When a new appointment is added, use binary search to find where it should be inserted...

  • Java Thank you!! In this assignment you will be developing an image manipulation program. The remaining...

    Java Thank you!! In this assignment you will be developing an image manipulation program. The remaining laboratory assignments will build on this one, allowing you to improve your initial submission based on feedback from your instructor. The program itself will be capable of reading and writing multiple image formats including jpg, png, tiff, and a custom format: msoe files. The program will also be able to apply simple transformations to the image like: Converting the image to grayscale . Producing...

  • Write a Java program for a fortune teller. The program should begin by asking the user...

    Write a Java program for a fortune teller. The program should begin by asking the user to enter their name. Following that the program will display a greeting. Next, the program will ask the user to enter the month (a number 1-12) and day of the month (a number 1-13) they were born. Following that the program will display their zodiac sign. Next, the program will ask the user their favorites color (the only choices should be: red, green and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT