Reading: Chapter 3
This chapter provides the basic tools for working with data. There is quite a bit of material in this chapter, so use the questions listed here to help you focus your attention on the most important elements of the material. Make sure that you can do the specified exercises before moving on:
Consider the following code segment:
size(300, 300); ellipse(150, 150, 250, 250); println("Hello, world!");
Which of the following are legal identifiers in Processing/Java?
coffeeTime
coffee-time
COFFEE_TIME
4coffeetime
Given the naming conventions discussed in the text (i.e., guidelines rather than actual rules), if the identifier above is legal, indicate whether it likely to be a constant or a variable.
Consider the following code:
final int UNIT = 200; int width; int height; width = UNIT; height = UNIT; size(width, height); strokeWeight(5); point(random(width), random(height));
width
& height
?
For each of the following expressions, explain what the expression’s value is, what type this value has and why.
1 / 2
2 % 3
1 + 2 * 3
(1 + 2) * 3
The Bitwise operators are less important here, so you can skim over those, and note that you can run these expressions using a program such as this:
println(1 + 1);
The program output will appear in the text pane at the bottom of your Processing IDE window.
For each of the following expressions, explain what the expression’s value is, what type this value has and why.
1 / 2.0
sqrt(2)
Again, you can check the value of your expressions using
println()
.
For each of the following code segments, explain what happens when you run the code and why.
int x = 10; x = x + 1;
int y = 1.0;
int z = 1; z++;
You can view the value of these variables by adding a call to the
println()
(e.g.,
println(x);
). Note also that you may get syntax errors when you run the
examples; be prepared to explain what the error is and why it was
raised.
3.3.5. Character and String Expressions
For each of the following code segments, explain what happens when you run the code and why.
char direction = 'SE';
String answer = "The answer the ultimate question of life is: " + 42;
String movie = "the good\tthe bad\nthe ugly";