In: Computer Science
JAVA: Suppose we have a number of triangles and quadrilaterals in a 2-dimensional coordinate plane. we want to be able to easily determine the perimeter of each shape and move shapes around the coordinate plane. Download the Shape.java and Point.java from Moodle. - Shape.java is an interface contains getPerimeter() and move() abstract methods. - Point.java is a class object which provides getX(), getY(), distanceTo(Point p) and move() methods. Design two classes named Quadrilateral and Triangle that implement Shape interface to determine their perimeter of each shape and move shapes around the coordinate plane. Test your classes in a Driver class that use (1.0, 1.0), (1.0, 2.0), (2.0, 1.0) and (2.0, 2.0) as coordinates for Quadrilateral vertices and (1.0, 1.0), (1.0, 2.0), (2.0, 1.0) as coordinate for Triangle. Use methods from Point Object to calculate their perimeters and display their results. Use move method from Point Object to increment x coordinate by 1.0 and y coordinate by 2.0 for all vertices of both shapes. Calculate their perimeters and display their results.
SHAPE
import java.util.*;
public interface Shape {
public double getPerimeter();
public void move(double deltaX, double deltaY);
}
POINT
import java.util.*;
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 double distanceTo(Point p) {
return Math.sqrt(Math.pow(this.getX() - p.getX(), 2) +
Math.pow(this.getY() - p.getY(), 2));
}
public void move(double deltaX, double deltaY) {
x += deltaX;
y += deltaY;
}
}
java code:
Shape.java
//interface
public interface Shape {
public double getPerimeter();
public void move(double deltaX, double deltaY);
}
Point.java
//point
public class Point {
//variable
private double x;
private double y;
//set points
public Point(double x, double y) {
this.x = x;
this.y = y;
}
//return points
public double getX() {
return x;
}
public double getY() {
return y;
}
//calculate distance
public double distanceTo(Point p) {
return Math.sqrt(Math.pow(this.getX() - p.getX(), 2) +
Math.pow(this.getY() - p.getY(), 2));
}
//move the point
public void move(double deltaX, double deltaY) {
this.x += deltaX;
this.y += deltaY;
}
}
//quadrilateral
class Quadrilateral implements Shape
{
//set variables
double a,b,c,d;
Point p1,p2,p3,p4; //points
//constructor
Quadrilateral(Point p1,Point p2,Point p3,Point p4) {
this.p1 =p1;
this.p2 =p2;
this.p3 =p3;
this.p4 =p4;
this.a = p1.distanceTo(p2);
this.b = p2.distanceTo(p3);
this.c = p3.distanceTo(p4);
this.d = p4.distanceTo(p1);
}
//return perimeter
@Override
public double getPerimeter() {
return (a+b+c+d);
}
//move
@Override
public void move(double deltaX, double deltaY) {
p1.move(deltaX, deltaY);
p2.move(deltaX, deltaY);
p3.move(deltaX, deltaY);
p4.move(deltaX, deltaY);
this.a = p1.distanceTo(p2);
this.b = p2.distanceTo(p3);
this.c = p3.distanceTo(p4);
this.d = p4.distanceTo(p1);
}
}
//triangle class
class Triangle implements Shape
{
double x,y,z;
Point p1,p2,p3;
//constructor
Triangle(Point p1, Point p2, Point p3)
{
this.p1 =p1;
this.p2 =p2;
this.p3 =p3;
this.x = p1.distanceTo(p2);
this.y = p2.distanceTo(p3);
this.z = p3.distanceTo(p1);
}
//return perimeter
@Override
public double getPerimeter() {
return (x+y+z);
}
//move
@Override
public void move(double deltaX, double deltaY) {
p1.move(deltaX, deltaY);
p2.move(deltaX, deltaY);
p3.move(deltaX, deltaY);
this.x = p1.distanceTo(p2);
this.y = p2.distanceTo(p3);
this.z = p3.distanceTo(p1);
}
}
//point driver
public class PointDriver {
public static void main(String[] args)
{
//set point
Point p1=new Point(1.0, 1.0);
Point p2=new Point(1.0, 2.0);
Point p3=new Point(2.0, 1.0);
//object for trianlge
Shape t =new Triangle(p1,p2,p3);
//object for quadrilateral
Shape q =new Quadrilateral(new Point(1.0, 1.0), new Point(1.0,
2.0), new Point(2.0, 1.0),new Point(2.0, 2.0));
//call function and display results
System.out.println("Triangle Perimenter: "+t.getPerimeter());
System.out.println("Quadrilateral Perimenter:
"+q.getPerimeter());
q.move(1.0,2.0);
t.move(1.0,2.0);
System.out.println("Triangle Perimenter after move:
"+t.getPerimeter());
System.out.println("Quadrilateral Perimenter after move:
"+q.getPerimeter());
}
}
output:
since all points moves with equal distance, the perimeter remains same even after the move.
if you found this solution please give me thumbs up, any issues please comment here.