# How to work out square root of Total

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-09-2011, 04:33 AM
ls7897
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! :)
• 03-09-2011, 04:34 AM
sunde887
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.
• 03-09-2011, 04:46 AM
ls7897
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'

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;         }```
• 03-09-2011, 04:48 AM
sunde887
Show me all the code pl0x
• 03-09-2011, 04:53 AM
ls7897
Whollaa! :)

Code:

```import java.util.Scanner; import java.lang.Math; public class SimpleCalc {     Double total = 0.0;     Scanner scan = new Scanner(System.in);     public void add(double numberToAdd) {         total += numberToAdd;     }     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);     }         public int displayMenu() {                 int choice;                   System.out.println("1. Add");                   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;         }                         public void controlMenu() {                         int choice;                 do {                     choice = displayMenu();                     if (choice == 1) {                         System.out.print("Enter value: ");                         double number = scan.nextInt();                         add(number);                     } 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();                         myCalc.controlMenu();                     }                 }```
• 03-09-2011, 04:58 AM
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.
• 03-09-2011, 05:02 AM
ls7897
Quote:

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:
• 03-09-2011, 05:04 AM
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?
• 03-09-2011, 05:13 AM
Junky
Quote:

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
• 03-09-2011, 05:14 AM
ls7897
Quote:

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) {''
• 03-09-2011, 05:17 AM
Junky
Ahhh!

Don't copy code unless you are 252% sure you know what it does.
• 03-09-2011, 05:18 AM
Junky
To use the methods in the Math class you just call them.
Code:

`int value  = Math.abs(-4);`
• 03-09-2011, 05:18 AM
sunde887
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.
• 03-09-2011, 05:20 AM
ls7897
Quote:

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

Quote:

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
• 03-09-2011, 05:23 AM
Junky
Quote:

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.
• 03-09-2011, 05:25 AM
sunde887
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.
• 03-09-2011, 05:47 AM
ls7897
Thanks! Got it working!

It's not exactly correct for some reason. Total was 9, asked for square root and got 3.01...
• 03-09-2011, 05:50 AM
sunde887
Lets see the code you used
• 03-09-2011, 05:54 AM
ls7897
Quote:

Originally Posted by sunde887
Lets see the code you used

I used a

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

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);```
• 03-09-2011, 05:57 AM
ls7897
Also, I know someone mentioned the percentage was wrong....

It seems to give the correct output....
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last