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

2) 3) 4) Extend: 1) The Shape abstract superclass to have an abstract method cal

ID: 3824802 • Letter: 2

Question

2)

3)

4)

Extend:

1) The Shape abstract superclass to have an abstract method called double computePerimeter() that computes the perimeter of the shape

2) Point class contains the public method

- double distance(Point p): which computes the distance from a given point p passed in as a parameter;

3) Circle subclass, concrete extension of Shape, has also the following public methods:

- void moveCenter(Point P): changes the center to point P, radius remains unchanged;

- void changeRadius(double r): changes the value of the radius to r

4) Triangle subclass is a concrete extension of Shape, and has the following private attributes:

- private Point vertex1;

- private Point vertex2;

- private Point vertex3;

And the following public methods:

- boolean isRight(): returns true if the triangle is a right triangle, false otherwise

- boolean isEquilateral(): returns true if the triangle is equilateral, false otherwise

Note: Use Heron’s formula to compute the area and use Pythagorean theorem to check if the triangle is right. Also, if the 3 given vertices are on a line, and therefore we do not have a triangle, computeArea should return the value 0.

5) Rectangle subclass, concrete extension of Shape, has also the following public method:

- boolean isSquare(): returns true if the rectangle is a square, false otherwise

Note: A rectangle is defined by 2 points (upper right corner and lower left corner). If the points have the same x coordinate or the same y coordinate, the rectangle is actually not defined. In this case, computeArea should return the value 0.

Explanation / Answer

Note: A rectangle is defined by 2 points (upper right corner and lower left corner). If the points have the same x coordinate or the same y coordinate, the rectangle is actually not defined. In this case, computeArea should return the value 0.

In given code, rectangle is being defined using upper left corner and height and length so not chnaging that and working based on it.

=====================================

Point.java

public 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;
}

public void setX(double x) {
this.x = x;

return;
}

public void setY(double y) {
this.y = y;

return;
}

public double distance(Point p)
{
double diffX = p.x - x;
double diffY = p.y - y;
return Math.sqrt(diffX*diffX + diffY*diffY);
}
}

Shape.java

public abstract class Shape {
protected Point position;
abstract double computeArea();
abstract double computePerimeter();
public Point getPosition() {
return this.position;
}
public void setPosition(Point position) {
this.position = position;
}
public void movePositionRelative(Point position) {
double x = this.position.getX() + position.getX();
double y = this.position.getY() + position.getY();
this.position.setX(x);
this.position.setY(y);
return;
}
}

Circle.java

public class Circle extends Shape {
private double radius;
public Circle(Point center, double radius) {
this.radius = radius;
this.position = center;
}
@Override
public double computeArea() {
return (Math.PI * Math.pow(radius, 2));
}

void moveCenter(Point P)
{
setPosition(P);
}

void changeRadius(double r)
{
this.radius = r;
}

@Override
double computePerimeter() {
  
return 2*Math.PI*this.radius;
}
}

Triangle.java


public class Triangle extends Shape{
private Point vertex1;
private Point vertex2;
private Point vertex3;
  
private double side1, side2, side3;
  
public Triangle(Point v1, Point v2, Point v3)
{
this.vertex1 = v1;
this.vertex2 = v2;
this.vertex3 = v3;
  
side1 = vertex1.distance(vertex2);
side2 = vertex2.distance(vertex3);
side3 = vertex3.distance(vertex1);
}
boolean isEquilateral()
{
if (computeArea() == 0)
return false;
return (side1 == side2 && side2 == side3);
}
boolean isRight()
{
if (computeArea() == 0)
return false;
if (side1 > side2 && side1 > side3){
return side1*side1 == (side2*side2 +side3*side3);
}
  
if (side2 > side1 && side2 > side3){
return side2*side2 == (side1*side1 +side3*side3);
}
  
return side3*side3 == (side2*side2 +side1*side1);
}
@Override
double computeArea() {
  
double s = computePerimeter()/2;
double area = Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
return area;
}

@Override
double computePerimeter() {
return side1 + side2 +side3;
}
  

}

Rectangle.java

public class Rectangle extends Shape {
private double length, height;
Rectangle(Point upperLeft, double length, double height) {
this.position = upperLeft;
this.length = length;
this.height = height;
}
@Override
public double computeArea() {
return (length * height);
}
boolean isSquare()
{
return length == height;
}
@Override
double computePerimeter() {
  
return 2*(length+height);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote