Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default 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. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #3
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    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. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Show me all the code pl0x

  5. #5
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    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();
    		    }
    		}

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #7
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by ls7897 View Post
    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. #10
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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.

    I used ''http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html''

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

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Ahhh!

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

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

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

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #14
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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 View Post
    - 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. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by ls7897 View Post
    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. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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. #17
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Thanks! Got it working!

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

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Lets see the code you used

  19. #19
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    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. #20
    ls7897 is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default

    Also, I know someone mentioned the percentage was wrong....

    It seems to give the correct output....

Page 1 of 2 12 LastLast

Similar Threads

  1. Java Calculator Square Root Pleas Help
    By TommyR in forum Advanced Java
    Replies: 6
    Last Post: 02-03-2011, 04:28 PM
  2. Find the square root with a particular method
    By roud9 in forum New To Java
    Replies: 2
    Last Post: 09-28-2010, 12:39 AM
  3. square root and prime numbers
    By roud9 in forum New To Java
    Replies: 16
    Last Post: 09-22-2010, 04:20 PM
  4. Simple square root problem!
    By nortski in forum New To Java
    Replies: 7
    Last Post: 04-01-2009, 03:11 PM
  5. Creating a New Method for Square Root Loop
    By SapphireSpark in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 02:21 PM

Posting Permissions

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