You were driving a little too fast and have been caught speeding by a police off
ID: 3792476 • Letter: Y
Question
You were driving a little too fast and have been caught speeding by a police officer. The officer is a bit of a geek and offers you 3 options to calculate your fine: Option 1 : Base fine of $50, +$7 for each mph you were over the limit. Option 2 : Base fine of $70, +$3 for each mph you were over the limit. Option 3 : Base fine of $100, +$1 for each mph you were over the limit. Write a program that takes as input the speeding limit and the speed clocked by the officer. Check that the input is valid. Then output the option that gives you the lowest fine, as well as the fine amount. If 2 options produce the same result, you can output any one of the options.
Explanation / Answer
//Speed.java
import java.util.Scanner;
public class Speed
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter speeding limit: ");
int speedLimit = sc.nextInt();
while (true)
{
if(speedLimit < 0)
{
System.out.println("Invalid Input Enter speeding limit: ");
speedLimit = sc.nextInt();
}
else
break;
}
System.out.println("Enter speed clocked by officer: ");
int speedClocked = sc.nextInt();
while (true)
{
if(speedClocked < 0)
{
System.out.println("Invalid Input Enter speed clocked by officer: ");
speedClocked = sc.nextInt();
}
else
break;
}
int fineAmount1 = 50 + 7*(speedClocked- speedLimit);
int fineAmount2 = 70 + 3*(speedClocked- speedLimit);
int fineAmount3 = 100 + 1*(speedClocked- speedLimit);
if(speedClocked < speedLimit)
{
System.out.println("No Fine Speed clocked does not exceed speedLimit ");
}
else if(fineAmount1 < fineAmount2 && fineAmount1 < fineAmount3)
{
System.out.println(" Option 1 gives the lowest fine Fine amount: " + fineAmount1);
}
else if(fineAmount2 < fineAmount1 && fineAmount2 < fineAmount3)
{
System.out.println(" Option 2 gives the lowest fine Fine amount: " + fineAmount2);
}
else
{
System.out.println(" Option 3 gives the lowest fine Fine amount: " + fineAmount3);
}
}
}
/*
Output:
Enter speeding limit:
34
Enter speed clocked by officer:
65
Option 3 gives the lowest fine
Fine amount: 131
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.