Given by Nancy McCracken at Sonia Kovalesky festival workshop (High School Girls) on October 23 1999. Foils prepared October 30 1999
Outside Index
Summary of Material
See Project Page supporting Class |
Introduction to Computers followed by Introduction to Java |
Talk given at High School Girl Event by Nancy McCracken and based on Java Academy |
Outside Index Summary of Material
Nancy McCracken |
NPAC at |
Syracuse University |
1999 |
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. |
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; |
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 */ } |
} |
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 |
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. |
One kind of Java program is an applet, which is a program that can run in (most) web browsers such as Netscape. |
The name of the applet is given in an <APPLET> tag in an HTML file. |
Think of a Java applet as a graphics window on which to draw text and other objects. |
The graphics window has a particular width and height as specified in the <APPLET> tag |
The origin is in the top left-hand corner of the graphics window: the x-coordinate increases from left to right, while the y-coordinate increases from top to bottom |
(0,0) |
(width,0) |
(0,height) |
(width,height) |
(x,y) |
x |
y |
(x,y) |
w |
h |
g.setColor ( purple ); |
g.fillOval ( x, y, w, h); |
Or your system may have some other way to specify compile and load in browser. |