Answer to Question #275092 in Java | JSP | JSF for harlington

Question #275092

Encapsulation

Create a class for a point (that has an x-coordinate and a y-coordinate). Also, create another class called Rectangle. The Rectangle should have 2 points, the top left and the bottom right. You should also implement the following methods for the Rectangle:


display() - draws the rectangle on the console based on the sample

area() - computes and returns the area of a given rectangle

perimeter() - computes and returns the perimeter of a given rectangle

centerPoint() - computes and returns the center point of a given rectangle

isSquare() - checks whether a given rectangle is a square or not.

Input


A pair of numbers representing the points for the rectangle (x and y).

5 8


Output

The first few lines contain the drawn rectangle. After one empty line, the next line prints either "RECTANGLE" or "SQUARE". This is then followed by its area, perimeter, and the coordinates of its center point.


RECTANGLE

AREA:·40

PERIMETER:·26

CENTER·POINT:·(2.50,4.00)


1
Expert's answer
2021-12-03T12:34:39-0500
public class Rectangle {
  private Point topLeft;
  private Point bottomRight;
  public Rectangle(Point topLeft, Point bottomRight) {
    this.topLeft=topLeft;
    this.bottomRight=bottomRight;
  }
  public void setTopLeft(Point topLeft) {
    this.topLeft=topLeft;
  }
  public void setBottomRight(Point bottomRight) {
    this.bottomRight=bottomRight;
  }
  public Point getTopLeft() {
    return topLeft; 
  }
  public Point getBottomRight() {
    return bottomRight;
  }
  public void display() {
    for (int i=0; i<2+bottomRight.x-topLeft.x; i++) 
      System.out.print('*');
      System.out.println();
    for (int i=0; i<topLeft.y-bottomRight.y; i++) {
      System.out.print('*');
      for (int i=0; i<bottomRight.x-topLeft.x; i++)
        System.out.print(' ');
      System.out.println('*');
    }
    for (int i=0; i<2+bottomRight.x-topLeft.x; i++)
      System.out.print('*');
      System.out.println();
  }
  public double area() {
    return (bottomRight.x-topLeft.x) * (topLeft.y-bottomRight.y);
  }
  public double perimeter() {
    return 2 * (bottomRight.x-topLeft.x+topLeft.y-bottomRight.y);
  }
  public Point centerPotin() {
    double x = topLeft.x+bottomRight.x;
    double y = topLeft.y+bottomRight.y;
    return new Point(x/2, y/2);
  }
  public bool isSquare() {
    return (bottomRight.x-topLeft.x) == (topLeft.y-bottomRight.y)
  }
}
class Point {
  private double x;
  private double y;
  Point(double x, double y) {
    this.x=x;
    this.y=y;
  }
  public double getX() { return x; }
  public double getY() { return y; }
  public void setX(double x) {
    this.x=x;
  }
  public void setY(double y) {
    this.y=y;
  }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS