1.How do I make the array in my Array class reference the array in my Driver class?
2.How do I pass my class methods from my Array class to my Driver class so I can print them out?
Here's my two bits of code
You also need to work out how many of those fields are needed. I think every field you have written should be a local variable in its method.
What class methods? You don’t have any class methods. You only have instance methods. You can get their return values the same way you get the return values for any other instance method: by invoking them on an instance of the class.
You don't. Here's how it should go (Campbell already explained but I'll tell it a different way):
1. Driver creates an array of random numbers.
2. Driver passes the array to an Array object.
3. The Array object keeps the array of numbers it is given so that it can perform various calculations on it upon demand.
>> 2.How do I pass my class methods from my Array class to my Driver class so I can print them out?
You don't "pass" methods, you invoke them. In other words, you "ask/tell" the object that has the methods to perform the commands in that method. A method can be passed any number of values, which you will usually declare as parameters to the method. A method can also "pass back" or return a value to the caller.
Here's another way to look at it:
Driver: "Abracadabra, give me a bunch of random numbers!"
(loud poof and a cloud of smoke, an array of random numbers appears)
Driver: "Abracadabra, give me something that can calculate statistics on these numbers!"
(loud poof and a cloud of smoke, a new Array object is standing there)
Driver, to new Array: "Henceforth, you shall be addressed as 'stats'!"
Driver, to 'stats': "stats, take this array and guard it with your life. I shall ask you to do things with this later..."
(Driver gives the array to stats. stats slips the array under his vest. The array is never seen again)
Driver: "stats, my good fellow, tell me this: what is the mode of those numbers I gave you?"
stats: (sticks his head under his vest and appears to be doing some calculations) "6"
Driver: "Very good! Let me write that on this board so everyone can see!"
(Driver goes to board that everyone can see and writes "The MODE is 6!")
Driver: "stats, my man, tell me this: what is the standard deviation?"
stats: (sticks his head under his vest and appears to be doing some calculations) ...
... so on and so forth
Do you see how the program should flow now?
Edit: Ok, I see what you did. That's fine too. That makes it unnecessary to declare a field in Stats to hold a reference to an array of numbers. It might also make some of the Stats methods to require a sorted array for them to work properly, depending on the algorithm you use.
The advantage of this is that at some point in the future you may want to calculate these values on a List of Integers instead of an array. In order to do this, all you would have to do is add a second constructor that accepted an List<Integer> as a parameter and in this constructor you create an int array of the correct size and copy all the Integers from the list and add them to the array as ints. None of your other methods would have to change.
Doing it your way, you would have to overload all your methods to accept a List<Integer>.
Kyle Wettengel wrote: my homework assignment (which is what this is for) says "Write a class Stats that has only class methods within it" thus I don't believe I'm allowed to use a constructor.....Is my logic correct?
I would interpret that to mean your methods should all be static and you never actually create an instance of the class.
i.e. Your test class would be something like
If this is the case, then each of the methods would have to take an int array as a parameter and you should ignore my last post.
But I would check with your teacher. It may just be loose terminology but class variable and class method are commonly used in place of static variable and static method
PS. I also changed the name to StatsDemo in my driver class thank you for pointing that out
Kyle Wettengel wrote:When I do this everything looks like its working as it should except my min method, do you know why this is?
What value do you initialise your min variable to ? Are any numbers in your array less than this ?
As Junilu says, utility classes like this (classes that only have static methods) should not have any class variables. They should all be local to each method. So your min method should look something like this.
Note that this will throw an exception if the numbers array is empty. Try it to see what the exact exception is. If your teacher has not covered error handling yet, you might want to put a comment in the code to this effect, just so that (s)he knows you have at least realised it's a possibility.
Campbell Ritchie wrote:I presume, Joanne, you expect OP to find the error in that method for himself
Apart from the addition of line 3 and the small change on line 4, the rest was copied and pasted from Kyle's post. I didn't actually read it !
I'll give him a clue - the error Campbell spotted is on line 8 (or at least I assume that's the one he's talking about).