I had to write a triangle code enabeling it make some functions and including constructors inside..based on using the class Point(i atached the description file of it here and the Point.class itself)..I can't write the " public static void main () " in my code cause it screams errors to me and i try to write a tester class checking the triangle class but it does nothing despite the triangle class itself passed the comilation..how to do it work??(i use BlueJ)
here is the Triangle class i wrote so far:

import java.util.Scanner;
import java.lang.Math;

class Triangle { //Class which represents 3 points of triangle
private Point p1;
private Point p2;
private Point p3;

public Triangle() {
this(1,0, -1,0,0,1);
}
public Triangle (int x1,int y1,int x2,int y2,int x3,int y3) {
p1=new Point(x1,y1);
p2=new Point (x2,y2);
p3=new Point(x3,y3);}
public Triangle (Point a,Point b,Point c) {p1=new Point(a);p2=new Point(b);p3=new Point(c); }
public Point getPoint1() {return p1;}
public Point getPoint2() {return p2;}
public Point getPoint3() {return p3;}
public boolean isValid() { //checking if its a a triangle by the its points,if there are 3 X's or 3 Y's which are same its a straight line..and if all X's and Y's
if (p1.getX()==p2.getX() && p2.getX()==p3.getX()) return false; //thats also makes it be not a triangle
else if (p1.getY()==p2.getY()&& p2.getY()==p3.getY()) return false;
else if (p1.getX()!=p2.getX()&&p2.getX()!=p3.getX()&&p1.getX()!=p3.getX() &&p1.getY()!=p2.getY()&&p2.getY()!=p3.getY() &&p1.getY()!=p3.getY()) return false;
else return true;
}
public String toString() {
return ""+ p1.getX()+" ,"+p1.getY()+" "+p2.getX()+","+p2.getY()+" "+p3.getX()+","+p3.getY();} //supposed to print all of the points as strings
public void moveTriangle(int deltaX , int deltaY) { //supposed to move the whole triangle using the function "move" (see the attached file)
p1.move(deltaX,deltaY);
p2.move(deltaX,deltaY);
p3.move(deltaX,deltaY);}
public double getLengthOfSide(Point pp1, Point pp2) { //function which returns us the length of a Side...by formula between 2 points
double power1,power2,squareResult;
if (pp1!=p1&&pp1!=p2&&pp1!=p3) return 0;
else if ( pp2!=p1 &&pp2!=p2&&pp2!=p3) return 0;
else {power1=Math.pow(pp2.getY()-pp1.getY(),2);
power2=Math.pow(pp2.getX()-pp1.getX(),2);
squareResult=Math.sqrt(power1+power2);
return squareResult;
}}
public double getPerimeter() { //by adding all the Sides together it returns us the Perimeter
return (getLengthOfSide(p1,p2)+getLengthOfSide(p2,p3)+getLengthOfSide(p3,p1));}
public double getArea() { //by the Haron formula it finds us the area of the triangle
double perimeter=getPerimeter();
return Math.sqrt(perimeter*(perimeter-getLengthOfSide(p1,p2)))*(perimeter-getLengthOfSide(p2,p3))*(perimeter-getLengthOfSide(p3,p1));}

boolean isIsosceles() { //checking if the triangle is isosceles....
if ( getLengthOfSide(p1,p2)==getLengthOfSide(p2,p3)) return true;
else if (getLengthOfSide(p2,p3)==getLengthOfSide(p3,p1)) return true;
else if (getLengthOfSide(p3,p1)==getLengthOfSide(p1,p2)) return true;
else return false;}
}

thanks..

Recommended Answers

All 4 Replies

First thing: try taking "public void moveTriangle" method out of the toString() method.

First thing: try taking "public void moveTriangle" method out of the toString() method.

How far?
---------------
TAboy24 try this

boolean isIsosceles() { //checking if the triangle is isosceles....
        if (getLengthOfSide(p1, p2) == getLengthOfSide(p2, p3)) {
            return true;
        } else if (getLengthOfSide(p2, p3) == getLengthOfSide(p3, p1)) {
            return true;
        } else if (getLengthOfSide(p3, p1) == getLengthOfSide(p1, p2)) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        System.out.println((new Triangle()).toString());
        System.out.println((new Point(1,2)).toString());
    }
}

Ready to copy & paste
I intentionaly duplicated Your code to show how many "} {" You need.
Post Yours errors.

Where is Your Point.class
1. move this to BlueJ folder "C:\BlueJ\lib\userlib"
2. Tools-->Preferences-->Libraries--> button Add add this
3. Maybe 2. step is not needed.

First thing: try taking "public void moveTriangle" method out of the toString() method.

it's not really formatted well, but it isn't inside the toString() method

Oops, you're right. I missed the brace stuffed into the middle of that line. My apologies for the irrelevant comment.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.