CS 148: Lab 2 – Scanner and Math
Welcome to the lab 2!
Our goal today is to become more familiar with user interaction and the various mathematical operations.
Checkpoint 1
Person A types. Start by creating a package in your Eclipse project named lab02
.
Let’s write a program that prompts the user for two colors and mixes them together. This sort of routine is used all the time when scaling images. New colors must be created for pixels that didn’t exist before, and these new colors are calculated by blending the colors of the original pixels.
- Create a new class named
ColorMixer
in yourlab02
package. - Declare and assign a
Scanner
variable. Import theScanner
class withControl-Shift-O
orCommand-Shift-O
. That’s the letterO
, not the number0
. - Prompt the user for a first color and store the inputs from the
Scanner
in threeint
s, one for each of the color’s red, green, and blue intensities. Assume the color intensities are in the interval [0, 255]. - Prompt the user for a second color and store the inputs from the
Scanner
in threeint
s. - Prompt the user for a proportion or weight by which to mix the colors. Assume the user enters a number in [0, 1].
- Compute the blended color as a weighted average of the input colors. Mix the reds, greens, and blues independently. If the proportion is 0.1, then the mix is 10% of the second color and 90% of the first. For example, If the red intensity of the first color is 50, and the red intensity of the second color is 100, then the red intensity of the blended color is 55. Store the results of your mixing in three
double
variables. - Print out the mixed intensities and compare them against the little utility below. Your numbers may not match exactly, but they should be close. You won’t actually be displaying any colors or anything graphical, just numbers.
Checkpoint 2
Person B types.
These exercises are intentionally designed to be more open-ended. We are happy to help you if you can’t figure out how to achieve your goal, but we don’t want to rob you of the learning that happens naturally when fewer directions are given.
Pause
Write a little game we’ll call Pause
. The rules of the game are this:
- The computer generates a random target time (in seconds) and communicates that number to the human player.
- The player attempts to wait the specified number of seconds and then hits Enter.
- The computer informs the player how close she was to the target time.
A possible interaction might look like this:
Wait 4.3 seconds... You waited 5.76323 seconds. That's 1.46323 seconds too many.
Math Mountain
The students at my children’s elementary school do a math exercise called Math Mountain. They are presented a number at the “peak” of the mountain, and another number at its left base. They are to enter the number at its right base, such that the two bases sum to to the peak. A prompt might look like this on their worksheet:
24 17 __
A student would enter 7 as the answer.
Write a program that lets the user complete one or more rounds of Math Mountain.