## Quiz 4: Study Guide

Quizzes are open book. 30 minutes at the start of class.

Topics:

This quiz includes the previous topics from Quiz 1, 2 and 3 plus

• arrays

• classes

• defining and using classes

• terms: methods, member variables, accessors (getters), mutators (setters)

• scope of member variables

General quiz self-assessment:

• Can you define terms?

• Are you familiar with the Processing functions we use in class and labs?

• Do you know the syntax for defining and using variables, if statements, loops, functions, and classes?

• Can you read and understand code? Could you spot errors? Can you trace the execution and deduce the values for all variables?

• Can you write code which solves a given problem? Can you solve it in different ways? For example, if a question asks you to use a function, can you solve the problem using a function?

## Practice questions

1) Loop practice. Can you write loops in `setup()` which produce the following console output?

• Write a loop that prints the following sequence to the console

```The next number is:  -5
The next number is:  -4
The next number is:  -3
The next number is:  -2
The next number is:  -1
The next number is:  0
The next number is:  1
The next number is:  2
The next number is:  3
The next number is:  4
The next number is:  5```
• Write a loop that prints the following sequence to the console

``````X =  10.0
X =  20.0
X =  30.0
X =  40.0
X =  50.0
X =  60.0
X =  70.0
X =  80.0
X =  90.0``````
• Write a program that outputs the following to the console

``````10
9
8
7
6
5
4
3
2
1
Blast off!``````
• Write a program that outputs the following pattern to the console

``````*
**
***
****
*****``````
• Write a program that outputs a 5x5 square of asterisk to the console

``````*****
*****
*****
*****
*****``````

2) Loop practice. Use loops to compute quantities over random numbers.

• Using a loop, compute the sum of 5 random numbers between -10 and 10. Output the results to the console. Below is an example

``````9.732847
3.099474
-9.973539
2.9620247
-6.101103
Sum: -0.28029633``````
• Using a loop, compute the maximum value of 5 random numbers between -10 and 10.

3) Function practice. Write functions corresponding to the following specifications. For each question, test your function from `setup()` using an array of random values as input. Your function should not hard-code the length of the array. You should also print the result to the console.

• Write a function that converts miles to kilometers. There are 1.609 kilometers in a mile. Test your function with the following sequence of values using a loop: 1.0, 1.25, 1.5, 1.75, 2.0, …​, 3.0.

``````// Implement function: milesToKilometers
// Input: miles (float)
// output: kilometers (float)``````
• Write a function that computes the tip.

``````// Implement function: computeTip
// Input: total cost (float)
// Input: tip percent (float)
// Output: float``````
• Write a function that computes the average of an array of numbers

``````// Implement function: computeAverage
// Input: values (array of float)
// Output: float``````
• Write a function that returns true if an array of float has a negative number on it.

``````// Implement function: containsNegative
// Input: values (array of float)
// Output: boolean``````
• Write a function that reverses the contents of an array. (Hint: create and return a new array in the function)

``````// Implement function: reverseArray
// Input: values (array of float)
// Output: values (array of float)``````

4) Class practice.

• Implement a Movie class. Movie should contain the following data and methods

``````// Data

// Title (string)
// Duration in minutes (float)
// Year (integer)
// actors (array of String)

// Methods

// Constructor
// Input: Title (string)
// Input: Duration in minutes (float)
// Input: Year (integer)
// Input: actors (array of String)

// Accessor for title
// Mutator for title``````
• Test your Movie class. Create a Movie object for "The Shining" which was made in 1980, is 2 hours and 26 minutes long, and stars Jack Nicholson and Shelley Duval. Create your object in `setup()`

5) Write a program that produces the following image using loops.

6) A programmer has a bug in her code. She expected compute to return 4.0 but it’s returning 3.0. What’s wrong?

``````float compute(float a, float b, float c) {
float tmp = a + b;
return tmp - c;
tmp += 1.0;
}

void setup() {
float result = compute(1.0, 4.0, 2.0);
println(result);
}``````

7) Consider the following program

``````int mystery(int[] values) {
int total = 0;
for (int i = 0; i < values.length; i++) {
int value = values[i];
if (values[i] < 0) {
value = -value;
}
total += value;
println("mystery: ", value, total);
}
}

void setup() {
int[] values = {-1, 0, 1, 2, 3};
println("Checkpoint 1:", values[2]);

int result = mystery(values);
println("Checkpoint 2: ", result);
}``````
• What is the output of this program?

• What local variables are in scope in `setup()`?

• What local variables are in scope in `mystery()`?

• What is the return type of `mystery()`?

8) Consider the following program

``````String snaf(int a) {
if (a < 5) {
return "pop";
}
else {
return "crackle";
}
}

int fee(int q) {
return -q;
}

int bar(int u, int v) {
return -10*u + fee(v);
}

int foo(int x, int y) {
return x*10 - fee(y);
}

void setup() {
int a = -5;
int b = 5;
int c = 10;
int d = foo(a, b);
int e = bar(b, a);
String result = snaf(d+e);
println(result, d, e, d+e);
}``````
• What console output is produced by this program?

10) Consider the following program

``````void setup() {

Mystery m1 = new Mystery(4,1);
Mystery m2 = new Mystery(1,1);

int result1 = 0;
int result2 = 0;
result1 = m1.compute(2);
result2 = m2.compute(2);
println("BEFORE compute1: ", result1);
println("BEFORE compute2: ", result2);

m1.setA(3);
m2.setB(3);

result1 = m1.compute(2);
result2 = m2.compute(2);
println("AFTER compute1: ", result1);
println("AFTER compute2: ", result2);
}``````

where `Mystery` is defined as

``````class Mystery {

int a = 0;
int b = 0;

Mystery(int x, int y) {
a = x;
b = y;
}

void setA(int x) {
a = x;
}

void setB(int y) {
b = y;
}

int compute(int factor) {
return a + factor*b;
}
}``````
• What is the console output of this program?

• What constructors are defined in this program?

• What member variables are defined in this program?

• What variables are in scope in Mystery’s `compute` method?

• What methods are defined in `Mystery`?

• Implement an accessor method for the attribute (e.g. member variable) `a` in `Mystery`.

1)

``````for (int i = -5; i <= 5; i++) {
println("The next number is:", i);
}``````
``````for (float i = 10; i < 100; i+=10) {
println("X =", i);
}``````
``````for (int i = 10; i > 0; i--) {
println(i);
}
println("Blast off!")``````
``````// Using an accumulator
String s = "*";
for (int i = 0; i < 5; i++) {
s += "*";
println(s);
}``````
``````// Using print statements, rather than an accumulator
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
print("*");
}
println();
}``````

2)

``````float total = 0.0;
for (int i = 0; i < 5; i++) {
float value = random(-10,10);
total += value;
println(value);
}
println("Sum: ", total);``````
``````float total = 0.0;
for (int i = 0; i < 5; i++) {
float value = random(-10,10);
total += value;
println(value);
}
println("Sum: ", total);``````
``````float maxValue = -11.0;
for (int i = 0; i < 5; i++) {
float value = random(-10,10);
if (value > maxValue) {
maxValue = value;
}
println(value);
}
println("Max: ", maxValue);``````

3)

``````// Implement function: milesToKilometers
// Input: miles (float)
// output: kilometers (float)
float milesToKilometers(float m) {
float km = m * 1.609;
return km;
}

void setup() {
for (float m = 1.0; m <= 3.0; m += 0.25) {
float result = milesToKilometers(m);
}
}``````
``````// Implement function: computeTip
// Input: total cost (float)
// Input: tip percent (float)
// Output: float
float computeTip(float totalCost, float percent) {
float tip = totalCost * percent;
return tip;
}``````
``````// Implement function: computeAverage
// Input: values (array of float)
// Output: float
float computeAverage(float[] values) {
float total = 0.0;
for (int i = 0; i < values.length; i++) {
float value = values[i];
total += value;
}
}``````
``````// Implement function: containsNegative
// Input: values (array of float)
// Output: boolean
boolean containsNegative(float[] values) {
for (int i = 0; i < values.length; i++) {
if (values[i] < 0) {
return true;
}
}
return false;
}``````
``````// Implement function: reverseArray
// Input: values (array of float)
// Output: values (array of float)
float[] reverseArray(float[] input) {
float[] result = new float[input.length];
for (int i = 0; i < input.length; i++) {
int reverseIdx = input.length - i - 1;
result[reverseIdx] = input[i];
}
return reverseIdx;
}``````

4)

``````class Movie {
String mTitle;
float mDuration;
int mYear;
String[] mActors;

// Constructor
Movie(String title, float duration, int year, String[] actors) {
mTitle = title;
mDuration = duration;
mYear = year;
mActors = actors;
}

// Accessor for title
String getTitle() {
return mTitle;
}

// Mutator for title
void setTitle(String title) {
mTitle = title;
}
}``````

And below is code to test the class

``````void setup() {
float minutes = 2*60 + 26;
String[] actors = {"Jack Nicholson", "Shelley Duval"};

// Be careful to list parameters in the correct order!
// They must match the inputs to the constructor
Movie theShining = new Movie("The Shining", duration, 1980, actors);
}``````

5)

``````void setup() {
size(600, 600);

int numRows = 3;
int numCols = 5;
int size = 100; // size of each circle
for (float y = size/2; y < numRows*size; y += size) {
for (float x = size/2; x < numCols*size; x += size) {
ellipse(x, y, size, size);
}
}
}``````

6) No code is executed after a function returns. Therefore, the line `tmp += 1.0` is not executed in the function `compute`.

7)

• Run the program to test the output

• values, result

• values, total, i, value

• integer

9)

• Run the program to test the output

• Mystery contains one constructor which takes x and y as input

• a, b

• a, b, factor

• setA, setB, compute, Mystery (the constructor) *

``````class Mystery {

int a = 0;
int b = 0;

Mystery(int x, int y) {
a = x;
b = y;
}

// The accessor for a
int getA() {
return a;
}