I can not figure out why my code won't work.

public static void GenerateRandomTriangles(int numTriangles)
to create a file RandomTriangles.dat having the following format (including some comments like the ones
shown below) with random integer coordinates (x, y) of the three vertices of a triangle. Each x and y will be a
random integer in the range 0 <= x, y < 150. (Use seed for random numbers.)

private void TestTriangles()
to test the two functions above. This function should output the points, the length of the edges (upto 3 decimal
points), and the triangle type. It should allow the user a choice to continue processing the triangles or to quit
(give a short, clear prompt).

Here is my code:

package testtriangletype;
import java.util.*;
import java.math.*;
import java.lang.*;
import java.io.*;

public class TestTriangleType {

   public void GenerateRandomTriangles(int numTriangles){
       int max = 150;
       int x1, y1, x2, y2, x3, y3;
       String str = "(%3d, %3d) (%3d, %3d) (%3d, %3d)";
       Random rand = new Random(0);
       try { 
           PrintWriter outFile = new PrintWriter("RandomTriangles.txt");
           Scanner scan = new Scanner(new File("RandomTriangles.txt"));
           try{numTriangles = rand.nextInt(10);
            }
            catch(InputMismatchException IME){
                System.out.println("cannot create file");
            }
         outFile.println(numTriangles+ "Each line below gives integer coordinates (x, y) of vertices of a triangle"+"Number of Triangles=");
        for (int i =0; i<numTriangles;i++){
            x1 = rand.nextInt(max);
            y1 = rand.nextInt(max);
            x2 = rand.nextInt(max);
            do{y2=rand.nextInt(max);}
            while(y1 == y2);
            do{ x3 = rand.nextInt(max);}
            while(x3 == x1);
            do{ y3 = rand.nextInt(max);}
            while (y3 == y2 || (y2 - y1)*(x3 - x1) == (y3 - y1)*(x2 - x1));
            outFile.printf(str, x1, y1, x2, y2, x3, y3);
            outFile.println();
       }
       outFile.close();
   }
   catch (FileNotFoundException e)
   {
     System.out.println("RandomTriangles.txt cannot be created");
     System.exit(0);
   }   

   }


    private class  Point{
        int x,y;
    public Point(int x, int y){this.x = x; this.y = y;}
    public void Print() {System.out.println(toString());}
    public String toString() {return ("("+ x + "," + y + ")");}
                               }
  public String TriangleType(Point a, Point b,Point c)

  {
   int distAC,distBC,distAB;
   String triangleType;
   distAC = (a.x-c.x)*(a.x-c.x)+(a.y-c.y)*(a.y-c.y);
   distBC = (b.x-c.x)*(b.x-c.x)+(b.y-c.y)*(b.y-c.y);
   distAB = (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);

   if (distAB == distBC)
       if(distBC == distAC)
       triangleType ="Equilateral";
       else triangleType ="Isosceles";
   else if ((distAB == distAC) || (distBC == distAC)) triangleType ="Isoceles"; 
     else triangleType ="Scalar"; 
  return(triangleType);
  }

  private void TestTriangles(){
  int numSetOfPoints, x,y;
  String str;
  Point[] points = new Point[3];
  try{ Scanner scan = new Scanner(new File("RandomTriangles.txt"));
       Scanner keyb = new Scanner(System.in);
       numSetOfPoints = scan.nextInt();scan.nextInt();
       for(int i=0; i<numSetOfPoints;i++){
           for (int j=0; j<3; j++){
            str = scan.next();
            x = Integer.parseInt(str.substring(0, str.length()-1));
            str = scan.next();
            y = Integer.parseInt(str.substring(0, str.length()-1));
            points[j] = new Point(x,y);
           }
              scan.nextLine();
              System.out.println("The Triangle with points{"+ (points[0]) + "," + (points[1]) + "," +(points[2])+"is" + TriangleType(points[0],points[1],points[2])); 

       }
  }
  catch (FileNotFoundException e)
   {
     System.out.println("RandomTriangles.txt cannot be created");
     System.exit(0);
   }       





  }



    public static void main(String[] args) {



}
}

What's wrong? You didn't tell us what the problem you are having. One thing I notice right away is that you have nothing in your main(), so you won't see anything when you run it.

I don't know how to run it. I'm trying to get it to generate the file, read the file in, and then produce the output for TestTriangles().

Then you won't get anything anyway because the way you implement won't be able to use outside the class. What you need to do is to implement the class's constructor. Then, implement main() definition to instantiate and call the method to generate triangles. If it works correctly, you should see a file named RandomeTriangles.txt inside the same folder where you run java.

class TestTriangleType() {
  public TestTriangleType() {  // constructor
    ... // add codes or leave it empty... depending on your implementation
  }

  public void GenerateRandomTriangles(int numTriangles) {
    ... // you already implement this
  }

  ...
  ...

  public static void main(String[] args) {
    TestTriangleType ttt = new TestTriangleType();  // instantiate it
    ttt.GenerateRandomTriangles(4);  // call it with a hard-code number
  }
}

// then on the command line, you compile the class...
>javac TestTriangleType.java

// then run
>java TestTriangleType

Edited 4 Years Ago by Taywin

Comments
good
This article has been dead for over six months. Start a new discussion instead.