Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 4: A Rectangle made of Points 1. Create a new Java project and then cre

ID: 3739486 • Letter: E

Question

Exercise 4: A Rectangle made of Points 1. Create a new Java project and then create a new class called Point that represents a point on the xy- plane as a pair of integer values (x,y) to represent a point using the screen coordinates; i.e. the screen's top-left corner is the origin. Provide getter methods and a no-arg constructor that initializes the default values of x and y to positive random values satisfying x600 and y 400 (Hint: use SecureRandom- as in Lab5). Also include a 2-arg constructor that initializes x and y to values passed as arguments. Ensure that the x and y values are all positive and that x600 and y 400 2. Proide a method called getDistance0 that takes a Point as a parameter and return the distance between the current point and the parameter. Signature is: public double getDistance(Point aPoint) 3. Provide a toString0 method that returns the current Point as a pair in the format (x.y) 4. In the same project create another class called Rectangle that represents a typical rectangle as two "Points" representing the top-left corner and the bottom-right corner of the rectangle (i.e. a Rectangle is composed of two Points or has two points). Include a third String attribute that represents the color of the rectangle, and a boolean attribute called is Filled that evaluates to true when the rectangle is filled with the given color and false otherwise Generate setters and getters for the above attributes. Also generate a parameterized constructor to initialize the attributes using the parameter values. Also include a no-arg constructor that sets topLeft and bottomRight to random Points and other attributes to Java's default values 5. 6. Add a method called getWidth0 and a method called getHeight0 to return the width and height 7. Provide a boolean method called isPortrait0 to return true if the width is smaller than the height and false otherwise. 8. Provide a method to return the area of a rectangle 9. Povide a toString) method that returns a string representing details of the current rectangle and its area, whether it is portrait or land scape 10. Provide a method that compares the area of the current rectangle with that of another rectangle; it returns 1 if the current rectangle is larger, -1 if the current rectangle is smaller, and 0 if both are equal. The method signature is public int compareTo (Rectangle other) 11. Create a RectangleTest class in which you demonstrate features of your Rectangle and Point classes.

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


Point.java
==========
import java.util.Random;

public class Point {
private int x, y;

public Point()
{
Random rand = new Random();
setX(1 + rand.nextInt(600));
setY(1 + rand.nextInt(400));

}

public Point(int x, int y)
{
setX(x);
setY(y);
}

public int getX() {
return x;
}

public void setX(int x) {
if(x >= 0 && x <= 600)
this.x = x;
else if(x < 0)
this.x = 0;
else if(x > 600)
this.x = 600;
}

public int getY() {
return y;
}

public void setY(int y) {
if(y >= 0 && y <= 400)
this.y = y;
else if(y < 0)
this.y = 0;
else if(y > 400)
this.y = 400;
}


public double getDistance(Point p)
{
int dx = x - p.x;
int dy = y - p.y;
return Math.sqrt(dx * dx + dy * y);
}

public String toString()
{
return "(" + x + "," + y +")";
}
}

Rectangle.java
-------------

public class Rectangle {
private Point topLeft, bottomRight;
private String color;
private boolean isFilled;
public Rectangle()
{
topLeft = new Point();
bottomRight = new Point();
color = null;
isFilled = false;
}

public Rectangle(Point topLeft, Point bottomRight, String color, boolean isFilled)
{
this.topLeft = topLeft;
this.bottomRight = bottomRight;
this.color = color;
this.isFilled = isFilled;
}

public Point getTopLeft() {
return topLeft;
}

public void setTopLeft(Point topLeft) {
this.topLeft = topLeft;
}

public Point getBottomRight() {
return bottomRight;
}

public void setBottomRight(Point bottomRight) {
this.bottomRight = bottomRight;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public boolean isFilled() {
return isFilled;
}

public void setFilled(boolean isFilled) {
this.isFilled = isFilled;
}

public int getHeight()
{
return bottomRight.getY() - topLeft.getY();
}

public int getWidth()
{
return bottomRight.getX() - topLeft.getX();
}


public boolean isPortrait()
{
return getWidth() < getHeight();
}

public int getArea()
{
return getWidth() * getHeight();
}

public String toString()
{
String s = "Rectangle: top-left = " + topLeft +", bottom-right = " + bottomRight ;
if(color != null)
s += ", color = " + color;

s += ", filled = " + isFilled;
s += ", width = " + getWidth() + ", height = " + getHeight() ;
s += ", area = " + getArea() ;
if(isPortrait())
s += ", portrait";
else
s += ", landscape";
return s;
}

public int compareTo(Rectangle r)
{
int a1 = getArea();
int a2 = r.getArea();
if(a1 > a2)
return 1;
else if(a1 == a2)
return 0;
else
return -1;
}
}

RectangleTest.java
---------------

public class RectangleTest {
public static void main(String[] args) {
Point top = new Point(100, 120);
Point bottom = new Point(300, 400);
Rectangle r1 = new Rectangle(top, bottom, "red", true);
System.out.println(r1);
}
}


output
======
Rectangle: top-left = (100,120), bottom-right = (300,400), color = red, filled = true, width = 200, height = 280, area = 56000, portrait