?
Solved

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

Posted on 2005-05-06
16
Medium Priority
?
213 Views
Last Modified: 2010-03-31
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!
0
Comment
Question by:c18drm
  • 6
  • 6
  • 4
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13944388
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
 

Author Comment

by:c18drm
ID: 13944423
>in its ctor

which means??
0
 

Author Comment

by:c18drm
ID: 13944434
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:CEHJ
ID: 13944448
>>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
 

Author Comment

by:c18drm
ID: 13944497
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13944518
>>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
 

Author Comment

by:c18drm
ID: 13944541
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
 
LVL 29

Assisted Solution

by:bloodredsun
bloodredsun earned 1200 total points
ID: 13944909
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 800 total points
ID: 13945648
In view of your having split the file line, you can add to that:

public PotentialTriangle(String[] strSides) {
    // put strSides into sides
}
0
 

Author Comment

by:c18drm
ID: 13945730
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
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13945827
>>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
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13945835
>>but now you put it in your instance

should read - but now you put it in your constructor...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13945966
Personally i think that code's better out of the ctor and the parsing is done before an instance is created
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13946027
>> 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
 

Author Comment

by:c18drm
ID: 13959441
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 13976311
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month16 days, 21 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question