Where I want everybody to be after demonstration 2:
You are able to make changes to programs, and compile and run them.
You understand that it is easy and productive to do the "save file, compile file, run program" -cycle as often as possible.
If you were told to create the "Hello World" -program that prints one line of text, you'd be able to build this as a Java class from scratch without looking at any examples.
You know how to use the calls "System.out.print()", "System.out.println()", and you have seen at least examples of the call "System.out.printf()"
You are able to use Java as a calculator: you know how to write mathematical expressions with numbers and operators and print out the result.
If you see an abstract syntax either as a set of "railroad" diagrams or as a BNF-like text representation, you know how it works: how you need to fill in sections to make a syntactically correct text -- what you are allowed to write and what not to, according to the syntax. An example would be a stripped-down version of the overall syntax of a Java Compilation unit:
CompilationUnit = ( [ PackageStatement ] {ImportDeclaration} {ClassDeclaration} ); PackageStatement = ( [Annotations] "package" QualifiedIdentifier ";" ); ImportDeclaration = ( "import" [ "static" ] Identifier { "." Identifier } [ "." "*" ] ";" ); NormalClassDeclaration = ( "class" Identifier [TypeParameters] ["extends" ClassOrInterfaceType] ["implements" ClassOrInterfaceTypeList] ClassBody ); ClassBody = ( "{" {ClassBodyDeclaration} "}" ); ClassBodyDeclaration = ( ";" | {Modifier} MemberDecl ); Modifier = ( Annotation | "public" | "protected" | "private" | "static" | "abstract" | "final" | "native" | "synchronized" | "transient" | "volatile" | "strictfp" );
Agenda for the next step:
We must learn the concept of variable:
- A variable is a storage place for digital data inside the computer's memory.
- No computing can happen unless the results of computations are stored somewhere. We must store them in the memory, and our means is to create and use variables.
- In Java (and many other languages) a variable has a name (symbol) associated with it. The name of a variable has the same syntax as any other Identifier in Java. Variables are used by writing their names in the source code.
- In Java (and many other languages) a variable has also a type associated with it. Each type of variable can be used in a different way.
- Before a variable can be used, it has to be declared. This means that in the source code, you have to write a variable declaration consisting of the type and the name of the variable. After the declaration, the variable can be used by writing its name, and it is applicable to operations that are available for its type (and no others!).
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
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".
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.
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
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.