• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Triangle program help

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I made a triangle program where a user inputs 3 sides. After receiving the three values, your program should provide answers for each of the following questions:
  • Is it really a triangle? If one side is longer than the sum of the other two sides, then it is not.
  • Is it a right triangle? That is, does it satisfy the Pythagorean theorem?
  • Is it an equilateral triangle? That is, are all three sides the same?
  • Is it an isosceles triangle? That is, are two sides the same? (If your program has already determined that the triangle is equilateral, then it should NOT state that it is isosceles.)
  • What is the area of the triangle?
  • What is the perimeter of the triangle?


  • The only thing that I'm stuck on is the part where it says if your program has already determined that the triangle is equilateral, then it should not state that it is isosceles. So I need a little help to figure out how to fix that if I input something like 2,2,2.
    Here is my code:
     
    Sheriff
    Posts: 8545
    615
    Mac OS X VI Editor BSD Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to the Ranch, Christian.

    Before I read the issue you're facing, first thing came to my mind is, that you got lots of code, and all this code laying down in one single method main. I'd try to divide all those singular tasks into separate methods, and in this way you'd easily solve your issue you're facing at the moment, as you could see the program flow. You'd need probably to add some "if - else" statements to it, but that is pretty much it.

    Consider creating methods: isTriangle, isRightTriangle, equilateralTriangle and so forth. Have you been taught about methods, how to call them, get returned value?

    One of the idea's how you could solve your issue:
    Of course there should be more elegant way, but that is the core logic that could be applied solving your issue. Think about it, you might will come up with something.

    But probably start by dividing your problem into small pieces (methods), where each piece is designated to achieve one requirement of your task.

     
    Marshal
    Posts: 76887
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome again
    Thank you for using the code button, but it highlighted one line far too long. I have sorted it out so you can actually read the code, and so you can see how to do it. Also put spaces round binary operators; your code style is not consistent.
    Don't use Math#pow for squares. Use x * x. Much faster execution.
    Liutauras has already shown you how to use else rather than plain if.
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:Consider creating methods: isTriangle, isRightTriangle, equilateralTriangle and so forth.


    Great advice (as usual).

    The only thing I'd say (and it's a real nitpick) is that 'Triangle' is redundant. We already know we're dealing with a Triangle, so my suggestions would be
    isValid(), isRightAngle(), isEquilateral(), isIsoceles() ... and so on.

    @Christian: Giving your methods (and classes, and fields, and interfaces...ie, basically everything you write) good, descriptive names is really important - possibly the MOST important thing you do in Java - but within that basic rule, it's also worth trying to keep them reasonably short (if you can).

    HIH

    Winston
     
    Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic