CSCI A201/A597

Lab Notes 3

Spring 2000


This is in-lab assignment 2 due at the end of the lab next week (Feb 3-4, 2000).


Write a program in a file called Crossed.java which expects one argument on the command line. This argument represents a size. The program should be printing a square of that size with both diagonals drawn. Here's an example, a sample session with a prototype:

school.cs.indiana.edu%java Crossed 8
 * * * * * * * * *
 * *           * *
 *   *       *   *
 *     *   *     *
 *       *       *
 *     *   *     *
 *   *       *   *
 * *           * *
 * * * * * * * * *
school.cs.indiana.edu%java Crossed 16
 * * * * * * * * * * * * * * * * *
 * *                           * *
 *   *                       *   *
 *     *                   *     *
 *       *               *       *
 *         *           *         *
 *           *       *           *
 *             *   *             *
 *               *               *
 *             *   *             *
 *           *       *           *
 *         *           *         *
 *       *               *       *
 *     *                   *     *
 *   *                       *   *
 * *                           * *
 * * * * * * * * * * * * * * * * *
school.cs.indiana.edu%java Crossed 12
 * * * * * * * * * * * * *
 * *                   * *
 *   *               *   *
 *     *           *     *
 *       *       *       *
 *         *   *         *
 *           *           *
 *         *   *         *
 *       *       *       *
 *     *           *     *
 *   *               *   *
 * *                   * *
 * * * * * * * * * * * * *
school.cs.indiana.edu%

Test the following programs using the floppies that will be distributed to you in the labs (which contain the packages that we need for this class).

(Note: these files are listed here so that you can easily cut and paste them and test them in your lab. Don't expect to understand them, but expect a significant reading assignment for next Tuesday which should clarify them entirely).

Just make sure that when you compile and run these programs they are in the same directory with the folders that contain the packages on the floppies distributed by the lab coordinator. So just put the programs in the root folder of the floppy.


1. This program is listed in your textbook at page 39. It is placing text in the console window, a specific text.
import element.*;

public class Printing {
    public static void main(String[] args) {
	ConsoleWindow c = new ConsoleWindow();
	c.out.print("Watson, come here!"); 
	c.out.println("  I want you!"); 
    } 
}

2. This program can be found on page 40 of your textbook. It performs an age computation from initial data (age) that is read from the terminal. So it establishes a dialogue with the user.
import element.*;

public class AgeInput {
    public static void main(String[] args) {
	ConsoleWindow c = new ConsoleWindow(); 
	c.out.println("How old are you?"); 
	double ageInYears = c.input.readDouble(); 
	c.out.println("I can't believe you're " + ageInYears + " years old!"); 
        c.out.println("So you're " + (ageInYears * 365) + " days old.");
    } 
}

3. This is Lab 2.5 in your textbook on page 67. It allows us to draw a single "curve" in the drawing window. Essentially you need to click the mouse and keep it pressed while you move it (drag it). While you drag the mouse the "curve" will be drawn. When you release the mouse button the process ends.
import element.*;
import java.awt.Color;

public class SimpleDraw {
    public static void main(String[] args) {
	DrawingWindow d = new DrawingWindow(200, 200); 
	Pt mouse;
	final int radius = 2; 
	Circle nib = new Circle(0, 0, radius);
	d.setForeground(Color.red); 
	d.awaitMousePress();
	while (d.mousePressed()) {
	    mouse = d.getMouse();
	    nib.center(mouse); 
	    d.fill(nib); 
	} 
    } 
}

4. This is Lab 2.4 in the book (experimenting with console and drawing windows). The program waits for the mouse to be pressed and released. The location of the mouse press is drawn on the screen and is printed in the console window as well.
import element.*;

public class Intro {
    public static void main(String[] args) {
	DrawingWindow d = new DrawingWindow();
	ConsoleWindow c = new ConsoleWindow();
        Pt p; 

        c.out.println("Press the mouse in the drawing window."); 
	p = d.awaitMousePress(); 

        d.draw(p); 
	d.draw(new Circle(p, 10)); 

	p = d.awaitMouseRelease(); 

	c.out.print("Thanks!  You pressed at "); 
	c.out.println(p); 

        c.out.println("Press the mouse to stop the program."); 
	d.awaitMousePress(); 

	System.exit(0); 
    } 
}

5. This program can be found on page 41 of the text. It is a program that carefully collects several types of information about a person's swimming workout. Then it reports its conclusions.
import element.*;

public class Swim {
    public static void main(String[] args) {
	final double SECONDS_PER_HOUR = 60.0 * 60.0; 
	final double FEET_PER_MILE = 5280.0; 
	final double FEET_PER_METER = 39.37/12.0;
	final int POOL_SIZE = 50; 
	final double FEET_PER_LAP = POOL_SIZE * FEET_PER_METER; 

	ConsoleWindow c = new ConsoleWindow(); 
	String first, last;
	int laps, minutes, seconds; 
	double hours, distance, lapTime;

	c.out.println("What are your first and last names?"); 
	first = c.input.readString();
	last = c.input.readString(); 
	c.out.println("Thanks, " + first + "."); 
	c.out.println("How many " + POOL_SIZE + 
		      " meter laps did you swim?"); 
	laps = c.input.readInt(); 

	c.out.println("How long did it take you " + 
		      "((in minutes and seconds)?"); 

	minutes = c.input.readInt();
	seconds = c.input.readInt();

	seconds = seconds + 60 * minutes; 
	hours = seconds / SECONDS_PER_HOUR;

	lapTime = seconds / (double)laps;
	c.out.println("It took you, on average, " + 
		      lapTime + " seconds per lap."); 

	distance = laps * FEET_PER_LAP / FEET_PER_MILE; 

        c.out.println("Your average speed was " +
		      (distance/hours) +" miles/hour.");

	c.out.println("You swim about " + 
		      Math.round(SECONDS_PER_HOUR/lapTime) +
		      " laps per hour!"); 

    }
}

6. This program can be found on page 45 in the text. It draws an X at the location that is read from the keyboard.
import element.*;

public class XMarks {
    public static void main(String[] args) {
	ConsoleWindow c = new ConsoleWindow();
	DrawingWindow d = new DrawingWindow(); 
	int centerX, centerY;
	final int RADIUS = 10; 

	c.out.println("Enter the coordinate of the X:"); 
	centerX = c.input.readInt();
	centerY = c.input.readInt(); 

	d.moveTo(centerX, centerY);
	d.move(-RADIUS, -RADIUS); 
	d.line(2*RADIUS, 2*RADIUS); 
	d.move(0, -2*RADIUS); 
	d.line(-2*RADIUS, 2*RADIUS);

    } 
}
As I mentioned before the goal is to experiment with these six program to compile and run them to see them running. To get an idea how they behave. Next week we will try to understand them very well so that we can write other programs similar to them, or to enhance these very programs (making them more robust, or adding new functionality).
Last updated: January 27, 2000 by Adrian German