Programming 1, fall 2007 -- Demonstration 3

Where I want everybody to be after demonstration 2:

Agenda for the next step:

In this demonstration, all our programs will be "simple for the last time", and they will have exactly the following structure:

public class TheNameOfYourProgram {
    public static void main(String[] args){

        /* All that we do at first is written here. */

    }
}

Small amount of complexity is introduced by making if-statements and for-statements in the later exercises. In demonstration 4, we will continue with the extremely important concept of subprogram (called "method" in Java jargon). But that requires a bit of thought, and we must learn one thing at a time.

Contents

Local variables

In this demonstration, we use only local variables. It means that the variable is declared inside a block (one important syntactic element in block-structured languages like Java), or inside the syntax of some statement (like an iteration loop). Other kinds of variables that we must know before December would be, in order of importance:

  • formal parameter of a method (==subroutine / subprogram / function)
  • field of a class
  • "global" variable (essentially a field, in Java)
  • constant (either a field or a local variable)

Exercise 1:

You already know how to print. Now, make a program with variables, using mathematical operations that use the variables. Example (which would go inside your overall program structure, of course):

double length = 10.0;
double width = 6.0;
double area, volume;
area = length * width;
volume = area * 7.6;
System.out.printf("I got %f and %f%n", area, volume);

int numberOfWolves = 10;
int numberOfSheep = 100;
int sheepAfterMeal = numberOfSheep - numberOfWolves;

System.out.printf("There are %d sheep now.", sheepAfterMeal);

You get the idea. Experiment with computations such as:

  • converting a number of seconds into hours, minutes and remaining seconds in the range 0..59 (you'll need integer division operator / and perhaps the modulo % operator, and maybe some helper variables to store intermediate results...)
  • Areas of triangles or circles, from elementary math...
  • anything that comes to your mind.

Exercise 2:

First, make a program that stores some temperature as Celsius degrees in a variable of type double and then transforms the temperature to Fahrenheit degrees (stored in another variable of type double). The conversion formula is mathematically the following:

tempFahr = (9/5)*tempCels + 32

Then, make a program that computes and prints out, one after the other, fahrenheit degrees for celsius temperatures 1.0, 2.0, 3.0, 4.0.

Exercise 3:

Truth values (type boolean) denote the fact that a logical proposition is either true or false. Example:

boolean isOneLessThanZero = 1 < 0;

You'd expect the value false for the above, wouldn't you...

Extend the program in exercise 2 so that it stores in a boolean variable the truth value of the proposition "Fahrenheit degrees are at least 100.0 degrees".

A first structure: conditional execution using the if statement

Example of an if:

if (isRaining) {
    System.out.println("You need an umbrella!");
}

Another with ''else'':

if (isRaining) {
    System.out.println("You need an umbrella!");
} else {
    System.out.println("You may not need an umbrella!");
}

Exercise 4:

Extend the previous program by a conditional execution: After computing the Fahrenheit degrees, print out if the result is above or below 100.0 degrees Fahrenheit.

Another structure: iteration with the for statement

Example of a for-loop:

for (int i=0; i<10; i++){
    System.out.prinf("I'm counting from 0 to 9, I'm at %d.", i);
}

Exercise 5:

Make a new program for Celsius-Fahrenheit conversions, using your previous code as a basis. With a for -construct, iterate from -50.0 degrees Celsius to 150.0 degrees Celsius in steps of 0.2 degrees. For each of these values, make the evaluation of Fahrenheit degrees, and print out nicely the 100 results.

There's not much code, is there. It is very useful to use for for iteration!

Exercise 6:

A construct can be embedded within another construct! (This follows from the way the syntax, and semantics, of programming is defined).

To your previous work, add the functionality that within each iteration, you report if the Fahrenheit value is greater than 100.0 (using if as in Exercise 4).

Exercise 7:

Make a program that works like this:

  • you store an integer in an integer variable "rows".

  • you store an integer in an integer variable "columns".

  • you iterate over integers "r" in the range 1..rows, and for each:

    • you iterate over integers "c" in the range 1..columns, and for each:

      • you print out the value of the expression "r * c", effectively printing out a "multiplication table" of integers.

Example output of your program (when you assign rows=6 and columns=3):

1 2 3
2 4 6
3 6 9
4 8 12
5 10 15
6 12 18

Pre-readings

Exercise 8:

For the next time, try to learn the concept of declaring and invoking (calling) a subroutine ("method"). Before december, we'll go into much detail in how subroutines are used in programming. Next time we'll be doing pretty much everything within subroutines. And there will be lots of new structures in addition to if-else and for.