2 Replies - 2169 Views - Last Post: 30 November 2011 - 08:29 PM Rate Topic: -----

#1 slehmann101   User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • 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);
	int added = added(two2 , one2);
	double sqroot = sqroot(added);
	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 added;
}
public static double sqroot(int added){
double sqroot = Math.sqrt(added);
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   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1576
  • View blog
  • 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.

There are more to mention, but start with fixing the above :)
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12302
  • View blog
  • 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)/>
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1