CSC1110 - Week 7 Exercise
Instructions
Implement the following classes based on their UML and descriptions. At the end, are the sample outputs from two separate runs.
Point
Point |
---|
-x:double {readOnly} -y:double {readOnly} |
+Point(x:double,y:double) +getX():double +getY():double |
This class represent a Point is 2D space that posses an (x,y) value.
Rectangle
Rectangle |
---|
-upperLeft:Point {readOnly} -width:double {readOnly} -height:double {readOnly} |
+Rectangle(width:double, height:double, x:double, y:double) +hit(testPoint: Point):boolean +display():void -getLowerRightCorner():Point |
This class represents a Rectangle that has a width and height, but also a Point that represents the location of the upper left-hand corner. The hit() method returns true if the passed-in x and y values passed in, fall within, but not on, the perimeter of the Rectangle. The display() method prints out "Rectangle", and then the x and y of the upper left-hand corner and the width and height. See the sample output.
RectangleDriver
RectangleDriver |
---|
+main(args:String[]) -makeRectangle(scan:Scanner):Rectangle -checkPoint(rec:Rectangle, scan:Scanner):void |
This is the driver class and contains a main() and two helper methods.
- The main() method creates a new Rectangle using the makeRectangle() method. It then calls the checkPoint() method with the Rectangle that was returned from makeRectangle().
- The makeRectangle() method asks the user for the width and height of a Rectangle and the x and y of the upper left hand corner. This method then returns a new Rectangle using the values entered by the user.
- The checkPoint() method asks the user for the x and y of a Point to check. It prints out whether the x and y locations enter fall inside the perimeter of the passed in Rectangle.
Sample Output 1
Enter the width: 4 Enter the height: 3 Enter the x of the top left corner: 0 Enter the y of the top left corner: 0 Rectangle: x:0.0,y:0.0 Width:4.0,Height:3.0 Enter the x of the Point you want to test: 1 Enter the y of the Point you want to test: -1 This point (1.0,-1.0) is within the Rectangle
Sample Output 2
Enter the width: 4 Enter the height: 3 Enter the x of the top left corner: 2 Enter the y of the top left corner: 3 Rectangle: x:2.0,y:2.0 Width:4.0,Height:3.0 Enter the x of the Point you want to test: 1 Enter the y of the Point you want to test: -1 This point (1.0,-1.0) is not within the Rectangle
Submission
See your professor's instructions for details on submission guidelines and due dates.