Experiments in Java


Experiments for Session J2: Objects and Methods

Name: ________________
ID:_______________

Experiment J2.1: Printing out a point

Required code files:

Step 1. Make a copy of Point.java. Compile it. Try to execute it. Record your results. Compare your notes with the notes at the end of this lab.


 
 
 

Step 2. Make a copy of PointFun.java. Compile and execute it and record the results. Do you observe anything odd? What do you think is going on? For an explanation, turn to the end of this lab.


 
 
 
 
 
 

Step 3. Update the main method of PointFun.java so that it shifts the point five spaces right, five spaces up, ten spaces left, five spaces down, and then five spaces right, printing the position and distance from the origin each time. Record your code.


 
 
 
 
 
 

Step 4. Run your code and record any observations.


 
 
 
 
 
 

Step 5. Update the main method of PointFun.java to read in the initial x and y coordinates. Record your solution.


 
 
 
 
 
 

Experiment J2.2: Coordinating classes

There are two versions of this experiment. Experiment J2.2A is designed for students working on computers that include graphics displays. Experiment J2.2B is designed for students working on computers that only support textual displays (or for other classes that do not wish to use graphics).

Experiment J2.2A: Coordinating classes (graphical application)

Required code files:

Step 1. Make copies of Point.java, Plane.java, and JustPlaneFun.java. Compile all three. Execute JustPlaneFun and record the results.


 
 
 

Step 2. Modify JustPlaneFun to use only one point that you move around. Start the point at (1,1) and shift the point right five, up five, left ten, down five, and right five, plotting it after each step. Execute the modified version and record the results. What do these results suggest?


 
 
 
 
 
 

Step 3. Modify JustPlaneFun to input the point to plot. Note that the point won't be plotted until after the main method terminates. Why? Because main just sets things in motion and Plane waits until main is done to display itself. Record your code.


 
 
 
 
 
 

Experiment J2.2B: Coordinating classes (textual application)

Required code files:

Step 1. Make copies of Point.java, TextPlane.java, and TextPlaneFun.java. Compile all three. Execute TextPlaneFun and summarize the output.


 
 
 

Step 2. Modify JustPlaneFun to use only one point that you move around. Start the point at (1,1) and shift the point right five, up five, left ten, down five, and right five, plotting it and displaying the plane after each step. Execute the modified version and record the results. What do these results suggest?


 
 
 
 
 
 

Step 3. Modify JustPlaneFun to input the point to plot. Record your code.


 
 
 
 
 
 

Step 4. Plot points at (0.5,0.5) and (1.2,2) and display the plane. Where does the point appear? What does that suggest?


 
 
 
 
 
 

Experiment J2.3: Your own method

Required code files:

Step 1. Modify Point so that it includes the new print method we developed earlier. Here is the code for that method.

  /**
   * Print information about the current point.
   */
  public void print(SimpleOutput out) {
    out.println("(" + this.getX() + "," + this.getY() + ")");
    out.println("  distance from origin: " + 
                this.distanceFromOrigin());
  } // print(SimpleOutput)

Step 2. Modify PointFun so that it uses the new print method to print the various points. Compile the various files and then execute PointFun. Summarize your changes to the program. Record the output from the program.


 
 
 
 
 
 

Step 3. Modify Point so that it prints out points as <X,Y> rather than (X,Y). Recompile just Point and then execute PointFun. Record the results.


 
 
 
 
 
 

Step 4. Change all instances of out to screen in the print method of Point. Recompile just Point and then execute PointFun. Record the results. What happened? Was that what you expected?


 
 
 
 
 
 

After you have written down your observations, you may wish to look at the notes on this problem.

Experiment J2.4: Your own class

Required code files:

Step 1. Modify PointFun so that it creates a PointPrinter object called printer and uses printer to print the various positions of the point. Compile the various files and then execute PointFun. Summarize your changes to the program. Record the output from the program.


 
 
 
 
 
 

Step 2. Modify PointPrinter so that it prints out points as <X,Y> rather than (X,Y). Recompile just PointPrinter and then execute PointFun. Record the results.


 
 
 
 
 
 

Step 3. Change all instances of pt to apoint in PointPrinter. Recompile just PointPrinter and then execute PointFun. Record the results. What happened? Was that what you expected?


 
 
 
 
 
 

You may wish to look at the notes on this problem.

Step 4. Change all instances of out to screen in PointPrinter. Recompile just PointPrinter and then execute PointFun. Record the results. What happened? Was that what you expected?


 
 
 
 
 
 

You may wish to look at the notes on this problem.

Experiment J2.5: Return values

Step 1. Create a new class, AverageComputer (stored in AverageComputer.java) with the following method.

  /**
   * Compute the average of two doubles.
   */
  public double average(double a, double b) {
    double ave = (a + b) / 2;
    return ave;
  } // average(double,double)

Step 2. Make a copy of AverageTester.java, which reads as follows.


import AverageComputer;
import SimpleInput;
import SimpleOutput;

/**
 * Some simple tests of the AverageComputer class.
 *
 * @author Samuel A. Rebelsky
 * @author Your Name Here
 * @version 1.0 of January 1999
 */
public class AverageTester {
  /**
   * Test away!
   */
  public static void main(String[] args) {
    // Prepare for reading input and printing output.
    SimpleOutput out = new SimpleOutput();
    SimpleInput in = new SimpleInput();
    // Build a new computer.
    AverageComputer computer = new AverageComputer();
    // The two values we'll be reading.
    double first;
    double second;

    // Read the two values.
    out.print("Enter a number: ");
    first = in.readDouble();
    out.print("Enter another number: ");
    second = in.readDouble();
    // Compute the average and print the result.
    out.println("The average of " + first + " and " + second + " is " +
                computer.average(first,second));
  } // main(String[])

} // AverageTester


Compile both AverageComputer and AverageTester. Execute AverageTester. Record the results.


 
 
 

Step 3. Change the average method of AverageComputer to read

  public double average(double a, double b) {
    double ave = (a + b) / 2;
  } // average(double,double)

What do you expect to happen when you try to recompile AverageComputer?


 
 
 

Step 4. Recompile AverageComputer and record the error messages you receive. What do the error messages suggest?


 
 
 
 
 
 

Step 5. Change the average method of AverageComputer to read

  public double average(double a, double b) {
    double ave = (a + b) / 2;
    return 0;
  } // average(double,double)

What do you expect to happen when you try to execute AverageTester with the modified AverageComputer?


 
 
 

Step 6. Recompile AverageComputer and execute AverageTester. Use 1 and 2 as the inputs. What is the output? What does this suggest?


 
 
 
 
 
 

Step 7. Change the average method of AverageComputer to read

  public double average(double a, double b) {
    double ave = (a + b) / 2;
    return 0;
    return ave;
  } // average(double,double)

Try to recompile AverageComputer and record the error messages. What do these messages suggest?


 
 
 
 
 
 

Experiment J2.6: Static methods

Required code files:

Step 1. Make a copy of PointReader.java, which is as defined as follows.


import SimpleInput;
import SimpleOutput;
import Point;

import SimpleOutput;
import SimpleInput;

/**
 * Simple methods for reading in points.  Arguably, these could also be
 * part of the Point class, but some issues have dictated that we use 
 * a separate class.
 * 
 * Developed as an illustration of static methods.
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of January 1999
 */
public class PointReader {
  // +----------------+------------------------------------------
  // | Static Methods |
  // +----------------+
  
  /**
   * Prompt for and read one point.
   */
  static public Point read(SimpleOutput out, SimpleInput in) {
    float x;  // The x coordinate
    float y;  // The y coordinate
    out.print("Enter the x coordinate: ");
    x = in.readFloat();
    out.print("Enter the y coordinate: ");
    y = in.readFloat();
    return new Point(x,y);
  } // read(SimpleOutput, SimpleInput)
} // class PointReader


Note that PointReader.java contains a PointReader class with only one method, a static read method. Compile PointReader.

Step 2. Add the following lines to the main method of PointFun.

    PointReader reader = new PointReader();
    PointPrinter printer = new PointPrinter();
    Point zebra = reader.read(out,in);
    printer.print(out,zebra);

If your copy of PointFun does not create a SimpleInput object called in, you should insert the following line before those lines.

   SimpleInput in = new SimpleInput();

What do you expect these lines to do?


 
 
 

Step 3. Recompile and execute PointFun. Record the new results. Were they what you expected? Explain the results.


 
 
 
 
 
 

Step 4. Add the following lines to the main method of PointFun.

    Point stripes = PointReader.read(out,in);
    printer.print(out,stripes);

Recompile and execute PointFun. Record the new results. Were they what you expected?


 
 
 
 
 
 

Step 5. Add the following lines to the main method of PointFun

    Point white = PointReader.read(out,in);
    PointPrinter.print(out,white);

Try to recompile and execute PointFun. What happens? What does this suggest?


 
 
 
 
 
 

After recording your answer, you may wish to look at the notes on this problem. You should also remove these lines from the program.

Step 6. Add the following lines to the main method of PointFun

    Point black = PointReader.read(in,out);
    printer.print(out,black);

Try to recompile and execute PointFun. What happens? What does this suggest?


 
 
 
 
 
 

After recording your answer, you may wish to look at the notes on this problem. You should also remove these lines from the program.

Experiment J2.7: Copying objects

Required code files:

Step 1. Write a class, PointExperiment, that imports the Point class and has an empty main method. Compile and execute the program. Your program should do nothing. You need not enter any results for this step.

Step 2. Add the following lines to the main method. Recompile and execute the program. Record the output. Is this what you expected? Why or why not?

    SimpleOutput out = new SimpleOutput();
    int a;
    int b;
    a = 3;
    b = 2*a;
    out.println("a = " + a + ", b = " + b);
    a = 1;
    out.println("a = " + a + ", b = " + b);


 
 
 
 
 
 

Step 3. Add the following lines to the main method. Recompile and execute the program. Record the output. Explain why the program generated that output.

    Point p1 = new Point(1,1);
    Point p2 = p1;
    out.println("(" + p1.getX() + "," + p1.getY() + ")");
    out.println("(" + p2.getX() + "," + p2.getY() + ")");
    p1.shiftLeft(1);
    out.println("(" + p1.getX() + "," + p1.getY() + ")");
    out.println("(" + p2.getX() + "," + p2.getY() + ")");
    p2.shiftDown(1);
    out.println("(" + p1.getX() + "," + p1.getY() + ")");
    out.println("(" + p2.getX() + "," + p2.getY() + ")");


 
 
 
 
 
 

Step 4. Add the following lines to the main method. Recompile and execute the program. Record the new output. Explain why the program generated that output. Explain why this output differs from that of the previous segment.

    Point p3 = new Point(1,1);
    Point p4 = new Point(1,1);
    out.println("(" + p3.getX() + "," + p3.getY() + ")");
    out.println("(" + p4.getX() + "," + p4.getY() + ")");
    p3.shiftLeft(1);
    out.println("(" + p3.getX() + "," + p3.getY() + ")");
    out.println("(" + p4.getX() + "," + p4.getY() + ")");
    p4.shiftDown(1);
    out.println("(" + p3.getX() + "," + p3.getY() + ")");
    out.println("(" + p4.getX() + "," + p4.getY() + ")");


 
 
 
 
 
 

Step 5. Consider the following code, which is missing some parts.

    Point p5 = new Point(0,0);
     ...
    Point p6 = __________________;
    out.println("Initially ...");
    out.println("  difference in X values is " + (p5.getX() - p6.getX()));
    out.println("  difference in Y values is " + (p5.getY() - p6.getY()));
    p6.shiftUp(1);
    out.println("After shifting p6 up 1 ...");
    out.println("  difference in X values is " + (p5.getX() - p6.getX()));
    out.println("  difference in Y values is " + (p5.getY() - p6.getY()));
    p5.shiftRight(3);
    out.println("After shifting p5 right 3 ...");
    out.println("  difference in X values is " + (p5.getX() - p6.getX()));
    out.println("  difference in Y values is " + (p5.getY() - p6.getY()));

What expression should be used in the blank so that the output is as follows?

Initially ...
  difference in X values is 0
  difference in Y values is 0
After shifting p6 up 1 ...
  difference in X values is 0
  difference in Y values is -1
After shifting p5 right 3 ...
  difference in X values is 3
  difference in Y values is -1

Your code should generate this output no matter how p5 changes in the section marked by ellipses.

Enter the expression assigned to p6 here.


 
 
 

Experiment J2.8: Changing the font

Optional applet experiment

Required files:

Before you begin, if you have not already done so, please familiarize yourself with the applet and HTML file. Make a copy of the two files. Read the code to make sure you understand the various parts. Compile HelloWorldApplet.java. Load the applet, using whatever technique your instructor specifies. (You might load it in a browser like Netscape Navigator or Microsoft Internet Explorer; you might run an application like Sun's appletviewer.)

Step 1. Add the following lines to the top of HelloWorldApplet.java.

import java.awt.Font;
import java.awt.Color;

Why do you think you were asked to add those lines?


 
 
 

After recording your answer, you may wish to consult the notes on this step.

Step 2. Replace the paint method of HelloWorldApplet with the following.

  public void paint(Graphics paintBrush) {
    Font myFont = new Font("Serif", 24, Font.BOLD);
    paintBrush.setFont(myFont);
    paintBrush.drawString("Hello World", 10, 30);
  } // paint(Graphics)

Recompile HelloWorldApplet and load the applet from helloworld.html. Record the result.


 
 
 

Step 3. Remove the line that reads

import java.awt.Color;

What do you expect will happen when you try to recompile HelloWorldApplet and load the applet from helloworld.html?


 
 
 

Confirm your answer by recompiling and loading the applet. You may also wish to consult the notes on this step.

Reinsert the line that you just deleted.

Step 4. Remove the line that reads

import java.awt.Font;

What do you expect will happen when you try to recompile HelloWorldApplet and load the applet from helloworld.html?


 
 
 

Confirm your answer by recompiling and loading the applet. You may also wish to consult the notes on this step.

Reinsert the line that you just deleted.

Step 5. What changes would you have to make for the message to be printed in 14pt sans serif italic?


 
 
 
 
 
 

Confirm your answer by making those changes to HelloWorldApplet, recompiling, and loading the applet with helloworld.html.

Step 6. Replace the paint method of HelloWorldApplet with the following

  public void paint(Graphics paintBrush) {
    paintBrush.setFont(new Font("Serif", 18, Font.ITALIC));
    paintBrush.drawString("Hello World", 10, 30);
  } // paint(Graphics)

Recompile HelloWorldApplet and load the applet from helloworld.html. What does this result suggest?

Warning! This is a conceptually difficult problem.


 
 
 
 
 
 

After entering your answer, you may wish to consult the notes on this step.

Step 8. Add the following line to the beginning of the paint method of HelloWorldApplet (before the call to setFont).

    paintBrush.setColor(Color.red);

What effect do you expect this change to have?


 
 
 

Confirm your answer by compiling HelloWorldApplet and loading the applet from helloworld.html.

Step 9. Move the setColor line to the end of the paint method of HelloWorldApplet (after the call to drawString). The body of this method should now read

    paintBrush.setFont(new Font("Serif", 18, Font.ITALIC));
    paintBrush.drawString("Hello World", 10, 30);
    paintBrush.setColor(Color.red);

What effect do expect this change to have?


 
 
 

Confirm your answer by compiling HelloWorldApplet and loading the applet from helloworld.html. You may also wish to consult the note on this step.

Step 10. Add the following line to the start of the paint method.

    paintBrush.setColor(new Color(255, 0, 255));

What effect do you expect this change to have?


 
 
 

Confirm your answer by compiling HelloWorldApplet and loading the applet from helloworld.html.

Step 11. Update the paint method to read

    paintBrush.setFont(new Font("Serif", Font.PLAIN, 24));
    paintBrush.setColor(Color.red);
    paintBrush.drawString("Hello world", 10, 30);
    paintBrush.setColor(Color.blue);
    paintBrush.drawString("Hello world", 12, 32);

What do you expect the new applet to show?


 
 
 

Confirm your answer by compiling HelloWorldApplet and loading the applet from helloworld.html.

Step 12. Update the paint method to read

    paintBrush.setFont(new Font("Serif", Font.PLAIN, 24));
    paintBrush.setColor(Color.blue);
    paintBrush.drawString("Hello world", 12, 32);
    paintBrush.setColor(Color.red);
    paintBrush.drawString("Hello world", 10, 30);

How will this applet differ from the one in the previous step?


 
 
 

Confirm your answer by compiling HelloWorldApplet and loading the applet from helloworld.html.

Experiment J2.9: Some simple pictures

Optional applet experiment

Required files:

Step 1. Make copies of CircleApplet.java and circle.html. Read both files. What do you expect the applet to do?


 
 
 

Step 2. Compile CircleApplet. Load the applet using circle.html. Note what you see.


 
 
 

Step 3. Indicate what you expect to happen if you change the line that reads

    paintBrush.fillOval(0,0,40,40);

to instead read

    paintBrush.fillOval(0,0,50,30);


 
 
 

Confirm your answer by compiling CircleApplet and loading the applet from circle.html.

Step 4. Change that line to read

    paintBrush.drawOval(0,0,50,30);

What effect do you expect this change to have?


 
 
 

Confirm your answer by compiling CircleApplet and loading the applet from circle.html.

Step 5. Change that line to read

    paintBrush.fillRect(10,0,50,30);

What effect do you expect this change to have?


 
 
 

Confirm your answer by compiling CircleApplet and loading the applet from circle.html.

Step 7. Change that line to read

    paintBrush.fillOval(10,-25,50,50);

What effect do you expect this change to have?


 
 
 

Confirm your answer by compiling CircleApplet and loading the applet from circle.html.

Step 8. Modify CircleApplet so that it draws a red circle of radius 25 centered at (30,30). Enter your code here.


 
 
 

Step 9. Modify CircleApplet so that it draws both the red circle and a blue circle of radius 10 centered at (30,30). Enter the new code here.


 
 
 

Step 10. What happens if the paint method draws the blue circle before drawing the red circle?


 
 
 

Step 11. Draw a series of five concentric circles centered at (50,50), using a different color for each circle. Enter your code here.


 
 
 
 
 
 


Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved.

Source text last modified Mon Oct 25 15:06:50 1999.

This page generated on Tue Oct 26 15:37:21 1999 by Siteweaver.

Contact our webmaster at rebelsky@math.grin.edu