Question

In: Computer Science

Write a java program that calculates a speeding fine. The user is prompted for the speed...

Write a java program that calculates a speeding fine. The user is prompted for the speed of the vehicle, the posted speed limit and if the offence took place in the construction zone. The fine is calculated at $75 + 3$/ km over the speed limit for the first 20km over + $6 / km for the next 20 and $9/km after that. If the posted limit is 40km, the fine is doubled. If the offence took place in a construction zone, the fine is doubled as well. For example, (no construction zone involved):Speed = 77Limit = 50Fine = 75 + 20*3 + 7*6 = $177OrSpeed = 55Limit = 40Fine = (75 + 15*3) * 2 = $240Fines resulting in more than $260 produce the message “Warning, dangerous driving”.If the speed limit is exceeded more than 30km/h produce the message “Criminal offence, the court subpoena is issued?

Solutions

Expert Solution

Solution:
This problem can be solved by using if-else constructs. The code has been provided below along with output for different scenarios.

import java.util.*;

public class Speed 
{

//Minimum function will be helpful in the calculation
 public static int min_num (int a, int b)
 {
    if (a<b)
        return a;
    else 
        return b;

 }

 public static void main(String args[])
 {
        int fine=0;
        int diff=0;
        
//For taking user input 
Scanner reader = new Scanner(System.in);

System.out.println("Please Enter the Speed: ");
int speed = reader.nextInt();

System.out.println("Please Enter the Posted Speed Limit: ");
int speedlimit = reader.nextInt();

System.out.println("Construction Zone (y/n): ");
char zone = reader.next().charAt(0);
while (zone != 'y' && zone !='n')
{
        System.out.println("Invalid Response please enter y/n : ");
        System.out.println("Construction Zone (y/n): ");
    zone = reader.next().charAt(0);
}


        if(speed < speedlimit)
        {
                System.out.println("No Fine!");
        }
        else
        {
                diff = speed - speedlimit;

                fine = 75 + (min_num(diff,20) * 3)  ;
                 

        if ((diff - 20) > 0 )
        {
                fine = fine + (min_num((diff-20),20) * 6);
        }
                if ((diff - 40 ) > 0 )
                {
                        fine = fine + (diff - 40) * 9;
                }

                if (speedlimit == 40 || zone == 'y')
                {
                        fine = fine * 2;
                }
        
        }

   System.out.println("Total Fine: $" + fine);

   if (fine > 260)
   {
                System.out.println("Warning, dangerous driving");

   }

   if (diff > 30)
   {
         System.out.println("Criminal offence, the court subpoena is issued?");
   }

}

        }

Output

Please Enter the Speed:
77
Please Enter the Posted Speed Limit:
50
Construction Zone (y/n):
n
Total Fine: $177

Please Enter the Speed:
77
Please Enter the Posted Speed Limit:
50
Construction Zone (y/n):
y
Total Fine: $354
Warning, dangerous driving

Please Enter the Speed:
55
Please Enter the Posted Speed Limit:
40
Construction Zone (y/n):
n
Total Fine: $240

Please Enter the Speed:
80
Please Enter the Posted Speed Limit:
40
Construction Zone (y/n):
y
Total Fine: $510
Warning, dangerous driving
Criminal offence, the court subpoena is issued?


Related Solutions

jgrasp environment, java write a complete program that calculates a restaurant bill. Prompt the user for...
jgrasp environment, java write a complete program that calculates a restaurant bill. Prompt the user for the check amount, then ask the user what type of tipp they would like to leave: the choices are 1 for good tip (20%), 2 for an average tip (15%), or 3 for poor tip (10%). Use their input and an if-else to calculate the tip amount. Then calculate and output the final bill: check+tip print out the bill, exactly as shown (print the...
1) Here is a program in c++ that calculates a speeding ticket, modify the program to...
1) Here is a program in c++ that calculates a speeding ticket, modify the program to double to cost of the ticket in a construction zone. // This project will calculate a speeding ticket between 0 to 150 mph. // 1. Ask for speed. // 2. Input speed of vehicle // 3. Calculate ticket cost (50$ if over 50mph with an additional 5$ for every mph over). // 4. Display cost of ticket. #include<iostream> using namespace std; int main() {...
Write a program that prompts user to enter integers one at a time and then calculates...
Write a program that prompts user to enter integers one at a time and then calculates and displays the average of numbers entered. Use a while loop and tell user that they can enter a non-zero number to continue or zero to terminate the loop. (Switch statement) Write a program that prompts user to enter two numbers x and y, and then prompts a short menu with following 4 arithmetic operations: Chose 1 for addition Chose 2 for subtraction Chose...
IN JAVA Write a program that calculates the occupancy rate for each floor of a hotel....
IN JAVA Write a program that calculates the occupancy rate for each floor of a hotel. (Use a sentinel value and please point out the sentinel in bold.) The program should start by asking for the number of floors in the hotel. A loop should then iterate once for each floor. During each iteration, the loop should ask the user for the number of rooms on the floor and the number of them that are occupied. After all the iterations,...
Write a Java program that calculates a random number 1 through 100. The program then asks...
Write a Java program that calculates a random number 1 through 100. The program then asks the user to guess the number.If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.The program must let the user continue to guess until the user correctly guesses the number. ★Modify the program to output how many guesses it took the user to correctly guess the right number
This is Java In this problem we will write a program that asks the user to...
This is Java In this problem we will write a program that asks the user to enter a) The user's first name and b) a series of integers separated by commas. The integers may not include decimal points nor commas. The full string of numbers and commas will not include spaces either, just digits and commas. An example of valid input string is:        7,9,10,2,18,6 The string must be input by the user all at once; do not use a loop...
Write a program that calculates the average of upto 100 English distances input by the user....
Write a program that calculates the average of upto 100 English distances input by the user. Create an array of objects of the Distance class, as in the ENGLARAY example in this chapter. To calculate the average, you can borrow the add_dist() member function from the ENGLCON example in Chapter 6. You’ll also need a member function that divides a Distance value by an integer. Here’s one possibility: void Distance::div_dist(Distance d2, int divisor) { float fltfeet = d2.feet + d2.inches/12.0;...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements and create an array. Then, write a Merge Sort function with recursion (in the main) that takes the user inputted elements in the array, sorts them, and prints them back.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT