Shop More Submit  Join Login

Details

Closed to new replies
January 26, 2013
Link

Statistics

Replies: 5

Need some Java coding help!

:icontwilittiger:
TwilitTiger Featured By Owner Jan 26, 2013  Hobbyist Traditional Artist
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!
Reply

You can no longer comment on this thread as it was closed due to no activity for a month.

Devious Comments

:iconferentix:
ferentix Featured By Owner Jan 27, 2013
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.
Reply
:icontwilittiger:
TwilitTiger Featured By Owner Jan 27, 2013  Hobbyist Traditional Artist
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!
Reply
:iconhai-etlik:
Hai-Etlik Featured By Owner Jan 26, 2013
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.
Reply
:icontwilittiger:
TwilitTiger Featured By Owner Jan 26, 2013  Hobbyist Traditional Artist
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?
Reply
:iconhai-etlik:
Hai-Etlik Featured By Owner Jan 27, 2013
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
Reply
Add a Comment: