Creating and Using Objects

Pairs Programming

Extreme Programming is a development technique that advocates writing tests first, then adding features to a class so that it will pass those tests. X Programmers program in pilot, co-pilot pairs. The pilot types, while the co-pilot makes suggestions. Pilot and co-pilot switch roles often.

1. Pair up with a lab mate. Exchange email addresses. Designate pilot and co-pilot. In the next lab you can switch.

Creating and Using Rectangle Objects

2. Find and display the javadoc page for the Rectangle class in your browser.

Here are a few things to notice:

What package does it belong to?

What constructors does it provide?

What fields does it provide? Are they public or private?

What inherited methods does it provide?

A rectangle lives on a virtual canvas with an inverted y-axis. Units are measured in pixels:

3. Create a file called TestRectangle.java. (Where is a good place to put this file?) Here's what goes in to this file:

import java.awt.*;

public class TestRectangle {
   public static void main(String[] args) {

   }
}

After each of the following steps compile and run TestRectangle.

4. In main create a square called "r1" with height (and width) 50 and corner at (0, 0). Print r1 in the console window.

5. Move r1 10 pixels right and 90 pixels down. Print r1 in the console window.

6. Make r1 10 pixels bigger on the left, right, top, and bottom. Print r1 in the console window.

7. Create a rectangle called r2 that has its upper left corner at the point (10, 20). The height of r2 is 40 and the width is 30. Print r2 in the console window.

8. In the console window print true if r1 intersects r2 and false otherwise.

9. Create a new rectangle called r3 that's the union of r1 and r2. Print r3 in the console window.

10. Create a point called p1 at (50, 50). Print p1 in the console window. How many public methods are declared in java.awt.Point?

11. In the console window print if r3 contains p1.

12. On a piece of paper exactly plot r1, r2, r3, and p1.

13. Print the area and perimeter of r3.

Creating a Circle class and some Circle objects

14. Create a new class called Circle. This class should have three integer fields: radius, xCenter, and yCenter:

private int radius, xCenter, yCenter;

The class should provide getter and setter methods for each field. For example:

// getter method for radius field:
public int getRadius() { return radius; }
// setter method for radius field:
public void setRadius(int newRadius) { radius = newRadius; }

It should also provide a constructor for initializing all three fields, a constructor for initializing only the radius-- setting the center to be (0, 0)—and a default constructor (no inputs) that initializes the radius to 1 and the center to (0, 0):

public Circle(int rad, int xc, int yc) { ??? }
public Circle(int rad) { ??? } // assumes center = (0, 0)
public Circle() { ??? } // assumes center = (0, 0), radius = 1

Provide area and perimeter methods for your class. Hint, see the javadoc page for the Math class to get the constant PI.

public double area() { ??? }
public double circumference() { ??? }

Provide a contains method that takes as input the x and y coordinates of a second point and returns true if the point is contained in the circle (or its perimeter) and false otherwise.

// = true if (xc, yc) inside this circle
public boolean contains(int xc, int yc) { ??? }

Hint, consider using the methods:

java.awt.geom.Point2D.distance(x1, y1, x2, y2);

15. Create a file called TestCircle containing main (see TestRectangle above). In main create two circles. One centered at (20, 30) with a radius of 10, and another centered at (25, 60) with a radius of 40.

16. Print the area and circumference of both circles.

17. Print true if the first circle contains the center of the second circle and false otherwise.