Methods
[Framework practical 3 of 7]
Now we'll build some methods. This will make our code clearer and more flexible.
We're going to build four methods in the Storage class based on the code we wrote last practical in the Analyst class.
These will be:
setRandomData
: fills the data array with random data; takes in nothing and returns void (i.e. nothing). Note that even though this is a "set" method, it isn't going to take anything in -- maybe "setupRandomData" would be a better name!
printArray
: prints the data array to the screen; takes in nothing and returns void.
getMaximum
: finds the array's maximum value; takes in nothing and returns the maximum as a double.
getMinimum
: finds the array's minimum value; takes in nothing and returns the minimum as a double.
We already have most of the code we need, so we just need to work out:
1) How to get it to work as methods within the Storage class.
2) How to test they work.
3) How to put the method calls in the Analyst class.
Note: if you merged together any of the code last practical (for example, doing the data randomisation within the loop working out the maximum and/or minimum) you'll need to separate out the code into four separate loops (each doing one of the jobs above) before cutting and pasting the code for each as we go through building the methods. If you have merged for-loops, sorting this out should just be a matter of copying and pasting the merged loop below itself, and deleting the bits from each you don't need. You can either do this yourself, based on your code, or use this copy: Analyst.java
Let's start by making the setRandomData
method. This is going set the array in Storage up with random data and return nothing.
Start by writing the method declaration in the Storage class. If you are unsure how to write a method declaration, check the lecture materials. Remember also to put in the start and end brackets for the block as well -- if you do these now you won't forget to do them later, and you'll also be able to test your class. Remember, like all methods, this method should be inside the Storage class block, and outside any other methods you have (not that you do, yet).
Once you've got the declaration written, compile the code to check it. It obviously won't yet work, because we haven't called it or put any code into it, but it should compile because it's not returning anything (and so doesn't need a return statement).
Now copy into the Storage class' new setRandomData
method the code you developed last practical within the Analyst class to randomise the data. Adjust the name of the array so it is no longer store.data
, but instead just data
, because we're now inside the same class as the array.
Again, compile the code to check it is properly written.
Now we need to test our code. You should always think of a way to test each chunk of new code you write. In the Analyst class you should still have code from last practical that prints the content of the store.data
array. Before this code in the Analyst class, put in a call to your new setRandomData
method in the Storage class:
store.setRandomData();
Comment out any other code in the Analyst class you may have that randomises the data, so you know that it is only your new method doing the job.
Now you should be able to compile and run your code. With any luck it will show the array still containing random numbers.
Once you've got that working, go on to Part Three, where we'll make the other three methods.