SDSU CS 535 Object-Oriented Programming & Design
Fall Semester, 2000
Assignment 2
    Assignment Index        
© 2000, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 23-Sep-00

Due October 2
You are not allowed to work in teams on this assignment.

1. Provide Unit tests for the Point class listed below. You do not need to test the hashCode method

2. Implement and test a Rectangle class. The Rectangle class is to contain the following operations:

Given a rectangle determine it center point. Given two rectangles determine if they are equal. Given rectangles, a and b, determine if b is inside of a. Given a rectangle (a) and a point (b) determine if b is inside of a. Given a rectangle (a) and a point (b) translate a by b.


/**
 * Represents a point in a two dimensional plane.
 */
public class Point
   {
   private float x;
   private float y;
   
   public Point( float x, float y)
      {
      this.x = x;
      this.y = y;
      }
   public Point add( Point aPoint)
      {
      return new Point( x + aPoint.x, y + aPoint.y);
      }
   public Point subtract( Point aPoint)
      {
      return new Point( x - aPoint.x, y - aPoint.y);
      }
   
   public Point divide(float scalar)
      {
      return new Point( x/scalar, y/scalar);
      }
      
   public double distance( Point aPoint)
      {
      float deltaX = x - aPoint.x;
      float deltaY = y - aPoint.y;
      return Math.sqrt( deltaX* deltaX + deltaY* deltaY);
      }
      
   /***
     Returns true if the receiver is above and to the right of aPoint.
     */
   public boolean greaterThan(Point  aPoint)
      {
      return (x > aPoint.x ) && (y  > aPoint.y);
      }
   /***
     Returns true if the receiver is below and to the left of aPoint.
     */
   public boolean lessThan(Point aPoint )
      {
      return (x < aPoint.x ) && (y  < aPoint.y);
      }
   
   public String toString()
      {
      return "(" + x + ", " + y + ")";
      }
   public int hashCode() 
      {
      long bits = java.lang.Double.doubleToLongBits( x );
      bits = bits ^ java.lang.Double.doubleToLongBits( y ) * 31;
      return (((int) bits) ^ ((int) (bits >> 32)));
      }
   
   public boolean equals( Point p)
      {
      return x == p.x && y == p.y;
      }
   }

Copyright ©, All rights reserved.
2000 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

    visitors since 23-Sep-00