# Please help me clean up my code!

Printable View

• February 24th, 2011, 05:31 PM
Java Neil
Please help me clean up my code!
Hi everyone. I need a little help with an assignment and hopefully learn something in the process. The assignment is to use methods to calculate the area of a triangle. I am to use a boolean method, and a double method. I'm also supposed to do this in JOptionPane.

Here is my code...

Code java:

``` import javax.swing.JOptionPane; public class MyTriangle { public static void main(String[] args) { //Declare everything boolean valid = false; double side1, side2, side3;   //Give the user directions JOptionPane.showMessageDialog(null, "Directions for Neil's Triangle Area Calculator:\n" + "1. Enter three lengths of a triangle\n" + "2. The sum of the first two sides of the triangle\n" + " HAS to be GREATER than the third side\n" + "3. HAVE FUN!\n" + "");   //Prompt user input side1 = Double.parseDouble(JOptionPane.showInputDialog(null,"Please enter the 1st side: ")); side2 = Double.parseDouble(JOptionPane.showInputDialog(null,"Please enter the 2nd side: ")); side3 = Double.parseDouble(JOptionPane.showInputDialog(null,"Please enter the 3rd side: ")); if (isValid(side1, side2, side3)) JOptionPane.showMessageDialog(null, "The area of a Trangle with the sides lengths of \n" + side1 + ", " + side2 + ", and " + side3 + " is \n" + area(side1, side2, side3)); { { } } } //Check if the sum of two sides is greater than the third side public static boolean isValid(double side1, double side2, double side3) { if(((side1 + side2) <= side3) || ((side2 + side3) <= side1) || ((side3 + side1) <= side2)){ JOptionPane.showMessageDialog(null,"Error: The sum of the first two sides\n " + "was NOT GREATER than the third side"); return false; } if(((side1 + side2) >= side3) || ((side2 + side3) <= side1) || ((side3 + side1) <= side2)){ } return true; } //Returns the area of the triangle public static double area(double side1, double side2, double side3) {   double s = (side1 + side2 + side3)/2; double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); return area; }   }```

The big thing here is not if it runs--it does, but to achieve the results in the most efficient way. My teacher is a stickler for unneeded code, and rightfully so. Anyway, could you guys tear me a new one with what I have written and give me some tips on how to do this the proper way.

I should state that we haven't gotten to arrays if that has any bearing on your advice. That will be the next chapter!

Thanks for you time and knowledge.

Neil
• February 24th, 2011, 05:43 PM
javapenguin
Re: Please help me clean up my code!
It would seem that you're saying that it's supposed to return true if side1 + side2 = side3 and also return false

if
side1 + side2 = side3;

I think it'll return true every time as that's what comes first.

Also would get rid of second if statement in that boolean method.

If all three aren't valid, your if should get it to return false.

If it makes it past your if statement without returning anything, obviously it should return true.

So change that second if to
Code java:

```else return true;```
• February 24th, 2011, 06:28 PM
Java Neil
Re: Please help me clean up my code!
Is this better? I might have misunderstood you...

Code java:

``` import javax.swing.JOptionPane; public class MyTriangle { public static void main(String[] args) { //Declare everything boolean valid = false; double side1, side2, side3;   //Give the user directions JOptionPane.showMessageDialog(null, "Directions for Neil's Triangle Area Calculator:\n" + "1. Enter three lengths of a triangle\n" + "2. The sum of the first two sides of the triangle\n" + " HAS to be GREATER than the third side\n" + "3. HAVE FUN!\n" + "");   //Prompt user input side1 = Double.parseDouble(JOptionPane.showInputDialog(null,"Please enter the 1st side: ")); side2 = Double.parseDouble(JOptionPane.showInputDialog(null,"Please enter the 2nd side: ")); side3 = Double.parseDouble(JOptionPane.showInputDialog(null,"Please enter the 3rd side: ")); if (isValid(side1, side2, side3)) JOptionPane.showMessageDialog(null, "The area of a Trangle with the sides lengths of \n" + side1 + ", " + side2 + ", and " + side3 + " is \n" + area(side1, side2, side3)); { { } } } //Check if the sum of two sides is greater than the third side public static boolean isValid(double side1, double side2, double side3) { if(((side1 + side2) <= side3) || ((side2 + side3) <= side1) || ((side3 + side1) <= side2)){ JOptionPane.showMessageDialog(null,"Error: The sum of the first two sides\n " + "was NOT GREATER than the third side"); return false; }   else{ return true; }   } //Returns the area of the triangle public static double area(double side1, double side2, double side3) {   double s = (side1 + side2 + side3)/2; double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); return area; }   }```
• February 24th, 2011, 06:51 PM
javapenguin
Re: Please help me clean up my code!
Looks good to me.
• February 24th, 2011, 07:58 PM
aussiemcgr
Re: Please help me clean up my code!
You actually don't even need the else statement. Think about it logically for a second. If the if statement is true, it will enter the if statement and will ultimately return true (thus leaving the method). If, however, the statement returns false, it will continue to either an else or the reset of the method. So just saying:
Code java:

```public static boolean isValid(double side1, double side2, double side3) { if(((side1 + side2) <= side3) || ((side2 + side3) <= side1) || ((side3 + side1) <= side2)){ JOptionPane.showMessageDialog(null,"Error: The sum of the first two sides\n " + "was NOT GREATER than the third side"); return false; } return true;   }```
also works. I dunno if it provides any performance advantages, but the else statement is unnecessary nonetheless.
• February 24th, 2011, 08:16 PM
javapenguin
Re: Please help me clean up my code!
Well...actually the if statement will return false if condition is met.

I don't see how it'll just return true on its own.
• February 24th, 2011, 10:02 PM
Java Neil
Re: Please help me clean up my code!
Quote:

Originally Posted by javapenguin
Looks good to me.

Hey, thanks for your help. I have a very good, but hard teacher and I'm having to learn on a learning curve!
• February 25th, 2011, 07:15 AM
aussiemcgr
Re: Please help me clean up my code!
Quote:

Originally Posted by javapenguin
Well...actually the if statement will return false if condition is met.

I don't see how it'll just return true on its own.

Oh, by bad. The if statement will return false if the condition is met, not true. Regardless, the else statement still isn't needed.
• February 25th, 2011, 01:48 PM
javapenguin
Re: Please help me clean up my code!
#-o#-o#-o#-o#-o#-o#-o#-o#-o#-o#-o#-o

I see what you're getting at.

I knew that but somehow it didn't cross my mind.

:">:">

You can just
Code java:

``` public static boolean isValid(double side1, double side2, double side3) { if(((side1 + side2) <= side3) || ((side2 + side3) <= side1) || ((side3 + side1) <= side2)){ JOptionPane.showMessageDialog(null,"Error: The sum of the first two sides\n " + "was NOT GREATER than the third side"); return false; } return true;     }```