Given by Nancy McCracken at NPAC Java Academy February--April 99 on Feb 13 1999. Foils prepared Feb 19 1999
Outside Index
Summary of Material
Introduction to programming |
Java variables, types, and assignment |
Java language statements |
Arrays |
Outside Index Summary of Material
Nancy McCracken |
NPAC @ SU |
111 College Place |
Syracuse, NY 13244-4100 |
Syracuse University |
College of Engineering and Computer Science |
Northeast Parallel Architectures Center |
present |
Introduction to programming |
Java variables, types, and assignment |
Java language statements |
Arrays |
A computer follows a set of step-by-step instructions, computing with values that are stored in its memory. |
These are simple instructions, like "add two numbers". |
A programming language like Java lets you write more complicated instructions. The compiler translates them into ones the machine can compute. |
instructions |
memory |
A variable is a name that you give to a place to store a value in memory. When you declare a variable name, you say what type the values will be. |
Declare 3 variables of type integer: There are many other types: |
int x; |
int y; |
int size; |
Or all at once for variables |
of the same type: |
int x, y, size; |
String s; |
Font f; |
Color c; |
The way that you put a value into the space named by the variable is called assignment: |
Note that this is not the same as "=" in math. You can say things like: which means take the old value of x, add 1, and store it back into x. In fact this is such a common operation, there is an abbreviation: |
int x, y; |
x = 8; |
y = 2 * x; |
Take the value on the right of |
the "=" and store it into the place |
named on the left. |
x = x + 1; |
x++; |
On the previous page, we had an example: |
Note that we can use numbers like 8 and arithmetic operators +, - , * and / just the same as in math (except that numbers can't get too big) |
Strings are sequences of characters in quotes: |
Values of other types are created with methods. |
int x, y; |
x = 8; |
y = 2 * x; |
String s; |
s = "This is a string." |
Also note that the statements on the previous page are computed in the order that you list them. This is true within each method. A Java program is a collection of methods - each is "called" to compute its statements. |
public class MyApplet extends Applet |
{ public void init ( ) |
{ |
/* statement1 */ |
/* statement2 */ |
. . . |
/* last statement */ |
} |
public void paint ( ) |
{ /* statements go here */ } |
public void helpermethod ( ) |
{ /* statements go here */ } |
} |
Start here |
End here |
A loop statement tells the computer to execute a set of instructions many times, and is controlled by the loop variable. |
int sum, num; |
num = 8; |
sum = 0; |
for ( int i = 0; i < num; i++ ) |
{ |
sum = sum + i; |
} |
repeat loop 8 times and |
add 1 to i each time |
The body of the loop is repeated. Values of i each time around the loop: |
i is 0 |
i is 1 |
i is 2 |
. . . |
i is 7 |
What is the value of sum at the end? |
Sometimes you have a collection of values that you want to give one name. One way to do that in Java is to have an array. |
To create an array like this, you must say what type the elements are (they must all be the same type) and how many there will be: |
An array has a set of positions to store values. Each position is given an index number: |
int numbers [ ] = new int [ 7 ] ; |
String labels [ ] = new String [ 64 ] ; |
0 1 2 3 4 5 6 |
To use an array value, you give the name of the array and the position number within [ ], and use it like any other variable. |
int numbers [ ] = new int [ 7 ] ; |
numbers [0] = 5 ; |
numbers [1] = numbers [0] + 2 ; |
String labels [ ] = new String [ 64 ]; |
labels [ 0 ] = "The first string label"; |
g.drawString ( labels [ 0 ], 10, 20 ) ; |
Create the array |
Assign position 0 |
Use position 0 |
To assign or use all the elements of an array in order, a "for" loop is handy. |
int max = 8; |
int numbers [ ] = new int [ max ] ; |
for ( int i = 0; i < numbers.length; i++ ) |
{ |
numbers [ i ]= i * 2; |
} |
0 1 2 3 4 5 6 7 |
0 2 4 6 8 10 12 14 |
Another type of values that you can use in most programming languages is a type for real numbers, i.e. numbers with decimal points. These are sometimes called floating point numbers, and Java has type float. But primarily Java uses a type double for these (so-called because they can be twice as big). |
The same arithmetic operators like +, -, * and / are used. |
double x, y ; |
x = 1.5 ; |
y = x / 2.0 ; |
In Java, additional mathematical operators are given in a package called Math as methods. These methods typically use arguments and results of type double. To compute exponents: |
The API: public static double pow(double a, double b) |
To use it: |
Also: public static double sqrt (double a ) |
And many more, such as sin, cos, min, max! |
double x, y, z ; |
x = 1.5 ; |
y = 2.0 ; |
z = Math.pow ( x, y ) ; |
You can use the Math methods on values of type int as well, but to get back an int result, you must convert the result by putting the type you want in ( ). This is called casting. |
int x, y, z ; |
x = 2 ; |
y = 3 ; |
z = ( int ) Math.pow ( x, y ) ; |
This statement is also called a "conditional" statement. It tests a "condition" and if it's true, it executes the first set of statements, and if it's false, instead it executes a second set of statements. |
if ( num == 0 ) |
{ y = 0; g.setColor ( Color.red ); } |
else |
{ y = 1; g.setColor ( Color.black ); } |
if and else are keywords |
The condition is in ( )s. |
If the condition is true, execute these statements, |
otherwise these. |
To test if two things are equal, for most types: |
Other tests for int and double types: |
To test if two strings have the same value: |
int num; |
if ( num == 0 ) ... value of num is 0 |
( num != 0 ) value of num is not 0 |
( num < 0 ) value of num is less than 0 |
( num <= 0 ) value of num is less than or = 0 |
( num > 0 ) value of num is greater than 0 |
. . . and so on |
String label; |
if ( label.equals ("enter") ) value of label is "enter" |