It will be annotated more later.
1. Hello, world!
Java programs are built from classes. From a class definition you can create any number of objects that are known as instances of that class, but you don't have to. In fact we won't be creating new (custom) objects until very late into this lecture. Later that will change.
We will first be interested in writing programs, sequences of staments in Java, performing actions, and we will pay only the necessary amount of attention to the structure and discipline imposed by the object oriented structure of Java.
A class can contain two kinds of members, called fields and methods. At first we will be interested mostly in methods.
Among the methods of that a class can define there's one, called
main, that will be executed when you run the class as an application.
tucotuco.cs.indiana.edu% vi Hello.java
tucotuco.cs.indiana.edu% cat Hello.java
public class Hello {
public static void main (String[] args) {
System.out.println("Hello, world!");
}
}
tucotuco.cs.indiana.edu% javac Hello.java
tucotuco.cs.indiana.edu% java Hello
Hello, world!
tucotuco.cs.indiana.edu%
2. if, for
3. Fibonaccifor (initialize; test; update) body if (expression) statement1 else statement2
This sequence is defined as:
xn+2 = xn+1 + xn (for n > 0)Here's a program that generates the first 20 elements of the sequence:x1 = 1
x2 = 1
tucotuco.cs.indiana.edu% vi Fibonacci.java
tucotuco.cs.indiana.edu% cat Fibonacci.java
public class Fibonacci {
public static void main (String[] args) {
int current, prevprev = 1, prev = 0;
for (int i = 0; i < 20; i++) {
current = prev + prevprev;
System.out.print(current + " ");
prevprev = prev;
prev = current;
}
System.out.println();
}
}
tucotuco.cs.indiana.edu% javac Fibonacci.java
tucotuco.cs.indiana.edu% java Fibonacci
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
tucotuco.cs.indiana.edu%
To mark the even numbers in the Fibonacci series we write:
tucotuco.cs.indiana.edu% vi Fibonacci.java
tucotuco.cs.indiana.edu% cat Fibonacci.java
public class Fibonacci {
public static void main (String[] args) {
int current, prevprev = 1, prev = 0;
for (int i = 0; i < 20; i++) {
current = prev + prevprev;
if (current % 2 == 0)
System.out.print("(" + current + ")" + " ");
else System.out.print(current + " ");
prevprev = prev;
prev = current;
}
System.out.println();
}
}
tucotuco.cs.indiana.edu% javac Fibonacci.java
tucotuco.cs.indiana.edu% java Fibonacci
1 1 (2) 3 5 (8) 13 21 (34) 55 89 (144) 233 377 (610) 987 1597 (2584) 4181 6765
tucotuco.cs.indiana.edu%
This program exemplies for, if, %, variables, and + for
strings (which in Perl is .) We'll continue to follow the structure of the Perl primer of lecture 3.
4. Echo
In which we get to work with Java's @ARGV (so to speak).
tucotuco.cs.indiana.edu% vi Echo.java
tucotuco.cs.indiana.edu% cat Echo.java
public class Echo {
public static void main (String[] args) {
int i=0;
while (i < args.length) {
System.out.print (args[i] + " ");
i++;
}
System.out.println();
}
}
tucotuco.cs.indiana.edu% javac Echo.java
tucotuco.cs.indiana.edu% java Echo my three arguments
my three arguments
tucotuco.cs.indiana.edu% java Echo one two three four five
one two three four five
tucotuco.cs.indiana.edu%
args is an array. args.length contains the
index of the last element of the array just as $#a contains the index
of the last element in @a, in Perl. 5. Reverse: esrever ni ohcE
In which we get to work with String's.
tucotuco.cs.indiana.edu% vi Reverse.java
tucotuco.cs.indiana.edu% cat Reverse.java
public class Reverse {
public static void main (String[] args) {
for (int i = args.length - 1; i >= 0; i--) {
for (int j=args[i].length() - 1; j >= 0; j--) {
System.out.print(args[i].charAt(j));
}
System.out.print(" ");
}
System.out.println();
}
}
tucotuco.cs.indiana.edu% javac Reverse.java
tucotuco.cs.indiana.edu% java Reverse esrever ni ohcE
Echo in reverse
tucotuco.cs.indiana.edu%
We note that the length of Strings is returned
by a method.
Accessing individual elements is done passing the index to the
object's charAt method.
6. Factorials
Just 5!
tucotuco.cs.indiana.edu% vi Factorial.java
tucotuco.cs.indiana.edu% cat Factorial.java
public class Factorial {
public static long factorial (long x) {
if (x == 0) return 1;
else return x * factorial (x - 1);
}
public static void main (String[] args) {
long val = 5;
System.out.println (
"Factorial of " + val +
" is: " + factorial(val)
);
}
}
tucotuco.cs.indiana.edu% javac Factorial.java
tucotuco.cs.indiana.edu% java Factorial
Factorial of 5 is: 120
tucotuco.cs.indiana.edu%
Now replace main with:
public static void main (String[] args) {
for (int val = 0; val < 10; val++)
System.out.println ("Factorial of " +
val + " is: " + factorial(val)
);
}
And that's going to give us more than just 5!.
For custom factorials changetucotuco.cs.indiana.edu% vi Factorial.java tucotuco.cs.indiana.edu% javac Factorial.java tucotuco.cs.indiana.edu% java Factorial Factorial of 0 is: 1 Factorial of 1 is: 1 Factorial of 2 is: 2 Factorial of 3 is: 6 Factorial of 4 is: 24 Factorial of 5 is: 120 Factorial of 6 is: 720 Factorial of 7 is: 5040 Factorial of 8 is: 40320 Factorial of 9 is: 362880 tucotuco.cs.indiana.edu%
main to:
public static void main (String[] args) {
int val = Integer.parseInt(args[0]);
System.out.println(val + "! = " + factorial(val));
}
and we have:
But what about errors?tucotuco.cs.indiana.edu% vi Factorial.java tucotuco.cs.indiana.edu% javac Factorial.java tucotuco.cs.indiana.edu% java Factorial 5 5! = 120 tucotuco.cs.indiana.edu% java Factorial 3 3! = 6 tucotuco.cs.indiana.edu%
tucotuco.cs.indiana.edu% java Factorial
java.lang.ArrayIndexOutOfBoundsException: 0
at Factorial.main(Compiled Code)
tucotuco.cs.indiana.edu% java Factorial abc
java.lang.NumberFormatException: abc
at java.lang.Throwable.(Compiled Code)
at java.lang.Exception.(Compiled Code)
at java.lang.RuntimeException.(Compiled Code)
at java.lang.IllegalArgumentException.(Compiled Code)
at java.lang.NumberFormatException.(Compiled Code)
at java.lang.Integer.parseInt(Compiled Code)
at java.lang.Integer.parseInt(Compiled Code)
at Factorial.main(Compiled Code)
tucotuco.cs.indiana.edu%
Java throws exceptions in those cases.
Exceptions can be caught with try/catch.
We could change main into:
public static void main(String[] args) {
try {
int val = Integer.parseInt(args[0]);
System.out.println(val + "! = " + factorial(val));
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("You must specify an argument");
System.out.println("Usage: java factorial <number>");
} catch (NumberFormatException e) {
System.out.println("The argument must be an integer.");
} catch (IllegalArgumentException e) {
System.out.println("Bad argument: " + e.getMessage());
}
}
And now we have:
Things to remember/revisit:tucotuco.cs.indiana.edu% vi Factorial.java tucotuco.cs.indiana.edu% javac Factorial.java tucotuco.cs.indiana.edu% java Factorial You must specify an argument Usage: java factorialtucotuco.cs.indiana.edu% java Factorial abc The argument must be an integer. tucotuco.cs.indiana.edu% java Factorial 4 4! = 24 tucotuco.cs.indiana.edu%
Integer.parseInt
ArrayIndexOutOfBoundExceptions
Java.Math.BigInteger
tucotuco.cs.indiana.edu% vi factorial.java
tucotuco.cs.indiana.edu% vi computeFactorial.java
tucotuco.cs.indiana.edu% cat factorial.java
public class factorial {
public static long factorial (long x) {
if (x == 0) return 1;
else return x * factorial (x - 1);
}
}
tucotuco.cs.indiana.edu% cat computeFactorial.java
public class computeFactorial {
public static void main (String[] args) {
int val = Integer.parseInt(args[0]);
System.out.println(val + "! = " + factorial.factorial(val));
}
}
tucotuco.cs.indiana.edu% javac computeFactorial.java
tucotuco.cs.indiana.edu% java computeFactorial 4
4! = 24
tucotuco.cs.indiana.edu%
7. Sorting numbers Two sorting algorithms that we will use later.
tucotuco.cs.indiana.edu% vi sort.java
tucotuco.cs.indiana.edu% cat sort.java
public class sort {
public static void bsort (int[] nums) {
int modified = 0;
int tmp;
do {
modified = 0;
for (int i=0; i < nums.length - 1; i++)
if (nums[i] >= nums[i+1]) {
modified = 1;
tmp = nums[i];
nums[i] = nums[i+1];
nums[i+1] = tmp;
}
for (int i=0; i < nums.length; i++)
System.out.print(nums[i] + " ");
System.out.println();
} while (modified == 1);
}
public static void ssort (int[] nums) {
for (int i=0; i < nums.length; i++) {
int min=i;
for (int j=i; j < nums.length; j++)
if (nums[j] < nums[min])
min = j;
int temp;
temp = nums[i];
nums[i] = nums[min];
nums[min] = temp;
for (int k=0; k < nums.length; k++)
System.out.print(nums[k] + " ");
System.out.println();
}
}
public static void main(String[] args) {
int[] nums = new int[10];
for (int i = 0; i < nums.length; i++) {
nums[i] = (int)(Math.random() * 100);
System.out.print(nums[i] + " ");
}
System.out.println();
if (args[0].equals("bsort")) bsort(nums);
else ssort(nums);
for (int i=0; i < nums.length; i++)
System.out.print(nums[i] + " ");
System.out.println();
}
}
tucotuco.cs.indiana.edu% javac sort.java
tucotuco.cs.indiana.edu% java sort ssort
25 51 27 50 28 94 2 84 6 41
2 51 27 50 28 94 25 84 6 41
2 6 27 50 28 94 25 84 51 41
2 6 25 50 28 94 27 84 51 41
2 6 25 27 28 94 50 84 51 41
2 6 25 27 28 94 50 84 51 41
2 6 25 27 28 41 50 84 51 94
2 6 25 27 28 41 50 84 51 94
2 6 25 27 28 41 50 51 84 94
2 6 25 27 28 41 50 51 84 94
2 6 25 27 28 41 50 51 84 94
2 6 25 27 28 41 50 51 84 94
tucotuco.cs.indiana.edu% java sort bsort
48 96 38 45 28 58 29 37 8 19
48 38 45 28 58 29 37 8 19 96
38 45 28 48 29 37 8 19 58 96
38 28 45 29 37 8 19 48 58 96
28 38 29 37 8 19 45 48 58 96
28 29 37 8 19 38 45 48 58 96
28 29 8 19 37 38 45 48 58 96
28 8 19 29 37 38 45 48 58 96
8 19 28 29 37 38 45 48 58 96
8 19 28 29 37 38 45 48 58 96
8 19 28 29 37 38 45 48 58 96
tucotuco.cs.indiana.edu%
This example shows that the two sorting methods
implement different sorting mechanisms. 8. Classes, constructors, objects, class and instance variables
9. Inheritance
10. The simplest
Scribble applet
tucotuco.cs.indiana.edu% pwd
/nfs/paca/home/user2/dgerman/httpd/htdocs
tucotuco.cs.indiana.edu% mkdir scribble
tucotuco.cs.indiana.edu% cd scribble
tucotuco.cs.indiana.edu% vi Scribble.java
tucotuco.cs.indiana.edu% cat Scribble.java
import java.applet.*;
import java.awt.*;
public class Scribble extends Applet {
private int last_x = 0, last_y = 0;
public boolean mouseDown (Event e, int x, int y) {
last_x = x; last_y = y; return true;
}
public boolean mouseDrag (Event e, int x, int y) {
Graphics g = getGraphics();
g.drawLine(last_x, last_y, x, y);
last_x = x; last_y = y; return true;
}
}
tucotuco.cs.indiana.edu% javac Scribble.java
Note: Scribble.java uses a deprecated API.
Recompile with "-deprecation" for details.
1 warning
tucotuco.cs.indiana.edu% vi scribble.html
tucotuco.cs.indiana.edu% cat scribble.html
<html><head><title>Scribble</title></head><body>
<table border> <tr> <td>
<applet code="Scribble.class" height=100 width=300>
An applet would be here if your browser...
</applet> </td> </tr> <tr> <td>
Use your mouse to scribble away in the applet above.
</td> </tr> </table>
</body></html>
tucotuco.cs.indiana.edu%
Which gives us: the Scribble
applet. ... To be continued
Assignment 3
Posted on the assignments page.