|
Spring Semester 2002 |
Goals for the first lab:
Here are the steps for this lab:
A201-1219 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 P1.2 in the book).
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");
}
}
.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.
A201/A597 LAB ASSIGNMENT ONE
Write a program that prints a staircase:
+--+
| |
+--+--+
| | |
+--+--+--+
| | | |
+--+--+--+--+
| | | | |
+--+--+--+--+ Show it to your AI in lab next week (Jan 17-18).
The AI may ask you any questions about the program, so be prepared to describe your work.