Lab Seven - Introduction to Objects

Creating a class

The first class we will create for this lab exercise is a Bug class. Objects of the Bug class simulate bugs that live on a two-dimensional surface. Each bug contains location information that places the bug at a specific point on the plane:

private double x;
private double y;

The Bug class has a default constructor that initializes the bug's coordinates to the origin:

public Bug() {
  x = 0.0;
  y = 0.0;
}

Create a new class named Bug, and place the member variables and the constructor shown here in that class.

Bugs can do two things: they can jump to a new location, and they can print their current locations.

Writing a jump method

Write the code for a method

public void jump()

which when called causes the bug to jump to a new, randomly chosen location. To compute the bug's new location, use Math.random() to generate a random double d between 0 and 1. This will determine how far the bug jumps. Next, use 2*Math.PI*Math.random() to generate an angle θ between 0 and 2 π. Compute the coordinates of the new point that is that distance and angle from the bug's old point, and make those be the bug's new coordinates.

Writing a print method

Write the code for a method

public void print()

Which prints the bug's location to System.out.

Testing the bug class

Create a second class named BugTest. In that class write a main method that does the following things.

Extending the bug class

Next, add a method

public double distance()

to the Bug class that computes and returns the bug's distance from the origin.

Creating an array of objects

Write a new main method in your BugTest class that will:

To help you do the work, you might find it useful to write a second method

public static double averageDistance(Bug[] A)

that computes the average distance from the origin for all of the Bugs in the array A.

Working with another class

Here is the code for a Point class which can be used to represent points in space.

public class Point {
  private double x;
  private double y;

  Point(double xCoord,double yCoord) {
    x = xCoord;
    y = yCoord;
  }

  public double getX() {
    return x;
  }

  public double getY() {
    return y;
  }

  public double magnitude() {
    return Math.sqrt(x*x+y*y);
  }
}

Add this Point class as a new class to your project.

Objects of this class are said to be immutable, which means that once they are created their internal data does not change. This class provides accessor methods which we can call to query a Point's internal state, but no mutator methods which allow us to change the internal state of the object once it has been created.

For example, if we create a Point object with the code

Point p = new Point(2.0,0.5);

the point p will stay fixed at the location (2.0,0.5). If we want to make a new point with a new location, we will have to replace the original Point object with a new object:

p = new Point(1.7,0.6);

Modifying the Bug class to work with Points

Next, modify the Bug class you wrote earlier by removing the member variables

private double x;
private double y;

and replacing them with a new member variable

private Point location;

The Point object location will store the location information for the bug.

Once you make this change, you will need to rewrite most of the code for the methods of the Bug class to work with location instead of x and y. Rewrite the Bug methods as needed and run your program again. The modified program should behave similarly to the original version. Since you changed only the internal structure of your Bug objects, you will not need to change any of the code in your BugTest class.

Once you get the final version of your program working, email it to me for grading.