You will calculate the fine one would have to pay for 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
Have you ever received a ticket for speeding? If so, how do you feel about it? Were you going more or less than 10 miles over the speed limit? The objective of this extension is not only to allow you to practice assignment statements and data types but also 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:
For this assignment, we model the fine after Massachusetts law:
Do this by
- right (control) clicking on the speeding package, and then
- chooose New…Class
- Type in the name of the class (SpeedLimit)
- Be sure to check the box so that public static void main is included
- Click OK
- Consider and decide upon the data type to represent information of interest to this problem.
- You must ask the user for some input values. If you do not recall how to use ArgsProcessor, review the video on the syllabus entitled Prompting the user for input values.
Beware: Make sure your program works for 0 MPH and all possible positive velocities.
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 $200.
You must compute the required information without using if statements. To evaluate an expression conditionaly, use the ternary operator, an example of which follows:int x = (y > 7) ? 12 : 5;The variable x is set to 12 if y's value is greater than 7; otherwise x is set to 5.
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
- If you request propagation, it does not happen immediately, but should be posted in the next day or so
This demo box is for extension 1.1
The word pixel stands for Picture Element. A pixel is a small part
of a picture. Pixels are the little dots that make up LCD
screens. When you see a low-resolution picture that is grainy, the
grain
you're seeing are the pixels.
Pixels are made up of 3 colors: red, green, and blue (hence the term RGB). The color of a pixel is made up of a combination of the intensities of each of red, green, and blue components. Intensities range from 0 (meaning none of that color is present) to 255 (meaning as much as possible of that color is present). You declare a new pixel by coding
new Color(redValue, greenValue,blueValue)where redValue is the intensity (an integer between 0 and 255) of red, greenValue is the intensity of green, and blueValue is the intensity of blue.
For example, you get the color black with new Color (0,0,0) (0 intensity for all colors). You get red with new Color (255,0,0) (highest intesity for red, 0 intensity for green and blue).
These methods are used by the course software as shown below:
This is the main program you run to see the results of your work.Go ahead and run it as a Java application. You should see a window pop up with some images preloaded.
By the way, you can drag images between the icon panel at the top and the working panels in the middle, or vice versa, so that you can manipulate other images than the ones that are preloaded.You can also load your own images by clicking on the icon that resembles a plus sign.
Now open the Filters class and begin your work as described below.
- On the left you should see a whole bunch of test names with red or blue x's by them, indicating that they are not being passed.
- Every time you complete a piece of code correctly, one of the tests will pass, and a small green x will appear beside it.
- Once all of the tests pass, a satisfying green bar will appear in place of the red one that you see now. You will not get this green bar in this extension, though, because you will not be doing any work on the bgSubtract or bdReplace methods. To get the green bar you must go on to the next extension.
It is strongly suggested that you re-run the unit test, and run ImageProcessor to test each filter after its completion before you move on to the next filter. This will ensure that you are making progress and not going down a wrong path.Complete the provided stub methods as described below. In the method bodies, use mathematical expressions. Do not use a conditional (if) statement.
Use no conditional execution for this part: only arithmetic expressions as you have learned them in Module 1.
Hint: This is a very simple method.
For example, if the parameter value is 0, you should return 255. If the parameter value is 1, you should return 254, and so on.
Remember that you are not allowed to use conditionals (if) statements for this part of the lab.Hint: : Recall that color components are in the range 0-255. Also, recall that if you divide an int by another int, the result number will be truncated. For example, 130 / 128 = 1, but 125 / 128 = 0.
Note that each pixel of an image is represented as a Color object.
Create and test methods (whose parameters and return values are
- Continue to use no conditional execution for this part: only arithmetic expressions as you have learned them in Module 1.
- To create a new Color, you must specify its red, green, and blue components in that order. For example
Color c = new Color(25,128,0);declares c to be a color with some red (25 out of 255), half of the possible green, and no blue.- If you have a Color c, then you can get its red, green, and blue components as follows:
int red = c.getRed(); int green = c.getGreen(); int blue = c.getBlue();
Colors)
with the following specifications.
The Color class makes this easy because it already provides a brighter() method that returns a brighter color.If c is a color, then c.brighter() is a brighter version of c's color.
Hint: To choose which value, average the three components of the original color.
OK, now you can use if statements!
For your return value, use the constant Color.BLACK or Color.WHITE. It's up to you how to decide when a color's components, taken as a whole, should be considered black or white.
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
- If you request propagation, it does not happen immediately, but should be posted in the next day or so
This demo box is for extension 1.2
This is a continuation of the previous extension. It is therefore suggested that you do the previous extension first.
So run Background as a Java application. You will see two similar images in the source panels, and some other images in the image bar.
Open the Filters class and begin your work as described below.
You can use if statements for this work.
We will define a new term for this lab, the Saturation Tolerance. What this tells us is how different two colors can be, and still be considered sufficiently similar. For instance; if the Saturation Tolerance is 10, one color has a red value of 130, and another color has a red value of 138, these would be considered sufficiently similar. But if one color had a red value of 35 and another had a red value of 50, they would not be considered sufficiently similar
To see this work, perform the following steps:
After dragging these images around the GUI, the results should resemble the following:
Note: No transformation is applied yet. The copy operation appears as the menu choice but the Go button was not pressed.
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
- If you request propagation, it does not happen immediately, but should be posted in the next day or so
This demo box is for extension 1.3
The concepts we explore are as follows:
Given the choice of two activities and absent any other concerns, a rational person would chose the activity that yielded the higher utility.
Let's consider the following two examples:
- You are given $1. The probability of this event is 1, and your value for achieving the outcome is $1, so your expected value is $1.
- You enter into a (no-cost) lottery with 9,999 other people for a chance to win $10,000. If the probability of the winner is uniformly distributed among those who enter the lottery, you win $10,000 with probabilty 0.0001 and you lose (win $0) with probability 0.9999. The expected value computation for this scenario is as follows:
(.0001 × $10,000) + (.9999 × $0) Your expected value here is therefore also $1.
However, we know there are people who would choose entering the lottery over receiving a sure $1 or $2.
In the Powerball Lottery, your chances of winning are 1 in 175,223,510. So a rational person shouldn't buy a ticket if the jackpot is under $175M. But allegedly rational people do buy tickets for jackpots under that threshold.
How can we account for this behavior? Read on.
| Expected utility function | Notes |
|---|---|
| u(d) = d | Linear value of wealth. The above analysis applies to such a person. NOTE TO SELF: 2nd derivative 0 here |
| u(d) = d2 | This person values $10,000 the same way a linear
person would value $100,000,000.
In the scenario given above, this explains why a person would enter the lottery. The person's perceived value of winning $10,000 is NOTE TO SELF: show the application of u here $10,0002=$100,000,000, which at probability 0.0001 yields an expected utility of $10,000: far greater than the $1 given for certain. An alternate view is that this person has a linear view of wealth (u(d)=d) but an unrealistic view of the probability of winning. For example, if this person holds the false belief that the probability of winning is 0.1 instead of .0001, then the expected value jumps from $1 to $1,000 using a linear function for expected utility. Because economists try to model human behavior assuming rationality, they translate this inflated view of luck into a nonlinear view of wealth. Thus, the function u(d)=d2 captures a form of risk-loving behavior. More generally, when viewed from below, any convex function, of which u(d)=d2 is an example, models risk-loving behavior. NOTE TO SELF: 2nd derivative is positive Such a person is either more certain of winning than probabilities would indicate, or alternatively has a convex expected utility function. |
| u(d) = sqrt(d) | On the other hand, this concave function captures risk-averse behavior. In the lottery scenario, a win of $10,000 would feel like a win of only $100 to this person. With a .0001 probability of winning, the expected utility under this function is only 1 cent. Such a person would not enter the lottery even if the no-risk payoff were reduced from $1 to 2 cents. NOTE TO SELF: 2nd derivateive is negative here |
Do this work by createing a class in the expectedvalue package of the extensions folder.Let's begin by assuming you have linear value for money. Based on the above description, write a program that does the following:
Think about the type of p. Also what is a good variable name for this concept?
Use the equation for the Expected Value that is shown in the background section. Again, think about the type you need to represent the computed value, and think of a good name for this concept.
Gamer: $97,500 Programmer: $115,000 You should be a gamer and not a programmer? falseRun your program for multiple values of p to be sure that your output makes sense.
Now lets assume that you DON'T have a linear value of wealth. Lets assume that your utility for wealth given your job differs as follows:
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
- If you request propagation, it does not happen immediately, but should be posted in the next day or so
This demo box is for extension 1.4
Issues: This will become part of studio for this module. They'll do some warm up stuff and then this, which will force them to understand the grading rubric.
Use ArgsProcessor to accept input and store these values using variables with the appropriate data types.
To accomplish these tasks, use a combination of casting, Math.round(...), and basic math operations.
Think about how you would use a boolean expression to determine each of these cases.
| name | Jarett |
| participation | 100 |
| quizzes | 92 |
| studios | 100 |
| labs | 85 |
| extensions | 90 |
| exam one | 88 |
| exam two | 94 |
| lab 10 + quiz 10 | 91 |
Jarett:
Total score: 90.65
Grade for this course: 90
Final grade has a...
Plus: false
Minus: true
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
- If you request propagation, it does not happen immediately, but should be posted in the next day or so
This demo box is for extension 1.10
- St. Louis is a baseball city. Not too familiar with the game? No worries. You will be able to successfully complete this lab and take away the same skills without any background knowledge. However, if you’re new to the game and would like some background information, you may find the following video useful:
Brief Overview of Baseball
For example, the values that would be provided for Matt Carpenter’s 2015 season would be:
| name | Matt Carpenter | |
| at-bats | 574 | |
| hits | 154 |
To accept the inputs, use the same code we used in studio and lab . Just after the main method declaration, you should have the line:ArgsProcessor ap = new ArgsProcessor(args);This will likely show an error because eclipse doesn't know about ArgsProcessor. Do you remember how to fix this? Ask a TA if you need help.
Matt Carpenter 0.272
All-Star Worthy? false
Notes:
You must arrange for your output to be exactly this precise. This will take some thought, but it can be done using what has been taught: int operations, double operations, casting, and Math.round(…).
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
- If you request propagation, it does not happen immediately, but should be posted in the next day or so
This demo box is for extension 1.11
We intend this to be studio 1 and we'll move the class creation and stuff to Lab 0.Read this before starting:
The results of your studio session are to be reported and documented in a file that you save in your workspace. You are to commit that report prior to leaving studio. In the descriptions of the studio exercises, verbs like report and document are indications of activities you should summarize and discuss in your report.In your groups, take turns documenting results, looking over shoulders, and staffing the keyboard.
It is unacceptable to copy anything without understanding it. At any point, the TA or instructor can point to something you've done and ask you why it works, or change it and ask what would happen with the modification.
- To receive credit for a studio, your team must cleared by a TA using the green box at the bottom of this page.
- Be careful how you use the web. You are required to develop solutions as a group by thinking not by finding solutions that have been thought out by others. You must be able to explain anything that you have done.
Sample Values:
| Name | Vanguard Crossing |
| Zip Code | 63104 |
| Rent | 600 |
| Utilities | 110.55 |
| Mortgage | 973.13 |
Task 0
Task 1
Prompt the user for the following inputs:
Consider the implications of using a double for the zip code versus a string. Will the zip code be involved with any calculations?
In banking and other activities with large quantities of money, doubles are not advised because in Java the type double estimates for values.
Sample output based on the example provided:Vanguard Crossing is located in the Zip Code 63104
Task 2
Sample output based on the example provided:Rent: 7200.0 Mortgage: 11677.56
Task 3 Given that there are 52 weeks in a year,
Sample output based on the example provided:Rent: 138.46153846153845 Mortgage: 224.56846153846152
Task 4
Sample output based on the example provided:Rent: 138.46 Mortgage: 224.57
Task 5
Using the values given in the instructions, if someone decides to move to Red City Hill Apartments which has a rent of $730 a month,
Sample output based on the example provided:The Rent will be 21.7% more.
Task 6
Using the values given in the instructions, if someone decides to move to a new house with a mortgage of $823.32 a month,
Sample output based on the example provided:The Rent will be 15.4% less.
Task 7
If Utilities were $110.55 a month,
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
- If you request propagation, it does not happen immediately, but should be posted in the next day or so
This demo box is for extension 1.12
Issues: Under development. Do not do this yet!
This extension is based on the Speed Limit Extension from Java, your task is the same but to code it in C++
You will calculate the fine one would have to pay for going over the speed limit in the state of Massachusetts: Massachusetts DMV.
By completing this work, you demonstrate that you can:
- Create a C++ source file on your own
- Arrange for the executable to take inputs of interest
- Compute output values of interest
- Produce meaningful output based on your computations
Have you ever received a ticket for speeding? If so, how do you feel about it? Were you going more or less than 10 miles over the speed limit? The objective of this extension is not only to allow you to practice assignment statements and Data Types but also to allow you to create a practical tool which could be used in everyday life.
Consider the following story: Your friend Joe is driving his Mini Cooper at 75MPH on a causeway that has the speed limit of 60MPH and he gets pulled over. Joe doesn't have a clue how much he is going to get fined because he is both under the pressure of talking to the police officer and doesn't know how speeding fines are computed. The officer does know how fines are computed. After the officer gets in his car, he considers Joe's speed and considers the speed limit and then gives Joe a fine.
Take a moment to think about the things the officer needs in order to compute the fine that Joe will have to pay.
Procedure
You will need to prompt the user for inputs.
For input, use the following code just after the main method declaration:
cin >> speedLimit;
After you have finished that part, you have made your program able to take inputs, which you must later ask for.
Say you want to prompt for the speed limit and save the answer the user gives: A way to do this would be writing an assignment statement, which you learned how to do in this module. Do you remember what part of the assignment statement executes first? Right or left? If you don't, you should review this part of the module because you will need it to complete this extension.
Once you know which part of the assignment statement executes first, you can prompt the user for their input. Think about the Data Type you want the user to be able to input first though. Do you want them to input a decimal? An integer? A String?
Once you know which Data Type you want the user to input, you use the cin statement, variable of data type you want the user to enter.For example, say you want to accept an integer, otherwise known as int. Asking the user for input would look like this:
int speedLimit;
cout << "Input speed limit: ";
cin >> speedLimit;
Arrange for your program to produce output such that:
Beware: Make sure your program works for logical inputs and doesn't fine you if you're going 0 miles per hour.
Finally, your program should print how many miles you were going over the speed limit and how much money the fine will be. To receive full credit be sure that the TA can clearly see your program is working correctly on a number of inputs.
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
- If you request propagation, it does not happen immediately, but should be posted in the next day or so
This demo box is for extension 1.100
Issues: Under development. Do not do this yet!
The concepts we explore are as follows:
Given the choice of two activities and absent any other concerns, a rational person would chose the activity that yielded the higher utility.
Let's consider the following two examples:
- You are given $1. The probability of this event is 1, and your value for achieving the outcome is $1, so your expected value is $1.
- You enter into a (no-cost) lottery with 9,999 other people for a chance to win $10,000. If the probability of the winner is uniformly distributed among those who enter the lottery, you win $10,000 with probabilty 0.0001 and you lose (win $0) with probability 0.9999. The expected value computation for this scenario is as follows:
(.0001 × $10,000) + (.9999 × $0) Your expected value here is therefore also $1.
However, we know there are people who would choose entering the lottery over receiving a sure $1 or $2.
In the Powerball Lottery, your chances of winning are 1 in 175,223,510. So a rational person shouldn't buy a ticket if the jackpot is under $175M. But allegedly rational people do buy tickets for jackpots under that threshold.
How can we account for this behavior? Read on.
| Expected utility function | Notes |
|---|---|
| u(d) = d | Linear value of wealth. The above analysis applies to such a person. NOTE TO SELF: 2nd derivative 0 here |
| u(d) = d2 | This person values $10,000 the same way a linear
person would value $100,000,000.
In the scenario given above, this explains why a person would enter the lottery. The person's perceived value of winning $10,000 is NOTE TO SELF: show the application of u here $10,0002=$100,000,000, which at probability 0.0001 yields an expected utility of $10,000: far greater than the $1 given for certain. An alternate view is that this person has a linear view of wealth (u(d)=d) but an unrealistic view of the probability of winning. For example, if this person holds the false belief that the probability of winning is 0.1 instead of .0001, then the expected value jumps from $1 to $1,000 using a linear function for expected utility. Because economists try to model human behavior assuming rationality, they translate this inflated view of luck into a nonlinear view of wealth. Thus, the function u(d)=d2 captures a form of risk-loving behavior. More generally, when viewed from below, any convex function, of which u(d)=d2 is an example, models risk-loving behavior. NOTE TO SELF: 2nd derivative is positive Such a person is either more certain of winning than probabilities would indicate, or alternatively has a convex expected utility function. |
| u(d) = sqrt(d) | On the other hand, this concave function captures risk-averse behavior. In the lottery scenario, a win of $10,000 would feel like a win of only $100 to this person. With a .0001 probability of winning, the expected utility under this function is only 1 cent. Such a person would not enter the lottery even if the no-risk payoff were reduced from $1 to 2 cents. NOTE TO SELF: 2nd derivateive is negative here |
Think about the type of p. Also what is a good variable name for this concept?
Again, think about the type you need to represent the computed value, and think of a good name for this concept.
Gamer: $97,500 Programmer: $115,000 You should be a gamer and not a programmer? falseRun your program for multiple values of p to be sure that your output makes sense.
Which should you do to maximize your utility?
Let's say your expected `utility for wealth given your job differs as follows:
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
- If you request propagation, it does not happen immediately, but should be posted in the next day or so
This demo box is for extension 1.400
Issues: Under development. Do not do this yet!
- In this extension you must re-do the Nutrition analyzer lab, create and code a simple program that prints nutrtional information about food.
Review or reference the studio exercises as necessary.
The lab1 class is currently empty, but don't let that bother you. Right (control) click on lab1 and create a new source file called Nutrition.
For example, the values for a Snicker bar are:
| name | Snickers | |
| carbs | 34.5 | |
| fat | 13.6 | |
| protein | 4.3 | |
| statedCals | 271 |
At this point, you should be thinking about the data types you would use to represent each of the above items. We tell you that the name is a String, but what about the other inputs?The above example is provided to inspire your thinking.
To accept the inputs, use the same code we used in studio. Just after the main method declaration, you should have the lines:
cout << "Please input food name: ";This will likely show an error because eclipse doesn't know about cin and cout. Do you remember how to fix this? Ask if you need help.
string foodName;
cin >> foodName;
After that, you should initialize your inputs using code modeled after the code you read and wrote in studio.
Snickers has 34.5 grams of carbohydrates = 138.0 Calories 13.6 grams of fat = 122.4 Calories 4.3 grams of protein = 17.2 Calories This food is said to have 271 (available) Calories. With 6.6 unavailable Calories, this food has 1.65 grams of fiber Approximately 50.9% of your food is carbohydrates 45.2% of your food is fat 6.3% of your food is protein This food is acceptable for a low-carb diet? false This food is acceptable for a low-fat diet? false By coin flip, you should eat this food? true
Notes:
In the above example, based on carbohydrates, fat, and protein, the food contains 277.6 Calories. However, the label claims a modest 271 Calories. Thus, 6.6 Calories are unavailable and are attributed to dietary fiber.From this we can compute 6.6 / 4 = 1.65 grams of the stated carbohydrates are fiber.
You must arrange for your percentages to exactly this precise. This will take some thought, but it can be done using what is taught in the book: int operations, double operations, casting, and floor included in the cmath header.You are not allowed to use printf or other functions you may find that would accomplish this task. You have to figure out how to do it using what you have learned in this module.
For the output lines whose conclusions are true or false, define an appropriately named boolean variable in your program and set it equal to the expression that determines its value.When you print the value it will print as true or false.
When you done with this extension, you must be cleared by the TA to receive credit.
- Commit all your work to your repository
- Fill in the form below with the relevant information
- Have a TA check your work
- The TA should check your work and then fill in his or her name
- Click OK while the TA watches
- If you request propagation, it does not happen immediately, but should be posted in the next day or so
This demo box is for extension 1.500