AP Computer Science I

Computer Science
Java Lab Assignment # 03a
The Speeding Ticket Program
Assignment Purpose:
100 Point Version Only
The purpose of this program is to demonstrate knowledge of selection control structures in a
practical program.
Write a program which will process data and information relating to a speeding violation, and then compute
the amount of the speeding ticket. The program will assign the posted speed limit, actual speed the car was
going, and whether or not the car was in a school zone to declared variables of appropriate data types.
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:
1. Check to make sure car was actually speeding (i.e. car speed is greater than speed limit).
2. If car was not speeding, print a message that says “Thank you for not speeding!” and end the program.
3. Otherwise, if the car WAS speeding, do the following:
a.
b.
c.
d.
All tickets will start with a minimum amount of $75.
An additional $6 is charged for every mph over the posted speeding limit.
If you are traveling over 30mph over the posted speed limit, an addition $160 is charged.
If the speeding occurred in a school zone, the cost of the ticket is doubled.
Lastname1stinitial_Lab03a_Speeding.java
Start with the program below.
// hannag_Lab03a_Speeding.java
// The Speeding Ticket Program
// 100 Point Version - This program uses 1 and 2 way selection to
// calculate the appropriate ticket amount based on different scenarios.
public class hannag_Lab03a_Speeding
{
public static void main(String args[ ])
{
System.out.println("Lab 03a, 100 Point Version");
System.out.println();
int speedLimit = 40;
int actualSpeed = 55;
int schoolZone = 0;
int ticket = 75;
int over;
//Posted speed limit
//Speed car was traveling
//School Zone? 1 = Yes, 0 = No
//Price of ticket. Use this variable to add to ticket amount if necessary!
//Variable to calculate how much over the speed limit the car was travelling
//Calculate how many miles over the speed limit the car was travelling and store the result in the “over” integer variable.
//Use one 2-way selection statement (if/else) to check the following condition:
//If the car was NOT going over the speed limit, print the message “Thank you for not speeding!”, and end the program.
//Otherwise, if car WAS going over the speed limit, do the following (HINT: Remember to use block structure { } for 1-6 below.):
1.) Add $6 per mph over the speed limit (HINT: Use the integer variable “over” to compute the amount to add to the integer
variable “ticket”.
2.) If car was going more than 30 mph over the speed limit, add $160 to ticket amount. (HINT: 1-way selection)
3.) If car was in a school zone, double the price of the ticket. (HINT: 1-way selection)
4.) Print statements for speed limit and car speed
5.) Use a 2-way selection statement (if/else) to decide whether to print “Yes” or “No”, after “School Zone: “.
6.) Print statement for final ticket amount.
System.out.println();
}//end of main
}//end of program
1 of 2
100 Point Version Output #1
100 Point Version Output #2
100 Point Version Output #3
2 of 2