Man, this class is pretty!

Lecture Notes One: Java Fundamentals

This will be the best class ever.

frilled.cs.indiana.edu%pwd
/nfs/moose/home/user3/dgerman/t540/One
frilled.cs.indiana.edu%cat Lamp.java
class Lamp {
    private boolean lampIsOn; 
    public void turnKnob() {
	lampIsOn = !lampIsOn; 
    }
    public Lamp() {
	lampIsOn = false; 
    }
    public static void main(String[] args) {
	Lamp lamp; 
	lamp = new Lamp(); 
	Lamp lamp1  = new Lamp(); 
	Lamp lamp2  = new Lamp(); 
	Lamp lamp3  = new Lamp(); 
	Lamp lamp4  = new Lamp(); 
	Lamp lamp5  = new Lamp(); 
	Lamp lamp6  = new Lamp(); 
	Lamp lamp7  = new Lamp(); 
	Lamp lamp8  = new Lamp(); 
	Lamp lamp9  = new Lamp(); 
	Lamp lamp10 = new Lamp(); 
	Lamp lamp11 = new Lamp(); 
	Lamp lamp12 = new Lamp(); 
	Lamp lamp13 = new Lamp(); 
	Lamp lamp14 = new Lamp(); 
	Lamp lamp15 = new Lamp(); 
	lamp.turnKnob(); 
	lamp13.turnKnob(); 
        lamp7.lampIsOn = true; // violates private visibility?
    }
}
frilled.cs.indiana.edu%javac Lamp.java
frilled.cs.indiana.edu%java Lamp
frilled.cs.indiana.edu%
Let's add some code, make a change, to the code above.
frilled.cs.indiana.edu%pwd
/nfs/moose/home/user3/dgerman/t540/One
frilled.cs.indiana.edu%cat Lamp.java
class Lamp {
    private boolean lampIsOn; 
    public void turnKnob() {
	lampIsOn = !lampIsOn; 
    }
    public Lamp() {
	lampIsOn = false; 
    }
    public void show() {
	System.out.println("Lamp is on: " + lampIsOn); 
    }
}
frilled.cs.indiana.edu%cat StrobeLamp.java
class StrobeLamp extends Lamp {
    private int strobeRate; 
    public void setStrobeRate(int s) {
	strobeRate = s; 
    }
}

frilled.cs.indiana.edu%cat One.java
class One {
    public static void main(String[] args) {
	StrobeLamp strobeLamp = new StrobeLamp(); 
	strobeLamp.show(); 
	strobeLamp.turnKnob(); 
        strobeLamp.show(); 
	strobeLamp.turnKnob(); 
	strobeLamp.show(); 
    }
}
frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
Lamp is on: false
Lamp is on: true
Lamp is on: false
frilled.cs.indiana.edu%
We need to explain, summarize this.

Finish, describe the following exercise.

frilled.cs.indiana.edu%pwd
/nfs/moose/home/user3/dgerman/t540/One
frilled.cs.indiana.edu%ls -ld *.java
-rw-------   1 dgerman        55 Nov  4 00:29 ColoredLamp.java
-rw-------   1 dgerman       231 Nov  3 23:57 Lamp.java
-rw-------   1 dgerman       300 Nov  4 00:30 One.java
-rw-------   1 dgerman       127 Nov  3 23:58 StrobeLamp.java
frilled.cs.indiana.edu%cat *.java
class ColoredLamp extends Lamp {
    String color; 

}
class Lamp {
    private boolean lampIsOn; 
    public void turnKnob() {
	lampIsOn = !lampIsOn; 
    }
    public Lamp() {
	lampIsOn = false; 
    }
    public void show() {
	System.out.println("Lamp is on: " + lampIsOn); 
    }
}
class One {
    public static void main(String[] args) {
	StrobeLamp strobeLamp = new StrobeLamp(); 
	strobeLamp.show(); 
	strobeLamp.turnKnob(); 
        strobeLamp.show(); 
	ColoredLamp coloredLamp = new ColoredLamp(); 
	coloredLamp.show(); 
	coloredLamp.turnKnob(); 
	coloredLamp.show(); 
    }
}
class StrobeLamp extends Lamp {
    private int strobeRate; 
    public void setStrobeRate(int s) {
	strobeRate = s; 
    }
}

frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%java One
Lamp is on: false
Lamp is on: true
Lamp is on: false
Lamp is on: true
frilled.cs.indiana.edu%
We also need to talk about constructors, then we move on.

frilled.cs.indiana.edu%pwd
/nfs/moose/home/user3/dgerman/t540/One
frilled.cs.indiana.edu%ls -ld Prim*.java
-rw-------   1 dgerman       584 Nov  4 00:40 PrimitiveTypes.java
frilled.cs.indiana.edu%cat Prim*.java
class PrimitiveTypes {
    byte b = 37;
    short s = 7; 
    int i = 2013; 
    long l = 200213L; 

    float f = 232.42f;
    double d = 177.77;

    boolean t = true; 
    boolean ugh = false; 

    char c = 'J'; 

    public static void main(String[] args) {
	PrimitiveTypes a = new PrimitiveTypes(); 
	System.out.println(a.b); 
	System.out.println(a.s); 
	System.out.println(a.i); 
	System.out.println(a.l); 
	System.out.println(a.f); 
	System.out.println(a.d); 
	System.out.println(a.t); 
	System.out.println(a.f); 
	System.out.println(a.b); 
	System.out.println(a.c); 
    }
}
frilled.cs.indiana.edu%javac Prim*.java
frilled.cs.indiana.edu%java PrimitiveTypes
37
7
2013
200213
232.42
177.77
true
232.42
37
J
frilled.cs.indiana.edu%
One could create arrays of such variables, arrays are actually of reference type, objects.

frilled.cs.indiana.edu%pwd
/nfs/moose/home/user3/dgerman/t540/One
frilled.cs.indiana.edu%ls -ld Foo* Obj*
-rw-------   1 dgerman       189 Nov  4 00:57 Foo.java
-rw-------   1 dgerman       262 Nov  4 00:57 Objects.java
frilled.cs.indiana.edu%cat Foo* Obj*
class Foo {
    int x = 0; 
    int add(int a) {
	x += a; 
        return x; 
    }
    static float y;
    static void minus() {
	y -= 1; 
    }
    public Foo(float z) {
	y = z; 
    }
}
class Objects {
    public static void main(String[] args) {
	Foo f;
	f = new Foo(13.0f); 
	f.x = 13;
	Foo.y = 17.0f;
	f.add(3); 
	Foo.minus(); 

        Foo g = new Foo((float)Constants.PI); 
    }
}

class Constants {
    static final double PI = 3.141592; 
}
frilled.cs.indiana.edu%javac Objects.java
frilled.cs.indiana.edu%java Objects
frilled.cs.indiana.edu%
That covered casting too, and methods, of all types.

frilled.cs.indiana.edu%pwd
/nfs/moose/home/user3/dgerman/t540/One
frilled.cs.indiana.edu%ls -ld Contr*
-rw-------   1 dgerman       873 Nov  4 12:36 ControlFlow.java
frilled.cs.indiana.edu%cat ControlFlow.java
class ControlFlow {
    public static void main(String[] args) {

	for (int i = 0; i < 17; i++) {
	    System.out.println(i); 
	}

	char charArray[] = { 'r', 'i', 'a', 'I', 'n'};
	int j = 0; 
	while (j < charArray.length) {
	    System.out.println(charArray[j++]); 
	}

	int k = 3; 
	do {
	    System.out.println(k); 
	    k--; 
	} while (k != 0); 

	int s = 13; 
	switch (s) {
	case 3: 
	    System.out.println("case 3"); 
	    break; 
	case 13: 
	    System.out.println("case 13"); 
	    break; 
	default: 
	    System.out.println("default case"); 
	    break; 
	}

	if (j - s == 17) { 
	    System.out.println("Hello"); 
	} else {
	    System.out.println("Bye"); 
	}

    find: 
	for (int i = 0; i < 13; i++) {
	    for (int l = 0; l < 17; l++) {
		if (i * l == 1713) {
		    System.out.println(i + " * " + l + " = " + (i * l));
		    break find; 
		}
	    }
	}
    }
}
frilled.cs.indiana.edu%javac ControlFlow.java
frilled.cs.indiana.edu%java ControlFlow
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
r
i
a
I
n
3
2
1
case 13
Bye
frilled.cs.indiana.edu%
And these were Java's control structures.

Now, let's take a look at exceptions. Threads will come later.

frilled.cs.indiana.edu%pwd
/nfs/moose/home/user3/dgerman/t540/One
frilled.cs.indiana.edu%ls -ld Kush*
-rw-------   1 dgerman      1066 Nov  4 13:18 Kushakov.java
frilled.cs.indiana.edu%cat Kushakov.java
class Kushakov {
    public static void main(String[] args) {
	int i = 6, j = 0, k;
 
	System.out.println("\nThe Carpenter Kushakov\n"); 

	try {
	    k = i / j; 
	} catch (Exception e) {
	    System.out.println("1. Once upon a time there lived a " + 
                               "carpenter.\n");
	} finally {
	    System.out.println("2. His name was Kushakov.\n"); 
	}
	System.out.println("3. Once he went out of his house and went to \n" + 
                           "a store to buy carpenter's glue.\n"); 
	j += 1; 
	try {
	    k = i / j; 
	} catch (Exception e) {
	    System.out.println("4. There was a thaw and the street was \n" + 
                               "very slippery.\n"); 
	} finally {
	    System.out.println("5. The carpenter took a few steps, slipped, " +
                               "\nfell, and broke his forehead.\n"); 
	}
	System.out.println("6. \"Ugh,\" said the carpenter, got up, went to " +
                           "\nthe drugstore, bought a bandage, and fixed up " +
                           "\nhis forehead.\n"); 
    }
}
frilled.cs.indiana.edu%javac Kushakov.java
frilled.cs.indiana.edu%java Kushakov

The Carpenter Kushakov

1. Once upon a time there lived a carpenter.

2. His name was Kushakov.

3. Once he went out of his house and went to 
a store to buy carpenter's glue.

5. The carpenter took a few steps, slipped, 
fell, and broke his forehead.

6. "Ugh," said the carpenter, got up, went to 
the drugstore, bought a bandage, and fixed up 
his forehead.

frilled.cs.indiana.edu%
Primitive and reference types, and parameter passing.

frilled.cs.indiana.edu%pwd
/nfs/moose/home/user3/dgerman/t540/One
frilled.cs.indiana.edu%ls -ld Param*
-rw-------   1 dgerman       331 Nov  4 13:34 ParamTest.java
frilled.cs.indiana.edu%cat Param*.java
class ParamTest {

    static void alpha(int beta) { beta++; } 

    static void gamma(int[] delta) { delta[0] = 1; }

    public static void main(String[] argv) {
	int x; 
	x = 3; 
	alpha(x); 
	System.out.println("x = " + x); 
	int y[]; 
	y = new int[12]; 
	y[0] = 17;
	gamma(y); 
	System.out.println("y[0] = " + y[0]); 
    }

}
frilled.cs.indiana.edu%javac ParamTest.java
frilled.cs.indiana.edu%java ParamTest
x = 3
y[0] = 1
frilled.cs.indiana.edu%
Reference types use pointers, basically. Here's a circular list, handled with care.

frilled.cs.indiana.edu%pwd
/nfs/moose/home/user3/dgerman/t540/One
frilled.cs.indiana.edu%ls -ld Circ*
-rw-------   1 dgerman       596 Nov  4 13:50 CircularList.java
frilled.cs.indiana.edu%javac CircularList.java
CircularList.java:31: next has private access in Node
	    roadRunner = roadRunner.next; 
                                   ^
1 error
frilled.cs.indiana.edu%clear

frilled.cs.indiana.edu%rm *~ *.class
rm: remove Node.class (yes/no)? y
frilled.cs.indiana.edu%clear

frilled.cs.indiana.edu%pwd
/nfs/moose/home/user3/dgerman/t540/One
frilled.cs.indiana.edu%ls -ld Circ*
-rw-------   1 dgerman       598 Nov  4 13:52 CircularList.java
frilled.cs.indiana.edu%cat Circ*.java
class Node { 
    private int value;
    private Node next; 
    public Node(int i) {
	value = i; 
	next = null;
    }
    public void print() {
	System.out.println(value); 
    }
    public Node next() {
	return next; 
    }
    public void setNext(Node n) {
	next = n; 
    }
}

class CircularList {
    public static void main(String[] args) {
	Node a, b, c; 
	a = new Node(1); 
	b = new Node(2); 
	c = new Node(3); 
	a.setNext(b); 
	b.setNext(c); 
	c.setNext(a); 
	Node roadRunner = a; 
	do {
	    roadRunner.print(); 
	    roadRunner = roadRunner.next(); 
	} while (roadRunner != a); 
    }
}
frilled.cs.indiana.edu%javac Circ*.java
frilled.cs.indiana.edu%java CircularList
1
2
3
frilled.cs.indiana.edu%
Now we have to make a committment. Applets are next.

frilled.cs.indiana.edu%pwd
/nfs/grouchy/home/user2/www/classes/a348-dger/t540/lectures/one
frilled.cs.indiana.edu%ls -l 
total 2
-rw-r--r--   1 dgerman       113 Nov  4 14:53 One.html
-rw-r--r--   1 dgerman       643 Nov  4 14:58 One.java
frilled.cs.indiana.edu%cat One.html
<html>
  <body bgcolor=white>
    <applet code="One.class" width=220 height=220>
    </applet>
  </body>
</html>
frilled.cs.indiana.edu%cat One.java
import java.awt.*;
import java.applet.*; 

public class One extends Applet {

    public void init   () { System.out.println("Initializing..."); }
    public void start  () { System.out.println("Starting...");     }
    public void stop   () { System.out.println("Stopping...");     }
    public void destroy() { System.out.println("Destroying...");   }

    public void paint(Graphics g) {

	g.drawLine(17, 17, 273, 273); 
	g.drawLine(17, 18, 273, 274); 

	g.setColor(Color.blue);
	g.fillRect(30, 130, 130, 130); 

	g.setColor(Color.red); 
	g.fillOval(60, 70, 60, 60); 

	g.setColor(Color.yellow); 
	g.fillOval(100, 50, 130, 130); 
    }

}

frilled.cs.indiana.edu%javac One.java
frilled.cs.indiana.edu%
To see this in action, we can load it here.

Let's write another applet. For this we need a background.

So let's speak a bit about Pieter Cornelis Mondriaan (1872-1944)

In the early 1900s many artists tried various abstract ways of representing reality. Mondrian went beyond them. In his final compositions he avoided any suggestion of reproducing the material world. Instead using horizontal and vertical black lines that outline blocks of pure white, red, blue or yellow, he expressed his conception of ultimate harmony and equilibrium.

Mondrian lived in Paris from 1919 to 1938. He moved to London in 1938 and left there for New York in 1940. His works were admired by other artists, but did not sell. His final painting, called "Victory Boogie Woogie", was still unfinished when he died in New York City on February 1, 1944.

Mondrian never met Kushakov.

frilled.cs.indiana.edu%pwd
/nfs/grouchy/home/user2/www/classes/a348-dger/t540/lectures/one
frilled.cs.indiana.edu%ls -ld Mond*.java Two*
-rw-r--r--   1 dgerman       873 Nov  4 15:44 Mondrian.java
-rw-r--r--   1 dgerman       158 Nov  4 15:37 Two.html
frilled.cs.indiana.edu%cat Two.html
<html>
  <head><title>Mondrian</title></head> 
  <body bgcolor=white>
    <applet code="Mondrian.class" width=300 height=300>
    </applet>
  </body>
</html>
frilled.cs.indiana.edu%cat Mondr*.java
import java.applet.*;
import java.awt.*;

public class Mondrian extends Applet {
    
    public void init() {
	System.out.println("Initializing... ");
	setBackground(Color.black);    
    }
    public void start() {
	System.out.println("Starting... ");
    }
    public void paint(Graphics g) {
	System.out.println("Painting... ");

	g.setColor(Color.yellow);
	g.fillRect(0,0,90,90);
	g.fillRect(250,0,40,190);
	g.fillRect(80,110,100,20);
	
	g.setColor(Color.blue);
	g.fillRect(80,200,220,90);
	g.fillRect(100,10,90,80);
	
	g.setColor(Color.lightGray);
	g.fillRect(80,100,110,90);
	
	g.setColor(Color.red);
	g.fillRect(200,0,45,45);
	g.fillRect(0,100,70,200);
	
	g.setColor(Color.magenta);
	g.fillRect(200,55,60,135);
    }
    public void stop() {	
	System.out.println("Stopping... ");
    }
    public void destroy() {	
	System.out.println("Destroying... ");
    }   
}
frilled.cs.indiana.edu%javac Mondr*.java
frilled.cs.indiana.edu%ls -ld Mond*
-rw-r--r--   1 dgerman      1250 Nov  4 15:52 Mondrian.class
-rw-r--r--   1 dgerman       873 Nov  4 15:44 Mondrian.java
frilled.cs.indiana.edu%
To see it, we can include it in this document.

How many yellow rectangles did we paint? How many do you see?

With the next lecture we'll boogie the invisible one into view. See you next time.


Last updated: Nov 4, 2001 by Adrian German for A348/A548