CSE 131 Module 1: Intro and Types & Names

Extensions

Extension 1: Speed Limit Fine Calculator (3 points):

Authors

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:

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:

Procedure

  1. First, create a SpeedLimit Java class in the speeding package of the extensions source folder.
    Do this by
    • right (control) clicking on the speeding package, and then
    • chooose NewClass
    • Type in the name of the class (SpeedLimit)
    • Be sure to check the box so that public static void main is included
    • Click OK
  2. Write code to prompt the user for the information you need to compute the fine, assuming the fine is computed as described above.
    • 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.
  3. Arrange for your program to produce output such that:

    Beware: Make sure your program works for 0 MPH and all possible positive velocities.
  4. Finally, your program should print how many miles you were going over the speed limit and how much money the fine will be.
    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.

This demo box is for extension 1.1
Last name WUSTL Key Propagate?
(NOT your numeric ID) Do not propagate
lower case only
e.g. Smith j.smith
1    

Acknowledgements and assertion of integrity

You must select one of the options below
The work submitted here was performed in accordance with this course's policy on collaboration.
On your honor, you have neither given nor received any unauthorized aid on this assignment.

However, the following TAs, students, or professors were supportive in completing this assignment.
Their help was also in accordance with course policies.

Thanks to (leave blank if appropriate):

In spite of seeking help as allowable by this course's policy on collaboration, you were unable to complete this assignment. No credit will be received for this assignment.

You would like to be contacted by an instructor to facilitate staying on track in this course.

Comments about this:

You have NOT abided by this course's policy on collaboration. No credit will be received for this assignment, but by checking this box, no academic integrity violation will be filed for this assignment.

You would like to be contacted by an intructor to faciliate staying on track in this course.

Comments about this:


TA: Password:

End of extension 1


Extension 2: Image Processor (6 points):

Authors

Background

Pixels

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.

Representing color in Java

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

Filters
This work involves writing a number of filters that are applied to images to achieve a given result. There are two kinds of methods that you complete, and each is described below:
int foo(int pixelComponent)
Methods with the above signature take in a single pixel component intensity value: an integer between 0 and 255 inclusively. They are obligated to return an intensity value as their result.

These methods are used by the course software as shown below:

  1. A Color object is broken into its red, green, and blue components.
  2. Each component is passed to your foo method, and the result of that function is retained.
  3. The three results are combined into a new Color object.
In other words, the foo function is used as a filter on each of a Color's components. The foo function is unaware of which component it is processing: it treats each equally.
Color bar(Color c)
Methods with the above signature accept and produce a standard Java Color object. These methods can themselves decompose a Color object into its components and create a resulting Color object to achieve whatever effect is desired.

Project: Image Processing Methods

  1. In Eclipse, open the extensions source folder and then open the imageprocessor package. Find and open the ImageProcessor class.
    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.

  2. Try the darker filter. It has been implemented for you, and it should produce an image in the Target window that resembles the image in the source1 window, but is a bit darker.
  3. Try the combine filter. It has also been implemented but its effects may seem a bit strange. You will soon implement a smarter version of this kind of a filter.
  4. The other methods are not yet implemented, but you are free to try them.
    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.

Directions:

First, right click on the ImageProcessorTest class, and Run As a JUnit Test.
Now open the Filters class and begin your work as described below.
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 or the ternary operator (?…:).

Intensity filters
Use no conditional execution for this part: only arithmetic expressions as you have learned them in Module 1.
  1. Complete the method called copy, so that each provided intensity value is copied exactly from the source to the target panel.
    Hint: This is a very simple method.
  2. Complete the method that will composite the images in the two source panels by averaging their components. This method accepts two parameters, which are color components from the two source panels.
  3. Complete the method called negative that will produce the negative image by inverting the intensity of each component.
    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.
  4. Complete the method posterize that will reduce the number of possible colors in an image. For a given color component, your method will choose between two intensities, 0 or 255, which correspond to that color component being turned off or on completely. So, since each color has three components (red, green, and blue), you will end up with an image that has only 8 different colors.
    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.

Color filters

Note that each pixel of an image is represented as a Color object.

  • 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();
    
Create and test methods (whose parameters and return values are Colors) with the following specifications.

  1. Complete the method brighter that will return, for each pixel, a Color that is brighter than the original.
    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.

  2. Complete the method grayscale that will make a grayscale image from a color image. To do this, you will take in one Color parameter (the Color object for a pixel from the original image) and will produce a new Color in which all the components (red, green, and blue) have the same value.
    Hint: To choose which value, average the three components of the original color.
More Color filters
OK, now you can use if statements!

  1. Complete the method blackAndWhite that produces a black and white image by returning, for each pixel, a Color that is either black or white, depending upon the overall intensity of the original color.

    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.

  2. Complete the method combineBrighter that combines two images by choosing for each pixel the color from the image that has the brighter pixel in that location. To determine which pixel is brighter, compare the sums of the red, green, and blue components of the two pixels. Since the ProcessorTool will run your method for every pair of pixels, the resulting image will have some pixels from the first image and some from the second image.

Demo

When you done with this extension, you must be cleared by the TA to receive credit.

This demo box is for extension 1.2
Last name WUSTL Key Propagate?
(NOT your numeric ID) Do not propagate
lower case only
e.g. Smith j.smith
1    

Acknowledgements and assertion of integrity

You must select one of the options below
The work submitted here was performed in accordance with this course's policy on collaboration.
On your honor, you have neither given nor received any unauthorized aid on this assignment.

However, the following TAs, students, or professors were supportive in completing this assignment.
Their help was also in accordance with course policies.

Thanks to (leave blank if appropriate):

In spite of seeking help as allowable by this course's policy on collaboration, you were unable to complete this assignment. No credit will be received for this assignment.

You would like to be contacted by an instructor to facilitate staying on track in this course.

Comments about this:

You have NOT abided by this course's policy on collaboration. No credit will be received for this assignment, but by checking this box, no academic integrity violation will be filed for this assignment.

You would like to be contacted by an intructor to faciliate staying on track in this course.

Comments about this:


TA: Password:

End of extension 2


Extension 3: Image Processor (continued) (4 points):

Authors
This is a continuation of the previous extension. It is therefore suggested that you do the previous extension first.

Directions:

The main program you should run for this extension is Background, which conveniently sets up the images in the bar and panels for you to do this work.

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.

Two more Color filters
You can use if statements for this work.

  1. Complete the method bgSubtract so that it returns a Color as follows:
    • If the source1Color and source2Color colors are sufficiently similar, return Color.BLUE. The idea is to subtract the background so that what is common between the two source images is shown in blue.
      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
    • Otherwise, return source1Color.
    An example is shown below:
    Where the two images are similar, the corresponding pixel in the target image is blue, because your method returned Color.BLUE. Where the images differ, the first image is shown in the target, because your method returned the pixel color from the first image.

  2. Complete the method bgReplace that functions as follows:
    • If the source1Color is Color.BLUE, then return the source2Color.
    • Otherwise, return source1Color.
    The effect of this filter is to replace the blue color from the first image with the corresponding pixels from the second image.

    To see this work, perform the following steps:

    1. Drag the blue-screen image from the target panel and drop it into the leftmost source panel. The GUI should replace what was there with the blue-screen image.
    2. Drag the other two-bear picture (the reverse of the one shown in the source panel) from the top icon bar into the middle source panel.

      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.
    3. Now apply the bgReplace filter, and the results resemble the following:
    4. For fun, drag the chicken image from the top icon bar into the middle source panel, and reapply the bgReplace filter. The results should resemble the following:
      The chicken is a smaller image, so it did not take up the entire size of the bear image.

To Demo

You must have received the satisfying green bar telling you that you've passed all of the ImageProcessor tests. Your TA must also be able to play with your ImageProcessor and be satisfied.
When you done with this extension, you must be cleared by the TA to receive credit.

This demo box is for extension 1.3
Last name WUSTL Key Propagate?
(NOT your numeric ID) Do not propagate
lower case only
e.g. Smith j.smith
1    

Acknowledgements and assertion of integrity

You must select one of the options below
The work submitted here was performed in accordance with this course's policy on collaboration.
On your honor, you have neither given nor received any unauthorized aid on this assignment.

However, the following TAs, students, or professors were supportive in completing this assignment.
Their help was also in accordance with course policies.

Thanks to (leave blank if appropriate):

In spite of seeking help as allowable by this course's policy on collaboration, you were unable to complete this assignment. No credit will be received for this assignment.

You would like to be contacted by an instructor to facilitate staying on track in this course.

Comments about this:

You have NOT abided by this course's policy on collaboration. No credit will be received for this assignment, but by checking this box, no academic integrity violation will be filed for this assignment.

You would like to be contacted by an intructor to faciliate staying on track in this course.

Comments about this:


TA: Password:

End of extension 3


Extension 4: Expected Value (3 points):

Authors

Background

How do you feel about money, risk, and job satisfaction? In this extension you will examine some simple yet powerful ideas from economics, and you will write code to compute how a person should behave based how he or she views risk, reward, and job satisfaction.

The concepts we explore are as follows:

Utility
Utility is an economic term referring to the total satisfaction you derive or receive from some action or activity.

Given the choice of two activities and absent any other concerns, a rational person would chose the activity that yielded the higher utility.

Expected Value
If there is uncertainty about outcomes, then the expected value of a given outcome is the product of its probability of occurring and the value of achieving that outcome.
Let's consider the following two examples:
  1. 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.
  2. 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 Theory
The above analysis assumes a linear value for wealth. We can account for those who favor or avoid risk using Expected Utility Theory. This theory admits that rational people may have nonlinear views of wealth. This means that you may like $1,000 more (or less) than 1,000 times as much as you like $1. Let's look at three examples:
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

Problem

Suppose you are trying to decide between one of the following two careers:
Gamer
You join a start-up company, which is exciting and could have high pay-off, but this venture might fail. Let's say you succeed with probability p, and if so you will earn $190,000. But if you fail (with probability 1-p), you earn only $5000.
Programmer
This job has no risk, and offers a salary in the range of $110,000 to $160,000. Your salary will be determined by a random choice of a value drawn uniformly from that range.

Procedure

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:
  1. Prompts the user for a value for p, which is the probability of the game start-up venture succeeding. The text of your prompt should be a meaningful message.
    Think about the type of p. Also what is a good variable name for this concept?
  2. Given p, compute the expected value of the gamer-job outcome.
    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.
  3. Now pick a salary for the programming job by choosing a random value between $110,000 and $160,000.
  4. Print both outcomes and a message that indicates whether you should accept the gamer job over the programmer job depending on which one will give you the most money, according to your calculation. Such output might resemble:
      Gamer: $97,500
      Programmer: $115,000
      You should be a gamer and not a programmer?  false
    
    Run 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:

Now:
  1. Take the values for the wealth for the two jobs that you got before, and plug them into the above utility functions
  2. Print both outcomes and a message that indicates whether you should accept the gamer job over the programmer job depending on which one will give you the most utility, according to your calculation.
When you done with this extension, you must be cleared by the TA to receive credit.

This demo box is for extension 1.4
Last name WUSTL Key Propagate?
(NOT your numeric ID) Do not propagate
lower case only
e.g. Smith j.smith
1    

Acknowledgements and assertion of integrity

You must select one of the options below
The work submitted here was performed in accordance with this course's policy on collaboration.
On your honor, you have neither given nor received any unauthorized aid on this assignment.

However, the following TAs, students, or professors were supportive in completing this assignment.
Their help was also in accordance with course policies.

Thanks to (leave blank if appropriate):

In spite of seeking help as allowable by this course's policy on collaboration, you were unable to complete this assignment. No credit will be received for this assignment.

You would like to be contacted by an instructor to facilitate staying on track in this course.

Comments about this:

You have NOT abided by this course's policy on collaboration. No credit will be received for this assignment, but by checking this box, no academic integrity violation will be filed for this assignment.

You would like to be contacted by an intructor to faciliate staying on track in this course.

Comments about this:


TA: Password:

End of extension 4


Extension 5: Home Owning (4 points):

Authors
In this assignment, you compare the monthly cost of renting vs. owning a home in the St. Louis area The following tasks will test your knowledge of Let's suppose that in deciding where you want to live, you have the following choices:
Renting
When you rent, you pay a fixed amount of money each month, and you may continue this arrangement indefinitely.
Owning
The price of a home is typically larger than one can afford up-front. Thus, most home owners acquire a loan from a bank or some other funding source. To pay off the loan, let's assume you make a fixed payment each month. Some of that payment is applied to the amount of money you borrowed, called the principal. The rest of the payment is applied to the interest that you must pay to the lending agency for the privilege of borrowing the money.
In evaluating whether to rent or to own, one consideration is the amount of money you pay without return each week or year. Rent brings no return whatsoever, as the owner of the apartment simply keeps all rent.

On the other hand, the money you pay via a mortage partially pays for the house, which brings you value, but partially goes to the lender (the interest portion).

Thus, one way to compare which is better for you is to consider rent vs. interest paid per week or year.

  1. In the extensions folder of your repository, find the homeowning package and open the HomeOwning class file.
  2. For each of the following items, think about the right data type to represent that item, and place code in your HomeOwning class to prompt for the corresponding inputs:
  3. After prompting for the above inputs, your task is to provide the following report:

Sample input and output

Although you must prompt for inputs, below is an example of data you might see and the output you would produce based on those inputs.
Sample Inputs
Name Vanguard Crossing
Zip Code 63104
Monthly Rent 600
Daily Interest 31.99

The following output would be produced:

Vanguard Crossing is located in the Zip Code 63104
Rent per year: 7200
Rent per week: 138.46

Interest paid per year: 11676.35
Interest paid per week: 223.93

I should rent

Should you move?

Your apartment manager decides to increase your rent to $980 per month. Assuming that ownership is still an option for you with the same mortgage interest payments, should you continue to rent or should you own?
When you done with this extension, you must be cleared by the TA to receive credit.

This demo box is for extension 1.5
Last name WUSTL Key Propagate?
(NOT your numeric ID) Do not propagate
lower case only
e.g. Smith j.smith
1    

Acknowledgements and assertion of integrity

You must select one of the options below
The work submitted here was performed in accordance with this course's policy on collaboration.
On your honor, you have neither given nor received any unauthorized aid on this assignment.

However, the following TAs, students, or professors were supportive in completing this assignment.
Their help was also in accordance with course policies.

Thanks to (leave blank if appropriate):

In spite of seeking help as allowable by this course's policy on collaboration, you were unable to complete this assignment. No credit will be received for this assignment.

You would like to be contacted by an instructor to facilitate staying on track in this course.

Comments about this:

You have NOT abided by this course's policy on collaboration. No credit will be received for this assignment, but by checking this box, no academic integrity violation will be filed for this assignment.

You would like to be contacted by an intructor to faciliate staying on track in this course.

Comments about this:


TA: Password:

End of extension 5


Extension 6: Baseball Statistics Calculator (3 points):

Authors

Baseball Statistics Calculator

Develop a simple program to calculate some offensive baseball statistics. This assignment will demonstrate the following skills:

Procedure

  1. Create a BaseballStats Java class in the baseball package in the extensions folder.
  2. Arrange for your program to accept the following inputs:
    name
    The name of the player, provided as a String
    at-bats
    The number of at-bats the player has had provided as an int
    hits
    The number of hits the player has had provided as an int

    For example, the values that would be provided for Matt Carpenter´┐Żs 2015 season would be:

    name Matt Carpenter
    at-bats 574
    hits 154

    Information taken from http://espn.go.com/mlb/player/stats/_/id/31015/matt-carpenter

    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.
  3. Calculate whether a player is All-Star Worthy?

    Notes:

  4. To test your lab, find a player (at http://espn.go.com/mlb/statistics) and run your program with the appropriate values for that player. The output should look similar to the following:
    Matt Carpenter 0.268
    All-Star Worthy? false
    
When you done with this extension, you must be cleared by the TA to receive credit.

This demo box is for extension 1.6
Last name WUSTL Key Propagate?
(NOT your numeric ID) Do not propagate
lower case only
e.g. Smith j.smith
1    

Acknowledgements and assertion of integrity

You must select one of the options below
The work submitted here was performed in accordance with this course's policy on collaboration.
On your honor, you have neither given nor received any unauthorized aid on this assignment.

However, the following TAs, students, or professors were supportive in completing this assignment.
Their help was also in accordance with course policies.

Thanks to (leave blank if appropriate):

In spite of seeking help as allowable by this course's policy on collaboration, you were unable to complete this assignment. No credit will be received for this assignment.

You would like to be contacted by an instructor to facilitate staying on track in this course.

Comments about this:

You have NOT abided by this course's policy on collaboration. No credit will be received for this assignment, but by checking this box, no academic integrity violation will be filed for this assignment.

You would like to be contacted by an intructor to faciliate staying on track in this course.

Comments about this:


TA: Password:

End of extension 6