1 2 Previous Next 22 Replies Latest reply: Mar 30, 2007 4:15 PM by 807599 RSS

    Arrays problem

    807599
      This is a programto initialize an array with 10 elements and the element should have the the value of its index squared.

      In average() use for loop and calculate avg and print.
      I am getting errors as exception errors

      public class TestStuff{


      private double testscores[];
      double sum = 0.0;
      double average;

      public TestStuff()
      {
      int i;
      int testscores[] = new int[10];


      for ( i = 0; i < 10; i++)
      {
      testscores[i] = i * i;
      }
      }

      public void average()
      {
      for (int i = 0; i < 10; i++)
      {
      sum = sum + testscores;
      }
      average = sum/testscores.length;
      System.out.println(average);
      }

      public static void main(String args[])
      {
      TestStuff t = new TestStuff();
      t.average();

      }

      }
        • 1. Re: Arrays problem
          807599
          Please repost your code within code tags and also post the exact error message you're encountering.

          That aside, it looks like you're confusing primitive arrays and ArrayList objects. Read up on ArrayLists here: http://java.sun.com/j2se/1.5.0/docs/api/

          Message was edited by:
          InsanePenguin
          • 2. Re: Arrays problem
            807599
            private double testscores[];
            int testscores[] = new int[10];
            Explain.
            • 3. Re: Arrays problem
              807599
              Why do you have an int array and a double array of the same name?

              Also, instead of putting
              int testscores[]
              you should put
              int[] testscores
              Your average() method should look like this:
              public void average()
              {
              for (int i = 0; i < 10; i++)
              {
              sum += testscores;
              }
              double average = sum/testscores.length;
              System.out.println(average);
              }



              And, you should have a different class for your main method. For example,
              public class testerClass
              {
               public static void main(String args[])
              {
              TestStuff t = new TestStuff();
              t.average();
              
              }
              }
              • 4. Re: Arrays problem
                807599
                And, you should have a different class for your main method. For example,
                In such a simple example, I don't see the need to put the main method in a separate class.
                • 5. Re: Arrays problem
                  807599
                  True, although (at least for me) it helps to keep things organized and not confusing...
                  • 6. Re: Arrays problem
                    807599
                    Well, it's always good to reinforce good habits.
                    • 7. Re: Arrays problem
                      807599
                      that was a mistake.I corrected it.I still get errors as Nuller Point Exception in line t.average()
                      • 8. Re: Arrays problem
                        807599
                        this is the program

                        Make a class name TestStuff, which has a single instance variable that is an array of test scores (you can assume they are whole numbers only). The class will have the following methods:

                        TestStuff(): This is a constructor that will initialize the array to have 10 elements. Give each element the value of whatever index it is squared. So index 5 would have the score 25, and so forth. A loop will make this easy!

                        average(): This method should use a for loop to calculate the average of the test scores and print it to the screen.

                        main(String args[ ]): This is the main method, where you must make an instance of the class and call the average method.
                        • 9. Re: Arrays problem
                          807599
                          If NullPointerException is really being thrown by the statement:
                          t.average();
                          then the only possible conclusion is that t is null. Or you're not
                          reporting the error correctly. Seeing as you haven't posted the
                          latest version of your code or the exact exception stack trace,
                          I'm going to bet on the latter. Any takers?
                          • 10. Re: Arrays problem
                            807599
                            this is the corrected program
                            line 23(TestStuff.average) and line 32 (TestStuff.main)are the errors when u run it.It compiles fine.
                            • 11. Re: Arrays problem
                              807599
                              public class TestStuff{


                              private int[] testscores;
                              double sum = 0.0;


                              public TestStuff()
                              {

                              int[] testscores = new int[10];

                              for (int i = 0; i < 10; i++)
                              {
                              testscores[i] = i * i;
                              }
                              }

                              public void average()
                              {
                              for (int i = 0; i < 10; i++)
                              {
                              sum += testscores;
                              }
                              double average = sum/testscores.length;
                              System.out.println(average);
                              }

                              public static void main(String args[])
                              {
                              TestStuff t = new TestStuff();
                              t.average();

                              }

                              }
                              • 12. Re: Arrays problem
                                807599
                                private int[] testscores;
                                int[] testscores = new int[10];
                                Do you realize these two testscores are different? Hmmm...
                                • 13. Re: Arrays problem
                                  807599
                                  You still didn't fix the errors that I pointed out in my last post... here's working code - I actually compiled and ran it, and it gave the correct output.
                                  public class TestStuff{
                                  
                                  int[] testscores;
                                  double sum = 0.0;
                                  
                                  
                                  public TestStuff()
                                  {
                                  
                                  testscores = new int[10];
                                  
                                  for (int i = 0; i < 10; i++)
                                  {
                                  testscores[i] = i * i;
                                  }
                                  }
                                  
                                  public void average()
                                  {
                                  for (int i = 0; i < 10; i++)
                                  {
                                  sum += testscores;
                                  }
                                  double average = sum/testscores.length;
                                  System.out.println(average);
                                  }

                                  public static void main(String args[])
                                  {
                                  TestStuff t = new TestStuff();
                                  t.average();

                                  }

                                  }
                                  • 14. Re: Arrays problem
                                    807599
                                    You still didn't fix the errors that I pointed out in
                                    my last post... here's working code - I actually
                                    compiled and ran it, and it gave the correct output.
                                    public class TestStuff{
                                    
                                    int[] testscores;
                                    double sum = 0.0;
                                    
                                    
                                    public TestStuff()
                                    {
                                    
                                    testscores = new int[10];
                                    
                                    for (int i = 0; i < 10; i++)
                                    {
                                    testscores[i] = i * i;
                                    }
                                    }
                                    
                                    public void average()
                                    {
                                    for (int i = 0; i < 10; i++)
                                    {
                                    sum += testscores;
                                    }
                                    double average = sum/testscores.length;
                                    System.out.println(average);
                                    }

                                    public static void main(String args[])
                                    {
                                    TestStuff t = new TestStuff();
                                    t.average();

                                    }

                                    }
                                    I got into trouble for this too; generally we don't just post the correct code for the OP, but let them puzzle it out, otherwise they won't learn.  If he didn't make the changes, let him know, he'll fix it if he wants to.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
                                    1 2 Previous Next