Difference between revisions of "Radar Trap"
(Fixed errors in the step-by-step solution) |
|||
(6 intermediate revisions by 5 users not shown) | |||
Line 3: | Line 3: | ||
|Problem= | |Problem= | ||
This program simulates a radar trap for catching people who are driving over the | This program simulates a radar trap for catching people who are driving over the | ||
− | speed limit. We will use JOptionPane.showInputDialog for input and JOptionPane.showMessageDialog for output | + | speed limit. We will use JOptionPane.showInputDialog for input and JOptionPane.showMessageDialog for output. We will assume that all user input will be in the form of integers. The user will first 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 | + | If a vehicle is traveling 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 | 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, | 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. | + | the driver gets a $120 ticket. In all cases where the vehicle is speeding, print a message stating by how much the vehicle is over the speed limit, and the applicable fine for that vehicle. |
In addition, for every 10 cars that are caught, the police officer on duty receives a $20 bonus. | 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 | + | You will need to keep track of this total separately from the speeding tickets. |
Once the program has ended, it should print the total number of cars caught speeding, | Once the program has ended, it should print the total number of cars caught speeding, | ||
− | the total | + | the sum total of all fines issued, and the total amount of the police officer's bonus. |
− | |SideSectionTitle=String Methods | + | |SideSectionTitle=String Methods and Debugging |
|SideSection= | |SideSection= | ||
[[Image:Wiki_array02.jpg|center]]<BR> | [[Image:Wiki_array02.jpg|center]]<BR> | ||
Line 22: | Line 22: | ||
|Solution= | |Solution= | ||
1. First declare the necessary variables. | 1. First declare the necessary variables. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
//variable declarations | //variable declarations | ||
int speedLimit; | int speedLimit; | ||
Line 32: | Line 33: | ||
int vehicleCount = 0; | int vehicleCount = 0; | ||
String input = ""; | String input = ""; | ||
− | + | }} | |
2. The first input dialog comes up asking for the speed limit of the road.<br> | 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. | The input string must be parsed to an int. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
input = JOptionPane.showInputDialog("Enter the speed limit for the road."); | input = JOptionPane.showInputDialog("Enter the speed limit for the road."); | ||
speedLimit = Integer.parseInt(input); | speedLimit = Integer.parseInt(input); | ||
− | + | }} | |
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. The input string must again | and the loop repeats as long as the user <br>does not enter -1. The input string must again | ||
be parsed to an int. | be parsed to an int. | ||
− | + | {{CodeBlock | |
− | while (speed != 1) | + | |Code= |
+ | while (speed != -1) | ||
{ | { | ||
input = JOptionPane.showInputDialog("Speed of current vehicle is"); | input = JOptionPane.showInputDialog("Speed of current vehicle is"); | ||
speed = Integer.parseInt(input); | speed = Integer.parseInt(input); | ||
amountOverLimit = speed - speedLimit; | amountOverLimit = speed - speedLimit; | ||
− | + | }} | |
− | 4. | + | 4. Next, check if the vehicle is within the speed limit. If it is, print a message confirming this. |
− | + | {{CodeBlock | |
+ | |Code= | ||
if(amountOverLimit <= 0) | if(amountOverLimit <= 0) | ||
{ | { | ||
− | + | ticket = 0; | |
− | + | JOptionPane.showMessageDialog(null, "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.<br>Print a message with this information. | 5. If the vehicle is speeding, check by how much it is speeding and ticket the driver for the appropriate amount.<br>Print a message with this information. | ||
− | + | {{CodeBlock | |
+ | |Code= | ||
else | else | ||
{ | { | ||
− | + | if(amountOverLimit > 40) | |
− | + | { | |
− | + | ticket = 120; | |
− | + | } | |
− | + | else if(amountOverLimit > 20) | |
− | + | { | |
− | + | ticket = 80; | |
− | + | } | |
− | + | else | |
− | + | { | |
− | + | ticket = 40; | |
− | + | } | |
− | + | JOptionPane.showMessageDialog(null, "Vehicle is " + (amountOverLimit) + "over the limit. " + | |
− | + | "The fine is $" + ticket + "."); | |
− | + | }} | |
6. Add the amount of the ticket to the total ticket amount, and increment the number of speeding vehicles.<br>Close the else block opened in Step 5 and the while loop opened in Step 3. | 6. Add the amount of the ticket to the total ticket amount, and increment the number of speeding vehicles.<br>Close the else block opened in Step 5 and the while loop opened in Step 3. | ||
− | + | {{CodeBlock | |
− | + | |Code= | |
− | + | ticketTotal += ticket | |
+ | vehicleCount++; | ||
} //end else | } //end else | ||
} //end while | } //end while | ||
− | + | }} | |
7. Calculate the police officer's bonus. | 7. Calculate the police officer's bonus. | ||
− | + | {{CodeBlock | |
− | bonus = ( | + | |Code= |
− | + | bonus = (vehicleCount/10) * 20; | |
− | 8. A final message dialog appears that states how many cars were speeding, the total | + | }} |
+ | 8. A final message dialog appears that states how many cars were speeding, the sum total of fines | ||
issued, and the total amount of the police officer's bonus. | issued, and the total amount of the police officer's bonus. | ||
− | + | {{CodeBlock | |
− | JOptionPane.showMessageDialog("A total of $" + ticketTotal + " in speeding tickets were issued for" + | + | |Code= |
− | + | JOptionPane.showMessageDialog(null, "A total of $" + ticketTotal + | |
− | + | " in speeding tickets were issued for " + vehicleCount + | |
+ | " vehicles. The officer receives a $" + bonus + " bonus."); | ||
+ | }} | ||
|SolutionCode = | |SolutionCode = | ||
− | |||
import javax.swing.*; | import javax.swing.*; | ||
Line 103: | Line 111: | ||
{ | { | ||
//variable declarations | //variable declarations | ||
− | + | int speedLimit; | |
− | + | int speed = 0; | |
− | + | int amountOverLimit = 0; | |
− | + | int ticket = 0; | |
− | + | int ticketTotal = 0; | |
− | + | int bonus = 0; | |
− | + | int vehicleCount = 0; | |
− | + | String input = ""; | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | //read in the speed limit from the input dialog | |
− | + | input = JOptionPane.showInputDialog("Enter the speed limit for the road."); | |
− | + | speedLimit = Integer.parseInt(input); | |
− | + | ||
− | + | while(speed != -1) | |
− | + | { | |
− | + | input = JOptionPane.showInputDialog("Speed of current vehicle is:"); | |
− | + | speed = Integer.parseInt(input); | |
− | + | amountOverLimit = speed - speedLimit; | |
− | + | ||
− | + | //first check if the vehicle is over the speed limit | |
− | + | if(amountOverLimit <= 0) | |
− | + | { | |
− | + | ticket = 0; | |
− | + | JOptionPane.showMessageDialog(null, "Vehicle is within the speed limit."); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
− | + | //if so, check by how much and determine the amount of the ticket | |
− | + | else | |
− | + | { | |
− | + | if(amountOverLimit > 40) | |
− | + | { | |
+ | ticket = 120; | ||
+ | } | ||
+ | else if(amountOverLimit > 20) | ||
+ | { | ||
+ | ticket = 80; | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | ticket = 40; | ||
+ | } | ||
+ | |||
+ | JOptionPane.showMessageDialog(null, "Vehicle is " + (amountOverLimit) + | ||
+ | " over the limit. The fine is $" + ticket + "."); | ||
+ | |||
+ | ticketTotal += ticket; | ||
+ | vehicleCount++; | ||
+ | } | ||
} | } | ||
+ | |||
+ | bonus = (vehicleCount/10) * 20; | ||
+ | |||
+ | JOptionPane.showMessageDialog(null, "A total of $" + ticketTotal + | ||
+ | " in speeding tickets were issued for " + vehicleCount + | ||
+ | " vehicles. The officer receives a $" + bonus + " bonus."); | ||
+ | } | ||
} | } | ||
− | |||
}} | }} |
Latest revision as of 20:14, 4 December 2011
Back to the Program-A-Day homepage
ProblemThis program simulates a radar trap for catching people who are driving over the speed limit. We will use JOptionPane.showInputDialog for input and JOptionPane.showMessageDialog for output. We will assume that all user input will be in the form of integers. The user will first 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 traveling 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. In all cases where the vehicle is speeding, print a message stating by how much the vehicle is over the speed limit, and the applicable fine 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 separately from the speeding tickets. Once the program has ended, it should print the total number of cars caught speeding, the sum total of all fines issued, and the total amount of the police officer's bonus. |
String Methods and Debugging | |
---|---|---|
Solution1. First declare the necessary variables. //variable declarations int speedLimit; int speed = 0; int amountOverLimit = 0; int ticket = 0; int ticketTotal = 0; int bonus = 0; int vehicleCount = 0; String input = ""; 2. The first input dialog comes up asking for the speed limit of the road. 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 while (speed != -1) { input = JOptionPane.showInputDialog("Speed of current vehicle is"); speed = Integer.parseInt(input); amountOverLimit = speed - speedLimit; 4. Next, check if the vehicle is within the speed limit. If it is, print a message confirming this. if(amountOverLimit <= 0) { ticket = 0; JOptionPane.showMessageDialog(null, "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. else { if(amountOverLimit > 40) { ticket = 120; } else if(amountOverLimit > 20) { ticket = 80; } else { ticket = 40; } JOptionPane.showMessageDialog(null, "Vehicle is " + (amountOverLimit) + "over the limit. " + "The fine is $" + ticket + "."); 6. Add the amount of the ticket to the total ticket amount, and increment the number of speeding vehicles. ticketTotal += ticket vehicleCount++; } //end else } //end while 7. Calculate the police officer's bonus. bonus = (vehicleCount/10) * 20; 8. A final message dialog appears that states how many cars were speeding, the sum total of fines issued, and the total amount of the police officer's bonus. JOptionPane.showMessageDialog(null, "A total of $" + ticketTotal + " in speeding tickets were issued for " + vehicleCount + " vehicles. The officer receives a $" + bonus + " bonus."); | ||
Code |