# Thread: How to work out square root of Total

1. Member
Join Date
Nov 2010
Posts
38
Rep Power
0

## How to work out square root of Total

How do you work out the square root of a number in java (or the total)
Thanks for the help guys! :)

2. Google the math class methods.

If you mean to do it manually with your own algorithm google how to find the square root of a number by hand then apply that code to java.

3. Member
Join Date
Nov 2010
Posts
38
Rep Power
0
Okay. I've been trying to use the java.lang.math method for a while, but keep getting

'non-static variable total cannot be referenced from a static context'

Java Code:
```	public void multiply(double numberToMultiply) {
total *= numberToMultiply;
}

public static double sqrt(double numberToSquareRoot) {
total = numberToSquareRoot;
}

public void percentage(double numberToPercentage) {
double percent;
percent = (total/numberToPercentage);
total = percent;
}```

4. Show me all the code pl0x

5. Member
Join Date
Nov 2010
Posts
38
Rep Power
0
Whollaa! :)

Java Code:
```import java.util.Scanner;
import java.lang.Math;

public class SimpleCalc {

Double total = 0.0;
Scanner scan = new Scanner(System.in);

}

public void minus(double numberToMinus) {
total -= numberToMinus;
}

public void divide(double numberToDivide) {
total /= numberToDivide;
}

public void multiply(double numberToMultiply) {
total *= numberToMultiply;
}

public static double sqrt(double numberToSquareRoot) {
total = numberToSquareRoot;
}

public void percentage(double numberToPercentage) {
double percent;
percent = (total/numberToPercentage);
total = percent;
}

public void clear() {
total = 0.0;
}

public void displayTotal() {
System.out.println("Total is " + total);
}

int choice;
System.out.println("2. Minus");
System.out.println("3. Divide");
System.out.println("4. Multiply");
System.out.println("5. Square Root");
System.out.println("6. Percentage of total");
System.out.println("7. Clear");
System.out.println("8. Display");
System.out.println("9. Quit");

System.out.print("\nEnter choice: ");
choice = scan.nextInt();

return choice;
}
int choice;
do {
if (choice == 1) {
System.out.print("Enter value: ");
double number = scan.nextInt();
} else if (choice == 2) {
System.out.print("Enter value: ");
double number = scan.nextInt();
minus(number);
} else if (choice == 3)	{
System.out.print("Enter value: ");
double number = scan.nextInt();
divide(number);
} else if (choice == 4) {
System.out.print("Enter value: ");
double number = scan.nextInt();
multiply(number);
} else if (choice == 5) {
System.out.print("Enter value: ");
double number = scan.nextInt();
sqrt(number);
} else if (choice == 6) {
System.out.print("What percentage of total would you like to find?: ");
double number = scan.nextInt();
percentage(number);
} else if (choice == 7) {
clear();
} else if (choice == 8) {
displayTotal();
}
} while (choice < 9);
}
public static void main (String[] args){
SimpleCalc myCalc = new SimpleCalc();
}
}```

6. Try declaring a double and setting it equal to the math methods sq rt and then set total to this new variable. Also post that methods code for me. Don't male the method static.

7. Member
Join Date
Nov 2010
Posts
38
Rep Power
0
Originally Posted by sunde887
Try declaring a double and setting it equal to the math methods sq rt and then set total to this new variable. Also post that methods code for me. Don't male the method static.
I understood zilch of that! I really am poor at programming! :mad:

8. It's no problem. You seem to have chosen a decent project for learning. Lets tale it one step at a time. What method in the math class does sqrt?

9. Originally Posted by ls7897
Whollaa!
Do you mean "voila"?

- Why is the sqrt method static?
- The sqrt method is missing a return statement
- The sqrt method does not work
- Try dividing by zero and see what happens
- the percentage method does not work

10. Member
Join Date
Nov 2010
Posts
38
Rep Power
0
Originally Posted by sunde887
It's no problem. You seem to have chosen a decent project for learning. Lets tale it one step at a time. What method in the math class does sqrt?
You've still lost me.

and just copied ''public static double sqrt(double numberToSquareRoot) {''

11. Ahhh!

Don't copy code unless you are 252% sure you know what it does.

12. To use the methods in the Math class you just call them.
Java Code:
`int value  = Math.abs(-4);`

13. You want to use that in your method. That is javas square root method is declared static so you don't need an instance of a class. Try making a separate class that has only a main method. In the main method simply call the maths square root method on some number and print the result.

14. Member
Join Date
Nov 2010
Posts
38
Rep Power
0
Originally Posted by Junky
Do you mean "voila"?
It's 4.30am and I've been doing this for 6 hours! Can you let me off that one? :D

Originally Posted by Junky
- Why is the sqrt method static?
- The sqrt method is missing a return statement
- The sqrt method does not work
- Try dividing by zero and see what happens
- the percentage method does not work
Okay, so maybe it's worth just scrapping the sqrt method and trying to work out my own algorithm?

I'll finish the percentage method tomorrow.... it returns a number at the moment which is something... :p

15. Originally Posted by ls7897
Okay, so maybe it's worth just scrapping the sqrt method and trying to work out my own algorithm?
Definately not worth scrapping as writing your own algorithm will be about 15 trillion times harder. To use the Math class square root is just a one liner similar to the snippet I provided above.

16. Do not write your own algorithm, that will probably be much more difficult. Check out how junky used a math method. You use the square root almost exactly the same.

17. Member
Join Date
Nov 2010
Posts
38
Rep Power
0
Thanks! Got it working!

It's not exactly correct for some reason. Total was 9, asked for square root and got 3.01...

18. Lets see the code you used

19. Member
Join Date
Nov 2010
Posts
38
Rep Power
0
Originally Posted by sunde887
Lets see the code you used
I used a

Java Code:
```system.out.print(Math.sqrt(total));
total = Math.sqrt(total);```
and was getting 3.01 on the system.out.print but was getting 3.00 when I viewed the total... so I just took the system.out.print statement out and they'll have to manually check the total rather than it being displayed automatically.

Used this in the end

Java Code:
```				} else if (choice == 4) {
System.out.print("Enter value: ");
double number = scan.nextInt();
multiply(number);
} else if (choice == 5) {
total = Math.sqrt(total);
} else if (choice == 6) {
System.out.print("What percentage of total would you like to find?: ");
double number = scan.nextInt();
percentage(number);```

20. Member
Join Date
Nov 2010
Posts
38
Rep Power
0
Also, I know someone mentioned the percentage was wrong....

It seems to give the correct output....

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•