In this extension you complete the functionality of a calculator. You accomplish this using the arithmetic, String, boolean, and casting operations discussed in the book.
To run the unit test, right (control) click on the ComputationsTest file in the package explorer, drag down to Run as and select JUnit Test.The results will be a red or green bar. The red bar means you have errors, and you can see those by clicking on the flagged methods below the red bar. The green bar means your code has passed the test.
Resist the urge to finish coding all of your work! If you are doing something wrong, you will go further down a bad path than you should go. Instead, proceed as follows.
To test the cast operations, use the copy and paste buttons to transfer a value from one type to another.
Some methods in this lab (specifically the casting methods) are not expected to produce any reasonable output. For example, a double cannot be cast to a boolean.Your method must nontheless return some value, so return any value you like, but call the error() method so that the program will report an error in the GUI window.
For example, if a method should return an int but the operation does not make sense, then you would write:
error(); return 0;
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
Credits:
- Dr. Anne Bracy greatly improved the write-up of this exercise.
- Logan Sorrentino implemented the GUI that drives the image processing filters.
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.
It is strongly suggested that you 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 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.
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
Credits:
- Alan Waldman
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