# Is this a sensible/ efficient way to calculate Pythagorean theorem

Page 1 of 1

## 2 Replies - 2169 Views - Last Post: 30 November 2011 - 08:29 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=257877&amp;s=d58c0798d61be69f017bbfdf1a9b8c94&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 slehmann101

Reputation: 21
• Posts: 146
• Joined: 30-November 11

# Is this a sensible/ efficient way to calculate Pythagorean theorem

Posted 30 November 2011 - 03:51 PM

this is the first program i as a beginning programmer have created. I would like to know if this is a good way to go about calculating the Pythagorean theorem.
```import java.util.InputMismatchException;
import java.util.Scanner;

public class Calculator {
public static void main (String []args){
boolean again =runlist();
while (again = true){
runlist();
}

}
public static boolean runlist(){
welcome();
int one =one();
int two = two();
int one2 = one2(one);
int two2 = two2(two);
print(sqroot);
boolean again = again();
return again;
}
public static boolean again(){
boolean again;
Scanner keyboard = new Scanner(System.in); //create a scanner object
System.out.println("do you want to use the calculator again?");
String a = keyboard.nextLine();
String yes = "yes";
again=false;
if  (a.equalsIgnoreCase(yes)){
again = true;
}
return again;
}
public static void welcome(){
System.out.println("Welcome to the Pythagorean theorum calculator \n We will calculate what the side of a triangle is when you give us the other two sides. \n" +
"The triangle must have a right angle.");
}
public static int one(){
int one;
Scanner keyboard = new Scanner(System.in); //create a scanner object
System.out.println("What is the first side of your triangle?");
one = keyboard.nextInt();
return one;
}
public static int two(){
int two;
Scanner keyboard = new Scanner(System.in); //create a scanner object
System.out.println("What is the other side of your triangle?");
two = keyboard.nextInt();
return two;
}
public static int one2(int one){
int one2 = one * one;

return one2;
}
public static int two2(int two){
int two2 = two * two;

return two2;
}
public static int added(int one2, int two2){
int added = one2 + two2;
}
return sqroot;
}
public static void print(double sqroot){
System.out.println("The third side is " + sqroot);

}
}

```

thank you

Is This A Good Question/Topic? 0

## Replies To: Is this a sensible/ efficient way to calculate Pythagorean theorem

### #2 CasiOo

• D.I.C Lover

Reputation: 1576
• Posts: 3,548
• Joined: 05-April 11

## Re: Is this a sensible/ efficient way to calculate Pythagorean theorem

Posted 30 November 2011 - 04:00 PM

Here you are actually assigning the boolean to true, and THEN it evaluates the expression.
Also it is a infinity loop as you never change 'again'. Your runlist returns a boolean, but you never use the returned boolean.
```while (again = true)

```

You have some redundant code. You could write some of your methods so they could be reused.

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12302
• Posts: 45,400
• Joined: 27-December 08

## Re: Is this a sensible/ efficient way to calculate Pythagorean theorem

Posted 30 November 2011 - 08:29 PM

How about just one method for calculating the magnitude? It doesn't make sense to write a method to simply call an existing API method.
```public double getC(double a, double B)/>{
//return the sqrt(a*a + b*B)/>
}

```