Hello all! i'm trying to complete the following Java programming assignment, which is late because it won't compile. The prof has not been helpful and the TA is nowhere to be found. I'm using two classes. For the life of me, I cannot figure out what is wrong. Please help! Here's the assignment:

Write a program with an array that is initialized with test data. Use any primitive data type of your choice. The program should also have the following methods:
• getTotal. This method should accept a one- dimensional array as its argument and return the total of the values in the array.
• getAverage. This method should accept a one- dimensional array as its argument and return the average of the values in the array.
• getHighest. This method should accept a one- dimensional array as its argument and return the highest value in the array.
• getLowest.
This method should accept a one- dimensional array as its argument and return the lowest value in the array. Demonstrate each of the methods in the program.

The compiler is saying that there is no getValues() method defined anywhere.

I suggest you swap the order of these two:

Code :

//Get total of test scores
getValues(scores);
//Create TestData object and initialize it with test data.
TestData total = new TestData(scores);

ie create the TestData instance before you get the total of the test scores. This makes sense because, until you have created the TestData instance there is nothing to get the total of.

Also give the TestData instance a reasonable name. total is not a reasonable name for something that represents a set of data. data is a better name, or test. In any case you will use this name later when you call the methods you have written: data.getTotal(), data.getAverage() etc.

February 11th, 2012, 09:06 PM

Norm

Re: Code won't compile

Which of the error messages don't you understand?
scores is already defined <<< the compiler sees that scores has already been defined.
cannot find symbol <<< the compiler can not find the symbol whose name is mentioned in the error message
incompatible types
found : double[] <<< The compiler found this data type but its the wrong one
required: int <<< you should use an int not a double[]

February 12th, 2012, 06:21 PM

JavaChallenged

Re: Code won't compile

Many thanks to pbrockway and Norm for your help. I have difficulty interpreting the compiler messges because, in my limited experience, the message does not always refer to the exact problem. Sometimes, the problem lies in an earlier part of the script.

Having said that, I still have 3 error messages for the following code. I'm sure it's an obvious fix, and I've looked at my textbook and tried changing the keywords, but nothing has worked:

Now you have three variables that the compiler can not find definitions for.
Where are these variables defined?
If they are defined in one method, you can not see and use them in another. Their definitions must be at the class level for multiple methods to use them.