Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please just help me check where is the mistake and make the program works. just fix it. i think the mistake would be in the

please just help me check where is the mistake and make the program works.

just fix it.

i think the mistake would be in the method contains method in triangle.java

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

the following is my code

//demo

import java.util.*; public class Triangle2DDemo { public static void main(String []args) { Scanner kb=new Scanner(System.in); int x1,y1,x2,y2,x3,y3; int x11,y11,x12,y12,x13,y13; x1=kb.nextInt(); y1=kb.nextInt(); x2=kb.nextInt(); y2=kb.nextInt(); x3=kb.nextInt(); y3=kb.nextInt(); x11=kb.nextInt(); y11=kb.nextInt(); x12=kb.nextInt(); y12=kb.nextInt(); x13=kb.nextInt(); y13=kb.nextInt(); int tpx=kb.nextInt(); int tpy=kb.nextInt(); Point p1=new Point(x1,y1); Point p2=new Point(x2,y2); Point p3=new Point(x3,y3); Point p11=new Point(x11,y11); Point p12=new Point(x12,y12); Point p13=new Point(x13,y13); Triangle2D t1=new Triangle2D(p1,p2,p3); Triangle2D t2=new Triangle2D(p11,p12,p13); System.out.println(t1); System.out.println(t2); Point test = new Point(tpx, tpy); System.out.println(test); boolean bao = t1.contains(test); System.out.println("T1 contains Point :" + bao); bao=t2.contains(test); System.out.println("T2 contains Point :" + bao); bao=t1.contains(t2); System.out.println("T1 contains T2 :" + bao); bao=t2.contains(t1); System.out.println("T2 contains T1 :" + bao); bao=t1.overlaps(t2); System.out.println("Triangles overlaps:" + bao); } }

//Triangle2D.java

import java.awt.geom.Line2D; //reference form //https://docs.oracle.com/javase/7/docs/api/java/awt/geom/Line2D.Double.html public class Triangle2D { private Point p1; private Point p2; private Point p3;

public Triangle2D() {}

public Triangle2D(Point p1, Point p2, Point p3) { this.p1 = p1; this.p2 = p2; this.p3 = p3; }

public void setPoint1(Point p) { this.p1=p; } public void setPoint2(Point p) { this.p2=p; } public void setPoint3(Point p) { this.p3=p; }

/** Return the area of the triangle. */

public double getArea() { double s1=p1.distance(p2); double s2=p2.distance(p3); double s3=p3.distance(p1); double s=(s1+s2+s3)/2.0; return Math.pow(s*(s-s1)*(s-s2)*(s-s3),0.5); }

public double getPerimeter() { double s1=p1.distance(p2); double s2=p2.distance(p3); double s3=p3.distance(p1); double s=(s1+s2+s3); return s; }

public boolean contains(Point p) { double s; double pp1=p.distance(p1); double pp2=p.distance(p2); double pp3=p.distance(p3); double p1p2=p1.distance(p2); double p2p3=p2.distance(p3); double p3p1=p3.distance(p1); s=(pp1+pp2+p1p2)/2.0; double pp1p2Area=Math.pow(s*(s-pp1)*(s-pp2)*(s-p1p2),0.5); s=(pp2+pp3+p2p3)/2.0; double pp2p3Area=Math.pow(s*(s-pp2)*(s-pp3)*(s-p2p3),0.5); s=(pp3+pp1+p3p1)/2.0; double pp3p1Area=Math.pow(s*(s-pp3)*(s-pp1)*(s-p3p1),0.5); if(getArea()==(pp1p2Area+pp2p3Area+pp3p1Area)) { return true; } else { return false; } } public boolean contains(Triangle2D t) { return contains(t.p1) && contains(t.p2) && contains(t.p3); } public boolean overlaps(Triangle2D t) { Point[] pt1 = getTrianglePoints(); Point[] pt2 = t.getTrianglePoints(); // check is triangle side intersect for (int i = 0; i

private Point[] getTrianglePoints() { Point[] points = new Point[3]; points[0] = p1; points[1] = p2; points[2] = p3; return points; }

@Override public String toString() { return "Triangle: "+p1+ " "+p2+" "+p3; } }

//point class

public class Point { private int xPos; private int yPos; public Point () {} public Point(int x,int y) { xPos=x; yPos=y; } public int getXPos() { return xPos; } public int getYPos() { return yPos; } public void setXPos(int x) { xPos=x; } public void setYPos(int y) { yPos=y; } //count distance fuction public double distance(Point p) { return Math.sqrt(Math.pow(this.xPos-p.getXPos(),2) +Math.pow(this.yPos-p.getYPos(),2)); } //check if it is equal public boolean isEquals(Point p) { if(((this.xPos)==(p.getXPos()))&& ((this.yPos)==(p.getYPos()))) { return true; } else { return false; } } //check if higher public boolean isHigher(Point p) { if(this.yPos>p.getYPos()) return true; else return false; } //toString public String toString() { return "x: "+xPos+" y: "+yPos; } }

Problem 2:: Triangle 2D Instructions Use the following UML and description of methods to define the Triangle2D program Triangle2D p1: Point Point - xPos: int p2: Point yPos: int p3: Point + Point : + Point (xint, y:int) +getXPos (): int +getYPos(): int +setXPos (x: int): void + setYPos (y:int): void +distance (p: Point): double +isEquals (p: Point): boolean + isHigher (p: Point): boolean +toString): String Triangle2D ): Triangle2D (p1: Point, p2: Point, p3: Point): +getPoint1) Point +getPoint2() Point +getPoint3) Point + setPoint1 (p: Point): void +setPoint2 (p: Point): void +setPoint2 (p: Point): void +getArea(): double +getPerimeter(): double +contains (p: Point) boolearn +contains(t: Triangle2D) Boolean +overlaps (t: Triangle2D) Boolean +toString (): Strin Different scenarios: Problem 2:: Triangle 2D Instructions Use the following UML and description of methods to define the Triangle2D program Triangle2D p1: Point Point - xPos: int p2: Point yPos: int p3: Point + Point : + Point (xint, y:int) +getXPos (): int +getYPos(): int +setXPos (x: int): void + setYPos (y:int): void +distance (p: Point): double +isEquals (p: Point): boolean + isHigher (p: Point): boolean +toString): String Triangle2D ): Triangle2D (p1: Point, p2: Point, p3: Point): +getPoint1) Point +getPoint2() Point +getPoint3) Point + setPoint1 (p: Point): void +setPoint2 (p: Point): void +setPoint2 (p: Point): void +getArea(): double +getPerimeter(): double +contains (p: Point) boolearn +contains(t: Triangle2D) Boolean +overlaps (t: Triangle2D) Boolean +toString (): Strin Different scenarios

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intranet And Web Databases For Dummies

Authors: Paul Litwin

1st Edition

0764502212, 9780764502217

More Books

Students also viewed these Databases questions