Question

In: Computer Science

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 your code with this file to make sure all the tests pass. Once you complete the code and all your tests pass, you need to write AppointmentBook.java file – a skeleton and a sample run of the program is given to you. This file has a main method. I have completed the classes: Appointment, OneTime, Daily, and Monthly. And all the tests passed. The classes look like this:

public abstract class Appointment {

String description;

  
public Appointment(String description) {
  
this.description = description;
}

  
public abstract boolean occursOn(int year, int month, int day);
}

public class OneTime extends Appointment {
int year;
int month;
int day;
   public OneTime(int year, int month, int day, String description){
       super(description);
       this.year = year;
       this.month = month;
       this.day = day;

  

}
  
@Override

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

return (this.year==year && this.month==month && this.day==day );
}

}

public class Daily extends Appointment {
  
   public Daily(String description){
super( description);
}
  
@Override
public boolean occursOn(int year, int month, int day){

return true;
}
}

public class Monthly extends Appointment {
int day;
   public Monthly(int day,String description){
super(description);
       this.day = day;
}
  
   @Override
public boolean occursOn(int year, int month, int day){

return (this.day == day);
}
  
}

Iam having trouble writing the AppointmentBook.java class, which skeleton is given to me and i guess i should write the methods given there. The file looks like this :

import java.util.Scanner;
/**
Build on the Appointment hierarchy.
Give the user the option to add new appointments.
The user must specify the type of the appointment and
description, and then, if required, the day or date.
*/
public class AppointmentBook
{

/**
Adds a new Appointment object based on user input.
@param in the Scanner to read from.
*/
public void addAppointment(Scanner in)
{
  

}

/**
Method to print all appointments on a certain date.
@param in the Scanner to read from.
*/
public void findAppointments(Scanner in)
{

}

// Just to test the appointment book
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
AppointmentBook ab = new AppointmentBook();

System.out.println("Welcome to the Appointment Book");
System.out.println("-------------------------------");

boolean done = false;
while (!done)
{
System.out.print("Appointments: (F)ind, (A)dd, or (Q)uit: ");
String choice = in.next();
if (choice.equals("F") || choice.equals("f"))
{
ab.findAppointments(new Scanner(System.in));
}
else if (choice.equals("A") || choice.equals("a"))
{
ab.addAppointment(new Scanner(System.in));
}
done = choice.equals("Q") || choice.equals("q");
}
System.out.println("Good bye. Have a nice day!");
in.close();
}
}

And this is a sample run provided to me:

Welcome to the Appointment Book
-------------------------------
Appointments: (F)ind, (A)dd, or (Q)uit: A
Enter type [(D)aily, (M)onthly, (O)netime] and description: D Exercise
Appointments: (F)ind, (A)dd, or (Q)uit: A
Enter type [(D)aily, (M)onthly, (O)netime] and description: D Eat dinner
Appointments: (F)ind, (A)dd, or (Q)uit: A
Enter type [(D)aily, (M)onthly, (O)netime] and description: M Pay Bills
Enter the day of the appointment: 1
Appointments: (F)ind, (A)dd, or (Q)uit: A
Enter type [(D)aily, (M)onthly, (O)netime] and description: M Pay Rent
Enter the day of the appointment: 10
Appointments: (F)ind, (A)dd, or (Q)uit: A
Enter type [(D)aily, (M)onthly, (O)netime] and description: O Visit Patagonia
Enter the date of the appointment (mm dd yyyy) : 12 01 2020
Appointments: (F)ind, (A)dd, or (Q)uit: a
Enter type [(D)aily, (M)onthly, (O)netime] and description: O Visit Norway
Enter the date of the appointment (mm dd yyyy) : 07 01 2020
Appointments: (F)ind, (A)dd, or (Q)uit: F
Enter the date (mm, dd, yyyy) to search: 09 10 2019
Exercise
Eat dinner
Pay Rent
Appointments: (F)ind, (A)dd, or (Q)uit: F
Enter the date (mm, dd, yyyy) to search: 07 01 2020
Exercise
Eat dinner
Pay Bills
Visit Norway
Appointments: (F)ind, (A)dd, or (Q)uit: f
Enter the date (mm, dd, yyyy) to search: 12 01 2020
Exercise
Eat dinner
Pay Bills
Visit Patagonia
Appointments: (F)ind, (A)dd, or (Q)uit: q
Good bye. Have a nice day!

Thanks for your help in advance.

Solutions

Expert Solution

***************Appointment class.********************

public abstract class Appointment {

   String description;

   public Appointment(String description) {
       this.description = description;
   }

   public abstract boolean occursOn(int year, int month, int day);
}


********************* OneTime class *********************

public class OneTime extends Appointment {
   int year;
   int month;
   int day;

   public OneTime(int year, int month, int day, String description) {
       super(description);
       this.year = year;
       this.month = month;
       this.day = day;
   }

   @Override
   public boolean occursOn(int year, int month, int day) {
       return (this.year == year && this.month == month && this.day == day);
   }
}


*******************Daily class ******************

public class Daily extends Appointment {

   public Daily(String description) {
       super(description);
   }

   @Override
   public boolean occursOn(int year, int month, int day) {
       return true;
   }
}

**************** Monthly class *******************

public class Monthly extends Appointment {
   int day;

   public Monthly(int day, String description) {
       super(description);
       this.day = day;
   }

   @Override
   public boolean occursOn(int year, int month, int day) {
       return (this.day == day);
   }

}
***************** AppointmentBook class ********************
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
* Build on the Appointment hierarchy. Give the user the option to add new
* appointments. The user must specify the type of the appointment and
* description, and then, if required, the day or date.
*/
public class AppointmentBook {

   private Appointment[] appointmentList = new Appointment[100];
   int size = 0;

   /**
   * Adds a new Appointment object based on user input.
   *
   * @param in
   * the Scanner to read from.
   */
   public void addAppointment(Scanner in) {
       System.out.println("Enter type [(D)aily, (M)onthly, (O)netime] and description: ");
       String input = in.nextLine();
       // this will give you first character in the input string which will be
       // type of the appointment.
       String type = input.substring(0, input.indexOf(" "));
       // this will give you rest of the string which will be description of
       // the appointment. After stripping first character rest will will be
       // the description.
       String description = input.substring(input.indexOf(" ") + 1, input.length());
       if (type.equalsIgnoreCase("D")) {
           Appointment dailyAppointment = new Daily(description);
           appointmentList[size++] = dailyAppointment;
       } else if (type.equalsIgnoreCase("M")) {
           System.out.println("Enter the day of the appointment:");
           int day = in.nextInt();
           Appointment monthlyAppointment = new Monthly(day, description);
           appointmentList[size++] = monthlyAppointment;
       } else if (type.equalsIgnoreCase("O")) {
           System.out.println("Enter the date of the appointment (mm dd yyyy) : ");
           String date = in.nextLine();
           String[] dateArray = date.split(" ");
           int day = Integer.parseInt(dateArray[0]);
           int month = Integer.parseInt(dateArray[1]);
           int year = Integer.parseInt(dateArray[2]);
           Appointment oneTimeAppointment = new OneTime(year, month, day, description);
           appointmentList[size++] = oneTimeAppointment;
       } else {
           System.out.println("Invalid Option");
       }
   }

   /**
   * Method to print all appointments on a certain date.
   *
   * @param in
   * the Scanner to read from.
   */
   public void findAppointments(Scanner in) {
       System.out.println("Enter the date (mm, dd, yyyy) to search: ");
       String date = in.nextLine();
       String[] dateArray = date.split(" ");
       int day = Integer.parseInt(dateArray[0]);
       int month = Integer.parseInt(dateArray[1]);
       int year = Integer.parseInt(dateArray[2]);
       for (Appointment appointment : appointmentList) {
           if (appointment.occursOn(year, month, day)) {
               System.out.println(appointment.description);
           }
       }
   }

   // Just to test the appointment book
   public static void main(String[] args) {
       Scanner in = new Scanner(System.in);
       AppointmentBook ab = new AppointmentBook();

       System.out.println("Welcome to the Appointment Book");
       System.out.println("-------------------------------");

       boolean done = false;
       do {
           System.out.print("Appointments: (F)ind, (A)dd, or (Q)uit: ");
           String choice = in.next();
           if (choice.equals("F") || choice.equals("f")) {
               ab.findAppointments(new Scanner(System.in));
           } else if (choice.equals("A") || choice.equals("a")) {
               ab.addAppointment(new Scanner(System.in));
           } else if (choice.equals("Q") || choice.equals("q")) {
               done = true;
           }
       } while (!done);

       System.out.println("Good bye. Have a nice day!");
       in.close();
   }
}


Related Solutions

I got a little problem when i was trying to write a program that takes a...
I got a little problem when i was trying to write a program that takes a hexadecimal number and convert it to a binary number. It just don't give me the right answer. Here's my code: #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> int main(int argc, char *argv[]) { unsigned long int i; i=0xffff; char buffer[65]; sprintf(buffer, "%lud", i); long int x = 0; while (buffer[x]) {    switch (buffer[x]) { case '0': printf("0000"); break; case '1': printf("0001"); break;...
We are to make a program about a car dealership using arrays. I got the code...
We are to make a program about a car dealership using arrays. I got the code to display all cars in a list, so I'm good with that. What I'm stuck at is how to make it so when a user inputs x for search, it allows them to search the vehicle. We need two classes, one that shows the car information and another that shows the insert, search, delete, display methods. Here is what I have so far package...
Hi there, I just got a homework spread sheet and have no idea how to solve...
Hi there, I just got a homework spread sheet and have no idea how to solve it. can you explain it to me? I would really appriciate your help.  Here are the questions With a tiny part from the spread sheet. A) Construct a time series of the ratio of the price of the fifth contract each day to the price of the fourth contract followed through time till the fourth contract expires. Pick a starting year below 2007 and a...
I need a NPV calculation. I just want to make sure I got it correct. Initial...
I need a NPV calculation. I just want to make sure I got it correct. Initial Cost at t=0 is $150 rate = 16% growth rate in perpetuity after year 5 at 1.5% Year 1 2 3 4 5 Cash Flow 5 11.4 14 21 28
Write an Html Page that uses JavaScript Program to make a Blackjack Game. I need to...
Write an Html Page that uses JavaScript Program to make a Blackjack Game. I need to write an html file (P5.html) that uses JavaScript program to create a Blackjack game. 1. Blackjack Games Rules: a. The object of the game is to "beat the dealer", which can be done in a number of ways: • Get 21 points on your first two cards (called a blackjack), without a dealer blackjack; • Reach a final score higher than the dealer without...
I need to write a program and can't seem to make it run properly. Someone can...
I need to write a program and can't seem to make it run properly. Someone can help with the coding? It's with python Thanks! Here is the program: The rules of Shut the Box are as follows (abbreviated version): + You have two 5-sided die + You have 5 wooden blocks (labeled 1 through 5) + Each turn, you roll the dice and knock down the blocks corresponding to the number on each die + You have to knock down...
Write a program in C A teacher will assign homework and give the number of days...
Write a program in C A teacher will assign homework and give the number of days for the students to work on. The student is responsible for calculating the due date. The teacher does not collect homework on Friday or weekend. Write a C program that let the user enter today’s day of the week (0 for Sunday, 1 for Monday, etc.) and the number of days to allow the students to do the work, which may be several weeks....
In C++ Please, using only the libraries given in this homework prompt, Write a program that...
In C++ Please, using only the libraries given in this homework prompt, Write a program that (1) prompts for two integers, (2) prints out their sum, (3) prints out the first divided by the second, and (4) prints out the natural log of the first number raised to the power of the second number. #include <iostream> #include <iomanip> using namespace std; int main() { <your answer goes here> return 0; }
I need to write PYTHON program which is like a guessing game using randint function which...
I need to write PYTHON program which is like a guessing game using randint function which is already done, the user has to guess a number between 1 and 1000 and the game musn't end until you guess the number, if you input a smaller number you must get hints, the same goes if your input is bigger than the wining number , also you must get notified if you repeat a number (example, you pick 1 and in the...
write a program to make scientific calculator in java
Problem StatementWrite a program that uses the java Math library and implement the functionality of a scientific calculator. Your program should have the following components:1. A main menu listing all the functionality of the calculator.2. Your program should use the switch structure to switch between various options of the calculator. Your Program should also have the provision to handle invalidoption selection by the user.3. Your calculator SHOULD HAVE the following functionalities. You can add any other additional features. PLEASE MAKE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT