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:

3.2. Types
3.2.1 Literals

Consider the following code segment:

size(300, 300);
ellipse(150, 150, 250, 250);
println("Hello, world!");
  • What literals to do you see in this code?
  • What type does each have?
3.2.2 Identifiers

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.

3.2.3 Declarations

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));
  • What declaration statements do you see in this code?
  • What initialization statements do you see in this code?
  • Can you rewrite the code to combine the declaration and initialization of width & height?
  • Which identifiers refer to variables? to constants?
  • Can you change the code to use a 300x300 pixel canvas? how about a 100x300 canvas?
3.2.4-3.2.6. There are no exercises for these sections; skim through them but focus on the other sections.
3.3. Expressions
3.3.1 Numeric Expressions

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.

3.3.2 Promotion and Casting

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() .

3.3.3 Assignment Expressions

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.4. There are no exercises for this section; skim through it but focus on the other sections.

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";						
    
3.3.6. Skim through this as well but focus on the other sections.
3.4 Revisiting the Example - there are no exercises for this section; skim through it as your interest leads you.