wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Array driver class Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array driver class Java" Watch "Array driver class Java" New topic
Author

Array driver class Java

Kyle Wettengel
Greenhorn

Joined: Feb 06, 2013
Posts: 14
Hey guys I'm having problems figuring out how to write my driver class for my Array class.

My questions

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

Main class:



Driver class:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
You can pass the array to the constructor as you would in any class. You will have to write a constructor first.
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.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

>> 1.How do I make the array in my Array class reference the array in my Driver 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?


Junilu - [How to Ask Questions] [How to Answer Questions]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

BTW, 'Array' is not a very good name for that class. 'ArrayStatistics' would be better. Even better: just 'Statistics' -- the fact that you are using an array is an implementation detail and you usually want to hide those kinds of things. Choose names that communicate the intent or purpose of something, whether it's a class, method, variable, parameter, etc. That makes your program easier to read, understand, and debug.
Kyle Wettengel
Greenhorn

Joined: Feb 06, 2013
Posts: 14
Thanks for explaining it for me I believe I'm beginning to understand what needs to be done, first I renamed my classes so there easier to follow, second I created a new Array object in my driver class to attempt to pass the array to my Main class which is now renamed Stats, and lastly I attempted to set up my methods to take my array in my driver class as a parameter. Any hints as to what to do next? I would like to be able to print out the values of min, max, average, standard deviation at the end but I do not know how to write this, any hints or additional directions would be greatly appreciated!

New code:

Main class



Driver Class

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

So, when do you hand the array of sorted numbers off to stats so it can calculate some interesting statistics from them?

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.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3410
    
  12
Your average, max, min, etc methods should not be expecting an int array to be passed to them. Instead your Stats class should have a constructor that accepts an int array and then you use this int array (or even better, a copy of it) whenever one of the methods is called.
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>.

Joanne
Kyle Wettengel
Greenhorn

Joined: Feb 06, 2013
Posts: 14
Thanks for the help guys, but 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?
Kyle Wettengel
Greenhorn

Joined: Feb 06, 2013
Posts: 14
The assignment also reads this for my demo class but i believe I've taken care of most of this already "Finally, create a StatsDemo class that creates a 20 element int array and populates it with random elements in the range 20 through 65 (both inclusive). The tester class should then test each of these methods. Use the java.util.Random class with a seed of 2621 to generate repeatable output for the data."
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3410
    
  12
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
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3410
    
  12
Kyle Wettengel wrote:create a StatsDemo class

If the assignment specifies a specific name for a class then that is the name you should use. i.e. StatsDemo instead of ArrayTest
Kyle Wettengel
Greenhorn

Joined: Feb 06, 2013
Posts: 14
Ill follow you lead on this and make my class methods static, however in order to do this I will also have to make my instance variables static as well correct? When I do this everything looks like its working as it should except my min method, do you know why this is?

PS. I also changed the name to StatsDemo in my driver class thank you for pointing that out

New code

Main class:



Driver class:

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

As was already mentioned before, none of your instance variables are needed. Instead, declare a local variable in the method that needs it. That's actually a good practice, to keep the scope of variables as small as possible, allowing only the code that absolutely needs them to have access to them.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3410
    
  12
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
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
I presume, Joanne, you expect OP to find the error in that method for himself
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3410
    
  12
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).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
And there I was thinking you had slipped the error in to see whether he was awake!

The big error I spotted is, as you say, Joanne, on line 8. The smaller style error is on line 4.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array driver class Java
 
Similar Threads
SiMpLe Program but......I still beginner
Help with sorting array of random numbers
Exam question for a beginner coder
standard deviation of any sized array java