| CSCI A201/A597Lecture Notes 7 Spring 2000 |
1. First we start with a brief review of what we did so far.
while, and for loops
One could think of approaching it in the following stages:
n) and print a line of n *'s
*'s whose side is of length n
print() repeatedly. To move to the next line we need to call
println() as soon as we are done printing the *'s on a line.
The next step could be to print a square of side n (in which only the
borders have *'s). The idea is to realize that we print the *'s
in rows and columns, and those *'s that don't appear on the first or the
last line, or the first or the last row, should be replaced with blank spaces.
Now you need to figure out how the blanks on the first and second diagonal should be
identified, so that we can replace them with *'s.
Hopefully this picture can help.
3. Then we look at the element package, what it has to offer and how can take advantage of that.
The element package is composed of two main components:
ConsoleWindow blueprint, and a
DrawingWindow blueprint
ConsoleWindow objects
The name of the blueprint for console windows is: ConsoleWindow.
As anything else in Java the name is case-sensitive.
We use this name every time we want to ask for a new console window, like this:
Every time a statement like this is executed a new console window is created and a reference to it is returned.new ConsoleWindow()
That is, if we stored the result of this expression in a variable say, c,
of the same type, we could later use the variable to refer to the console window:
We need to clarify two things:c = new ConsoleWindow();
So if we declare c and then store a reference to a newly created
console window, the code could certainly look like this (remember that the text
that follows a pair of slashes up to the end of the line is a comment, and is
therefore ignored by the compiler):
ConsoleWindow c; // the declaration
c = new ConsoleWindow(); // creating the window and storing the
// reference to it in the variable c
To answer the second question we need to say that a console window that is created
out of the blueprint that the element package is offering allows us to
communicate through the monitor and the keyboard. To print a string of characters in such a window we need to call one of two methods
print() or
println()
out) contains and offers To read something from such a window we can invoke methods like this:
readInt()
readString()
readDouble()
input) contains and offers. See appendix E in the book for more details.
Let's write a program that we will call Echo.
The program will ask us for text and will print it back.
Here's an approximation of how our session in the console window might look like:
Here's the program:Welcome to the Echo program! ***(Please type some text below, then hit Enter)*** OK, here's some text... You typed: (OK, here's some text...) I see, you're simply repeating my lines... You typed: (I see, you're simply repeating my lines..) One, two, three... You typed: (One, two, three...) Bye You typed: (Bye) quit You typed: (quit) How do you stop this?... You typed: (How do you stop this?...)
import element.*;
public class Echo {
public static void main(String[] args) {
ConsoleWindow c = new ConsoleWindow();
c.out.println("Welcome to the Echo program!");
c.out.println("***(Please type some text below, then hit Enter)*** ");
while (1 == 1) {
String user = c.input.readLine();
c.out.println("You typed: (" + user + ")");
c.out.println("***(Please type some more, then hit Enter)***");
}
}
}
We'll explain this program in class and also change it to understand the quit command. Check Appendix E and section 1.4.1 for useful information.
Creating and Using DrawingWindow objects
First we need to know how we can create a drawing window.
The answer is: just like we do with a console window.
The difference will be in how the two kinds of windows will be used.
To start with we will use a drawing window to draw lines.
We need to understand the coordinate system, illustrated below:
As the books says the drawing window has functions for:
And here's a program that would do just that:
import element.*;
public class ZigZag {
public static void main(String[] args) {
DrawingWindow d = new DrawingWindow();
int x = 10, y = 10;
d.moveTo(x, y);
for (int i = 0; i < 18; i++) {
d.line(40, 10);
d.line(-40, 0);
}
}
}
Exercise. Put together a program that gets commands from a console window and draws lines in a drawing window, incrementally.
A demo of such a program will be shown in class.