In Java, create a class named Appointment that stores information about appointm
ID: 3678729 • Letter: I
Question
In Java, 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”
Create a class named AppointmentTester that tests the Appointment class created in the above. The code must contain a main method and perform the following.
1. Create an object using the default constructor.
2. Create another object using the parameterized constructor.
3. Call both mutator methods for the first object.
4. Call the accessor method and printout the value for the second object.
5. Call the toString method and printout the value for the second object.
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package practice;
import java.util.Scanner;
/**
*
* @author Shivakumar
*/
public class Appointment {
Scanner sc=new Scanner(System.in);
int startTime;
int endTime;
String date;
public Appointment()
{
startTime=0000;
endTime=0000;
date="00-00-0000";
}
public Appointment(int startTime,int endTime,String date)
{
this.startTime=startTime;
this.endTime=endTime;
this.date=date;
}
public String accessor()
{
System.out.println("Enter the date in format: Jan20th,2013");
date=sc.next();
return date;
}
public int mutators()
{
System.out.println("Enter the start time range 0-2359");
startTime=sc.nextInt();
String temp=String.valueOf(startTime);
char[] digi=temp.toCharArray();
String t1=String.valueOf(digi[0]);
String t2=String.valueOf(digi[1]);
String hours=t1.concat(t2);
String t3=String.valueOf(digi[2]);
String t4=String.valueOf(digi[3]);
String min=t3.concat(t4);
int h=Integer.parseInt(hours);
int m=Integer.parseInt(min);
if(h>23 || m>59)
{
System.out.println("Enter the correct time again in range 0-2359");
mutators();
}
System.out.println("start Time is: "+startTime);
return startTime;
}
public int mutatore()
{
System.out.println("Enter the start time range 0-2359");
endTime=sc.nextInt();
String temp=String.valueOf(endTime);
char[] digi=temp.toCharArray();
String t1=String.valueOf(digi[0]);
String t2=String.valueOf(digi[1]);
String hours=t1.concat(t2);
String t3=String.valueOf(digi[2]);
String t4=String.valueOf(digi[3]);
String min=t3.concat(t4);
int h=Integer.parseInt(hours);
int m=Integer.parseInt(min);
if(h>23 || m>59)
{
System.out.println("Enter the correct time again in range 0-2359");
mutatore();
}
System.out.println("End time is: "+endTime);
return endTime;
}
public String toString()
{
return "StartTime: "+startTime+" EndTime : "+endTime+" Date: "+date;
}
}
public class AppointmentTester
{
public static void main(String[] args)
{
Appointment ap1=new Appointment();
Appointment ap2=new Appointment(ap1.mutators(),ap1.mutatore(),ap1.accessor());
System.out.println(ap2.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.