Question

In: Computer Science

Write the following java program. Desc: The program computes the cost of parking a car in...

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;
}
  
}

Solutions

Expert Solution

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


Related Solutions

JAVA Program: Computes The Molar Mass of a chemical Formula. You shall write a program that:...
JAVA Program: Computes The Molar Mass of a chemical Formula. You shall write a program that: -Loads chemical-element data by scanning through the file (accessed via file path or URL) exactly once. FILENAME: Elements.txt -Expects one or more command-line arguments that constitute a chemical formula, as described below. *A chemical formula, for the sake of this program, shall be defined as: One or more whitespace-delimited tokens Each token consists of either: An element symbol, implying one atom of that element...
Write a program that computes loan payments. The loan can be a car loan, astudent loan,...
Write a program that computes loan payments. The loan can be a car loan, astudent loan, or a home mortgage loan. The program let’s the user enter theinterest rate, number of years, and loan amount, and displays the monthly andtotal payments. in Java. COP 2510
in java pls Write a Car Dealership management program. This program will allow the employees of...
in java pls Write a Car Dealership management program. This program will allow the employees of the Dealership to add cars to their database and sell them. You will be implementing two classes: Car and CarDealership. Car will store data associated with the cars and CarDealership will be the management program. Note: 5 points of your grade is based on Coding Style. You will need to update the Starter Code to follow the standards described here. Use the "Run" button...
In the space provided below write a C++ program that computes the total cost of books...
In the space provided below write a C++ program that computes the total cost of books you want to order from an online bookstore. It does so by first asking how many books are in your shopping cart and then based on that number, it repeatedly asks you to enter the cost of each item. It then calls two functions: one for computing the taxes and another for computing the delivery charges. The function which computes delivery charges works as...
In the space provided below write a C program that computes the total cost of items...
In the space provided below write a C program that computes the total cost of items you want to order online. It does so by first asking how many items are in your shopping cart and then based on that number, it repeatedly asks you to enter the cost of each item. It then adds a 5% delivery charge for standard delivery if the total is below $1,000, and an additional 10% charge if you want an expedited next day...
In the space provided below write a C program that computes the total cost of items...
In the space provided below write a C program that computes the total cost of items you want to order online. It does so by first asking how many items are in your shopping cart and then based on that number, it repeatedly asks you to enter the cost of each item. It then adds a 5% delivery charge for standard delivery if the total is below $1,000, and an additional 10% charge if you want an expedited next day...
Write an inventory program in java for a used car lot. You should have one Car...
Write an inventory program in java for a used car lot. You should have one Car parent class and child classes for 3 vehicle types (convertible, SUV, Classic, etc). The parent class should have 3 variables and 3 functions, each child class should have 1 variable unique to it and 1 function. Your program should allow the user to store up to 100 cars in the inventory and write the cars out to a file when done.
Write a program in java processing. Write a program that does the following: · Assume the...
Write a program in java processing. Write a program that does the following: · Assume the canvas size of 500X500. · The program asks the user to enter a 3 digit number. · The program then checks the value of the first and last digit of the number. · If the first and last digits are even, it makes the background green and displays the three digit number at the mouse pointer. · If the two digits are odd, it...
Write the loop invariant for the following program the computes quotients (q) and remainders (r) of...
Write the loop invariant for the following program the computes quotients (q) and remainders (r) of “a” divided by ”d”. Then prove all three cases (Initialization, Maintenance, Termination). Assume “a” and “d” are positive integers. r=a q=0 while r>=d             r=r-d             q=q+1
write a Java program Write a program to assign a letter grade according to the following...
write a Java program Write a program to assign a letter grade according to the following scheme: A: total score >= 90 B: 80 <= total score < 90 C: 70 <= total score < 80 D: 60 <= total score < 70 F: total score < 60 Output - the student's total score (float, 2 decimals) and letter grade Testing - test your program with the following input data: test1 = 95; test2 = 80; final = 90; assignments...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT