Difference between revisions of "Radar Trap"

From CompSciWiki
Jump to: navigation, search
Line 35: Line 35:
 
int bonus = 0;
 
int bonus = 0;
 
int vehicleCount = 0;
 
int vehicleCount = 0;
 +
String input = "";
 
</pre>
 
</pre>
2. The first input dialog comes up asking for the speed limit of the road.
+
2. The first input dialog comes up asking for the speed limit of the road.<br>
 +
The input string must be parsed to an int.
 
<pre>
 
<pre>
speedLimit = (int)JOptionPane.showInputDialog("Enter the speed limit for the road.");
+
input = JOptionPane.showInputDialog("Enter the speed limit for the road.");
 +
speedLimit = Integer.parseInt(input);
 
</pre>
 
</pre>
 
3. Now the main loop begins. The user is asked to enter the current vehicle's speed,
 
3. Now the main loop begins. The user is asked to enter the current vehicle's speed,
and the loop repeats as long as the user <br>does not enter -1.
+
and the loop repeats as long as the user <br>does not enter -1. The input string must again
 +
be parsed to an int.
 
<pre>
 
<pre>
 
while (speed != 1)
 
while (speed != 1)
 
{
 
{
     speed = (int)JOptionPane.showInputDialog("Speed of current vehicle is");
+
     input = JOptionPane.showInputDialog("Speed of current vehicle is");
 +
    speed = Integer.parseInt(speed);
 
     amountOverLimit = speed - speedLimit;
 
     amountOverLimit = speed - speedLimit;
 
</pre>
 
</pre>
Line 108: Line 113:
 
int bonus = 0;
 
int bonus = 0;
 
int vehicleCount = 0;
 
int vehicleCount = 0;
 +
                String input = "";
 
 
 
//read in the speed limit from the input dialog
 
//read in the speed limit from the input dialog
speedLimit = (int)JOptionPane.showInputDialog("Enter the speed limit for the road.");
+
input = JOptionPane.showInputDialog("Enter the speed limit for the road.");
+
                speedLimit = Integer.parseInt(input);
 +
 
while(speed != -1)
 
while(speed != -1)
 
{
 
{
speed = (int)JOptionPane.showInputDialog("Speed of current vehicle is");
+
input = JOptionPane.showInputDialog("Speed of current vehicle is");
 +
                        speed = Integer.parseInt(input);
 
amountOverLimit = speed - speedLimit;
 
amountOverLimit = speed - speedLimit;
 
 

Revision as of 23:02, 5 April 2010

Back to the Program-A-Day homepage

Problem

This program simulates a radar trap for catching people who are driving over the speed limit. The program uses JOptionPane.showInputDialog for input and JOptionPane.showMessageDialog for output. All inputs will be integers. The user will enter the speed limit for the road being monitored, and all subsequent inputs will be the speed of each vehicle that passes through the radar trap. An input of -1 ends the program.

If a vehicle is travelling at or under the speed limit, print a message stating that the vehicle is within the speed limit. Otherwise, if the vehicle is over the speed limit by 20 km/hr or less, the driver gets a $40 speeding ticket. If the vehicle is over by more than 20 km/hr and less than or equal to 40 km/hr, the driver gets an $80 ticket. If the vehicle is over by more than 40 km/hr, the driver gets a $120 ticket. If the vehicle is over the speed limit, print a message stating by how much the vehicle is over the speed limit, and the amount of the speeding ticket for that vehicle.

In addition, for every 10 cars that are caught, the police officer on duty receives a $20 bonus. You will need to keep track of this total as well, separately from the speeding tickets.

Once the program has ended, it should print the total number of cars caught speeding, the total amount of fines issued, and the total amount of the police officer's bonus.

 

SideSectionTitle

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

Solution

1. First declare the necessary variables.

//variable declarations
int speedLimit;
int speed = 0;
int amountOverLimit = 0;
int ticket = 0;
int totalTickets = 0;
int bonus = 0;
int vehicleCount = 0;
String input = "";

2. The first input dialog comes up asking for the speed limit of the road.
The input string must be parsed to an int.

input = JOptionPane.showInputDialog("Enter the speed limit for the road.");
speedLimit = Integer.parseInt(input);

3. Now the main loop begins. The user is asked to enter the current vehicle's speed, and the loop repeats as long as the user
does not enter -1. The input string must again be parsed to an int.

while (speed != 1)
{
    input = JOptionPane.showInputDialog("Speed of current vehicle is");
    speed = Integer.parseInt(speed);
    amountOverLimit = speed - speedLimit;

4. First check if the vehicle is within the speed limit. If it is, print a message confirming this.

if(amountOverLimit <= 0)
{
	ticket = 0;
	JOptionPane.showMessageDialog("Vehicle is within the speed limit.");
} 

5. If the vehicle is speeding, check by how much it is speeding and ticket the driver for the appropriate amount.
Print a message with this information.

else
{
	if(amountOverLimit > 40)
	{
	    ticket = 120;
	}
	else if(amountOverLimit > 20)
	{
  	    ticket = 80;
	}
	else
	{
	    ticket = 40;
	}
	
	JOptionPane.showMessageDialog("Vehicle is " + (amountOverLimit) + "over the limit. " +
        "The fine is $" + fine + ".");

6. Add the amount of the ticket to the total ticket amount, and increment the number of speeding vehicles.
Close the "else" block opened in Step 5.

	totalTickets += ticket
	vehicleCount++;
} //end else

7. Calculate the police officer's bonus.

bonus = (carCount/10) * 20;

8. A final message dialog appears that says how many cars were speeding, the total amount of fines issued, and the total amount
of the police officer's bonus.

JOptionPane.showMessageDialog("A total of $" + totalTickets + " in speeding tickets were issued for" + 
totalVehicles +  " vehicles. The officer receives a $" + bonus + " bonus.");

Code

Solution Code

Back to the Program-A-Day homepage