CSCI A201/A597

Lecture Notes 7

Spring 2000


Help with the assignment problem. The Element Package. The Console Window and the Drawing Window.

1. First we start with a brief review of what we did so far.

2. Then we discuss how the assignment problem can be approached.

One could think of approaching it in the following stages:

At this point we should realize that we print in slices (lines) and that to print a line we should be using 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:

  1. a ConsoleWindow blueprint, and a
  2. a DrawingWindow blueprint
Using these two blueprints we could produce Creating and Using 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:

new ConsoleWindow()
Every time a statement like this is executed a new console window is created and a reference to it is returned.

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:

c = new ConsoleWindow();
We need to clarify two things:
  1. what is appropriate same type of the variable, and
  2. what can we do with a console window to justify referring to it through a reference
To answer the first question, the type is the name of the blueprint.

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

that a member object of any such window (called out) contains and offers

To read something from such a window we can invoke methods like this:

that a member object of any such window (called 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:

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?...)
Here's the program:
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:

Here's a picture we want to obtain:

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.