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

In Ruby A dentist appointment schedule validation software Implement a superclass Appointment and sub classes OneTime,...
In 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...
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...
Create a generic superclass Shoe, which has at least 3 subclasses with attributes as shown below:...
Create a generic superclass Shoe, which has at least 3 subclasses with attributes as shown below: class Shoe: Attributes: self.color, self.brand class Converse (Shoe): # Inherits from Shoe Attributes: self.lowOrHighTop, self.tongueColor, self.brand = "Converse" class CombatBoot (Shoe): # Inherits from Shoe Attributes: self.militaryBranch, self.DesertOrJungle class Sandal (Shoe): # Inherits from Shoe Attributes: self.openOrClosedToe, self.waterproof Implement the classes in Python. Create a separate test module where 2 instances of each subclass are created. Test the methods by displaying their information.
in java Design and implement a class named Person and its two subclasses named Student and...
in java Design and implement 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 (year 1,year 2,year 3,or year 4). An employee has an office, salary, and date hired. Use the Date class from JavaAPI 8 to create an object for date hired. A faculty member has office hours and a rank. A staff...
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...
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.
How to write a Java program that has a base class with methods and attributes, subclasses...
How to write a Java program that has a base class with methods and attributes, subclasses with methods and attributes. Please use any example you'd like.
If a superclass is abstract, then its subclass must implement all of the abstract methods in...
If a superclass is abstract, then its subclass must implement all of the abstract methods in the superclass. Is this statement true or false?A. trueB. false I appreciate if you add some descriptions
Implement a superclass Person. Make two classes, Student and Instructor, that inherit from Person. A person...
Implement a superclass Person. Make two classes, Student and Instructor, that inherit from Person. A person has a name and a year of birth. A student has a major, and an instructor has a salary. Write the class declarations, the constructors, and the methods toString for all classes. Supply a test program that tests these classes and methods. Add a method Public static Measurable max(Measurable[ ] objects) to the Data class that returns the object with the largest measure. Implement...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT