Results 1 to 9 of 9
  1. #1
    hypes057 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default Display Square Roots

    Basically, I need to write a program that displays the squares of the numbers from 1 to 10. I need to do this using 2 procedures and call them squareFor and the other should be called, squareDoWhile. The procedures should not take any arguments nor return any values. Lastly, I need to simply display a message telling what kind of loop it is using and then use the appropriate loop to display the number, followed by its square.

    I have the theory down and I could do this in a flowchart, I just cant seem to wrap my head around how to code it. Can anyone please help me out? I am having a very hard time grasping the java syntax and how to give variables.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I have a couple of suggestions, but the most important one is for you to dive in yourself and try it out writing the code first. You can't really break your computer by trying.

    Suggestion 2: only add a small amount of code at a time and compile often.
    Suggestion 3: Fix any compilation errors that you run across immediately, and don't add any new code until the compilation errors are fixed.
    Suggestion 4: If after the above your code doesn't work, come on back, post your code with code tags (see my signature) and ask a specific question about your code.

    Best of luck!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  4. #4
    hypes057 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default

    Thanks for the welcome! I have sample code, and attached it below. This was taken from my text book and its suggested by the professor that we use these examples as learning tools. This is exactly what I've done. So first, the code:
    Java Code:
    public class SquareRoot3
    {
    	public static void main(String[] args)
    	{
    		final int LIMIT = 100;
    		int var;
    		int number;
    		for (number = 1; number <= LIMIT; ++number)
    			{
    				System.out.print(number + " the square root of ");
    				for(var = 1; var <= number; ++var)
    					if(number % var == 0)
    						System.out.print(var + " ");
    						//Print the number and two spaces
    			System.out.println();
    			}
    	}
    }
    This shows the numbers that are divisible into 100 (I think). What I am having trouble figuring out is where & how do I enter 1 - 10 and how do I tell it to calculate the square, loop out, and stop after the last digit.

    Also, thanks for any help !
    Last edited by Fubarable; 08-19-2009 at 12:30 AM. Reason: *** code tags added ***

  5. #5
    DiamondDog is offline Member
    Join Date
    Aug 2009
    Posts
    18
    Rep Power
    0

    Default

    Something like this, maybe?

    Java Code:
    public class Squares {
        
        public static void main(String args[])
        {
            // call the method which uses a for .. loop
            squareFor();
    
            // call the method which uses a do .. while loop
            squareDoWhile();
        }
    
        static void squareFor() // method which uses a for .. loop
        {
            System.out.println("\'for\' loop");
            for(int i = 1; i<11; i++)
            {
              int square = i*i;
              System.out.println(i + "  " + square);
            }
    
            System.out.println("");
        } // end squareFor
        
        static void squareDoWhile() // method which uses a do .. while loop
        {
            System.out.println("\'do ... While\' loop");
            int x = 1;
            do{
                int square = x*x;
                System.out.println(x + "   " + square);
                x++;
            } while(x<11);
    
            System.out.println("");
        } // end squareDoWhile
     
    } // end class

  6. #6
    hypes057 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by DiamondDog View Post
    Something like this, maybe?

    Java Code:
    public class Squares {
        
        public static void main(String args[])
        {
            // call the method which uses a for .. loop
            squareFor();
    
            // call the method which uses a do .. while loop
            squareDoWhile();
        }
    
        static void squareFor() // method which uses a for .. loop
        {
            System.out.println("\'for\' loop");
            for(int i = 1; i<11; i++)
            {
              int square = i*i;
              System.out.println(i + "  " + square);
            }
    
            System.out.println("");
        } // end squareFor
        
        static void squareDoWhile() // method which uses a do .. while loop
        {
            System.out.println("\'do ... While\' loop");
            int x = 1;
            do{
                int square = x*x;
                System.out.println(x + "   " + square);
                x++;
            } while(x<11);
    
            System.out.println("");
        } // end squareDoWhile
     
    } // end class
    Thanks! Let me see if I understand this and please, correct me where I'm wrong so I can learn this....

    for(int i = 1; i<11; i++)

    You could have named the int "i" anything, for example I could have entered

    for(int xyz = 1; xyz<11; xyz++)

    This also makes it increase by 1 until it gets to 11, upon which the loop stops

    int square = i*i;
    refers to squareFor(); so that when its compiled java knows that the int square is a for loop

    And the same goes for the do...while loop, correct?

  7. #7
    DiamondDog is offline Member
    Join Date
    Aug 2009
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by hypes057 View Post
    for(int i = 1; i<11; i++)

    You could have named the int "i" anything, for example I could have entered

    for(int xyz = 1; xyz<11; xyz++)
    Yes, that's correct. "i" was just my choice of convenient name. Any valid name would do.

    int square = i*i;
    refers to squareFor(); so that when its compiled java knows that the int square is a for loop
    No, this is not correct.
    Java Code:
    int square = i*i
    means that I'm declaring a variable called 'square', and I'm declaring it as an integer (a whole number). Whenever you declare a variable you have to say what type of variable it is. (int, double, String etc etc)

    In that same line of code, I set the value of my variable equal to i*i (in other words, the square that I'm trying to calculate).

    If you wanted to, you could split that one line into two lines, like this:
    Java Code:
    int square; 
    square = i*i
    but there's no good reason to do that.

    Like you, I'm a noob when it comes to Java (although I've written simple stuff in other languages) so there might be a better way to do what you're trying to do, but this way seems to get the job done.

    Good Luck.

  8. #8
    hypes057 is offline Member
    Join Date
    Aug 2009
    Posts
    13
    Rep Power
    0

    Default

    Quote Originally Posted by DiamondDog View Post
    Yes, that's correct. "i" was just my choice of convenient name. Any valid name would do.



    No, this is not correct.
    Java Code:
    int square = i*i
    means that I'm declaring a variable called 'square', and I'm declaring it as an integer (a whole number). Whenever you declare a variable you have to say what type of variable it is. (int, double, String etc etc)

    In that same line of code, I set the value of my variable equal to i*i (in other words, the square that I'm trying to calculate).

    If you wanted to, you could split that one line into two lines, like this:
    Java Code:
    int square; 
    square = i*i
    but there's no good reason to do that.

    Like you, I'm a noob when it comes to Java (although I've written simple stuff in other languages) so there might be a better way to do what you're trying to do, but this way seems to get the job done.

    Good Luck.
    ahhhh I see. so if my number had a decimal i would use double instead of int, so it'd be something along the lines of

    double square;
    square = i*i

    i would equal say 1.5. This would give me the square root of 1.5. I think I am understanding better now, thanks alot I really appreciate it!

  9. #9
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    Quote Originally Posted by hypes057 View Post
    double square;
    square = i*i
    i would equal say 1.5. This would give me the square root of 1.5. I think I am understanding better now, thanks alot I really appreciate it!
    If i is 1.5 then i*i will give you the square of 1.5, not the square root... i would be the square root of i*i.

Similar Threads

  1. change the square to triangle java
    By anotsu in forum New To Java
    Replies: 3
    Last Post: 07-09-2009, 11:17 AM
  2. Simple square root problem!
    By nortski in forum New To Java
    Replies: 7
    Last Post: 04-01-2009, 02:11 PM
  3. 8-Square puzzle loop
    By SapphireSpark in forum New To Java
    Replies: 7
    Last Post: 12-04-2008, 07:21 PM
  4. 26 roots
    By Nicholas Jordan in forum Advanced Java
    Replies: 6
    Last Post: 08-25-2008, 11:33 PM
  5. SWT OpenGL snippet: draw a square
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:29 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
  •