Week 2

This week’s highlights:

Covered variables and data types, and started to go over how they are used in functions.

Reading

None required this week.

Homework

This set is not due until 1 hour before week 4’s class. More problems will be added after week 3.

In the first problem, you’ll be investigating variables of different data types, but not drawing anything to the screen. You don’t have to worry about defining the setup() and draw() functions we mentioned in class for the first problem.

The Processing environment provides a console that you can print messages to.
For example, try the following program:

  println("Hello!");
  println("Hello again (from the next line)!");
  int x = 10;
  println("The value of x is: " + x);

You can use the println() function to show the current value of variables you define. Note the way that the binary operation + is used to combine the first value (of type String) with the value of x. What happens in that particular case is that the value of x is converted to a String that is appended to the first String value – so in the example above, "The value of x is: " + x is converted to "The value of x is: 10". For a variable of any basic data type, you can print its value using the same pattern.

  1. Write a program that creates four (or more) variables and repeatedly sets the variables to new values. Print the new value each time. For example:
    int x = 5;
    println("x = " + x);
    x = 5 * 20;
    println("x = " + x);
    x = x - 50;
    println("x = " + x);
    

    Include at least one variable of type int and one of type float. Use binary operations like addition, subtraction, multiplication, division, and modulus on each.
    See what happens when you try to combine values of different data types using binary operations. Write a few comments to describe what you find when you combine different data types.