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

Assignment: A parking garage charges a $2.00 minimum fee to park for up to three

ID: 3530777 • Letter: A

Question

Assignment: A parking garage charges a $2.00 minimum fee to park for up to three hours. The garage charges an additional $0.50 per hour for each hour or any part of an hour in excess of three hours. The maximum charge for any given 24 hour period is $10.00. Assume that no car parks for longer than 24 hours at a time. Write a program that will calculate and print the parking charges for each of 3 customers who parked their cars in the garage yesterday. You should enter the hours parked for each customer. The program should print the results in a neat tabular format, and should calculate and print the total of yesterday

Explanation / Answer

// Program calculates charges for parking

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Garage extends JApplet implements ActionListener {
JTextField hoursInput;
JLabel hoursPrompt;
double totalReceipts, fee;

public void init()
{
// create a label and a text field
hoursPrompt = new JLabel( "Enter number of hours:" );
hoursInput = new JTextField( 4 );
hoursInput.addActionListener( this );

// add to applet
Container container = getContentPane();
container.setLayout( new FlowLayout() );
container.add( hoursPrompt);
container.add( hoursInput );
}

// adds newest fee to total charges
public void actionPerformed( ActionEvent e )
{
double hours = Double.parseDouble( e.getActionCommand() );

fee = calculateCharges( hours );
totalReceipts += fee;
showStatus( "Current charge: " + fee +
"; Total receipts: " + totalReceipts );
}

// determines fee based on time
public double calculateCharges( double hours )
{
// apply minimum charge
double charge = 2.0;

// add extra fees as applicable
if ( hours > 3.0 )
charge = 2.0 + 0.5 * Math.ceil( hours - 3.0 );

// apply maximum value if needed
if ( charge > 10.0 )
charge = 10.0;

return charge;
}

} // end class Garage
Report Abuse

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote