• Post Reply Bookmark Topic Watch Topic
  • New Topic

Triangle program help  RSS feed

 
Christian Nehila
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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:
     
    Liutauras Vilda
    Sheriff
    Posts: 4917
    334
    BSD
    • Likes 1
    • Mark post as helpful
    • send pies
    • 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.

     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • 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.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • 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
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!