Week 15: Final Exam Review for CS 1711

This is not a complete review and will not be turned in.  The presenters will help you write the classes and this is for study purposes only.

Inheritance and Implementing the Comparable Interface

1 (a). Write a class that is to be used to represent a point in a plane.  PointDouble.java
 
Data
    double myX      holds the x value of the ordered pair
    double myY      holds the y value of the ordered pair

Constructor
   public PointDouble(double x, double y);
 
Accessors
    double getX()   returns the x value
  double getY()   returns the y value
  String toString()  returns the x and y coordinates in the form (x,y)

   1. (b)  Write a class NormedPoint that extends the class PointDouble and implements the interface Comparable.
 
            Data
                double myNorm        holds the distance of the point from the origin.

            Constructor
        public NormedPoint(double x, double y)
            Calls the super constructor
                        Calls the method getNorm to initialize the data value myNorm

            Accessors
                String toString()      Returns the super toString and the value of the norm
                int compareTo(Object rhs)  compares the objects norm to the rhs's norm and returns the appropriate value

            Modifiers
        double getNorm()      Calculates the norm and returns this value
                                                        norm = Math.sqrt( x2 + y2 )
          


Array of Objects

    You are to write a class that represents a rectangle.  The rectangle class contains the following

    Data
        NormedPoint[] myVertices    Contains the values that represent the corners of the rectangle.
    int myNumVertices;
 
    Constructor
        public Rectangle(NormedPoint upperLeft, NormedPoint upperRight,
                     NormedPoint lowerRight, NormedPoint lowerLeft )
         Stores all 4 NormedPoints in the array Clockwise starting with upperLeft

    Accessors
                String toString()    returns all the vertices in the rectangle
                NormedPoint getMax()  Finds and returns the point that is farthest from the origin.
                double calculatePerimeter()  calculates and returns the distance around the rectangle

    Private Helper
                double getDistance(NormedPoint x, NormedPoint y)
        given points (x1,y1) and (x2,y2) the distance between is
        Math.sqrt( (x1-x2) * (x1-x2) + (y1-y2)*(y1-y2) )


Two-Dimensional Arrays

Write method borderSum, as started below.  borderSum should return the sum of the values in the border of its matrix parameter M(the top and bottom rows and the leftmost and rightmost columns,  including the corner values in the sum only once).

Assume that M has at least two rows and at least two columns.  numRows >=2 and
numCols >= 2

In the following examples, the values to be summed are shown in bold font for emphasis

 Matrix M  Result of the call borderSum(M,numRows, numCols)

 1  0  2  1    borderSum returns 15
 0  1  2  2
 2  3  0  4

 2  3  0  4    borderSum returns 33
 5  0  1  2
 3  6  0  4
 1  2  3  4

 1  2     borderSum returns 10
 3  4

Complete method borderSum below.

int BorderSum( int[][] M, int numRows, int numCols )
{
 



Last Revised:  November 28, 2001  4:49pm