Question

In: Computer Science

JAVA: Suppose we have a number of triangles and quadrilaterals in a 2-dimensional coordinate plane. we...

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

Solutions

Expert Solution

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.


Related Solutions

Consider a two-dimensional ideal flow in the x-y plane (with radial coordinate r2 = x2 +...
Consider a two-dimensional ideal flow in the x-y plane (with radial coordinate r2 = x2 + y2). Given the velocity potentials of 1) a uniform flow, 2) a source φ = (q/2π) ln r, and 3) a dipole φ = −d · r/(2πr2): a) Using the principle of superposition, construct a linear combination of the ingredients above that gives the flow past an infinite cylinder. [10 points] b) Sketch the streamlines of the flow everywhere in space. [10 points]
3. Suppose we have a coordinate system (x, y, z) with the origin at one corner...
3. Suppose we have a coordinate system (x, y, z) with the origin at one corner of a cube, and the axes parallel to the edges of the cube. We want to perform a rotation to a coordinate system (x' , y' , z' ), where the x 0 axis is along the diagonal of the cube, and the y' axis remains in the original x ? y plane. (a) (0.5 points) Using the Z-Y’-Z” Euler angle convention that is...
So we have a paraboloid x^2 + y^2 - 2 = z and the plane x...
So we have a paraboloid x^2 + y^2 - 2 = z and the plane x + y +z = 1 how do we find the center of mass? For some reason we have to assume the uniform density is 8? Seems complicated because I don't know where to start?
[JAVA] How to do this problem. we have to tell if given list of three number...
[JAVA] How to do this problem. we have to tell if given list of three number is in ascending, descending or no order. Instructions You will be given three integers: ?, ? and ? (read in for you). Then you will be given two boolean values that tell you which way the numbers are going - if the numbers are in increasing order or in decreasing order, respectively. Details Input The program reads in: a sequence of three integers: ?,...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 2: Find the largest value of each row of a 2D array             (filename: FindLargestValues.java) Write a method with the following header to return an array of integer values which are the largest values from each row of a 2D array of integer values public static int[] largestValues(int[][] num) For example, if...
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2....
java by using Scite Objectives: 1. Create one-dimensional arrays and two-dimensional arrays to solve problems 2. Pass arrays to method and return an array from a method Problem 1: Find the average of an array of numbers (filename: FindAverage.java) Write two overloaded methods with the following headers to find the average of an array of integer values and an array of double values: public static double average(int[] num) public static double average(double[] num) In the main method, first create an...
Suppose we have a sack with 2 red balls and 2 black balls, and we draw...
Suppose we have a sack with 2 red balls and 2 black balls, and we draw balls without replacement until the second red ball is drawn. Describe the random variable ? = "the number of balls drawn". Describe by giving the range, probability distribution, expected value, standard deviation, and variance.
1)In a tow dimensional plane draw a graph of y=3x-1 and y=2x^2. 2)Find the roots of...
1)In a tow dimensional plane draw a graph of y=3x-1 and y=2x^2. 2)Find the roots of the equation 2x^2 -3x+1=0 3)If function f(x)=2x^2-3x+1, what the value of x where f(x) reaches its minimum?
Suppose we have the following values on the number of customers (X) and the average profits...
Suppose we have the following values on the number of customers (X) and the average profits (Y) for fifteen stores: Store                   Customers (X)          Average Profits (Y) A                               161                              157 B                               99                              93 C                               135                              136 D                               120                              123 E                                164                              153 F                                221                              241 G                               179                              201 H                               204                              206 I                                 214                              229 J                                 101                              135 K                               231                              224 L                                206                              195 M                               248                              242 N      ...
Suppose we have the following values on the number of customers (X) and the average profits...
Suppose we have the following values on the number of customers (X) and the average profits (Y) for fifteen stores: Store                   Customers (X)            Average Profits (Y) A                               161                              157 B                               99                              93 C                               135                              136 D                               120                              123 E                                164                              153 F                                221                              241 G                               179                              201 H                               204                              206 I                                 214                              229 J                                101                              135 K                               231     ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT