Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Create a class named Appointment that stores information about appointments.

ID: 3678733 • Letter: 1

Question

1. Create a class named Appointment that stores information about appointments. All times are military or 24 hour times. So 1:00 am is 100, and 1:00pm is 1300. The class must ensure that startTime and endTime instance variables are within the valid range of times (0 – 2359). If an invalid time is passed to the object, assign the time to -1 and print the following error message: Invalid time: the time must be between 0 and 2359.

NOTES: No need to code for leading zero related to the time variables, so 0 is valid. Only write the constructors and methods specified, any additional code will not be graded. Import statements or comments are not required.

Instance variables

i.      startTime - an integer representing the start time of the appointment.

ii.      endTime - an integer representing the end time of the appointment.

iii.      date – a String representing the date

Constuctors

i.      Default constructors that sets the instance variables to a default value.

ii.      A parameterize constructor that sets the instance variables to the parameters.

Methods

i.      Accessor method for date instance variable.

ii.      Mutator methods for startTime and endTime.

iii.      toString method – returns a String representing the appointment such as:

“Start Time: 100   End Time: 1300          Date: March 29th, 2013”

2.Create a class named AppointmentTester that tests the Appointment class created in the previous question. The code must contain a main method and perform the following.

Create an object using the default constructor.

Create an object using the parameterized constructor.

Call both mutator methods.

Call the accessor method and printout that value.

Call the toString method and printout that value.

Explanation / Answer

/***The java Appointment class that has constructors
* to set start , end and date . The method toString
* returns the description of the Appointment object*/
//Appointment.java
public class Appointment {

   //instance variables
   private int startTime;
   private int endTime;
   private String date;

   //Default constructor
   public Appointment() {
       startTime=0;
       endTime=0;
       date="Not Set";
   }

   //Parameterized constructor
   public Appointment(int startTime, int endTime, String date) {
       //calling setter methods to set start,end and date values
       setStartTime(startTime);      
       setEndTime(endTime);      
       setDate(date);
   }

   public void setStartTime(int startTime){
       //Check if the start time is valid
       if(startTime<100 || startTime>2300)
           startTime=-1;
       else
           this.startTime=startTime;
   }
  
   public void setEndTime(int endTime){

       //Check if the end time is valid
       if(endTime<100 || endTime>2300)
           endTime=-1;
       else
           this.endTime=endTime;
   }
  
   public void setDate(String date){
       this.date=date;      
   }
  
   //Getter methods of the class instance variables
   public int getStartTime(){
       return startTime;
   }
   public int getEndTime(){
       return endTime;
   }
   public String getDate(){
       return date;
   }
  
   //Returns the string representation of Appointment object
   @Override
   public String toString() {      
       return "Start Time: "+startTime+" End Time: "+endTime+
               "Date: "+date;
   }


}


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

/***The java class AppointmentTester that tests the Appointment
* class and prints the result of calling accessor and mutators
* to console.*/
//AppointmentTester.java
public class AppointmentTester {
  
   public static void main(String[] args) {
      
       //Create anobject of default Appointment class
       Appointment defaultapp=new Appointment();
      
      
       //Create anobject of parameterized Appointment class
       Appointment parameterapp=new Appointment();
      
       //Call mutator methods of parameterized constructor
       parameterapp.setStartTime(100);
       parameterapp.setEndTime(1300);
       parameterapp.setDate("March 29th, 2013");
      
      
       System.out.println("Default consructor values");
       //Call accessor method on defaultapp object
       System.out.println(defaultapp.getStartTime());
       System.out.println(defaultapp.getEndTime());
       System.out.println(defaultapp.getDate());
          
       System.out.println("Parameterized consructor values");
       //Call accessor method on parameterapp object
       System.out.println(parameterapp.getStartTime());
       System.out.println(parameterapp.getEndTime());
       System.out.println(parameterapp.getDate());
      
       //Calling toString method of defaultapp
       System.out.println("Calling toString of default constructor");
       System.out.println(defaultapp.toString());
      
       //Calling toString method of parameterapp
       System.out.println("Calling toString of parameterized constructor");
       System.out.println(parameterapp.toString());
              
   }
}


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

Sample Output:

Default consructor values
0
0
Not Set
Parameterized consructor values
100
1300
March 29th, 2013
Calling toString of default constructor
Start Time: 0 End Time: 0Date: Not Set
Calling toString of parameterized constructor
Start Time: 100 End Time: 1300Date: March 29th, 2013