Full HTML for

Basic foilset Computing and Java Language Basics

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

Table of Contents for full HTML of Computing and Java Language Basics

Denote Foils where Image Critical
Denote Foils where Image has important information
Denote Foils where HTML is sufficient
denotes presence of Additional linked information which is greyed out if missing

1 Java Academy: Language Basics
2 Part 1A: Language Basics
3 What is programming?
4 Variables and Types
5 Assignment
6 Arithmetic
7 Order of Execution
8 "For" Loops
9 Arrays
10 Using array values
11 Arrays and "For" Loops
12 Real Numbers
13 Java API of Math package Additional Mathematical Operators
14 Type Conversions
15 "if" statements
16 More on conditions

Outside Index Summary of Material



HTML version of Basic Foils prepared Feb 19 1999

Foil 1 Java Academy: Language Basics

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index
Nancy McCracken
NPAC @ SU
111 College Place
Syracuse, NY 13244-4100
Syracuse University
College of Engineering and Computer Science
Northeast Parallel Architectures Center
present

HTML version of Basic Foils prepared Feb 19 1999

Foil 2 Part 1A: Language Basics

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index
Introduction to programming
Java variables, types, and assignment
Java language statements
Arrays

HTML version of Basic Foils prepared Feb 19 1999

Foil 3 What is programming?

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index
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

HTML version of Basic Foils prepared Feb 19 1999

Foil 4 Variables and Types

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index
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;

HTML version of Basic Foils prepared Feb 19 1999

Foil 5 Assignment

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index
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++;

HTML version of Basic Foils prepared Feb 19 1999

Foil 6 Arithmetic

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index
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."

HTML version of Basic Foils prepared Feb 19 1999

Foil 7 Order of Execution

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index
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

HTML version of Basic Foils prepared Feb 19 1999

Foil 8 "For" Loops

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index
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?

HTML version of Basic Foils prepared Feb 19 1999

Foil 9 Arrays

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index
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

HTML version of Basic Foils prepared Feb 19 1999

Foil 10 Using array values

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index
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

HTML version of Basic Foils prepared Feb 19 1999

Foil 11 Arrays and "For" Loops

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index
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

HTML version of Basic Foils prepared Feb 19 1999

Foil 12 Real Numbers

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index
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 ;

HTML version of Basic Foils prepared Feb 19 1999

Foil 13 Additional Mathematical Operators

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index Java API of Math package
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 ) ;

HTML version of Basic Foils prepared Feb 19 1999

Foil 14 Type Conversions

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index
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 ) ;

HTML version of Basic Foils prepared Feb 19 1999

Foil 15 "if" statements

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index
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.

HTML version of Basic Foils prepared Feb 19 1999

Foil 16 More on conditions

From Computing and Java Language Basics NPAC Java Academy February--April 99 -- Feb 13 1999. *
Full HTML Index
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"

© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Mon Jul 5 1999