For loop with math involved

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javatech007
    New Member
    • Nov 2007
    • 51

    For loop with math involved

    Any ideas in how to do this question?? its causing me alot of trouble!

    Q.Write a class that uses a for loop to multiply your weight by itself eight times, storing the amount in another variable.
    i.e. on the first iteration your weight to the power of one will be stored. On the next iteration, this amount will again be multiplied by your weight and stored, and so on. Include any relevant classes to run and test the application.

    Thanks in advance
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by javatech007
    Any ideas in how to do this question?? its causing me alot of trouble!

    Q.Write a class that uses a for loop to multiply your weight by itself eight times, storing the amount in another variable.
    i.e. on the first iteration your weight to the power of one will be stored. On the next iteration, this amount will again be multiplied by your weight and stored, and so on. Include any relevant classes to run and test the application.

    Thanks in advance
    ... and what have you done so far?

    Comment

    • javatech007
      New Member
      • Nov 2007
      • 51

      #3
      Originally posted by r035198x
      ... and what have you done so far?

      [CODE=java]public class Project_1_3
      {
      public static void main(String[] args)
      {

      for(int i = 1; i <= 8; i++)
      {
      int weight=5;
      System.out.prin tln("weight is "+Math.pow(weig ht,1.0));
      }
      }
      }[/CODE]

      i dont know how to store the variable so i can use it again!!
      Last edited by Killer42; Nov 19 '07, 09:06 PM. Reason: Added CODE=java tag

      Comment

      • dav3
        New Member
        • Nov 2006
        • 94

        #4
        Originally posted by javatech007
        code: (java)

        public class Project_1_3
        {
        public static void main(String[] args)
        {

        for(int i = 1; i <= 8; i++)
        {
        int weight=5;
        System.out.prin tln("weight is "+Math.pow(weig ht,1.0));
        }
        }
        }

        i dont know how to store the variable so i can use it again!!

        I think you need to move "int weight = 5;" out of your for loop and in your Math.pow line you need to use your dummy variable of "i" to increment the exponent each time.

        Comment

        • javatech007
          New Member
          • Nov 2007
          • 51

          #5
          Originally posted by dav3
          I think you need to move "int weight = 5;" out of your for loop and in your Math.pow line you need to use your dummy variable of "i" to increment the exponent each time.

          how would i do that??
          where would i move 'int weight = 5' in that line??

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by javatech007
            code: (java)

            public class Project_1_3
            {
            public static void main(String[] args)
            {

            for(int i = 1; i <= 8; i++)
            {
            int weight=5;
            System.out.prin tln("weight is "+Math.pow(weig ht,1.0));
            }
            }
            }

            i dont know how to store the variable so i can use it again!!
            1.) Use code tags when posting code.
            2.) first time you have weight^1
            on second run you have weight^2
            on third run you have weight^3

            You can see then that it is your exponent that needs to be incremented every time. Also pluck that weight outside the for loop.

            Comment

            • javatech007
              New Member
              • Nov 2007
              • 51

              #7
              Originally posted by r035198x
              1.) Use code tags when posting code.
              2.) first time you have weight^1
              on second run you have weight^2
              on third run you have weight^3

              You can see then that it is your exponent that needs to be incremented every time. Also pluck that weight outside the for loop.

              Cheers
              but how would i increment my exponent every time??

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by javatech007
                how would i do that??
                where would i move 'int weight = 5' in that line??
                Your weight is constant while you are writing this program isn't it? Unless you are eating lots of pizza while writing the code, but I digress..

                You want to have it declared before the for loop probably as final.
                What you have means that the weight is being recreated on each iteration of the for loop.

                Comment

                • javatech007
                  New Member
                  • Nov 2007
                  • 51

                  #9
                  How do i incement my exponent every time??

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by javatech007
                    How do i incement my exponent every time??
                    That's what the for loop index(i) is doing already.

                    Comment

                    • javatech007
                      New Member
                      • Nov 2007
                      • 51

                      #11
                      how do i store the amount(weight) in another variable so that the same value isnt been used all the time??

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by javatech007
                        how do i store the amount(weight) in another variable so that the same value isnt been used all the time??
                        Just declare a variable outside the loop(so that you can access it after the loop) and update that in the loop.

                        Comment

                        • javatech007
                          New Member
                          • Nov 2007
                          • 51

                          #13
                          Originally posted by r035198x
                          Just declare a variable outside the loop(so that you can access it after the loop) and update that in the loop.
                          Whats going wrong??

                          [CODE=java]public class Project_1_3
                          {
                          public static void main(String[] args)
                          {
                          weight_1_3 a = new weight_1_3();

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

                          {

                          System.out.prin tln("weight is "+Math.pow(a.we ight,2.0));

                          }
                          }
                          }


                          class weight_1_3
                          {
                          int weight =7 ;

                          }[/CODE]
                          Last edited by Killer42; Nov 19 '07, 09:07 PM. Reason: Added CODE=java tag

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by javatech007
                            Whats going wrong??

                            Code: (Java)
                            public class Project_1_3
                            {
                            public static void main(String[] args)
                            {
                            weight_1_3 a = new weight_1_3();

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

                            {

                            System.out.prin tln("weight is "+Math.pow(a.we ight,2.0));

                            }
                            }
                            }


                            class weight_1_3
                            {
                            int weight =7 ;

                            }
                            Your weight_1_3 class doesn't seem to be doing much. Is it neccessary?
                            You want to store the results of 8 computations. You need either 8 variables or (preferably) an array.
                            Have a look at this loop and try to understand it
                            [CODE=java]double[] weights = new double[8];
                            double weight = 10;
                            for(int i = 0; i < 8;i++) {
                            weights[i] = Math.pow(weight , (double)(i + 1));
                            }
                            System.out.prin tln(Arrays.toSt ring(weights));
                            [/CODE]
                            P. S Going over a tutorial on loops and variable declaration wouldn't hurt as well.

                            Comment

                            Working...