Question: In Java, Toll roads have different fees based on the time of day and on weekends. Write a method calcToll ( ) that has three

In Java, Toll roads have different fees based on the time of day and on weekends. Write a method calcToll() that has three parameters: the current hour of time (int), whether the time is morning (boolean), and whether the day is a weekend (boolean). The method returns the correct toll fee (double), based on the chart below.
Weekday Tolls
Before 7:00 am ($6.15)
7:00 am to 9:59 am ($7.95)
10:00 am to 2:59 pm ($6.90)
3:00 pm to 7:59 pm ($8.95)
Starting 8:00 pm ($6.40)
Weekend Tolls
Before 7:00 am ($6.05)
7:00 am to 7:59 pm ($7.15)
Starting 8:00 pm ($6.10)
Ex: The method calls below, with the given arguments, will return the following toll fees:
calcToll(8, true, false) returns 7.95
calcToll(1, false, false) returns 6.90
calcToll(3, false, true) returns 7.15
calcToll(5, true, true) returns 6.05
calcToll(8, true, false) should return 7.95
calcToll(1, false, false) should return 6.90
calcToll(1, false, true) should return 7.15
calcToll(3, false, true) should return 7.15
calcToll(5, true, true) should return 6.05
calcToll(6, true, false) should return 6.15
calcToll(6, true, true) should return 6.05
calcToll(7, false, false) should return 8.95
calcToll(10, false, true) should return 6.10
calcToll(11, true, false) should return 6.90
calcToll(11, false, false) should return 6.40
code:
public class LabProgram {
public static double calcToll(int hour, boolean isMorning, boolean isWeekend){
if (isWeekend){
// Weekend Tolls
if (hour <7){
return 6.05; // Before 7:00 am
} else if (hour <20){
return 7.15; //7:00 am to 7:59 pm
} else {
return 6.10; // Starting 8:00 pm
}
} else {
// Weekday Tolls
if (hour <7){
return 6.15; // Before 7:00 am
} else if (hour <10){
return 7.95; //7:00 am to 9:59 am
} else if (hour <15){
return 6.90; //10:00 am to 2:59 pm
} else if (hour <20){
return 8.95; //3:00 pm to 7:59 pm
} else {
return 6.40; // Starting 8:00 pm
}
}
}
public static void main(String[] args){
// Testing the calcToll method
System.out.println(calcToll(8, true, false)); // Expected: 7.95
System.out.println(calcToll(1, false, false)); // Expected: 6.90
System.out.println(calcToll(1, false, true)); // Expected: 7.15
System.out.println(calcToll(3, false, true)); // Expected: 7.15
System.out.println(calcToll(5, true, true)); // Expected: 6.05
System.out.println(calcToll(6, true, false)); // Expected: 6.15
System.out.println(calcToll(6, true, true)); // Expected: 6.05
System.out.println(calcToll(7, false, false)); // Expected: 8.95
System.out.println(calcToll(10, false, true)); // Expected: 7.15
System.out.println(calcToll(11, true, false)); // Expected: 6.90
System.out.println(calcToll(11, false, false)); // Expected: 6.40
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!