[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

Compiler errors with arrays

Can someone please tellme why my program will not compile. What I am trying to do is make a program that accepts a number of scores then computes the scores to output an average. I also want it to output each array element on a seperate line at the end.

public class Tests

{

   public static void main(String[] args)

    {
             /*Variables*/

             int numberOfTests = 0;
             double averageScore = 0;
             private TestTaker[] record;
             int sum = 0;

             public void getTests()

             {

             /*Description of program for user.*/

             System.out.println("This program will calculate your average");
             System.out.println("for a certain number of test scores that you enter.\n");
             System.out.println("How many test scores will you be entering?");

             numberOfTests = readInput();

             test = new TestTaker[numberOfTests];
             int i;
             for (i=0; i<numberOfTests;i++)

             {
                  test[i] = new TestTaker();
                  System.out.println("Enter the score for test number " + (i+1));
                  test[i].readInput();
                  System.out.println();
             }

            }

            /*Computes the average test scores*/

            public void computation()
            {

                  int i;
                  double nextTest = test[0].getTests();
                  double sum = nextTest;
                  for (i=1; i < numberOfTests; i++)

                  {
                        nextTest = test[i].getTests();
                        sum = sum + nextTest;
                  }

                  averageScore = sum/numberOfTests;

                  System.out.println("The average test score for all tests entered is " + averageScore);

            };


}

}
0
nikese
Asked:
nikese
2 Solutions
 
CEHJCommented:
You need a class called 'TestTaker' for one thing. I would make your main class, 'Tests' responsible for reading input. Assign the scores to TestTaker
0
 
nikeseAuthor Commented:
Is there a way to do it without a different class? Like writing in some line that accepts input? I am wanting to make it all in one class.
0
 
CEHJCommented:
>>Is there a way to do it without a different class?

Yes, you can do it all in one class, but you've already specified another class:

>>private TestTaker[] record;
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
CEHJCommented:
You need to make that something like:

int[] testScores = new int[numberOfTests];
0
 
CEHJCommented:
You don't actually even need an array - you can just calculate a running total:

total += readInput();
0
 
nikeseAuthor Commented:
ooops, I am reading a book and having a hard time understading this. I guess I could just take that out then if I don't actually have another class already. Right? In doing so, I still get an error on public void getTests (). Once I get past these and find a line that accepts input, then I can tell if the rest of what I have done works.
0
 
CEHJCommented:
Or rather

sum += readInput();
0
 
nikeseAuthor Commented:
Unfortunately, I am trying to learn arrays so I am making myself use them in this program.

Once I substituted the above line, I still get an error on public void getTests(). Do I even need this line?
0
 
nikeseAuthor Commented:
Well, I guess I do because when I take it out I get a million other errors. I am stuck.
0
 
objectsCommented:
try the following:

import java.io.*;

public class Tests

{

   public static void main(String[] args) throws IOException

    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
       
           /*Variables*/

           int numberOfTests = 0;
           double averageScore = 0;
           int[] test;

           /*Description of program for user.*/

           System.out.println("This program will calculate your average");
           System.out.println("for a certain number of test scores that you enter.\n");
           System.out.println("How many test scores will you be entering?");

           numberOfTests = Integer.parseInt(in.readLine());

           test = new int[numberOfTests];
           for (int i=0; i<numberOfTests;i++)

           {
               System.out.println("Enter the score for test number " + (i+1));
               test[i] = Integer.parseInt(in.readLine());
               System.out.println();
           }

         /*Computes the average test scores*/

                double sum = 0;
               for (int i=0; i < numberOfTests; i++)

               {
                    sum = sum + test[i];
               }

               averageScore = sum/(double)numberOfTests;

               System.out.println("The average test score for all tests entered is " + averageScore);

}

}
0
 
CEHJCommented:
You should begin by commenting out nearly everything. Start with main. Don't have other methods nesting inside it. In main, create an instance of Tests, then work on the constructor of Tests
0
 
nikeseAuthor Commented:
I will try these ideas and get back with you guys in about a half an hour. I will let you know.
0
 
RuadRauFlessaCommented:
Check the code that objects dished out. It should work correctly. except for the fact that you would like to print out the results of the diffirent test at the end as well. For that you can simply change the following:


  /*Computes the average test scores*/

  double sum = 0;
  for (int i=0; i < numberOfTests; i++)
  {
    System.out.println("test[" + i + "] = " + test[i]);
    sum = sum + test[i];
  }

  then you should have your output sorted.

One more thing I don't know what you planned on doing with the [code]public void computation()[/code] method but if you were planning on calling it from the [code]public static void main(String[] args)[/code] method then you would have to declare it as being static. ([code]public static void computation()[/code])
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now