In java, and please comment code Using: class Shape: class Point: circle Extensi
ID: 3814884 • Letter: I
Question
In java, and please comment code
Using:
class Shape:
class Point:
circle Extension:
rectangle Extension:
10 Points Given the definitions of the class Point, the abstract Class Shape, and the concrete extensions Circle and Rectangle in Chapter 10 of the zyBook, extend them so that The Shape abstract superclass has also an abstract method called double computePerimetert) that computes the perimeter of the shape Point class contains has also the public method o double distance (Point p): which computes the distance from a given point p passed in as a parameter; Circle subclass, concrete extension of Shape, has also the following public methods: o void movecenter(Point P): changes the center to point P, radius remains unchanged; o void changeRadius(double r): changes the value of the radius to r Triangle subclass is a concrete extension of Shape, and has the following private attributes o private Point vertex1; o private Point vertex2; o private Point vertex3; And the following public methods: o boolean is Right(): returns true if the triangle is a right triangle, false otherwise o boolean is Equilateral 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. Rectangle subclass, concrete extension of Shape, has also the following public method o boolean is Square(): 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 Finally, create a class NewTestShapes containing a main program which will test all the functionalities of the created classes. A minimum set of tests should include: 1. randomly generate 3 objects of type Point, P1, P2 and P3, and compute the distance between each pair of points;Explanation / Answer
public abstract class Shape {
protected Point position;
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;
}
public abstract double computeArea();
public abstract double computePerimeter();
}
--------------------------------------------------------------------
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 Dx = x-p.x;
double Dy = y-p.y;
return Math.sqrt(Dx*Dx + Dy*Dy);
}
@Override
public String toString() {
return "("+ getX() + ", " + getY()+")";
}
}
--------------------------------------------------------------------
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));
}
@Override
public double computePerimeter() {
return 2*Math.PI*radius;
}
public void changeRadius(double radius) {
this.radius = radius;
}
public void moveCenter(Point newCenter) {
movePositionRelative(newCenter);
}
}
--------------------------------------------------------------------
public class Rectangle extends Shape {
private double length, height;
public Rectangle(Point upperLeft, double length, double height) {
this.position = upperLeft;
this.length = length;
this.height = height;
}
@Override
public double computeArea() {
return (length * height);
}
@Override
public double computePerimeter() {
return 2 * (length + height);
}
public boolean isSquare() {
if (length == height) {
return true;
}
return false;
}
}
--------------------------------------------------------------------
public class Triangle extends Shape {
private Point vertex1, vertex2, vertex3;
public Triangle(Point vertex1, Point vertex2, Point vertex3) {
this.vertex1 = vertex1;
this.vertex2 = vertex2;
this.vertex3 = vertex3;
}
@Override
public double computeArea() {
/*
* If (x1, y1) , (x2, y2), (x3, y3) are three vertexes,
* then they are collinear if
*
* x2 - x1 x3 - x2
* ------- = --------- ==> (x2-x1)(y3-y2) = (x3-x2)(y2-y1)
* y2 - y1 y3 - y2
*
*/
double LHS = (vertex2.getX() - vertex1.getX()) * (vertex3.getY() - vertex2.getY());
double RHS = (vertex3.getX() - vertex2.getX()) * (vertex2.getY() - vertex1.getY());
if (LHS == RHS) {
return 0;
}
/*
* Heron's Formula
*/
double A = vertex1.distance(vertex2);
double B = vertex2.distance(vertex3);
double C = vertex3.distance(vertex1);
double S = (A+B+C)/2;
return Math.sqrt(S*(S-A)*(S-B)*(S-C));
}
@Override
public double computePerimeter() {
double A = vertex1.distance(vertex2);
double B = vertex2.distance(vertex3);
double C = vertex3.distance(vertex1);
return A+B+C;
}
public boolean isRight() {
double A = vertex1.distance(vertex2);
double B = vertex2.distance(vertex3);
double C = vertex3.distance(vertex1);
return ((A*A + B*B == C*C) || (B*B + C*C == A*A) || (A*A + C*C == B*B));
}
public boolean isEquilateral() {
double A = vertex1.distance(vertex2);
double B = vertex2.distance(vertex3);
double C = vertex3.distance(vertex1);
return (A==B && B==C);
}
}
--------------------------------------------------------------------
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class NewTestShapes {
public static void main(String args[]) {
Random random = new Random();
/* TEST CASE - 1 */
Point point1 = new Point(random.nextInt(), random.nextInt());
Point point2 = new Point(random.nextInt(), random.nextInt());
Point point3 = new Point(random.nextInt(), random.nextInt());
double p1ToP2 = point1.distance(point2);
double p1ToP3 = point1.distance(point3);
double p2ToP3 = point2.distance(point3);
System.out.println("P1 = "+ point1.toString()+", P2 = "+point2.toString()+", P3 = "+point3.toString());
System.out.println("Distance between P1 and P2 is "+p1ToP2);
System.out.println("Distance between P1 and P3 is "+p2ToP3);
System.out.println("Distance between P2 and P3 is "+p1ToP3);
/* TEST CASE - 2 */
Triangle triangle = new Triangle(point1, point2, point3);
System.out.println(" Area of the triangle is " + triangle.computeArea());
System.out.println("Perimeter of the triangle is " + triangle.computePerimeter());
System.out.println("Is right-angled triangle ? " + triangle.isRight());
System.out.println("Is Equilateral triangle ? " + triangle.isEquilateral());
/* TEST CASE 3
*
* -- three rectangles defined by P1 and P2, P2 and P3, P1 and P3
*
* It won't make sense, because the points are randomly generated
* and it is not always possible for one point to be upper-right
* and other to be lowerleft
*
* Instead we assume the given point to be upper left and
* use the absolute x and y distances to the other as length and height from it
*
* */
Rectangle rectOne = new Rectangle(point1, Math.abs(point1.getX() - point2.getX()), Math.abs(point1.getY() - point2.getY()));
Rectangle rectTwo = new Rectangle(point2, Math.abs(point2.getX() - point3.getX()), Math.abs(point2.getY() - point3.getY()));
Rectangle rectThree = new Rectangle(point1, Math.abs(point1.getX() - point3.getX()), Math.abs(point1.getY() - point3.getY()));
System.out.println(" Is rectangle-1 a square ? " + rectOne.isSquare());
System.out.println("Is rectangle-2 a square ? " + rectTwo.isSquare());
System.out.println("Is rectangle-3 a square ? " + rectThree.isSquare());
System.out.println(" Area of rectangle-1 is " + rectOne.computeArea());
System.out.println("Area of rectangle-2 is " + rectTwo.computeArea());
System.out.println("Area of rectangle-3 is " + rectThree.computeArea());
System.out.println(" Perimeter of rectangle-1 is " + rectOne.computePerimeter());
System.out.println("Perimeter of rectangle-2 is " + rectTwo.computePerimeter());
System.out.println("Perimeter of rectangle-3 is " + rectThree.computePerimeter());
/* TEST CASE 4 */
Circle circleOne = new Circle(point1, p1ToP2);
Circle circleTwo = new Circle(point2, p2ToP3);
System.out.println(" Area of circle-1 is " + circleOne.computeArea());
System.out.println("Area of circle-2 is " + circleTwo.computeArea());
System.out.println(" Perimeter of circle-1 is " + circleOne.computePerimeter());
System.out.println("Perimeter of circle-2 is " + circleTwo.computePerimeter());
/* TEST CASE 5 */
List<Shape> shapeList = new ArrayList<Shape>();
shapeList.add(rectOne);
shapeList.add(rectTwo);
shapeList.add(rectThree);
shapeList.add(circleOne);
shapeList.add(circleTwo);
shapeList.add(triangle);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.