|
Spring Semester 2004 |
Here are the steps for this lab:
in
A201-1315 even if
you are taking I210 or A597.)
If something is wrong please e-mail dgerman@indiana.edu immediately.
public class One {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
One.java on the desktop.
dir One.java or something similar). Note that you need to make sure you're accessing the folder that corresponds to the desktop.
javac One.java)
dir One.class)
java One)
.java)
.java file with javac (a .class gets created)
.class file) with java
One.java as follows:
public class One {
public static void main(String[] args) {
System.out.println("Hi, there!");
}
}
Note that the part that changed is in blue.
One once again. What's the outcome?
public class One { public static void main(String[]
args) { System.out.println("Hi, there!"); } }
Notice we only change the amount of existing blank space, nothing else.
public
class
One
{
public
static
void
main
(
String
[
]
args
)
{
System.out.println("Hi, there!");
}
}
public class One {
public static void main(String[] args) {
System.out.println("Hello, world!");
System.out.println("Hello, again!");
}
}
public class One {
public static void main(String[] args) {
System.out.println(" again!");
System.out.println("Hello, ");
}
}
public class One {
public static void main(String[] args) {
System.out.println(" are ");
System.out.println("How ");
System.out.println(" you?");
}
}
4
4
4
4 4
44444444
4
4
It's a big "4" made out of little "4"'s. (See also exercise 7 on page 141).
public class One {
public static void main(String[] args) {
System.out.println(1 + 2 + 3);
}
}
public class One {
public static void main(String[] args) {
System.out.println(1 + 2 * 3);
}
}
public class One {
public static void main(String[] args) {
System.out.println((1 + 2) * 3);
}
}
public class One {
public static void main(String[] args) {
System.out.println("Hello, D'Artagnan!");
}
}
We can print single quotes quite easily.
public class One {
public static void main(String[] args) {
System.out.println("Hello, "D'Artagnan"!");
}
}
Compile and run this program. What happens?
public class One {
public static void main(String[] args) {
System.out.println("Hello, \"D'Artagnan\"!");
}
}
Explain the outcome.
public class One {
public static void main(String[] args) {
System.out.println("Hello, \nworld!");
}
}
Explain what it does.
4
4
4
4 4
44444444
4
4
Can you write that program with just one System.out.println?
"
"
"
" "
""""""""
"
"
public class One {
public static void main(String[] args) {
System.out.print("Hello, ");
System.out.print("world!");
}
}
public class One {
public static void main(String[] args) {
System.out.print("A");
System.out.print("B\n");
System.out.print("C");
System.out.print("D");
System.out.print("E\n");
}
}
(Try to make some sense of it, but don't despair if some parts seem incomprehensible).
.8. 8 888888888o ,o888888o.
.888. 8 8888 `88. 8888 `88.
:88888. 8 8888 `88 ,8 8888 `8.
. `88888. 8 8888 ,88 88 8888
.8. `88888. 8 8888. ,88' 88 8888
.8`8. `88888. 8 8888888888 88 8888
.8' `8. `88888. 8 8888 `88. 88 8888
.8' `8. `88888. 8 8888 88 `8 8888 .8'
.888888888. `88888. 8 8888 ,88' 8888 ,88'
.8' `8. `88888. 8 888888888P `8888888P'
You don't need to write this program, just think whether you can write it or not.
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
public class SliderTest
{
public static void main(String[] args)
{
SliderFrame frame = new SliderFrame();
frame.setTitle("SliderTest");
frame.show();
}
}
class SliderFrame extends JFrame
{
public SliderFrame()
{
final int DEFAULT_FRAME_WIDTH = 300;
final int DEFAULT_FRAME_HEIGHT = 300;
setSize(DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT);
addWindowListener(new WindowCloser());
colorPanel = new JPanel();
ColorListener listener = new ColorListener();
redSlider = new JSlider(0, 100, 100);
redSlider.addChangeListener(listener);
greenSlider = new JSlider(0, 100, 70);
greenSlider.addChangeListener(listener);
blueSlider = new JSlider(0, 100, 100);
blueSlider.addChangeListener(listener);
JPanel southPanel = new JPanel();
southPanel.setLayout(new GridLayout(3, 2));
southPanel.add(new JLabel("Red", SwingConstants.RIGHT));
southPanel.add(redSlider);
southPanel.add(new JLabel("Green", SwingConstants.RIGHT));
southPanel.add(greenSlider);
southPanel.add(new JLabel("Blue", SwingConstants.RIGHT));
southPanel.add(blueSlider);
Container contentPane = getContentPane();
contentPane.add(colorPanel, "Center");
contentPane.add(southPanel, "South");
setSampleColor();
}
public void setSampleColor()
{
float red = 0.01F * redSlider.getValue();
float green = 0.01F * greenSlider.getValue();
float blue = 0.01F * blueSlider.getValue();
colorPanel.setBackground(new Color(red, green, blue));
colorPanel.repaint();
}
private JPanel colorPanel;
private JSlider redSlider;
private JSlider greenSlider;
private JSlider blueSlider;
private class ColorListener implements ChangeListener
{
public void stateChanged(ChangeEvent event)
{
setSampleColor();
}
}
private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
}
}
You don't need to understand this program, but you should
have no problem creating, compiling, and running it. And
when you see it running you should feel happy about it!
The goal for
this semester's work is to understand thoroughly a program such as this.
(See http://www.cs.indiana.edu/classes/a201-dger/spr2001/labs/nine/alienU.html)
A second large program will be discussed, you can work with it
here.
(See http://www.cs.indiana.edu/classes/a348/t540/lectures/iceblox/iceblox.html)
A201/A597 LAB ASSIGNMENT ONE
There are two parts to this first lab assignment:
First, write a program that prints a staircase:
+--+
| |
+--+--+
| | |
+--+--+--+
| | | |
+--+--+--+--+
| | | | |
+--+--+--+--+ Show it to your AI next lab (Thursday).
Second, write a program as described below.
In your program:
Penguin, and add it to the room in the 8th line and 3rd column.
(7, 2) given our the numbering convention.
(2, 2).
(2, 7).
(7, 7).
(2, 7).
(7, 2), passing through (2, 2).
What you will need (essentially) two things:
Penguin.java (which you can find below):
import java.awt.*;
public class Penguin extends Thread {
ActionList action;
public void run() {
while (true) {
perform(action.get());
}
}
private void _pause() {
try {
this.sleep(speed * 10);
} catch (InterruptedException e) { }
}
private void _think() {
try {
this.sleep(100);
} catch (InterruptedException e) { }
}
private void _turnLeft() {
_think();
if (direction.equals("south")) {
direction = "east";
look = 12;
} else if (direction.equals("east")) {
direction = "north";
look = 5;
} else if (direction.equals("north")) {
direction = "west";
look = 7;
} else {
direction = "south";
look = 2;
}
report();
}
private void _wave() {
for (int i = 0; i < 4; i++) {
_think(); look = 0; report();
_think(); look = 39; report();
}
_think(); look = 0; report();
}
private void _happy() {
_think();
if (direction.equals("south")) {
_wave(); _think(); look = 2; report();
} else if (direction.equals("east")) {
look = 2; _wave(); _think(); look = 12; report();
} else if (direction.equals("north")) {
look = 7; report(); _think();
look = 2; report(); _wave(); _think();
look = 7; report(); _think();
look = 5; report(); _think();
} else { // west
look = 2; report(); _wave();
look = 7; report(); _think();
}
}
private void _turnRight() {
_think();
if (direction.equals("south")) {
direction = "west";
look = 7;
} else if (direction.equals("east")) {
direction = "south";
look = 2;
} else if (direction.equals("north")) {
direction = "east";
look = 12;
} else {
direction = "north";
look = 5;
}
report();
}
void _moveForward() {
for (int i = 0; i < 5; i++) {
_think();
if (direction.equals("south")) {
y += dy;
look = animP[12 + (i + 1) % 4];
} else if (direction.equals("east")) {
x += dx;
look = animP[ 4 + (i + 2) % 4];
} else if (direction.equals("north")) {
y -= dy;
look = animP[ 8 + (i + 1) % 4];
} else { // west
x -= dx;
look = animP[ 0 + i % 4];
}
report();
}
}
public void perform(String action) {
if (action.equals("turnLeft")) {
this._turnLeft();
} else if (action.equals("turnRight")) {
this._turnRight();
} else if (action.equals("moveForward")) {
this._moveForward();
} else if (action.equals("backwards")) {
_turnLeft();
_turnLeft();
_moveForward();
_turnRight();
_turnRight();
} else if (action.equals("pause")) {
this._pause();
} else if (action.equals("think")) {
this._think();
} else if (action.equals("happy")) {
this._happy();
} else System.out.println("Don't understand " + action);
}
void turnRight() {
action.put("turnRight");
}
void moveForward() {
action.put("moveForward");
}
void wave() { action.put("wave"); }
void pause() { action.put("pause"); }
void think() { action.put("think"); }
void happy() { action.put("happy"); }
int animP[] = { 7, 8, 9, 8, // left ( west)
10, 11, 12, 11, // right ( east)
4, 5, 6, 5, // up (north)
1, 2, 3, 2 // down (south)
};
Rink location;
void placeIn(Rink placement, int x, int y) {
this.location = placement;
frames = this.location.small;
this.x = x * 30; this.y = y * 30;
this.report();
}
Image[] frames;
void report() { location.repaint(); }
int x, y, dx = 6, dy = 6, look = 2;
void draw(Graphics g) {
g.drawImage(frames[look], x, y, location);
}
int speed = 100;
Penguin() {
action = new ActionList();
this.speed = 100;
}
void turnLeft() {
action.put("turnLeft");
}
String direction = "south";
}
Rink.java (which represents the theater where all this happens)
/*
<applet code="Rink.class" width=300 height=300>
<param name="columns" value= "10">
<param name="rows" value= "10">
<param name="penguin" value="yes">
<param name="pengo_x" value= "1">
<param name="pengo_y" value= "7">
</applet>
*/
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.awt.image.*;
import java.awt.event.*;
public class Rink extends NoFlickerApplet implements KeyListener {
int columns, rows;
Thread animation;
Image small[];
int cellWidth = 30, cellHeight = 30;
public Rink() { }
public Rink (int columns, int rows) {
this.columns = columns;
this.rows = rows;
}
int wide, tall;
public void init() {
if (columns == 0)
this.columns = Integer.parseInt(this.getParameter("columns"));
if (rows == 0)
this.rows = Integer.parseInt(this.getParameter("rows"));
this.wide = columns * cellWidth + cellWidth / 2;
this.tall = (1 + rows) * cellHeight + cellHeight / 2;
this.setSize(this.wide, this.tall);
String pictureURL = "http://www.cs.indiana.edu/classes/a348/CT" +
"ED/moduleFour/lectures/iceblox/iceblox.gif";
MediaTracker tracker = new MediaTracker(this);
Image collection;
try {
collection =
Toolkit.getDefaultToolkit().getImage(new URL(pictureURL));
} catch (Exception e) {
collection = Toolkit.getDefaultToolkit().getImage("iceblox.gif");
}
tracker.addImage(collection, 0);
try {
tracker.waitForID(0);
} catch (InterruptedException e) { }
ImageProducer collectionProducer = collection.getSource();
int smalls = 48;
small = new Image[smalls];
int k = 0, i = 0, j = 0;
ImageFilter filter;
while (k < smalls) {
filter = new CropImageFilter(j * 30, i * 30, 30, 30);
small[k] = createImage(
new FilteredImageSource(
collectionProducer, filter));
tracker.addImage(small[k], 1);
k++; j++;
if (j == 8) {
j = 0; i++;
}
}
try {
tracker.waitForID(1);
} catch (InterruptedException e) { }
if (this.getParameter("penguin").equals("yes")) {
this.add(new Penguin(),
Integer.parseInt(this.getParameter("pengo_x")),
Integer.parseInt(this.getParameter("pengo_y")));
}
this.addKeyListener(this);
}
int fontSize = 10; // in pixels
Font digitsFont = new Font("Serif", Font.PLAIN, fontSize);
Penguin skater;
void add(Penguin p, int x, int y) {
this.skater = p;
p.placeIn(this, x, y);
skater.start();
}
public void paint(Graphics g) {
((Graphics2D)g).setFont(digitsFont);
g.setColor(Color.black);
g.fillRect(0, 0, columns * cellWidth, rows * cellHeight);
g.setColor(Color.gray);
for (int i = 0; i <= rows; i++) {
g.drawLine(0, i * cellHeight, columns * cellWidth, i * cellHeight);
}
for (int i = 0; i <= columns; i++) {
g.drawLine(i * cellWidth, 0, i * cellWidth, rows * cellHeight);
}
g.drawRect(0, 0, cellWidth * columns, cellHeight * rows);
for (int j = 0; j < columns; j = j + 1)
for (int i = 0; i < rows; i++)
g.drawString(i + ", " + j,
j * cellWidth + 2, i * cellHeight + fontSize);
if (skater != null) {
int x = skater.x,
y = skater.y;
g.setColor(Color.black);
g.fillRect(x, y, 31, 31);
skater.draw(g);
}
}
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_L: // left
// System.out.println("left");
skater.action.put("turnLeft");
break;
case KeyEvent.VK_F: // forward
// System.out.println("forward");
skater.action.put("moveForward");
break;
case KeyEvent.VK_R: // right
// System.out.println("right");
skater.action.put("turnRight");
break;
case KeyEvent.VK_B: // backwards
// System.out.println("back");
skater.action.put("backwards");
break;
}
}
public void keyReleased(KeyEvent e) { }
public void keyTyped(KeyEvent e) { }
}
Here's all the code (including the two classes above) that you need:
-rw-r--r-- 1 dgerman faculty 473 Nov 1 07:59 ActionList.java -rw-r--r-- 1 dgerman faculty 2376 Nov 1 07:59 AppletFrame.java -rw-r--r-- 1 dgerman faculty 854 Nov 1 07:59 NoFlickerApplet.java -rw-r--r-- 1 dgerman faculty 3987 Nov 1 07:59 Penguin.java -rw-r--r-- 1 dgerman faculty 4016 Nov 1 07:59 Rink.java
Once you have these five classes
LabOne.java) and
main method perform what the lab assignment is asking for (which is described above).
Here are two examples, to make sure we have enough to start from.
EXAMPLE ONE: What happy() Really Means (One.java)
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
import java.io.*;
public class One {
public static void main(String[] args) {
Rink ballroom = new Rink(10, // number of columns
10); // number of rows
/* Note the Rink created is called 'ballroom'. We'll
have to use this name to refer to it thereafter.*/
new AppletFrame(ballroom); // ask me why you need this...
Penguin p = new Penguin(); // create a Penguin, call it
// ... 'p' (what's in a name?)
ballroom.add(p, 1, 7); // add the Penguin to our Rink, in
// column 1 and line 7 (and remember our numbering scheme)
p.pause(); p.turnLeft(); // control the Penguin
/* Remember 'The Wrong Trousers' (the video)? */
p.moveForward();
p.moveForward();
p.moveForward();
p.happy();
p.moveForward();
p.moveForward();
p.moveForward();
p.pause();
p.turnLeft();
p.pause(); // commands are issued in sequence
p.moveForward(); p.moveForward(); p.moveForward(); p.happy();
p.moveForward(); p.moveForward(); p.moveForward(); p.pause();
p.turnLeft(); p.pause();
p.moveForward(); p.moveForward(); p.moveForward(); p.happy();
p.moveForward(); p.moveForward(); p.moveForward(); p.pause();
p.turnLeft(); p.pause();
p.moveForward(); p.moveForward(); p.moveForward(); p.happy();
p.moveForward(); p.moveForward(); p.moveForward(); p.pause(); p.turnLeft();
p.pause(); p.moveForward(); p.turnRight(); p.moveForward(); p.turnLeft(); p.
moveForward(); p.moveForward(); p.turnLeft(); p.turnLeft(); p.moveForward();
p.moveForward(); p.turnLeft(); p.turnLeft(); p.moveForward(); p.moveForward();
p.turnLeft(); p.turnLeft(); p.moveForward(); p.moveForward(); p.turnLeft();
p.turnLeft(); p.moveForward(); p.moveForward(); p.turnLeft(); p.turnLeft();
/* can you still say you know where the Penguin is right now?
Remember that not only the computer reads your programs!
Write your programs as if they were essays.
Make your code crystal clear.
*/
p.moveForward();
p.moveForward();
p.turnLeft();
p.happy();
}
}
(This really clarifies what the Penguin
does when you're asking it to show that it's happy.)
EXAMPLE TWO: Staircase Improvisation (Dance.java)
class Dance {
public static void main(String[] args) {
Rink ballroom = new Rink(6, 6);
new AppletFrame(ballroom); // again, ask me why you need this...
Penguin p = new Penguin();
ballroom.add(p, 1, 4);
p.pause();
p.turnLeft();
p.turnLeft(); p.moveForward(); // go left
p.turnRight(); p.moveForward(); // go right
p.turnLeft(); p.moveForward(); // go left
p.turnRight(); p.moveForward(); // go right
p.turnLeft(); p.moveForward(); // go left
p.turnRight(); p.moveForward(); // go right
// now stop, rotate once, stay some more
p.pause(); p.turnRight(); p.pause();
// come south three tiles
p.moveForward(); p.moveForward(); p.moveForward();
// stop and catch your breath
p.pause();
// pirouette
p.turnLeft(); p.turnLeft(); p.turnLeft(); p.turnLeft();
// stop, for applause
p.pause();
// another pirouette, followed by immediate movement west
p.turnRight(); p.turnRight(); p.turnRight(); p.turnRight();
p.turnRight(); p.moveForward(); p.moveForward(); p.moveForward();
// stop
p.pause();
// turn left, then stop
p.turnLeft();
p.pause();
// one final pirouette, after which just thank the audience
p.turnLeft(); p.turnLeft(); p.turnLeft(); p.turnLeft();
p.happy();
// Don't worry(), be happy().
}
}
(This was a smaller program). As a reminder, please note.
A Penguin:
Rink at a certain location (you have to
ask the rink for that, though)
Rink:
add
in that order, in the method add.
Note though that when the Rink shows, it labels the cells by first printing
the line, then the column, for each of the tile. The reason this numbering is
also important is because it is the numbering used in 2 dimensional arrays in
Java (of the kind we will encounter a bit later).
Note that x and y still keep the meaning that they
originally had:
x is the number of columns to the left, and
y is the number of lines above (to the top)
When we create the Rink, and when we add a Penguin to it, we
mention x first, and y next, almost
as we do in analytical geometry. However when we refer to the
table of cells that the Rink is, we can also denote the cells
in the array by printing (y, x), that is,
by specifying the line first, and the column next. The point
being that both notations are well-established, and we need
to be aware of them both.
You should now write the program with no problems.
And here's a picture of Wallace and Gromit (the Aardman Penguin is pictured in disguise at the beginning of the assignment) from The Wrong Trousers to remind you of it, and for motivation.

Here also is a picture taken during Dance.java
to know what to expect of it:
