CS 145 Lab 1 – Variables and I/O
Welcome to the lab section of CS 145!
Lab is a time intended for you to work on programming exercises in a low-stakes environment and with lots of help at your disposal. This is how it works:
- You complete a few checkpoints, which consist of short programming exercises.
- Questions are always fair game. Your instructor and teaching assistant are here to help.
- You work with a partner of your choosing, but this must be a different person each week. Talking through technical matters with other human beings is a key part of lab.
- Groups of three are not allowed except by permission from the instructor.
- You work at one computer only.
- One person types for the first checkpoint. The other for the second checkpoint. And so on.
- You must show your work to either your instructor or your teaching assistant to receive credit.
- Both you and your partner must be present during checkoff time to receive credit.
- Checkpoints are only checked during lab, not during office hours or over email.
- If you don’t finish your checkpoints during a lab meeting, you can complete your work before next lab and get checked off during the first 10 minutes of the subsequent lab. No credit is granted if the work is not already complete before lab starts.
Many of these rules were crafted out of failures from earlier offerings of this course. These are designed to maximize the number of passing grades at the end of the semester.
Please form your groups and one of you log in!
Checkpoint 1
Person A types.
Recall the Madeup models that we programmed together on the first day of class. For instance, here’s the polygon program that we wrote:
This program reflects two crucial ideas relevant to this exercise:
- variables, which give symbolic names for to the “actors” in our computation and which also allow us to tweak values in just one spot in our code
- loops, which make mind-numbing repetition more mind-delighting
Your task is to reverse engineer one of the following two models and write a Madeup program to generate it. Use variables and loops where appropriate!
Madeup is available at http://madeup.xyz. The example above contains all the ideas you need to go on, but more extensive documentation is available.
The first is a chain link:
The second is a node from a Flexi Puzzle:
When you have completed your model, invite your instructor or teaching assistant over to offer feedback and make note of your accomplishment in the gradebook. If both are working with others, and you are confident that you’ve satisfactorily completed this checkpoint, feel free to move on to the next.
Checkpoint 2
Person B types.
We now leave Madeup and use the programming environment that we’ll spend far more time in this semester: Eclipse. Open it and follow these steps:
- You’ll be prompted to choose a workspace, the place where all your code is stored. Enter
H:\workspace
. (TheH:
drive is your university drive, which is accessible all over campus and even from home. Do not put anything on theC:
drive, which gets wiped frequently by LTS, our system administrators. You are free to place your workspace on a removable drive or simply use Eclipse on your personal machine, but you are responsible for properly managing and backing up your files. We cannot support your personal hardware.) - Create a new Java project in your workspace and name it
labs145
. - Right-click on the
src
directory and create a package namedlab01
. - Close Eclipse.
- Open Eclipse again and make sure you it loads the workspace you created a moment ago. Do you see your project and package?
Now, 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 enlarging images. New colors must be created for pixels that didn’t exist before, and these colors are calculated by blending the colors of the original pixels.
- Right-click on the
lab01
package and create a new class namedColorMixer
. - Add a
main
method inside between the curly braces—which look like{}
. - Declare and assign a
Scanner
variable. Import theScanner
class. - 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. - 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].
- Mix the reds, greens, and blues together using the proportion. For example, if the proportion is 0.1, then the mix is 10% of the first color and 90% of the second. If the red intensity of the first color is 50, and the red intensity of the second color is 100, then the mixed intensity is 95. Store the results of your mixing in three
double
variables. - Print out the mixed intensities.
Test your work with this mixing widget.
If you finish early, feel free to leave or reverse engineer the second model.