public class TestShapes { public static void main( String [] args) { Circle circ
ID: 3694174 • Letter: P
Question
public class TestShapes {
public static void main(String[] args) {
Circle circle1 = new Circle(new Point(0.0, 0.0), 1.0);
Circle circle2 = new Circle(new Point(0.0, 0.0), 2.0);
Shape rectangle = new Rectangle(new Point(0.0, 0.0), new Point(1.0, 1.0));
System.out.println("Area of circle 1 is: " + circle1.computeArea());
System.out.println("Area of circle 2 is: " + circle2.computeArea());
System.out.println("Area of rectangle is: " + rectangle.computeArea());
return;
}
}
Create a subclass titled ‘Triangle’ and is a concrete extension 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
Explanation / Answer
package assignment;
import java.awt.Point;
public class TestTriangleShape {
public static void main(String[] args) {
Triangle t1 = new Triangle(new Point(0,0),new Point(0,3),new Point(4,0));
System.out.println(" Equilateraol : "+t1.isEquilateral()+ " Right Angle:"+ t1.isRight());
}
}
class Triangle {
private Point vertex1;
private Point vertex2;
private Point vertex3;
// returns true if the triangle is a right triangle, false otherwise
public Triangle(Point vertex1, Point vertex2, Point vertex3) {
this.vertex1 = vertex1;
this.vertex2 = vertex2;
this.vertex3 = vertex3;
}
public boolean isRight() {
//Pythagorean theorem: in right angle triangle, largest side should be equal to sqrt of (sum of squares of other sides)
//find largest side
double h = 0, a = 0, b = 0;
double sides[] = getSides();
if(sides[0] > sides [1]) {
a = sides[1];
if(sides[0] > sides [2]) {
h = sides[0];
b = sides[2];
} else {
h = sides[2];
b = sides[0];
}
} else {
a = sides[0];
if(sides[1] > sides [2]) {
h = sides[1];
b = sides[2];
} else {
h = sides[3];
b = sides[1];
}
}
if(h == Math.sqrt(a * a + b* b))
return true;
return false;
}
//: returns true if the triangle is equilateral, false otherwise
public boolean isEquilateral() {
double sides[] = getSides();
if(sides[0] == sides[1] && sides[1] == sides[2])
return true;
return false;
}
private double[] getSides() {
double sides[] = new double[]{0,0,0};
//side1.
//distance between point p1(x1,y1) and point p2(x2,y2) is give by sqrt((x1-x2)^2+(y1-y2)^2)
sides[0] = Math.sqrt(Math.pow(vertex1.getX()-vertex2.getX(), 2)
+Math.pow(vertex1.getY()-vertex2.getY(), 2));
sides[1] = Math.sqrt(Math.pow(vertex3.getX()-vertex2.getX(), 2)
+Math.pow(vertex3.getY()-vertex2.getY(), 2));
sides[2] = Math.sqrt(Math.pow(vertex1.getX()-vertex3.getX(), 2)
+Math.pow(vertex1.getY()-vertex3.getY(), 2));
return sides;
}
public Point getVertex1() {
return vertex1;
}
public void setVertex1(Point vertex1) {
this.vertex1 = vertex1;
}
public Point getVertex2() {
return vertex2;
}
public void setVertex2(Point vertex2) {
this.vertex2 = vertex2;
}
public Point getVertex3() {
return vertex3;
}
public void setVertex3(Point vertex3) {
this.vertex3 = vertex3;
}
}
--outut------
Equilateraol : false Right Angle:true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.