Starting from:

$25

CS5007- HomeWork 3 Solved

HomeWork 3 - CS 5007

1             Point 
State a class Point that has two instance variables, its rational coordinates self.x and self.y.

•     Implement in this class a constructor with two arguments, the values of the coordinates. Each argument should have a default value 0.0, allowing to call this method without arguments.

•     Write two methods named getX and getY that respecitvely return the value of self.x and the value of self.y.

•     Write a method named setX that takes a rational value as argument and modifies self.x so as it becomes equal to this value.

•     Write a method named setY that takes a rational value as argument and modifies self.y so as it becomes equal to this value.

•     Write a method named toString that returns a string containing the two coordinates of the point self, within parenthesis and separated by a comma.

•     Write a method named equals that takes another object of class Point as argument and returns True if the x and y coordinates of this points are equal to the x and y coordinates of self, and returns False otherwise.

Outside from the class, write the following statements: Create a point p1 of coordinates (0,0) and un point p2 of coordinates (1,2). Print out the coordinates of the two points on the same line, by calling toString on the two points. Print the result of applying the method equals on point p1, using p2 as argument. Set the x coordinate of p2 equal to the x coordinate of p1, using the methods setX and getX. Set the y coordinate of p2 equal to the y coordinate of p1, using the method setY and getY. Print again the result of applying the method equals on point p1, using p2 as argument. Increment by 1 the x coordinate of p2, using the methods setX and getX . Print again the result of applying the method equals on point p1, using p2 as argument.

2             Rectangle
State a class Rectangle that has three instance variables: a point (an object of the class Point previously defined, corresponding to the left bottom corner), a rational width and a rational height.

•     Implement in this class a constructor with three arguments, a point and two rational values, to be assigned to the three instance variables. No default values should be stated (the constructor must be called with three arguments).

•     Write a method named perimeter that returns the perimeter of the rectangle.

•     Write a method named area that returns the area of the rectangle.

•     Write a method named getOrigin that returns a reference to the point representing the left bottom corner of the rectangle.

•     Write a method named toString that returns a string containing, on the same line, the string representing the bottom left corner, followed by the rectangle width and height, formatted as follows (the values will differ depending on each object):

(0.0,0.0), L=2, H=5.5.

Outside from the class, write the following statements: Create a rectangle r1 whose bottom left corner is p1, width is 2 and height is 5.5. Create a rectangle r2 whose bottom left corner is p2, width is 3 and height is 6. Print out the bottom left corner of r1 and the perimeter of r1, by calling the appropriate methods. Print out the bottom left corner of r2 and the area of r2, by calling the appropriate methods. Print the result of the call to the method toString respectively applied to the two rectangles r1 and r2.

3             Movable rectangle 
State a class MovableRectangle that inherits from class Rectangle. This class has a supplementary instance variable, assumed to be boolean, called self.move.

•     Implement in this class a constructor with three arguments, a point and two rational values, to be assigned to the three instance variables. self.move is initially always set to False. Your code MUST call the constructor of the superclass.

•     Write a method named unlock that sets self.move to True.

•     Write a method named lock that sets self.move to False.

•     Write a method named moveTo that takes an object of class Point as argument, and: If the rectangle is unlocked, moves it so as its new bottom left is the point given as argument. Otherwise, the method prints the following message: Warning: locked.

•     Write a method named toString that returns a string containing, on the same line, the string representing the bottom left corner, followed by the rectangle width and height, followed by the current status (locked or unlocked) formatted as follows (the values will differ depending on each object):

(5,15), W=2, H=2, Movable? True

(if it is locked, the end of string should be ...Movable? False).

Outside from the class, write the following statements: Create a movable rectangle r3 whose bottom left corner is p1, width is 2 and height is 2. Print the rectangle r3, using toString. Create a point p3 of coordinates (5,15). Call the method moveTo on rectangle r3 in order to try moving its origin to p3. Print again the rectangle r3, using toString. Unlock r3. Call again the method moveTo on rectangle r3 in order to try moving its origin to p3. Print again the rectangle r3, using toString.

More products