|
CSCI A201/A597 and I210
|
These lab notes contain the code we may be writing Thursday in class.
First the Selection Sort that we developed on Tuesday.
class One {
static void sort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = i; j < a.length; j++) {
if (a[i] > a[j]) { // sorting in ascending order
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
public static void main(String[] args) {
int[] numbers = new int[args.length];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = Integer.parseInt(args[i]);
}
System.out.println("Here's the initial array: ");
One.show(numbers);
System.out.println("Let me sort it in ascending order...");
One.sort(numbers);
System.out.println("... Done\nHere it is sorted: ");
One.show(numbers);
}
static void show(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}
Here's how it runs:
To clarify the idea of scope of a variable let's look at this example:frilled.cs.indiana.edu%javac One.java frilled.cs.indiana.edu%java One 3 4 1 2 6 7 Here's the initial array: 3 4 1 2 6 7 Let me sort it in ascending order... ... Done Here it is sorted: 1 2 3 4 6 7 frilled.cs.indiana.edu%
class Two {
public static void main(String[] args) {
int a = 1;
{
int b = 2;
System.out.println("a: " + a);
a = 4; // change will be seen by the other block
System.out.println("b: " + b);
}
// System.out.println("b: " + b);
{
int b = 3;
System.out.println("a: " + a);
System.out.println("b: " + b);
}
}
}
I'll let you experiment with it. Here's a second method of sorting: Bubble Sort.
class Three {
static void sort(int[] a) {
boolean done;
do {
done = true;
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) { // sort in ascending order
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
done = false;
}
}
} while (! done);
}
static void main(String[] args) {
int[] numbers = new int[args.length];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = Integer.parseInt(args[i]);
}
System.out.println("Here's the initial array: ");
Three.show(numbers);
System.out.println("Let me sort it in ascending order...");
Three.sort(numbers);
System.out.println("... Done\nHere it is sorted: ");
Three.show(numbers);
}
static void show(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}
It runs just like the other one, but the mechanism is different.
The outer do-while loop is normally added at the end.
For this reason I find the following method much more intuitive.
class Four {
static void sort(int[] a) {
boolean done = true;
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) { // sort in ascending order
int temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
done = false;
}
}
if (! done)
sort(a);
}
static void main(String[] args) {
int[] numbers = new int[args.length];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = Integer.parseInt(args[i]);
}
System.out.println("Here's the initial array: ");
One.show(numbers);
System.out.println("Let me sort it in ascending order...");
One.sort(numbers);
System.out.println("... Done\nHere it is sorted: ");
One.show(numbers);
}
static void show(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
}
Yes, recursion, in some cases, amounts to just a loop. Can you draw a picture of this, like we did in class of Tuesday?
One could improve on the Bubble Sort technique above.
Here's an
applet that illustrates the differences between three sorting methods:
It shows how the methods work, and illustrates topics from chapter 15.
Let's move on.
We will talk about inheritance.
The basic part we need to know is this example:
class Horse {
void fun() {
System.out.println("I am a Horse.");
}
}
class Unicorn extends Horse {
void fun() {
System.out.println("I am a Unicorn. Like a horse, but with a horn.");
}
}
class Experiment {
public static void main(String[] args) {
Horse a = new Horse();
Horse b = new Unicorn();
Unicorn c = new Unicorn();
// Unicorn d = new Horse();
System.out.print("First test: ");
a.fun(); // a horse, of course
System.out.print("Second test: ");
c.fun(); // a unicorn, of course
System.out.print("Third test: ");
b.fun(); // what will this be?
}
}
Here's a picture to get the idea:
That's what you need to know to understand applets.
Now we have the following examples, for you to practice with them:
1. A Simple Applet
Create a file One.html with the following contents.
Create a file<HTML> <HEAD> <TITLE>Applet One</TITLE> </HEAD> <BODY> <P>Here is my <U>first applet</U>.</P> <APPLET CODE="One.class" WIDTH=300 HEIGHT=300> </APPLET> <P> Its title is <I>"Picasso in disbelief"</I>.</P> </BODY> </HTML>
One.java with the following contents.
import java.applet.Applet;
import java.awt.*;
public class One extends Applet {
public void paint (Graphics g) {
Graphics2D g2 = (Graphics2D)g;
Rectangle a = new Rectangle(5, 10, 20, 30);
g2.draw(a);
a.translate(15, 25);
g2.draw(a);
}
}
Compile One.java and look at One.html with
either Netscape or an appletviewer.
2. Graphical Shapes
Create a file Two.html with the following contents.
Create a file<HTML> <HEAD> <TITLE>Applet Two</TITLE> </HEAD> <BODY> <P>Here is my <U>second applet</U>.</P> <APPLET CODE="Two.class" WIDTH=300 HEIGHT=300> </APPLET> <P> Its title is <I>"Graphical Shapes"</I>.</P> </BODY> </HTML>
Two.java with the following contents.
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
public class Two extends Applet {
public void paint (Graphics g) {
Graphics2D g2 = (Graphics2D)g;
Ellipse2D.Double m = new Ellipse2D.Double(5, 10, 150, 80);
g2.draw(m); // an ellipse
int x = 90;
int y = 90;
int diam = 70;
Ellipse2D.Double q = new Ellipse2D.Double(x, y, diam, diam);
g2.draw(q); // a circle
Line2D.Double segment = new Line2D.Double(15, 150, 55, 90);
g2.draw(segment); // a line
Point2D.Double start = new Point2D.Double(20, 155);
Point2D.Double stop = new Point2D.Double(60, 95);
segment = new Line2D.Double(start, stop);
g2.draw(segment); // another line
}
}
Compile Two.java and look at Two.html with
either Netscape or an appletviewer.
3. Color and Shapes
Create a file Three.html with the following contents.
Create a file<HTML> <HEAD> <TITLE>Applet Three</TITLE> </HEAD> <BODY> <P>Here is my <U>third applet</U>.</P> <APPLET CODE="Three.class" WIDTH=300 HEIGHT=300> </APPLET> <P> Its title is <I>"Colored Graphical Shapes"</I>.</P> </BODY> </HTML>
Three.java with the following contents.
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
public class Three extends Applet {
public void paint (Graphics g) {
Graphics2D g2 = (Graphics2D)g;
Color magenta = new Color(1.0F, 0.0F, 1.0F);
g2.setColor(magenta);
Ellipse2D.Double m = new Ellipse2D.Double(5, 10, 150, 80);
g2.draw(m); // an ellipse
g2.fill(m); // fill it with the current color
// magenta is still current color here
int x = 90;
int y = 90;
int diam = 70;
g2.setColor(Color.red); // predefined color
Ellipse2D.Double q = new Ellipse2D.Double(x, y, diam, diam);
g2.draw(q); // a circle
g2.fill(q); // fill it with the current color
g2.setColor(Color.blue); // another predefined color
Line2D.Double segment = new Line2D.Double(15, 150, 55, 90);
g2.draw(segment); // a line
Point2D.Double start = new Point2D.Double(20, 155);
Point2D.Double stop = new Point2D.Double(60, 95);
segment = new Line2D.Double(start, stop);
g2.draw(segment); // another line
// current color is blue here
}
}
Compile Three.java and look at Three.html with
either Netscape or an appletviewer.
4. Text in Applets
Create a file Four.html with the following contents.
Create a file<HTML> <HEAD> <TITLE>Applet Four</TITLE> </HEAD> <BODY> <P>Here is my <U>fourth applet</U>.</P> <APPLET CODE="Four.class" WIDTH=300 HEIGHT=300> </APPLET> <P> Its title is <I>"Applet Applet"</I>.</P> </BODY> </HTML>
Four.java with the following contents.
import java.applet.Applet;
import java.awt.*;
public class Four extends Applet {
public void paint (Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.drawString("Applet", 50, 160);
final int HUGE_SIZE = 36; // fotn point size
String message = "Applet"; // actual text
// create Font object then call setFont on it
Font hugeFont = new Font("Serif", // font face name
Font.BOLD, // style
HUGE_SIZE); // point size
g2.setFont(hugeFont); // now g2 will write that way
g2.setColor(Color.green); // set color to predefined green
g2.drawString(message, 50, 100); // then write string
}
}
Compile Four.java and look at Four.html with
either Netscape or an appletviewer.
5. Simple Drawing
Create a file Five.html with the following contents.
Create a file<HTML> <HEAD> <TITLE>Applet Five</TITLE> </HEAD> <BODY> <P>Here is my <U>fourth applet</U>.</P> <APPLET CODE="Five.class" WIDTH=300 HEIGHT=300> </APPLET> <P> Its title is <I>"Happy Hour"</I>.</P> </BODY> </HTML>
Five.java with the following contents.
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
public class Five extends Applet {
public void paint (Graphics g) {
Graphics2D g2 = (Graphics2D)g;
Ellipse2D.Double e1 = new Ellipse2D.Double( 75, 40, 30, 70);
Ellipse2D.Double e2 = new Ellipse2D.Double(115, 40, 30, 70);
Ellipse2D.Double c1 = new Ellipse2D.Double( 85, 85, 15, 15);
Ellipse2D.Double c2 = new Ellipse2D.Double(125, 85, 15, 15);
Ellipse2D.Double n = new Ellipse2D.Double(55, 120, 110, 25);
Arc2D.Double m =
new Arc2D.Double(-40, -120, 300, 300, 230, 80, Arc2D.OPEN);
g2.draw(e1);
g2.draw(e2);
g2.fill(c1);
g2.fill(c2);
g2.draw(n);
g2.draw(m);
}
}
Compile Five.java and look at Five.html with
either Netscape or an appletviewer.
Lab assignment: Take a look at the pictures below.
Choose one that you like. Write an applet that draws
that picture. Then turn the code in on Tuesday with the
minute paper, in class.