63.3k views
21 votes
Write a program which will enter information relating to a speeding violation and then compute the amount of the speeding ticket. The program will need to enter the posted speed limit, actual speed the car was going, and whether or not the car was in a school zone and the date of the violation. Additionally, you will collect information about the driver (see output for specifics). The way speeding tickets are computed differs from city to city. For this assignment, we will use these rules, which need to be applied in this order:

User Sateesh K
by
4.2k points

1 Answer

9 votes

Answer:

In Java:

import java.util.*;

public class Main{

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

float speedlimit, actualspeed;

String ddate;

int schoolzone;

System.out.print("Speed Limit: ");

speedlimit = input.nextFloat();

System.out.print("Actual Speed: ");

actualspeed = input.nextFloat();

System.out.print("Date: ");

ddate = input.nextLine();

System.out.print("School Zone (1-Yes): ");

schoolzone = input.nextInt();

float ticket = 75;

ticket += 6 * (actualspeed - speedlimit);

if(actualspeed - speedlimit > 30){

ticket+=160;

}

if(schoolzone == 1){

ticket*=2;

}

System.out.print("Date: "+ddate);

System.out.print("Speed Limit: "+speedlimit);

System.out.print("Actual Speed: "+actualspeed);

System.out.print("Ticket: "+ticket);

}

}

Step-by-step explanation:

See attachment for complete program requirements

This declares speedlimit and actualspeed as floats

float speedlimit, actualspeed;

This declares ddate as string

String ddate;

This declares schoolzone as integer

int schoolzone;

This prompts the user for speed limit

System.out.print("Speed Limit: ");

This gets the speed limit

speedlimit = input.nextFloat();

This prompts the user for actual speed

System.out.print("Actual Speed: ");

This gets the actual speed

actualspeed = input.nextFloat();

This prompts the user for date

System.out.print("Date: ");

This gets the date

ddate = input.nextLine();

This prompts the user for school zone (1 means Yes, other inputs means No)

System.out.print("School Zone (1-Yes): ");

This gets the input for schoolzone

schoolzone = input.nextInt();

This initializes ticket to $75

float ticket = 75;

This calculates the additional cost based on difference between speed limits and the actual speed

ticket += 6 * (actualspeed - speedlimit);

If the difference between the speeds is greater than 30, this adds 160 to the ticket

if(actualspeed - speedlimit > 30){

ticket+=160;

}

If it is in a school zone, this doubles the ticket

if(schoolzone == 1){

ticket*=2;

}

The following print the ticket information

System.out.print("Date: "+ddate);

System.out.print("Speed Limit: "+speedlimit);

System.out.print("Actual Speed: "+actualspeed);

System.out.print("Ticket: "+ticket);

Write a program which will enter information relating to a speeding violation and-example-1
User Darkenor
by
4.2k points