Need some Java coding help!


TwilitTiger's avatar
Hey! Hopefully this is the right place to find the right kind of people :D I have an assignment to make a number statistics class with arrays, which I've finished all of at least I think, aside from a bonus, but my main within the program is telling me that the array isn't compatible with the method. Can anyone help and tell me what's wrong? Here's the class with the main up top, Javadoc excluded to prevent a huge entry. My teacher never really gave us direction as to how to write a main for a program like this :/

public class NumberStatistics
{
// Class variables and constants to be used by all methods.

public static int[] arrayOfIntegers = new int[10];
private static int logicalSize = 0;

public static void main(String[] args)
{
arrayOfIntegers.initializeArray();
arrayOfIntegers.getMean();
arrayOfIntegers.getMedian();
arrayOfIntegers.getMode();
arrayOfIntegers.printMode();
}

public static void initializeArray()
{

Random generate = new Random();

{

for (int i = 0; i < generate.nextInt(5) + 1; i++)
{

arrayOfIntegers[i] = generate.nextInt(10) + 1;
if ( i < 6 ) arrayOfIntegers[i]++;
logicalSize++;
}
}
}

public static double getMean()
{
int sum = 0;

for (int i = 0; i < logicalSize; i++)
sum += arrayOfIntegers[i];

return sum / arrayOfIntegers.length;
}

public static int getMedian()
{
int sum = 0;

arrayOfIntegers.sortArray();

for (int i = 0; i < logicalSize; i++)
sum += arrayOfIntegers[i];

return arrayOfIntegers[(sum + 1) / 2];
}

public static int[] getMode()
{
{
int[] occurence = new int[logicalSize];

for (int i = 0; i < logicalSize; i++)
{
for (int j = 1; j < logicalSize; j++)
if (j == arrayOfIntegers[i])
occurence[i]++;
}

return occurence;
}
}

public static void printMode(int[] array)
{
int[] occurence = new int[logicalSize];

for (int i = 0; i < logicalSize; i++)
{

for (int j = 1; j < logicalSize; j++)
{
System.out.print("Number of " + j + "'s: ");
if (j == arrayOfIntegers[i])
occurence[i]++;
System.out.print(occurence[i]);
System.out.println();

}

}
}

/**
* THIS METHOD IS A BONUS METHOD!
*/
public static void printModeHistogram()
{

}

private static void sortArray()
{
for (int i = 0; i < logicalSize - 1; i++)
for (int j = i + 1; j < logicalSize; j++)
if (arrayOfIntegers[j] < arrayOfIntegers[i])
{
int temp = arrayOfIntegers[i];
arrayOfIntegers[i] = arrayOfIntegers[j];
arrayOfIntegers[j] = temp;
}
}
}

Brackets may be off too, as I deleted or left out some stuff in pasting, but brackets shouldn't be the problem.

Thanks for anyone that can help!
Comments6
Join the community to add your comment. Already a deviant? Log In
ferentix's avatar
Perhaps you need to remind yourself exactly what the "." means here, how you can call methods depending on where they live, and how to pass arguments to methods :)

For example, the signature of printMode is

public static void printMode(int[] array)

which means that when calling printMode you must *pass* it an array, like so:

int[] arrayOfIntegers = {1, 1, 2, 3, 5, 8}
printMode(arrayOfIntegers)

In contrast, what you're doing is

arrayOfIntegers.printMode()

Let's look at what these two mean:

// Call the local method printMode which takes one parameter. pass arrayOfIntegers to it as that parameter.
printMode(arrayOfIntegers)

versus:

// we have arrayOfIntegers, which is an instance of some class and has a method "printMode" which takes no parameters. Call that method.
// OR arrayOfIntegers is the name of some class, and in that class is a static method called "printMode" which takes no parameters. Call that method
arrayOfIntegers.printMode()

In short, the parentheses enclose the parameter (aka "argument") list for the method, whilst . means "the thing on the right is inside the thing on the left" (slightly oversimplifying but that's the gist).

As an extra note- you never actually use the array parameter inside printMode.

Also, this line:

for (int i = 0; i < generate.nextInt(5) + 1; i++)

you might want to consider if this is really what you mean- the termination condition potentially changes each loop. If you want to simply say "do this a random number of times between 1 and 6" then

int maxSize = generate.nextInt(5) + 1;
for (int i = 0; i < maxSize; i++)
{
...
}

Looking at other parts of your code, I think you also need to take another look at arrays- what they are and how you access their elements (as a hint to why I'm saying this, take a closer look at getMedian()).

I hope some of this helps- sorry if I've gone off track, but I think these are all important fundamentals to nail down as soon as you can. If you have any questions or would like actual code examples to clarify some of this stuff, I'll happily help. On the plus side, I can more or less see what you were going for with most of this code, you just need to work out the kinks in your understanding of the language itself.
TwilitTiger's avatar
Thanks for the help! I'm a little on the slower side of my classof super nerds, and my teacher moves too quickly with these things and it doesn't soak in right away (Plus he's also a rageful bear when it comes to asking questions). I get what you mean though, makes sense. Thank you again!
Hai-Etlik's avatar
In future, use a pastebin rather than dumping code into a forum post. [link]

Consider carefully where the methods you are calling are. One big hint is that arrays don't have methods.
TwilitTiger's avatar
Hmm, never heard of that. Thanks for showing me!

Hmm, true. But then...hmm...calling a loop to do things then, to access all elements?
Hai-Etlik's avatar
No, I mean that arrayOfIntegers.initializeArray() doesn't make sense, as arrayOfIntegers is an instance of type int[] while initializeArray() is a class/static method of NumberStatistics