Why is the code not returning expected output? AppointmentTester.java import java.util.Scanner; /** Demonstration ofthe appointment classes */
Question:
Why is the code not returning expected output?
AppointmentTester.java
import java.util.Scanner;
/**
Demonstration ofthe appointment classes
*/
public class AppointmentTester{
public static void main(String[] args){
Appointment[] appointments = new Appointment[2];
appointments[0] = new Daily(2000, 8, 13, "Brush your teeth");
appointments[1] = new Monthly(2003, 5, 20, "Visit grandma");
String dates0 = "2000 9 13 2000 6 13 2001 8 13 2000 8 14";
String dates1 = "2003 4 20 2003 7 20 2004 5 20 2002 5 20";
Scanner in = new Scanner(dates0);
int year, month, day;
while (in.hasNextInt()){
year = in.nextInt();
month = in.nextInt();
day = in.nextInt();
if (appointments[0].occursOn(year, month, day)){
System.out.println(appointments[0]);
}
}
System.out.println("Expected:Brush your teethBrush your teethBrush your teeth");
in = new Scanner(dates1);
while (in.hasNextInt()){
year = in.nextInt();
month = in.nextInt();
day = in.nextInt();
if (appointments[1].occursOn(year, month, day))
System.out.println(appointments[1]);
}
System.out.println("Expected:Visit grandmaVisit grandma");
}
}
Appointment.java
/**
A class to keeptrack of a single appointment.
*/
public class Appointment{
private String description;
private int year;
private int month;
private int day;
/**
Initializes appointmentfor a given date.
@param yearthe year
@param monththe month
@param daythe day
@param description thetext description ofthe appointment
*/
public Appointment(int year,int month,int day,String description){
this.year = year;
this.month = month;
this.day = day;
this.description = description;
}
/**
Returns theyear of theappointment.
@return the year
*/
public int getYear(){
return year;
}
/**
Returns themonth of the appointment.
@returnthe month
*/
public int getMonth(){
return month;
}
/**
Returns theday of the appointment.
@returnthe day
*/
public int getDay(){
return day;
}
/**
Determines ifthe appointment is onthe date given.
@param yearthe year to check
@param month themonth to check
@param day theday to check
@return true if theappointment matches allthree parameters
*/
public boolean occursOn(int year, int month, int day){
return (year == this.year) && (month == this.month) && (day == this.day);
}
/**
Converts appointmentto string description.
*/
public String toString(){
return description;
}
}
Monthly.java
/**Monthly appointment.*/
public class Monthly extends Appointment{
/**
Initializes appointmentfor a given date.
@param year theyear
@param month themonth
@param daythe day
@param description the text description of the appointment
*/
public Monthly(int year, int month, int day, String description){
//-----------Start belowhere. To do: approximatelines of code =1
// Initialize theinherited variables- make sure of thesuper() keyword
super(year,month,day,description);
//-----------------End here.Please do notremove this comment.Reminder: no changes outside the todo regions.
}
/**
Determines if the appointment occurs on the same day of the month.
@param year the year
@param month the month
@param day the day
@return true if day matches the appointment date and is later than the
appointment date stored in this object
*/
public boolean occursOn(int year, int month, int day){
//-----------Start below here.To do: approximatelines of code = 5
// Override theoccursOn() method. Check to seeif the appointment occurson the
// same day ofthe month and is later than theappointment date storedin this object .
if(super.getMonth()==month || super.getDay()==day && super.getYear() >year || super.getMonth()>month || super.getDay()>day){
return true;
}
return false;
//-----------------End here. Please do not remove this comment. Reminder: no changes outside the todo regions.
}
}
Daily.java
/**Daily appointment.*/
public class Daily extends Appointment{
/**
Initializes appointmentfor a given date.
@param yearthe year
@param month themonth
@param day theday
@param description thetext description of theappointment
*/
public Daily(int year, int month, int day, String description){
//-----------Start below here.To do: approximatelines of code = 1
// Initialize theinherited variables- makeuse of the super() keyword
super(year,month,day,description);
//-----------------End here.Please do not removethis comment. Reminder:no changes outsidethe todo regions.
}
/**
Determines if thedate is later thanthe appointment date
@param year theyear
@param monththe month
@param daythe day
@return true if base appointment isearlier than theappointment date
*/
public boolean occursOn(int year, int month, int day){
//-----------Start below here.To do: approximatelines of code = 9
// Override occursOn. Checks the given dateparameters year, month, day to see ifit is a later
// date than the appointmentdate stored in thisobject. Return true if so,false otherwise
if(super.getYear() >year || super.getMonth()>month || super.getDay()>day){
return true;
}
return false;
//-----------------End here.Please do not removethis comment. Reminder:no changes outside the todoregions.
}
}
Consumer Behavior Buying, Having and Being
ISBN: 978-0133450897
11th edition
Authors: Michael R. Solomon