Once receiving values from a data file, I want to check if the values make a triangle

I've been forced to open another question to hopefully get some more expert advice, as my other question seems to have died a death.
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21412397.html

Anyways, my code currently looks as follows:


package Triangle;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.lang.*;
import java.util.*;

class TriClass{

public TriClass(){
  potentialTriangle myTri = new potentialTriangle(line);
}


public String TestTri(){
    try{

    BufferedReader in = new BufferedReader(new FileReader("sizes.dat"));

String str;
while (str = in.readLine()) !=null)
{
  StringTokenizer st = new StringTokenizer(str, " ");
String[] tokens = new String[st.countTokens()];
int tokIndex = 0;
while (st.hasMoreTokens()) {
    tokens[tokIndex++] = st.nextToken();
}
 potentialTriangle myTri = new potentialTriangle(str);
  if(myTri.isTriangle()){
    System.out.println("Its a triangle");
  }

  if(myTri.isScalene()){
    System.out.println("Its a scalene triangle");
  }

  if(myTri.isEquilateral()){
    System.out.println("Its an equilateral triangle");
  }

  if(myTri.isIsosceles()){
    System.out.println("Its an Isosceles triangle");
}



      }
catch(Exception e){
  System.out.println("Catching exception:" +e);
}
}

public boolean isScalene();
public boolean isTriangle();
public boolean isEquilateral();
public boolean isIsosceles();




does anyone think i'm possibly on the right lines.
I got that confused with trying to install new versions of java that I'm using a method that I'm not entirely comfortable with, yet it seems theres not many other options!
c18drmAsked:
Who is Participating?
 
CEHJCommented:
In view of your having split the file line, you can add to that:

public PotentialTriangle(String[] strSides) {
    // put strSides into sides
}
0
 
CEHJCommented:
That's fine so far, but if you've gone to the trouble of tokenizing the line, PotentialTriangle should accept the tokens in its ctor
0
 
c18drmAuthor Commented:
>in its ctor

which means??
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
c18drmAuthor Commented:
i asked the question in my other topic aswell, but how would i get the 4methods at the bottom of my original post to start comparing the string that I have tokenized?
0
 
CEHJCommented:
>>which means??

In its constructor

>>but how would i get the 4methods at the bottom of my original post t

You have to use the criteria that i and others have suggested there
0
 
c18drmAuthor Commented:
yes thanks.
I know the criteria....I knew that before I signed on this site.
The reason I signed up(and paid money to register) was to hopefully get some advice as to how to implement the criteria in java(since I'm nowhere near as clued up on Java as any of the experts on here!)

Now people have suggested using the 'split' idea, yet I can't as I don't have the correct java versioin.

So in summary, I can use the tokenizer to split the string....excellent.
However, once its split I don't know how to use the split parts....thats what i'm struggling with now
0
 
CEHJCommented:
>>I know the criteria....

You misunderstand. I'm talking about the criteria use to assess what type (if any) triangle you've got. We've given you those criteria
0
 
c18drmAuthor Commented:
No i didn't misunderstand.
I knew that criteria from the start. I know what a scalene/equilateral/isosceles triangle is!!!

I maybe can't program java very well but I'm not completely daft!!
;-)
0
 
bloodredsunCommented:
What CEHJ is saying is that all the information was provided for you in the original question. In the fifth post, someone has given you the actual code you need.  You have also got the framework and the splitting sorted out.

The experts (myself, CEHJ and zzynx) aren't being deliberately cryptic but the membership/expert rules here state that for people doing graded assignments, we cannot provide complete code, only partial code and provide avenues to explore. And while I can understand your frustration as a paying member, you might not know that us experts don't get paid but do this for fun/enjoyment/challenge.

In that theme, here's some assistance with your problem.
-------------------------------------------------

To complete you assignment you will need to write a class to represent your potential triangle so it can be used in your main class. This will be a separate class that has the methods you need to complete this adn will need to be in the same package.

Here's the skeleton for it, note the capitalization change: PotentialTriangle.java:

-------------------

package com.bloodredsun.dev;

public class PotentialTriangle {
      
      private int[] sides ;

      public PotentialTriangle(){
            //basic constructor
      }

      public PotentialTriangle( String arg){
            //constructor that can create a PotentialTriangle
            //object when provided with the String that contains
            //the data points and puts them in the instance variable sides
      }
      
      //method that determines whether this object is a triangle      
      public boolean isTriangle(){
            return ( sides.length == 3) ;
      }
      
        //method that determines whether this object is a scalene triangle      
      public boolean isScalene(){
                //do stuff
            return true ;
      }
      //the rest of the methods
}
0
 
c18drmAuthor Commented:
thanks for the post bloodredsun.
as you mentioned, it is frustrating sometimes when I'm looking for an answer.
Personally, I'm not looking for a full set of code whatsoever (what will I learn from that??), merely a few ideas/seeds/bit of guidance when i'm struggling.

Thanks for the summary code structure.
Only thing is to me it looks like that all looks good if you can use the .split method, which I already discovered I can't because I can't get the latest version of java on my machine.

So I'm trying to use this StringTokenizer which i've never come across before, which is where i start getting confused!
0
 
bloodredsunCommented:
>>So I'm trying to use this StringTokenizer which i've never come across before, which is where i start getting confused!

This is where you can call the code already provided by CEHJ but now you put it in your instance, and also go here http://www.javaalmanac.com/egs/java.util/ParseString.html for another good example.

>> StringTokenizer st = new StringTokenizer(str, " ");
>>String[] tokens = new String[st.countTokens()];
>>int tokIndex = 0;
>>while (st.hasMoreTokens()) {
>>    tokens[tokIndex++] = st.nextToken();
>>}


and turn it into this

      public PotentialTriangle( String arg){
            StringTokenizer st = new StringTokenizer(arg, " ");
            int tokIndex = 0;
            while (st.hasMoreTokens()) {
                  sides[tokIndex++] = Integer.parseInt( st.nextToken() );
            }
      }

so that your contructor can take a String, break it up into tokens and turn the Strings into ints and put them in the array, creating your new PotentialTriangle object.

There are pieces deliberately missing from this code like error handling but it should be enough to get you over the hump and back to your assignment.
0
 
bloodredsunCommented:
>>but now you put it in your instance

should read - but now you put it in your constructor...
0
 
CEHJCommented:
Personally i think that code's better out of the ctor and the parsing is done before an instance is created
0
 
bloodredsunCommented:
>> Personally i think that code's better out of the ctor and the parsing is done before an instance is created

Actually I agree, what with creating a new StringTokenizer each time being a little redundant, but I figure that our friend here is not too experienced in java so I went for simplicity over "technical purity" by taking it out of the operating code and putting it in PotentialTriangle as it makes the final code a lot more "join-the-dots"...

:-)
0
 
c18drmAuthor Commented:
I'm going to post a copy of what I have so far.
I'm not sure where to add in the code for reading in the file, should this be in the main method?
I hope this looks half-decent, but if not can someone please say so that I can change stuff!!


package Triangle;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.lang.*;
import java.util.*;

public class TriClass{


public TriClass(String arg){
  StringTokenizer st = new StringTokenizer(arg, " ");
  int tokIndex = 0;
  while (st.hasMoreTokens()) {
    sides[tokIndex++] = Integer.parseInt(st.nextToken());
  }
}

  if(myTri.isRightAngle(){
    System.out.println("Its a right angle triangle");
  }

  if(myTri.isTriangle()){
    System.out.println("Its a triangle");
  }

  if(myTri.isScalene()){
    System.out.println("Its a scalene triangle");
  }

  if(myTri.isEquilateral()){
    System.out.println("Its an equilateral triangle");
  }

  if(myTri.isIsosceles()){
    System.out.println("Its an Isosceles triangle");
  }
}



catch(Exception e){
  System.out.println("Catching exception:" +e);
}
}

public boolean isRightAngle(int a, int b, int c){
  int hyp = Math.max(a, Math.max(b,c));
  int kat1, kat2;
  if (a==hyp){
    kat1=c;
    kat2=b;
  }
  else{
    kat1=a;
    if(b==hyp){
      kat2=c;
    }
  else{
    kat2=b;
  }
}
boolean result;
result = Math.pow(hyp,2) == Math.pow(kat1,2)+Math.pow(kat2,2);
return result;
}


public boolean isScalene(int a, int b,int c){
boolean result;
result = a!=b!=c;
return result;
}


public boolean isEquilateral(int a,int b,int c){
boolean result;
result a=b=c;
return result;
}
public boolean isIsosceles();
}

public static void main (String args[])
{

}
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.