• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Triangle Evaluation

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For an assignment I need to read an input text file and evaluate the triangle based on the data found in the text file which consists of an unknown number of groups of 3 numbers (Each group representing a different triangle). According to the project specifications I need to have a main method and several other methods. I have the main method set-up to receive data correctly but I am confused as to how I would get the values out of the text file and use them in the main method. For example, if I were to call the computePerimeter method in my main method, how would I make sure I get the correct perimeter for the given data?

Here is the code I have so far, thanks in advance for the help!

Please forgive my lack of javadoc comments!

 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks ok to me so far.

What does the input file look like? Is it just going to be a series of numbers? (then just read the next three numbers)
Is it three numbers per line? (Might lend itself to reading in a line of text and splitting it up)

In general I think the content of your loop should be something like:

read data for one triangle
perform calculations on triangle.

Does that help at all?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have one other recent post about a program dealing with triangles. I wonder if the two of you are taking the same class but it looks like she already has some grasp of the ideas about ”objects". Your code, however, is not really using objects since all you have are static methods and your code is more procedural in nature.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Search for Java using scanner to read text file
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Mac McPherson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To clarify the input file consists of an unknown amount of groups of three numbers, with each group represented on a different line and each group representing a different triangle.

In response to Stefan, I am unfamiliar with how I would go back reading the data for just one triangle.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The API documentation for the Scanner class has a number of examples that you can follow to do what you need to do.
 
Mac McPherson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I looked at the API and I found it difficult to follow. To further clarify, the example triangleData.txt was

18.7 30 18.7
3 4 5
20.72 20.72 20.72

And when I ran my code I only got the perimeter for the third line (20.72 three times). How would I go about making sure it reads all of the input data. Here is the code I have so far

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mac McPherson wrote:
And when I ran my code I only got the perimeter for the third line (20.72 three times). How would I go about making sure it reads all of the input data. Here is the code I have so far


That's because of this:

Do lines 19 and 20 get executed every time through the loop or only once after the loop exits?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code is redundant:

The code from lines 25-27 can be reduced to a single line of code without loss of clarity. Hint: the return statement can be followed by an expression and the value returned will be the value obtained after the expression is completely evaluated.

For example:
 
Mac McPherson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:This code is redundant:

The code from lines 25-27 can be reduced to a single line of code without loss of clarity. Hint: the return statement can be followed by an expression and the value returned will be the value obtained after the expression is completely evaluated.

For example:



I understand that is redundant, but the project requirements tell me I need to have a method called computePerimeter and that it should be called in the main method.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By redundant, I meant that there were lines of code that you could eliminate. I didn't mean that you could eliminate the entire method. With the example code that I gave, the redundant version would have been:

Do you see the similarity to what you wrote? You only need one line of code to do the same thing. It may seem trivial but you want to write code that's concise and to-the-point.
 
Mac McPherson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:By redundant, I meant that there were lines of code that you could eliminate. I didn't mean that you could eliminate the entire method. With the example code that I gave, the redundant version would have been:

Do you see the similarity to what you wrote? You only need one line of code to do the same thing. It may seem trivial but you want to write code that's concise and to-the-point.



I see what you mean, thanks for that!

Thanks for the all of the help y'all, unfortunately I do have even more questions

The requirements state that I need to create various methods which will help classify the triangle, a method that tests to see if the triangle is Equilateral (all three sides are the same), another method to see if the triangle is isosceles (two sides are equal). And yet another method which should accept two double values and return true if they are with in 00000001 of eachother. I'm pretty sure I have done this with the code below.



It then tells me to call these two methods from another method which should accept three double values based on the sides and then returns the type of triangle (It specifically says it will not return a value). I am terribly confused as to how I would do that within the context of these specifications, here is all the code for that I could muster. Also how do I utilize the tolerance value I created in the floatEquals method? I believe that the printTriangleType method needs to be utilized in the main method if that helps with anything.

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, let's go back to eliminating redundant code again first. Boolean expressions evaluate to true or false. So, it's redundant to write:

Again, all this is a roundabout way of saying something that can be reduced to one succinct line of code:

That is,
 
Mac McPherson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So those two methods can be re-written as


 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ouch!

Those if‑elses are really poor style. Look at the old Sun Style Guide: look for §10.5.2. You can shorten those methods greatly, and make it more obvious what their purpose is.If a triangle has three equal sides, is it isosceles? If not, then you will need another test in the isosceles method. Also is it right to say that three ints are equilateral or isosceles? Surely only a triangle can be those things. So I think you would do better to have the parameter to that method be a Triangle.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ouch again!
Never write == true or == false. They are not only poor style but also error‑prone if you write = by mistake.
Never write if (b == true) ...  but  if (b) ...
Never write if (b == false) ... but if (!b) ...
 
Mac McPherson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ignoring the style for now (I promise I will fix those when I have the code fully functioning!)

I think I have all of my methods set up correctly:



I'm assuming there are style errors with the printTriangleType method, but moving past those for now.

I am confused as to how to call the printTriangleType method in my main method


When I use that the compiler says that "void type not allowed here" flagging this line



If I want the method to print out the triangle type, how do I write the printTriangleType method so it can be called in the main method and produce the correct output (Equilateral, isosceles, scalene)?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You get an error because printTriangleType is a void method. You can't pass void to System.out.println(). Just call printTriangleType() on a line by itself. It already has the System.out.println statements to display the type of triangle.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also clear up the poor style now. If you leave it for later you will never do it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic