Extension 1.1: Speed Limit Fine Calculator (3 points)¶
Authors:
Michael Waldman
Alan Waldman
Cameron Wong
You will calculate the fine drivers have to pay when going over the speed limit in the state of Massachusetts according to the Massachusetts DMV.
By completing this work, you demonstrate that you can:
Create a Java class on your own
Arrange for the class to take inputs of interest
Compute output values of interest
Produce meaningful output based on your computations
The objective of this extension is to allow you to practice assignment statements and data types as well as for you to create a practical tool, though of course we hope you never need to compute a speeding fine for yourself.
Consider the following story:
Pat is driving a Mini Cooper at 85 MPH on a road that has the speed limit of 60 MPH.
Pat is therefore going 25 MPH over the speed limit.
If Pat is caught speeding, what would be Pat’s fine?
We’ll model the fine after Massachusetts law:
A fine is assessed only if the driver’s speed is over the speed limit.
The fine is $10 for every mile over the speed limit, but there is a $100 minimum fine.
Example: If someone is going 75 MPH on a road with a speed limit of 60 MPH, the fine would be 15*$10 = $150.
Example: If someone is going 65 MPH on a road with a speed limit of 60 MPH, the fine would be $100.
Procedure¶
First, create a SpeedLimit Java class in the
speeding
package of theextensions
source folder.Consider and decide upon the data type to represent information of interest to this problem.
You must ask the user for input values for the driver’s speed and the speed limit. For the automated test to work properly, please ask in that order.
Arrange for your program to produce output such that if the driver is going at or under the speed limit, the fine is $0 and the only thing you print is “Have a nice day.”. (You may use an if statement for this and wrap the rest in an else statement, but you may not use any other if or else if statements.)
If the driver is speeding, your program should print how many miles you were going over the speed limit and how much money the fine will be.
Reminder:
If the driver is going less than 10 miles over the speed limit, the fine is $100.
If the driver is going over 10 miles over the speed limit, the fine is 10 times the number of miles over the speed limit.
You must compute the fine without using if-statements. To evaluate an expression conditionally, use the ternary operator, an example of which follows:
int x = (y > 7) ? 12 : 5;
If y
’s value is greater than 7
, the variable x
is set to 12
; otherwise x
is set to 5
.
Sample output based on the example story above:
You reported a speed of 85 MPH for a speed limit of 60 MPH.
You went 25 MPH over the speed limit.
Your fine is $250.
Make sure to match the format exactly in order to pass the tests.
Running JUnit tests¶
JUnit tests allow you to automatically check your work. To run the tests for this extension, right-click on SpeedLimitTestSuite.java
> Run As… > JUnit Test. You may also simply open the Test Suite file and click the green Run button.
Important: when you’re running JUnit tests, make sure to comment out any Scanner input-related print statements (such as asking for the driver’s speed). The tests don’t expect those lines at the beginning.
If your code passes all the tests, you’ll see a green bar.
If your code does not pass all the tests, you’ll see a red bar. On the bottom-left corner of the screen, you’ll see a section titled the “Failure Trace”. This section will be extremely useful throughout this class for figuring out how to fix your code!
Usually, test errors will say something along the lines of “expected abc, actual xyz”, which means that the correct output would have been “abc”, but your program is currently outputting “xyz” for those inputs.
Use these differences to guide your understanding of what parts of your program you need to fix.