Results 1 to 20 of 24
 03092011, 04:33 AM #1Member
 Join Date
 Nov 2010
 Posts
 38
 Rep Power
 0
 03092011, 04:34 AM #2
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
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.
 03092011, 04:46 AM #3Member
 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
'nonstatic 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; }
 03092011, 04:48 AM #4
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
Show me all the code pl0x
 03092011, 04:53 AM #5Member
 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 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(); } }
 03092011, 04:58 AM #6
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
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.
 03092011, 05:02 AM #7Member
 Join Date
 Nov 2010
 Posts
 38
 Rep Power
 0
 03092011, 05:04 AM #8
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
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?
 03092011, 05:13 AM #9
 03092011, 05:14 AM #10Member
 Join Date
 Nov 2010
 Posts
 38
 Rep Power
 0
 03092011, 05:17 AM #11
Ahhh!
Don't copy code unless you are 252% sure you know what it does.
 03092011, 05:18 AM #12
To use the methods in the Math class you just call them.
Java Code:int value = Math.abs(4);
 03092011, 05:18 AM #13
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
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.
 03092011, 05:20 AM #14Member
 Join Date
 Nov 2010
 Posts
 38
 Rep Power
 0
It's 4.30am and I've been doing this for 6 hours! Can you let me off that one? :D
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
 03092011, 05:23 AM #15
 03092011, 05:25 AM #16
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
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.
 03092011, 05:47 AM #17Member
 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...
 03092011, 05:50 AM #18
 Join Date
 Jan 2011
 Location
 Richmond, Virginia
 Posts
 3,069
 Blog Entries
 3
 Rep Power
 11
Lets see the code you used
 03092011, 05:54 AM #19Member
 Join Date
 Nov 2010
 Posts
 38
 Rep Power
 0
I used a
Java Code:system.out.print(Math.sqrt(total)); total = Math.sqrt(total);
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);
 03092011, 05:57 AM #20Member
 Join Date
 Nov 2010
 Posts
 38
 Rep Power
 0
Similar Threads

Java Calculator Square Root Pleas Help
By TommyR in forum Advanced JavaReplies: 6Last Post: 02032011, 04:28 PM 
Find the square root with a particular method
By roud9 in forum New To JavaReplies: 2Last Post: 09272010, 11:39 PM 
square root and prime numbers
By roud9 in forum New To JavaReplies: 16Last Post: 09222010, 03:20 PM 
Simple square root problem!
By nortski in forum New To JavaReplies: 7Last Post: 04012009, 02:11 PM 
Creating a New Method for Square Root Loop
By SapphireSpark in forum New To JavaReplies: 14Last Post: 02252009, 02:21 PM
Bookmarks