samedi 31 août 2019

Trouble calling method and Populating Random Array

I am new to coding and trying to call a method (RandomArray) which I wrote and defined in a separate class, but in my driver code I keep getting the error message "Couldn't find symbol- RandomArray()".

the code SHOULD create an array (size of which is chosen by the user) and then populate it with random numbers and output the Highest, Lowest, and Average of the numbers in said array.

All spellings match up, and the call itself works and displays no errors, but when using it in the for-loop I get the error message.

// this is the class where I created the method

import java.util.Random;
public class RandomArray
{
    //things declared at class level
    public int minimun,maximun,adverage, mn, mx, avg;
    public String range;

    //constucotr for inital numbers
    public RandomArray()
    {
        minimun = 0;
        maximun = 1000 + 1;
    }


static int RandomArray()
{
    Random ran = new Random();
    return (ran.nextInt(1000) + 1);
}

//define types
public RandomArray (int mn, int mx, String r, int a)
{
    minimun = mn;
    maximun = mx;
    range = r;
    adverage =  a;
}

//set minimun
public void setMinimun (int m)
{
    minimun = mn;
}

//get minimun
public int getMinimun()
{
    return minimun;

}

//set maximun
public void setMaximun(int x)
{
    maximun = mx;
}
//get maximun
public int getMaximun()
{
    return maximun;
}

//compute adverage
public int adverage(int...array)
{
   int adverage = 0;
   if (array.length > 0)
   {
       int sum = 0;
       for(int num : array)
            sum = sum + num; //add numbers in array 
       adverage = (int)sum / array.length; //divide numbers in array by the array lenth

    }

   return adverage;
}

//return values as a string
public String toString()
{

    return String.valueOf(getMinimun() + getMaximun() + adverage());

}

}

// and this is the driver program that should be populating the array (of users choice) with random numbers and printing the highest, lowest and average.

import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;
public class DemoRandomArray
{
    // variable go here
    final int minimun = 0;


    public static void main(String[] args)
    {
        final int max = 100;

        RandomArray ra = new RandomArray(); 

        int[] anArray = new int[1000];


        for(int i=1; i < max; i++)
        {
           System.out.println(RandomArray());
        }

    }

}




Aucun commentaire:

Enregistrer un commentaire