Question

In: Computer Science

In Java, implement a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has a...

In 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, you must check whether the day of the month matches.

In Main Class, fill an array of Appointment objects with a mixture of appointments i.e Onetime, Daily, and Monthly (at least 1 from each). Then prompt the user to enter a date and print out all appointments that occur on that date.

Solutions

Expert Solution

Hi,

Please find the answer below:
-------------------------------

Java Program:

package appointment;

/******************************************************************************
*Appointment.java
******************************************************************************/
public class Appointment {
   protected String description;
   protected int day;
   protected int month;
   protected int year;

   //Appointment Constructor
   public Appointment(String desc, int d, int m, int y)
   {
       this.description = desc;
       this.day = d;
       this.month = m;
       this.year = y;
   }

   //getters and setters
   public String getDescription() {
       return description;
   }

   public void setDescription(String description) {
       this.description = description;
   }

   public int getDay() {
       return day;
   }

   public void setDay(int day) {
       this.day = day;
   }

   public int getMonth() {
       return month;
   }

   public void setMonth(int month) {
       this.month = month;
   }

   public int getYear() {
       return year;
   }

   public void setYear(int year) {
       this.year = year;
   }


    // this method would be overridden in the subclasses
   public boolean occursOn(int d, int m, int y)
   {
       if(getDay() == d && getMonth() == m && getYear() == y)
           return true;
       else
           return false;
   }

   //toString()
   public String toString()
   {
       return description + " on "+ day + "/" + month + "/" +year ;
   }

}


----------------

package appointment;

/******************************************************************************
* OneTime.java
* @author
*
*****************************************************************************/
public class OneTime extends Appointment {

   public OneTime(String desc, int d, int m, int y) {
       super(desc, d, m, y);
   }


   public String toString()
   {
       return "OneTime Appt -" + super.toString();
   }

}

-----------------

package appointment;

/******************************************************************************
* Daily.java
* @author
*
*****************************************************************************/
public class Daily extends Appointment {

   public Daily(String desc, int d, int m, int y) {
       super(desc, d, m, y);
   }

   //daily events except future events
   public boolean occursOn(int d, int m, int y)
   {
       if(getDay() < d || getYear() < y || getMonth() < m)
           return true;
       else
           return false;
   }


   public String toString()
   {
       return "Daily Appt -"+ description +" from " + getDay() + "/" + getMonth() + "/" + getYear();

   }

}

----------------

package appointment;

/******************************************************************************
* Monthly.java
* @author
*
*****************************************************************************/
public class Monthly extends Appointment {

   public Monthly(String desc, int d, int m, int y) {
       super(desc, d, m, y);
   }

   public boolean occursOn(int d, int m, int y)
   {
       //future events are not returned
       if(getDay() == d && (getYear() < y || getMonth() < m))
           return true;
       else
           return false;
   }

   public String toString()
   {
       return "Monthly Appt - "+ description + " on the day " + day + " of the month from " + getMonth() + "/" + getYear();
   }

}


-------------

package appointment;

import java.util.Scanner;

public class Main {
   public static void main(String[] args) {
       int day,month,year;
       Appointment[] appointments= new Appointment[6];
       appointments[0]= new OneTime("Get Together",1,1,2019);
       appointments[1]=new Monthly("Pay Electric bill",1,2,2019);
       appointments[2]= new Daily("Get Milk Packets",1,1,2017);
       appointments[3]= new OneTime("Meeting School Friend",1,1,2020);
       appointments[4]=new Monthly("Pay House Tax",1,3,2019);
       appointments[5]= new Daily("Gym Visit",2,1,2020);
       Scanner scanner = new Scanner(System.in);
       System.out.println("Enter date to show appointments:");
       System.out.println("Enter day: ");
       day = scanner.nextInt();
       System.out.println("Enter month: ");
       month = scanner.nextInt();
       System.out.println("Enter year: ");
       year = scanner.nextInt();
      
       System.out.println("Appointments are : ");
       for(Appointment a : appointments) {
           if(a.occursOn(day, month, year))
               System.out.println(a.toString());
       }
   }
}

-----------------


Program Output:

Enter date to show appointments:
Enter day:
1
Enter month:
1
Enter year:
2021
Appointments are :
Monthly Appt - Pay Electric bill on the day 1 of the month from 2/2019
Daily Appt -Get Milk Packets from 1/1/2017
Monthly Appt - Pay House Tax on the day 1 of the month from 3/2019
Daily Appt -Gym Visit from 2/1/2020


Screenshot:

-------------------------------
Hope this helps.
Let me know if you need more help with this.
Kindly, like the solution if you find it useful
Thanks.


Related Solutions

Using Ruby A dentist appointment schedule validation software Implement a superclass Appointment and sub classes OneTime,...
Using Ruby A dentist appointment schedule validation software Implement a superclass Appointment and sub classes OneTime, Day and Month. An appointment has a description (for example, "Root Canal"), and dates information (you can use Date object or int Year, Int Month, Int Day). Fill an array of Appointment objects with a mixture of appointments. Write a method OccursOn inside each of the sub classes that checks whether the appointment occurs on that date (OneTime), day (Day) or month (Month). Ask...
Describe the methods for converting inherited types, or 8) superclass/subclasses instances. Include the four options for...
Describe the methods for converting inherited types, or 8) superclass/subclasses instances. Include the four options for conversion and describe the tradeoffs of using each one in terms of total/partial participation and support for overlapping/disjoint subtypes. Then, describe how to represent 9) union types.
I got this homework, to make an appointment program. it says like this: Write a superclass...
I got this homework, to make an appointment program. it says like this: Write 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. You are provided with a Junit test AppointmentTest .java. Run...
Java Question Design a class named Person and its two subclasses named Student and Employee. Make...
Java Question Design a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name, address, phone number, and email address. A student has a class status (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and date hired. Use the Date class to create an object for date hired. A faculty member has office hours and a rank. A...
Java instructions: 1. Modify abstract superclass (Employee10A) so it comment out the abstract payPrint method and...
Java instructions: 1. Modify abstract superclass (Employee10A) so it comment out the abstract payPrint method and uses a toString method to print out it’s instance variables. Make sure toString method cannot be overridden.​​​​​​ Source code below: public abstract class Employee10A {    private String firstName, lastName; static int counter = 0;    public Employee10A(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }    @Override public String toString() { return ("The employee's full name is " + firstName...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑...
Java programming year 2 Polymorphism superclass variables and subclass objects polymorphic code -Classwork Part A ☑ Create a class Employee. Employees have a name. Also give Employee a method paycheck() which returns a double. For basic employees, paycheck is always 0 (they just get health insurance). Give the class a parameterized constructor that takes the name; Add a method reportDeposit. This method prints a report for the employee with the original amount of the paycheck, the amount taken out for...
using Java Your mission in this exercise is to implement a very simple Java painting application....
using Java Your mission in this exercise is to implement a very simple Java painting application. Rapid Prototyping The JFrame is an example that can support the following functions: 1. Draw curves, specified by a mouse drag. 2. Draw filled rectangles or ovals, specified by a mouse drag (don't worry about dynamically drawing the shape during the drag - just draw the final shape indicated). 3. Shape selection (line, rectangle or oval) selected by a combo box OR menu. 4....
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads in the principle 2) reads in additional money deposited each year (treat this as a constant) 3) reads in years to grow, and 4) reads in interest rate And then finally prints out how much money they would have each year. See below for formatting. Enter the principle: XX Enter the annual addition: XX Enter the number of years to grow: XX Enter the...
Please show solution and comments for this data structure using java.​ Implement a program in Java...
Please show solution and comments for this data structure using java.​ Implement a program in Java to convert an infix expression that includes (, ), +, -, *,     and / to postfix expression. For simplicity, your program will read from standard input (until the user enters the symbol “=”) an infix expression of single lower case and the operators +, -, /, *, and ( ), and output a postfix expression.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT