Extension 1.1: Speed Limit Fine Calculator (3 points)


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:

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:

We’ll model the fine after Massachusetts law:


  1. First, create a SpeedLimit Java class in the speeding package of the extensions source folder.

  2. Consider and decide upon the data type to represent information of interest to this problem.

  3. 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.

  4. 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.)

  5. 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.


    • 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.

You have attempted of activities on this page