In: Computer Science
Write the following java program.
Desc: The program computes the cost of parking a car in a public garage at the rate $5.00/hour. The client will always be charged for whole hours. For example, if a car parked for 2 hours and 1 minute, the client will be charged for 3 hours.
Input: User inputs the entry time and exit time in 24-hr clock format (hh:mm)
Output: The enter and exit times, the length of time the car is parked and the total charges.
Note:
Your main program must call method readTime to read Time24 objects.
You must document the main program as well as Time24:
import java.util.Scanner;
import java.util.StringTokenizer;
import java.text.DecimalFormat;
/**
A data structure that stores integer values for hour (0..23) and
minute (0..59) to represent the time of day in a 24-hour
clock
*/
public class Time24 {
private int hour;
private int minute;
//Post: Sets the hour value in the range 0 to 23 and the minute
value in the range 0 to 59
private void normalizeTime()
{
int extraHours = minute / 60;
minute %= 60;
hour = (hour + extraHours) % 24;
}
/**
Desc:Initializes this Time24 object
Post:hour and minute of this Time24 object both initialized to
0
*/
public Time24()
{
this(0,0); //calls the 2-argument constructor of class Time24
}
/**
Desc:Initializes this Time24 object
Pre:h and m cannot be negative
Post:hour and minute of this Time24 object initialized to h and
m
respectively. This operation will normalize the time if necessary
(e.g.
9:75 is stored as 10:15).
Throw:IllegalArgumentException if h or m is negative
*/
public Time24(int h, int m)
{
setTime(h, m);
}
/**
Desc:Sets the hour and minute of this Time24 object to a particular
time
Pre:h and m cannot be negative
Post:hour and minute of this Time24 object set to h and m
respectively. This operation will normalize the time if necessary
(e.g.
9:75 is stored as 10:15).
Throw:IllegalArgumentException if h or m is negative
*/
public void setTime(int h, int m)
{
if (h < 0 || m < 0)
throw new IllegalArgumentException("Time24.setTime: argument"
+ " must not be negative");
this.hour = h;
this.minute = m;
normalizeTime();
}
/**
Desc:Adds minutes to this Time24 object
Pre:m cannot be negative
Post:This Time24 object set to m minutes later. This operation
will
normalize the time if necessary (e.g. 9:75 is stored as
10:15).
Throw:IllegalArgumentException if m is negative
*/
public void addTime(int m)
{
if (m < 0)
throw new IllegalArgumentException("Time24.addTime: argument"
+ " must not be negative");
minute += m;
normalizeTime();
}
/**
Desc:Measures the interval from this Time24 object to another
time
Return:The interval from this Time24 object to t as a Time24
*/
public Time24 interval(Time24 t)
{
int currTime = hour * 60 + minute;
int tTime = t.hour * 60 + t.minute;
if (tTime < currTime)
tTime += 24 * 60;
return new Time24(0, tTime-currTime);
}
/**
Desc:Gets the hour value of this Time24 object
Return:The hour value of this Time24 object
*/
public int getHour()
{
return hour;
}
/**
Desc:Gets the minute value of this Time24 object
Return:The minute value of this Time24 object
*/
public int getMinute()
{
return minute;
}
/**
Desc:Converts this Time24 object to a string
Return:This Time24 object as a String in the form "hh:mm"
*/
public String toString()
{
DecimalFormat f = new DecimalFormat("00");
return hour + ":" + f.format(minute);
}
/**
Desc:Convert a String to a Time24
Pre:s must be in the form "hh:mm" where hh and mm are positive
integers
Return:A Time24 object that corresponds to s
*/
public static Time24 parseTime(String s)
{
StringTokenizer t = new StringTokenizer(s, ":");
int h = Integer.parseInt(t.nextToken());
int m = Integer.parseInt(t.nextToken());
return new Time24(h, m);
}
/**
Desc: Read from f the hour and minute for this Time24 object
Pre: f is ready to be read, and the next line contains hh:mm where
hh:mm gives a valid 24-hour time
Post:The entire line hh:mm read in from f.
The hour and minute of this
Time24 object set to hh and mm respectively.
The operation will normalize the time if
necessary (e.g. 9:75 is stored as 10:15).
*/
public void readTime(Scanner f)
{
String line = f.nextLine();
Time24 obj = parseTime(line);
this.hour = obj.hour;
this.minute = obj.minute;
}
}
import java.util.Scanner;
import java.util.StringTokenizer;
import java.text.DecimalFormat;
public class Main123
{
public static void main(String args[])
{
System.out.print("\nPlease enter entry time 24-hr clock format
/(hh:mm/)\n");
Scanner obj =new Scanner(System.in);
Time24 ob=new Time24();
ob.readTime(obj);
System.out.print("\nPlease enter exit time 24-hr clock format
/(hh:mm/)\n");
Time24 ob1=new Time24();
ob1.readTime(obj);
System.out.print("\nYour entered entry time in 24-hr clock format
/(hh:mm/)is\t");
System.out.print(ob.toString());
System.out.print("\nYour entered exit time in 24-hr clock format
/(hh:mm/)is\t");
System.out.println(ob1.toString());
Time24 ob2=ob.interval(ob1);
System.out.print("\nYour parking time is\t");
System.out.println(ob2.toString());
System.out.println("\nYour have to pay\t");
if(ob2.getMinute()>0)
System.out.println((ob2.getHour()+1)*5+" Rs");
else
System.out.println((ob2.getHour())*5+" Rs");
}
}
Description: Here in this main program three objects(ob,ob1,ob2) of Time24 class is created.Object ob respresent entry time and ob1 represents exit time.ob2 object contains the total parking duration.I have also used a Scanner class object(obj) to read the data from keyboard