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.
Processing is designed for programming images and animations, but it does provide a text output pane for simple console output.
Program: |
Text output: |
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.
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.
Implement your statements in one code file and save the code file so that you can turn it in later.
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.
Do the following expressions give you different results? Why or why not?
2 * 3 2.0 * 3.0
What is the pattern in the return value of the following expressions?
Do you see the same pattern when you change the values to real numbers? Why or why not?1 / 3 2 / 3 3 / 3 4 / 3 5 / 3 6 / 3
Compute and record the return value of the following expression:
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?16 + 8 / 4
Compute and record the return value of the following expressions:
Explain the results.1 / 0 5.1 % 2.0
What is the return type of the following expressions?
Explain your results.1.0 / 2 x = 1 y = 1.0 + 2.0
What is the return value of the following expressions?
Explain your results.(double) 3 / 5 (double)(3 / 5) (double)(int)1.5
What values does the following code give to counter1
and counter2
?
Explain your results.int counter1 = 1, counter2 = counter1 + 1;
What does the following code do?
Explain your results.int aValue; println(aValue);
What does the following code do?
PI = 3.0;
Explain your results.
What does the following code do?
float anotherValue, anotherValue;
Explain your results.
Write expressions that return the following:
Processing provides a full range of pre-defined mathematical functions; see the Processing reference manual (“Calculation” and “Trigonometry”).
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.
Explain your results.char char1 = 'H'; char friendlyChar = 'Hi'; char emptyChar = ''; String string1 = char1; String emptyString = "";
What is the return value of the following expressions (if any)?
Explain your results."All your base" + "are belong to us." "Somebody" + ' ' + "set us up the bomb." "A.D. " + 2101
What is the return value of the following expressions (if any)?
Explain your results.true true == "true" false || !false !!true
What is the return value of the following expressions (if any)?
Explain your results.5 = 5 5 == 5 4 != 5 4 <> 5
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: |
surrenderFlag.png: (Yes, it’s white!) |
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 |
Submit all the code and supporting files for the exercises in this lab.