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

Write a java program that asks the user for a positive integer N and then calculates...
Write a java program that asks the user for a positive integer N and then calculates a new value for N based on whether it is even or odd: if N is even, the new N is N/2. (Use integer division.) if N is odd, the new N is 3*N + 1. Repeat with each new N until you reach the value 1. For example, say the initial value is 12. Then the successive values are: 12 (even, next value...
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...
3. Write a program that simulates a vending machine.   The user will be prompted to enter...
3. Write a program that simulates a vending machine.   The user will be prompted to enter a number then a letter. As part of the prompt you must display a message to indicate the number and letter, along with the possible choices. That is, what selections will give user which item.   If the user enters a number or a letter that is not valid, then display the message “Bad Entry” and end the program. (100 pts) Selections with messages. 1a...
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 calculates the salary of employees. The program should prompt the user to...
Write a program that calculates the salary of employees. The program should prompt the user to enter hourly rate and number of hours of work a day. Then, the program should display the salary daily, bi-weekly (5 days a week), and monthly. Sample program: Enter your hourly rate: >>> 20 Enter how many hours you work a day: >>> 8 Your daily salary is: $160 Your bi-weekly salary is: $1600 Your monthly: $3200
write a program to calculate and print payslips write program that calculates and prints payslips. User...
write a program to calculate and print payslips write program that calculates and prints payslips. User inputs are the name of employee, numbers of hours worked and hourly rate c++ language
JAVA Program Write a program that prompts the user for data until the user wishes to...
JAVA Program Write a program that prompts the user for data until the user wishes to stop (you must have a while loop) (You must read in at least 8 to 10 sets of voter data using dialog boxes) The data to read in is: The registration of the voter (Democrat, Republican or other) The gender of the voter The Presidential candidate the voter is choosing (Trump or Biden) Which candidate has done better to manage the economy? (Trump or...
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...
Write a C ++ program that asks the user for the speed of a vehicle (in...
Write a C ++ program that asks the user for the speed of a vehicle (in miles per hour) and how many hours it has traveled. The program should then use a loop to display the distance the vehicle has traveled for each hour of that time period. Here is an example of the output: What is the speed of the vehicle in mph? 40 How many hours has it traveled? 3 Hour Distance Traveled -------------------------------- 1           40 2           80...
Write a java program. The program requirements are: the program will let the user input a...
Write a java program. The program requirements are: the program will let the user input a starting number, such as 2.  It will generate ten multiplication problems ranging from 2x1 to 2x10.  For each problem, the user will be prompted to enter the correct answer. The program should check the answer and should not let the user advance to the next question until the correct answer is given to the question being asked currently. After testing a total of 10 multiplication problems,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT