콘텐츠로 이동

Mini Project Set 2: Parking Garage Report

Name: ________

Time: 35 minutes

Write a Java program that reads the parking duration for five cars and prints a report for the garage. Each duration is a whole number of hours.

Learning Goals

  • Store user input in an array.
  • Process all array elements with a loop.
  • Use if / else if / else to apply different rules.
  • Accumulate totals and calculate an average.

Fee Rules

Parking duration Fee
0-2 hours $3
3-5 hours $5
More than 5 hours $8

Requirements

  • Use Scanner to read exactly five non-negative integer durations into an int[] array.
  • While reading each value, print Car 1 hours: through Car 5 hours:.
  • In one loop over the array, calculate all of the following:
  • total hours
  • total fees
  • number of long-stay cars (more than 5 hours)
  • Print the average duration with exactly two decimal places.
  • Do not use additional arrays, ArrayList, or Math methods.

Sample Input

1
3
6
2
8

Expected Output

Car 1 hours: Car 2 hours: Car 3 hours: Car 4 hours: Car 5 hours:
Total hours: 20
Total fees: $27
Long-stay cars: 2
Average stay: 4.00 hours

Starter Code

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int[] hours = new int[5];

        // TODO: Read five durations into hours.

        int totalHours = 0;
        int totalFees = 0;
        int longStayCars = 0;

        // TODO: Use one loop to update all three values.

        // TODO: Print the report.
        input.close();
    }
}

Before You Submit

  • The value at index 0 belongs to Car 1, so display i + 1 in the prompt.
  • Check the most specific range first: more than 5 hours.
  • Cast one value to double before division so the average keeps its decimal part.