zerocrys zerocrys - 9 months ago 33
Java Question

Displaying Average Integer in Array using Driver Program

I need practice with my syntax. I have an array of numbers and with that are methods to find the average, report highest number, and report the lowest number. To report the highest/lowest numbers I will be sorting the array.
But first my problem is with reporting the average. I think that if I can understand that part, then the min/max will be no problem. I have tried changing it to say Driver.randomArray[].getAverage() as well.

Are there any suggestions? Thanks in advance.
~Crystal

error code:


Exception in thread "main" java.lang.Error: Unresolved compilation problems:
randomArray cannot be resolved to a type
Class cannot be resolved to a type
Syntax error, insert ". class" to complete Expression

at IntegerArray.main(IntegerArray.java:48)


and it refers to my attempt to call the average from this line,


System.out.println(randomArray[].getAverage());


First, Driver Class

import java.util.Arrays;

public class Driver {


private static final int MAX = 0;

public Driver(){
int[] randomArray = new int [MAX];
int sum;
final int MAX;
}

public int getAverage(){
for (int index = 0; index < MAX; index++){
int sum = 0;
int[] randomArray = null;
int average;
sum = sum + randomArray[index];
average = sum/MAX;
return average;}

}

public void sortArray(){
// sort the array from smallest to biggest
int[] randomArray;
Arrays.sort(randomArray);

System.out.println("The sorted array from smallest to biggest is:");
for (int number : randomArray) {
System.out.println( + number)}
}
public int getMin(){
int[] randomArray;
// find the lowest number
return randomArray[0];

}
public int getMax(){
int[] randomArray;
// find the highest number
return randomArray[MAX];

}


}


Then my main class:




import java.util.Random;
import java.lang.reflect.Array;
import java.util.Arrays;


public class IntegerArray {



public static void main (String[] args) {
// set up the constant for the size of the array
final int MAX = 10;
int sum = 0;
int average = 0;

int[] randomArray = new int [MAX];

for (int index = 0; index < MAX; index++)

// values of the array go from 0-10
randomArray[index] = (int) (Math.random() *10);

// prints the array
for (int value : randomArray)
System.out.println (value);


System.out.println("The length of the array is: " + randomArray.length);


System.out.println(randomArray[].getAverage());









}

}

Answer Source
  1. The way you're creating your methods eg. getAverage() you can't call it on the array you created. On the other hand you can call them on the a Driver object you create. for example: Driver driver = new Driver(); System.out.println(driver.getAverage()); If you want to call them on the Array Object you should add them on the Array class (but that is something more advanced Java than this).

  2. In Java Code you need to add a ; after a statement don't forget them ;). Your IDE should warn you about them.

  3. When you create your getMax() method you should add the array as a parameter. So that the method knows for what Array object it should get the highest number. For example:

    public int getMax(int[] randomArray) { // find the highest number return randomArray[MAX]; }

This counts for all your methods.

I hope this solves some of your answers if not please add a comment or something.

So here is your code after that:

Driver class:

import java.util.Arrays;

public class Driver {


    private static final int MAX = 0;

    public Driver() {
        int[] randomArray = new int[MAX];
        int sum;
        final int MAX;
    }

    public int getAverage() {
        int average = 0;
        for (int index = 0; index < MAX; index++) {
            int sum = 0;
            int[] randomArray = null;
            sum = sum + randomArray[index];
            average = sum / MAX;
        }
        return average;
    }

    public void sortArray(int[] randomArray) {
        // sort the array from smallest to biggest
        Arrays.sort(randomArray);

        System.out.println("The sorted array from smallest to biggest is:");
        for (int number : randomArray) {
            System.out.println(+number);
        }
    }

    public int getMin(int[] randomArray) {
        // find the lowest number
        return randomArray[0];

    }

    public int getMax(int[] randomArray) {
        // find the highest number
        return randomArray[MAX];

    }
}

IntegerArray class:

public class IntegerArray {
    public static void main(String[] args) {
        // set up the constant for the size of the array
        final int MAX = 10;
        int sum = 0;
        int average = 0;

        int[] randomArray = new int[MAX];

        for (int index = 0; index < MAX; index++)

            // values of the array go from 0-10
            randomArray[index] = (int) (Math.random() * 10);

        // prints the array
        for (int value : randomArray)
            System.out.println(value);


        System.out.println("The length of the array is: " + randomArray.length);

        Driver driver = new Driver();
        System.out.println(driver.getAverage());
    }
}

Reply to the question in the comments:

You should give the randomArray as the parameter with your getAverage method so that method knows on which array it should operate, example: driver.getAverage(randomArray);

Your getAverage method also has some flaws: 1. Your sum variable should be outside the loop because else you would set your sum to 0 every time you iterate inside the loop. 2. int[] randomArray = null; You shouldn't really do this line at all, this line will create a NEW int array and set its value to null while you should you use the int array you give as a parameter.

So your getAverage method becomes:

public int getAverage(int[] randomArray) {
        int sum = 0;
        for (int index = 0; index < randomArray.length; index++) {
            sum = sum + randomArray[index];
        }
        int average = sum / randomArray.length;
        return average;
    }