Answers
// do comment if any problem arises
//code
import java.util.Scanner;
class charge {
public double get_charge(int hours) {
// if car is parked for 10 hours
if (hours == 24)
return 10.00;
// initail charge of 2$
double charge = 2.00;
// if car has been parked for greater than 3 hours
if (hours > 3)
charge += (hours - 3) * 0.5;
return charge;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double total = 0;
// create a temporary object
charge temp = new charge();
while (true) {
// read number of hours from user
System.out.print("Enter number of hours (a negaitve to quit): ");
int hours = sc.nextInt();
// if user enters -ve break
if (hours < 0)
break;
// calculate charge for given hours and display
double current_charge = temp.get_charge(hours);
total += current_charge;
System.out.println("Current charge: $" + current_charge + ", Total reciepts: $" + total);
}
}
}
Output: