Week 7
Exam review
Topics:
- data types:
int
,char
,float
,String
,boolean
- variables
- binary operations
- What happens when you divide one
int
by another? - What happens when you divide an
int
by afloat
? - How to use String variables to print messages.
- What happens when you divide one
- boolean logic
- Evaluating boolean binary operations
- Creating truth tables
- Translating English statements into boolean logic
- control flow
if
statementsfor
loopswhile
loops
Practice problems
- For each data type we’ve covered, create a few variables of that type, and use
binary operations to combine them. Print the results. What happens with you
apply
+
to aString
and aboolean
? - Write a program that prints out all the numbers from 2 to 100 that 254541 is divisible by. For example, all the numbers from 2 to 100 that 10 is divisible by are: 2, 5, and 10.
- starting with
n = 1000
, ifn
is odd, multiply it by 3 and add 1; ifn
is even, divide it by 2. Count the number of iterations it takes untiln
is 1. This is related to a math problem called the Collatz conjecture (or the 3n + 1 conjecture). Optional: compute the number of iterations required when starting fromn = 1
throughn = 100
. - Write a program that draws 5 concentric circles of alternating colors (like a target). Optional: label scores for each circle (50, 40, 30, 20, and 10 for the inner to outer scores).
- Write a program that generates 1000 random numbers in the range [0, 1), and counts the number that land in each bucket [0, 0.25), [0.25, 0.5), [0.5, 0.75), and [0.75, 1).
- Write a program that does the following:
- Pick two random points
- Pick two random diameters
- Draw a circle at each point, each with the corresponding randomly chosen diameter
- Indicate if the circles overlap (either by printing a message, or changing color based on
whether they overlap). You’ll need to use the formula for the distance between two points.
Here’s a sample program outline:
void setup() { size(500, 500); frameRate(1); // slow framerate so we can see what's happening } void draw() { background(255); // clear last drawn circles. // Pick random centers and diameters // Determine if circles will overlap // Draw circles and indicate if they overlap }
- (A little harder) Suppose you have the following food options:
- Twinkies, with 330 calories and 11mg vitamin C each.
- Fruitcakes, with 850 calories and 30mg vitamin C each.
Write a program that determines how many of each you should eat if you want to maximize your vitamin C intake while limiting yourself to 2800 calories. Assume you have to eat the entire item if you choose to eat it. How does the problem change if you’re allowed to eat only part of a Twinkie or fruitcake?
- (Harder, for the math/physics inclined) Make a simple version of Gorillas.
Sample exam problems
- Suppose that beer costs $0.50/can, carrots cost $1/pound, and that when you buy them a 6% sales tax is applied to the total cost. Write a program that declares variables that represent those costs, and declares a variable that is set to the total cost (including tax) of buying 24 cans of beer and 5 pounds of carrots.
- Change the previous problem to reflect new tax rates: 10% on beer, and 2% on all other food.
- Suppose you’re have a variable
money
that indicates how much money you currently have. Write a program that will print out the amount of beer and carrots you will buy according to the following rule: if you have less than $10, buy as much beer as possible. Otherwise, buy $10 worth of beer, and buy carrots with the rest of your money. Assume the same prices as above, but with no tax. A possibly useful function:floor(x)
returns the largest integer less than or equal tox
. - Suppose that the function
foo(x)
takes an integerx
as input and as output returns twicex
. What is the output of the following program?int a = 5; int y = foo(a); println(y); y = foo(foo(a)); println(y); y = foo(a - 1); println(y); y = foo(y); println(y);
- Write a program that prints the square of each number from 1 to 100. (So the first few lines are 1, 4, 9, 16).
- What do each of the following programs print?
// Part 0 println("EASY"); // Part 1 int x = 5; int y = 4; int z = x + y; println("z = " + z); // Part 2 int q = 5; q = q * q; println("q = " + q); q = q * q; println("q = " + q); // Part 3 int a = 100; float b = 200; if (a > b) { b = a; } else { b = -a; } println(b); // Part 4 int r = 11; int s = r/2; println(s); // Part 5 int t = 15; while (t > 0) { t = t / 2; println(t); } // Part 6 boolean b1 = true boolean b2 = false boolean b3 = false; if (b1 && b2) { println("A"); } if (b1 || b2) { println("B"); } if (b1 && !b3) { println("C"); } if (b1 && (b3 || !b2)) { println("D"); }
- Write a program that repeatedly chooses a random number in the range [0, 10) and prints its value, and stops as soon as it chooses a number greater than 8. Recall that random(x) returns a random number in the range [0, x).
- Write a program that draws a circle of radius 20 and a square of width 40, both centered at (50, 50) (so the circle lies inside the square). Recall that the ellipse() function takes parameters (centerX, centerY, diamX, diamY), and the rect() function takes parameters (topLeftX, topLeftY, width, height).
- Consider the following program:
if (x > 5) { if (x < 10) { println("A"); } else { println("B"); } } else if (x > 1) { println("C"); }
Give values of
x
that will cause (1) “A” to be printed, (2) “B” to be printed, (3) “C” to be printed, and (4) nothing to be printed.
More exercises
- Write programs that do the following. Most are more natural with a for loop, but try to
write one version with a for loop, and one version with a while loop.
- Print the numbers from 1 to 50
- Print the numbers from 50 to 100
- Print the odd numbers between 100 and 200
- Print the even numbers between 100 and 200
- Print the multiples of 5 between 1 and 1000
- Print the 99 bottles of beer on the wall song
- Draw stripes with alternating colors on the screen
- Draw a US flag
- (While loop drills) Write programs that do the following.
- Declare a variable
int x = 1000000
. How many factors of 2 doesx
contain? Hint: repeatedly dividex
by 2 until it is odd. For example, 20 contains 2 factors of 2, since 20 = 2 * 2 * 5. - Starting with
int x = 1
, determine how many times you have to doublex
until it is larger than 10000.
- Declare a variable
- Write a program that draws 20 circles in a row.
- Write a program that prints “Top” if you click on the top half of the screen
and “Bottom” if you click on the bottom half. Possible outline:
void draw() { // Nothing is required here } void mouseClicked() { // Insert code to determine which half of screen was clicked. }
- Write a program that draws a different color background depending on where your mouse is located.
Make two versions: one that only depends on the vertical position of the mouse, and one that
depends on both the horizontal and vertical position. Possible outline:
void draw() { float bg = ...; // set background to value that depends on current mouse position background(bg); }
- What is the output of this program?
String x = ""; for (int i = 0; i < 10; i++) { x = x + "-" + i; } println(x);