Download Java Assignment: Implementing a Rectangle Class and more Assignments Computer Science in PDF only on Docsity! COP3330.01, Fall 2001 Assigned: 9/11, 2001 S. Lang Assignment #2 Due: 9/20 in class Note: Minor revisions are included on 9/13, highlighted in the boldface font. The purpose of this assignment is to learn the basics of Java classes, including the use of access modifiers, constructors, parameter passing, and the syntax of simple Java statements. Before you start, be sure that you have understood the sample programs discussed in class: TwoDPoint.java (http://www.cs.ucf.edu/courses/cop3330.01-fall2001/homework/TwoDPoint.java), TestTwoDPoint.java (http://www.cs.ucf.edu/courses/cop3330.01-fall2001/homework/TestTwoDPoint.java ), and ThreeDPoint.java (http://www.cs.ucf.edu/courses/cop3330.01-fall2001/homework/ThreeDPoint.java) In this assignment, you are to implement a Java class Rectangle which models the 2-dimensional rectangles that reside on the computer screen. Each rectangle is identified by its upper-left corner (a TwoDPoint object), plus an integer (horizontal) length and an integer (vertical) width. The Rectangle class must contain the following instance variables and methods: (1) Instance variables (or fields): A TwoDPoint object upperLeft, an integer length, and an integer width, all use the private access modifier. We use the screen coordinate system which means the upper-left corner of the screen has the (0, 0) coordinates, the horizontal (x) axis extends from left to right, and the vertical (y) axis extends from top to bottom. (2) Constructors: Two constructors with the following signatures: (a) public Rectangle(TwoDPoint p, int len, int wid) /* create a Rectangle object with the specified upper-left corner, integer length len and integer width wid */ (b) public Rectangle(Rectangle r) // create a new Rectangle object identical to r (3) Instance methods: The class must provide the following public methods; other private methods for “internal” use may be used if necessary: (a) public TwoDPoint getUpperLeft() // return a “reference” to the upper-left corner (b) public int getLength() // return the integer length (c) public int getWidth() // return the integer width (d) public Rectangle translate(int dispX, int dispY) /* create a new Rectangle object with the same length and width as that of the invoking object, but the upper-left corner being shifted by dispX and dispY in the x and y directions, respectively (we assume dispX and dispY are non-negative) */ (e) public int area() // return the area of the invoking Rectangle object (f) public String toString() /* return a String representation of the invoking Rectangle object */ (g) public boolean intersect(Rectangle r) /* return true if Rectangle r overlaps with the invoking object, false otherwise; a simple algorithm is to test if any corner of r lies on or within the boundary of the invoking object, or if any corner of the invoking rectangle lies on or within the boundary of Rectangle r */ The instructor will provide a test program (named TestRectangle.java) which includes a main() method for testing your Rectangle class. Basically, the main() method creates several Rectangle objects and tests the constructors and other methods of the Rectangle class. To turn in the assignment, you need to prepare a diskette to save your program files (Rectangle.java, TwoDPoint.java, and TestRectangle.java). Print a hardcopy copy of your Rectangle.java file and a hardcopy of the output when running the program. Put all required materials in a brown envelope, and write on the envelope your name, SS#, and the Java environment (jdk or JBuilder) which you use in preparing your program.