public class Triangle {
private Point p1,p2,p3;
public Triangle() {
setP1(new Point());
setP2(new Point());
setP3(new Point());
}
public Triangle(Point x, Point y, Point z ) {
setP1(p1);
setP2(p2);
setP3(p3);
}
public Point getP1() {
return new Point(p1);
}
public Point getP2() {
return new Point(p2);
}
public Point getP3() {
return new Point(p3);
}
public void setP1(Point p1) {
if (p1!=null) {
this.p1 = p1;
}
else {
this.p1 = new Point();
}
}
public void setP2(Point p2) {
if (p2!=null) {
this.p2 = p2;
}
else {
this.p2 = new Point();
}
}
public void setP3(Point p3) {
if (p3 != null) {
this.p3=p3;
}
else {
this.p3 = new Point();
}
}
public boolean isTriangle() {
int m1 = (int) (this.p2.getY() - this.p1.getY() / (this.p2.getX()-this.p1.getX()));
int m2 = (int) (this.p3.getY() - this.p1.getY() / this.p3.getX()-this.p1.getX());
if(m1 == m2) {
return false;
}
else {
return true;
}
}
public boolean isRightTriangle() {
double res = p1.getX() * (p2.getY() - p3.getY()) + p2.getX() * (p3.getY() - p1.getY()) + p3.getX() * (p1.getY() - p2.getY());
if(res==0) {
return false;
}
else {
return true;
}
}
public String toString(){
return p1.toString()+ " " + p2.toString() + " " + p3.toString();
}
}```
#my values aren't being passed correctly in my code
10 messages · Page 1 of 1 (latest)
Hey, @acoustic cave!
Please remember to /close this post once your question has been answered!
I have a Point Class as well
public class Point {
private double x, y;
private static final double DEFAULT_COORDINATE=0;
public Point() {
setX(DEFAULT_COORDINATE);
setY(DEFAULT_COORDINATE);
}
public Point(double newX, double newY) {
super();
setX(newX);
setY(newY);
}
public Point(Point newP) {
setX(newP.getX());
setY(newP.getY());
}
public Point(int angleDegrees, double radius) {
setX(radius*Math.cos(Math.toRadians(angleDegrees)));
setY(radius*Math.sin(Math.toRadians(angleDegrees)));
}
public double getX() { return x; }
public double getY() { return y; }
public void setX(double newX) { this.x=newX; } // note no validation is needed
public void setY(double newY) { this.y=newY; }
public void reflectXY() {
double temp=x;
setX(y);
setY(temp);
}
public double distanceToOrigin() { return Math.sqrt(x*x + y*y); }
public double distanceTo(Point otherPoint) { // local scope parameter
double otherX=otherPoint.getX(); // local scope variables
double otherY=otherPoint.getY();
return Math.sqrt(Math.pow(x-otherX,2) + Math.pow(y-otherY,2)); // x and y, class instance scope
}
public String toString() {
return "("+x+", "+y+")";
}
}```
this is my triangleTest file:
public class TriangleTest {
public static void main(String[] args) {
System.out.println("Test Case 1b - constructors, toString");
Triangle t2 = new Triangle(new Point(0.,0.), new Point(3.,0.), new Point(0.,4.));
System.out.println("EXPECTED RESULT:(0.0, 0.0) (3.0, 0.0) (0.0, 4.0)");
System.out.println("YOUR RESULT :"+t2.toString());
System.out.println("PASSED="+t2.toString().equals("(0.0, 0.0) (3.0, 0.0) (0.0, 4.0)")+"\n");
System.out.println("Test Case 1c - constructors, toString");
Point x = new Point(-1., 0.);
Point y = new Point(1., 0.);
Point z = new Point(0., 2.);
Triangle t3 = new Triangle(x,y,z);
System.out.println("EXPECTED RESULT:(-1.0, 0.0) (1.0, 0.0) (0.0, 2.0)");
System.out.println("YOUR RESULT :"+t3.toString());
System.out.println("PASSED="+t3.toString().equals("(-1.0, 0.0) (1.0, 0.0) (0.0, 2.0)")+"\n");
System.out.println("Test Case 1e - constructors, toString");
t3.setP1(x);
System.out.println("EXPECTED RESULT:(-2.0, 0.0) (1.0, 0.0) (0.0, 2.0)");
System.out.println("YOUR RESULT :"+t3.toString());
System.out.println("PASSED="+t3.toString().equals("(-2.0, 0.0) (1.0, 0.0) (0.0, 2.0)")+"\n");
System.out.println("Test Case 3a - isRightTriangle");
Triangle t3a = new Triangle();
System.out.println("EXPECTED RESULT:false");
System.out.println("YOUR RESULT :"+t3a.isRightTriangle());
System.out.println("PASSED="+(t3a.isRightTriangle()==false)+"\n");
System.out.println("Test Case 3e - isRightTriangle");
Triangle t3e = new Triangle( new Point(-5., 0.),new Point(0., 0.),new Point(0., -12.));
System.out.println("EXPECTED RESULT:true");
System.out.println("YOUR RESULT :"+t3e.isRightTriangle());
System.out.println("PASSED="+(t3e.isRightTriangle()==true)+"\n");
}
}```
i need help figuring out my error in my triangle file because I cannot get the right print statement to go
as well as the isRightTriangle method is throwing the wrong values
I don't know where I went wrong that it is not displaying the right values or being passed the right values to get the right output