Objectives

Flags

Nations and other entities have flags, and they frequently specify the layout of their flag very carefully. Can you guess which of the following sketches of a Japanese flag is (or are) the most accurate model(s)?

Consider which of these flags conforms to the specification of the Japanese flag? People are pretty good at graphical discernment tasks like this one, so it’s important to have the ratios and scaling done properly. This requires the use of types and expressions.

With types and expressions, you can write a program that encapsulates the key shapes of the Japanese flag, their colors, and their geometric inter-relationships. The only given value that the program would need is the intended size of the flag drawing (say the unit size for a flag on a 3-unit by 2-unit canvas). Such a program would implement a concise pattern or plan, commonly called a model, for a regulation Japanese flag.

Types

Processing is designed for programming images and animations, but it does provide a text output pane for simple console output.

Program:
println("Hello, world!");
Text output:
Hello, world!
Visual output:

Text output can be helpful in debugging and is used throughout these lab exercises. You can use the println() statement to print out any expression (e.g., here it prints out the literal string expression "Hello, world!"). The general pattern is as follows:

println(expression);

For each of the following exercises, record your code (as executable statements) and your short (1-line!) written answers (as comments) in one code file for each exercise (i.e., one for exercises 3.1, another for 3.2, etc.). Note that, as with last week, it is a good idea to create a lab03 directory in your course directory now and then save all your programs for this lab in that directory.

Exercise 3.1:

To represent, initialize and print your age, you might use the following code:

int myAge = 18;
println(myAge);

Write a statement or statements that declare and initialize data items for the following things. Choose the most appropriate data type, identifier and value, and decide whether the items should be variables or constants.

  1. Your lucky number;
  2. Your favorite pet’s name;
  3. The letter grade you hope to get in this course;
  4. The speed of light (nb. use scientific notation for this one);
  5. Your answer to the following question: “Do you like the television show Glee?”.

Implement your statements in one code file and save the code file so that you can turn it in later.

Expressions

For these exercises, decide what the output of the given expressions should be, record your hypotheses (as comments), and then execute the expressions to confirm your hypotheses. Use println() to check return values where appropriate. Be sure to take particular note of the cases where your hypotheses are wrong. Keep your comments brief! If a given expression is invalid, comment the offending expression and note the error given.

Numeric Expressions

Exercise 3.2:
  1. Do the following expressions give you different results? Why or why not?

    2 * 3
    2.0 * 3.0
    
  2. What is the pattern in the return value of the following expressions?

    1 / 3
    2 / 3
    3 / 3
    4 / 3
    5 / 3
    6 / 3
    
    Do you see the same pattern when you change the values to real numbers? Why or why not?
    What pattern do you see when you use the modulus operator instead?
  3. Compute and record the return value of the following expression:

    16 + 8 / 4
    
    Now compute/record an alternate expression that uses parentheses to force the interpreter to evaluate the operators in the other (non-default) order. Does the order of evaluation matter?
  4. Compute and record the return value of the following expressions:

    1 / 0
    5.1 % 2.0
    
    Explain the results.

Promotion and Casting

Exercise 3.3:
Begin by declaring x to be a float variable, and declaring y to be an integer. Then:
  1. What is the return type of the following expressions?

    1.0 / 2
    x = 1             
    y = 1.0 + 2.0     
    Explain your results.
  2. What is the return value of the following expressions?

    (double) 3 / 5
    (double)(3 / 5)
    (double)(int)1.5
    
    Explain your results.

Assignment Expressions

Exercise 3.4:
  1. What values does the following code give to counter1 and counter2?

    int counter1 = 1, counter2 = counter1 + 1;
    
    Explain your results.
  2. What does the following code do?

    int aValue;
    println(aValue);
    
    Explain your results.
  3. What does the following code do?

    PI = 3.0;
    

    Explain your results.

  4. What does the following code do?

    float anotherValue, anotherValue;
    

    Explain your results.

  5. Write expressions that return the following:

    1. the sine of 2*pi;
    2. the natural logarithm of 1000.0;
    3. 10.0 raised to the power 3.0;
    4. the ceiling of 9.1.

    Processing provides a full range of pre-defined mathematical functions; see the Processing reference manual (“Calculation” and “Trigonometry”).

Character and String Expressions

Exercise 3.5:
  1. What do the following statements do (if anything)? You will want to evaluate each statement in turn. Remember, if any are invalid, comment them out, and note the error.

    char char1 = 'H';
    char friendlyChar = 'Hi';
    char emptyChar = '';
    String string1 = char1;
    String emptyString = "";
    
    Explain your results.
  2. What is the return value of the following expressions (if any)?

    "All your base" + "are belong to us."
    "Somebody" + ' ' + "set us up the bomb."
    "A.D. " + 2101
    Explain your results.

Logical Expressions

Exercise 3.6:
  1. What is the return value of the following expressions (if any)?

    true
    true == "true"
    false || !false
    !!true
    
    Explain your results.
  2. What is the return value of the following expressions (if any)?

    5 = 5
    5 == 5
    4 != 5
    4 <> 5
    
    Explain your results.

Flags Revisited

Processing allows you to save your sketches as image files. You do this using the save() method. For example, this code saves an image of a “surrender” flag:

Program:
// Create the flag.
size(200,100);
background(255);

// Save the flag image.
save("surrenderFlag.png");
surrenderFlag.png:

(Yes, it’s white!)

Exercise 3.7:

Write a program that displays a regulation Japanese flag. It should implement a model of the flag that scales to any size, as demonstrated here, based on the value of a single variable initialized in the program representing the unit size for the flag (i.e., the Japanese flag is defined for a 3-unit by 2-unit canvas).

Save two image saves of your flag at different scales (using save()). Store these images in your Processing code directory so that you can submit them with your code.

Checking In

Submit all the code and supporting files for the exercises in this lab.