Answer to Question #298837 in Java | JSP | JSF for Bruhh

Question #298837

2. Rectangle

by CodeChum Admin


A rectangle can be formed given two points, the top left point and the bottom right point. Assuming that the top left corner of the console is point (0,0), the bottom right corner of the console is point (MAX, MAX) and given two points (all "x" and "y" coordinates are positive), you should be able to draw the rectangle in the correct location, determine if it is a square or a rectangle, and compute for its area, perimeter and center point.


More details here, Important!

pastebin.com/8NDksx9M


1
Expert's answer
2022-02-17T05:21:25-0500


import java.util.Scanner;


/**
 * class for a point (that has an x-coordinate and a y-coordinate).
 *
 */
class Point {
	private double x;
	private double y;


	public Point(double x, double y) {
		this.x = x;
		this.y = y;
	}


	public double getX() {
		return x;
	}


	public double getY() {
		return y;
	}


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


class Rectangle {
	private Point topLeft;
	private Point bottomRight;


	public Rectangle(Point topLeft) {
		this.topLeft = topLeft;
		this.bottomRight = new Point(0, 0);
	}


	/**
	 * display() - draws the rectangle on the console based on the sample
	 */
	public void display() {
		int width = (int) (bottomRight.getX() - topLeft.getX());
		width = Math.abs(width);
		int height = (int) (topLeft.getY() - bottomRight.getY());
		height = Math.abs(height);
		for (int i = 0; i <= height; i++) {
			for (int j = 0; j <= width; j++) {
				if (j == 0 || j == width || i == 0 || i == height) {
					System.out.print("#");
				} else {
					System.out.print(".");
				}
			}
			System.out.println();
		}
		System.out.println(isSquare() ? "\nSQUARE" : "\nRECTANGLE");
		System.out.println("AREA: " + area());
		System.out.println("PERIMETER: " + perimeter());
		System.out.println("CENTER POINT: " + centerPoint());
	}


	/**
	 * area() - computes and returns the area of a given rectangle
	 * 
	 * @return
	 */
	private double area() {
		return Math.abs((bottomRight.getX() - topLeft.getX())) * Math.abs((topLeft.getY() - bottomRight.getY()));
	}


	/**
	 * perimeter() - computes and returns the perimeter of a given rectangle
	 * 
	 * @return
	 */
	private double perimeter() {
		return 2 * (Math.abs((bottomRight.getX() - topLeft.getX())) + Math.abs((topLeft.getY() - bottomRight.getY())));
	}


	/**
	 * centerPoint() - computes and returns the center point of a given rectangle
	 * 
	 * @return
	 */
	private Point centerPoint() {
		return new Point(Math.abs((bottomRight.getX() - topLeft.getX())) / 2,
				Math.abs((topLeft.getY() - bottomRight.getY())) / 2);
	}


	/**
	 * isSquare() - checks whether a given rectangle is a square or not.
	 * 
	 * @return
	 */
	private boolean isSquare() {
		return Math.abs((bottomRight.getX() - topLeft.getX())) == Math.abs((topLeft.getY() - bottomRight.getY()));
	}
}


public class App {


	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in);
		int x = keyBoard.nextInt();
		int y = keyBoard.nextInt();
		Rectangle rectangle = new Rectangle(new Point(x, y));
		rectangle.display();
		keyBoard.close();
	}
}

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