![]() |
![]() |
Here are the steps for this lab:
A201-4256
).
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:
Note that the part that changed is inpublic class One { public static void main(String[] args) { System.out.println("Hi, there!"); } }
blue
.
One
once again. What's the outcome?
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("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?"); } }
It's a big "4" made out of little "4"'s. (See also exercise P1.2 in the book).4 4 4 4 4 44444444 4 4
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); } }
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, \"D'Artagnan\"!"); } }
public class One { public static void main(String[] args) { System.out.println("Hello, \nworld!"); } }
Explain what it does.
Can you write that program with just one4 4 4 4 4 44444444 4 4
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"); } }
You don't need to write this program, just think whether you can write it or not..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 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!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); } } }
Consider the Robot Problem described in the lecture. Then take the following Java code and place it in a file calledNote: in the example below I provide a sampleRoom.java
then write a program (call itOne.java
) that moves the robot along the path that the program is indicating with red. When you're done, show the program to your lab instructor.
One.java
which moves
the robot incorrectly on a different (arbitrary path). You do not need to understand
the code in Room.java
in any way, but you should be able to write the
code for One.java
with no problem, as described in class, and as
illustrated below.
Here's my sampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; class Room extends JFrame { int WD = 50, HT = 50; int lines, cols; Robot robot; Drawing panel; public Room () { robot = new Robot(0, 0, "north"); lines = 4; cols = 4; panel = new Drawing(lines, cols); Container contentPane = getContentPane(); contentPane.add(panel, "Center"); setSize((cols + 1)* WD, (lines + 1) * HT); WindowCloser listener = new WindowCloser(); addWindowListener(listener); setTitle("Lab 1 Exercise"); show(); } private class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent event) { System.exit(0); } } class Drawing extends JPanel { int lines, cols; Drawing(int l, int c) { lines = l; cols = c; } public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.yellow); g.fillRect(0, 0, cols * WD, lines * HT); g.setColor(Color.black); for (int i = 0; i <= lines; i++) { g.drawLine(0, i * HT, cols * WD, i * HT); } for (int i = 0; i <= cols; i++) { g.drawLine(i * WD, 0, i * WD, lines * HT); } g.setColor(Color.red); dL(0, 0, 1, 0, g); dL(1, 0, 1, 2, g); dL(1, 2, 2, 2, g); dL(2, 2, 2, 1, g); dL(2, 1, 3, 1, g); dL(3, 1, 3, 3, g); dL(3, 3, 0, 3, g); robot.draw(g); } void dL(int x1, int y1, int x2, int y2, Graphics g) { x1 = x1 * WD + (int)(WD / 2.0); x2 = x2 * WD + (int)(WD / 2.0); y1 = y1 * HT + (int)(HT / 2.0); y2 = y2 * HT + (int)(HT / 2.0); g.drawLine(x1, y1, x2, y2); } } class Robot { int x, y; String dir; Robot(int col, int line, String facing) { x = col; y = line; dir = facing; } void moveForward() { try { Thread.sleep(1000); } catch (Exception e) { } System.out.println("Moving forward."); if (dir.equals("north")) { y -= 1; if (y < 0) y = 0; } else if (dir.equals("east")) { x += 1; if (x >= cols) x = cols - 1; } else if (dir.equals("south")) { y += 1; if (y >= lines) y = lines - 1; } else { // assume "west" x -= 1; if (x < 0) x = 0; } panel.repaint(panel.bounds()); } void turnLeft() { try { Thread.sleep(1000); } catch (Exception e) { } System.out.println("Turning left."); if (dir.equals("north")) { dir = "west"; } else if (dir.equals("east")) { dir = "north"; } else if (dir.equals("south")) { dir = "east"; } else { // assume "west" dir = "south"; } panel.repaint(panel.bounds()); } void draw(Graphics g) { double out = 0.3; int xL, yL, w, h; int xT, yT; double wT, hT; xL = x * WD + (int)(out * WD); yL = y * HT + (int)(out * HT); w = (int)((1 - 2 * out) * WD); h = (int)((1 - 2 * out) * HT); g.setColor(Color.blue); g.fillRect(xL, yL, w, h); g.setColor(Color.black); g.drawRect(xL, yL, w, h); wT = w/3.0; hT = h/3.0; if (dir.equals("north")) { xT = (int)(xL + w / 2.0 - wT / 2); yT = (int)(yL - hT / 2); } else if (dir.equals("east")) { xT = (int)(xL + w - wT / 2); yT = (int)(yL + h / 2.0 - hT / 2); } else if (dir.equals("south")) { xT = (int)(xL + w / 2.0 - wT / 2); yT = (int)(yL + h - hT / 2); } else { // assume "west" xT = (int)(xL - wT / 2); yT = (int)(yL + h / 2.0 - hT / 2); } g.setColor(Color.white); g.fillRect(xT, yT, (int)wT, (int)hT); g.setColor(Color.black); g.drawRect(xT, yT, (int)wT, (int)hT); } } }
One.java
:
class One { public static void main(String[] args) { Room room = new Room(); room.robot.turnLeft(); room.robot.turnLeft(); room.robot.turnLeft(); room.robot.moveForward(); room.robot.turnLeft(); room.robot.turnLeft(); room.robot.turnLeft(); room.robot.moveForward(); room.robot.moveForward(); room.robot.turnLeft(); room.robot.moveForward(); room.robot.moveForward(); room.robot.moveForward(); room.robot.turnLeft(); room.robot.turnLeft(); room.robot.turnLeft(); room.robot.turnLeft(); room.robot.turnLeft(); System.out.println("That's it!"); } }
The goal for
this semester's work is to understand thoroughly a program such as this.