Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes How would i break this down into seperate metods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How would i break this down into seperate metods?" Watch "How would i break this down into seperate metods?" New topic
Author

How would i break this down into seperate metods?

todd stolz-schroeder
Greenhorn

Joined: Feb 08, 2010
Posts: 7
I have my whole program written but i need to break it down into 5 seperate methods one for the values, one for the average, one for the smallest, one for the largest and one for the median. im a little confused on how to do it, thanks.


Anupam Jain
Ranch Hand

Joined: Mar 16, 2010
Posts: 61

todd stolz-schroeder wrote:
I have my whole program written but i need to break it down into 5 seperate methods one for the values, one for the average, one for the smallest, one for the largest and one for the median. im a little confused on how to do it, thanks.


You have the answer in your question itself...

try to make the array reference variable as a class member. make five methods as:

getValues() //To store values in array
getAverage() //to find the average value
getSmallest() //to find the smallest value
getLargest() //to find the largest value
getMedian() //to find the Median value

Hope it helps....


SCJP-6.0 OCPJWCD-5.0
Bert Wilkinson
Ranch Hand

Joined: Oct 28, 2009
Posts: 33
Todd,

Previous post is correct. If you have a good understanding of how to make a method and what a class variable is, this shouldn't take long. Your program is a fine first whack at the solution.

Consider what your program will need to pass the method for input information (the parameters)....this will likely be your array... and what the output will be after it is done calculating (if a calculation is to be returned.)

Badda bing...

another minor formatting point: Your class name should always be a capital letter....and to avoid confusion with an already defined class, you may want to rename it something like "MyArray". Eclipse or NetBeans let you refactor and change names real easily.

Good luck
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
Cross posted: http://www.java-forums.org/new-java/26927-how-do-i-make-bunch-arrays-into-list.html
Ritesh Pareek
Ranch Hand

Joined: Nov 04, 2008
Posts: 50
Hi,

Hope it will work for.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Ritesh Pareek, you have been on the Ranch long enough to know not to give out straight answers like that. It helps nobody to be given such an answer; in fact if it is found out it will earn anybody using it a mark of 0.

I hope I was in time to delete your code before it did any harm.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Campbell Ritchie wrote:It helps nobody to be given such an answer; in fact if it is found out it will earn anybody using it a mark of 0.

Or worse; it can lead to official, or unofficial, disciplinary action.
todd stolz-schroeder
Greenhorn

Joined: Feb 08, 2010
Posts: 7
Anupam Jain wrote:
todd stolz-schroeder wrote:
I have my whole program written but i need to break it down into 5 seperate methods one for the values, one for the average, one for the smallest, one for the largest and one for the median. im a little confused on how to do it, thanks.


You have the answer in your question itself...

try to make the array reference variable as a class member. make five methods as:

getValues() //To store values in array
getAverage() //to find the average value
getSmallest() //to find the smallest value
getLargest() //to find the largest value
getMedian() //to find the Median value

Hope it helps....
Yeah get that but i dont know what the write code for it would be and where to put it I worked on this all day yesterday and just wannna get it done
Bert Wilkinson
Ranch Hand

Joined: Oct 28, 2009
Posts: 33
Well, if you're doing this for a class, there should be some text examples of how to make class methods....it's an early concept...so you might be missing some key knowledge.

You already have the code... you just need to rearrange a bit.

your final answer will have a "main method" to get things started and then other methods to do the subtask that are outside the main method. A variable that is accessible to all of these methods is a "class variable" or "instance variable" of your class, and you'll need one of those to retain info (at this level)

this will snap right into place after you do a little work on figuring out class methods.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The code would be essentially identical to the code you have now, but in methods taking parameters and possibly returning values (depending on the method). It's unlikely anybody is going to write any of it for you (it's against JavaRanch policy). If you take a stab at it and have specific questions you're more likely to get help.
todd stolz-schroeder
Greenhorn

Joined: Feb 08, 2010
Posts: 7
Im trying to do it but when its like this why does it return nothing??

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why does *what* return nothing? And do you mean *return*, or display?
todd stolz-schroeder
Greenhorn

Joined: Feb 08, 2010
Posts: 7
it looks like this





instead of this

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Oh, that's obnoxious--just cut-and-paste next time, please.

Are you calling the functions? They won't call themselves.
todd stolz-schroeder
Greenhorn

Joined: Feb 08, 2010
Posts: 7
David Newton wrote:Oh, that's obnoxious--just cut-and-paste next time, please.

Are you calling the functions? They won't call themselves.


You cant copy out of the command promt so thats what i had to do.

And i added the return command and still the same thing

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

todd stolz-schroeder wrote:You cant copy out of the command promt so thats what i had to do.

Of course you can:
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Where do you *call* the methods you've written?
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Expanding on Anupam Jain's advice, here's an approach to consider. Move the
code you have written into the methods below. Since they are members of class
MyArray, they can all freely manipulate myArray. Jim... ...


BEE MBA PMP SCJP-6
Anupam Jain
Ranch Hand

Joined: Mar 16, 2010
Posts: 61

todd stolz-schroeder wrote:
David Newton wrote:Oh, that's obnoxious--just cut-and-paste next time, please.

Are you calling the functions? They won't call themselves.


You cant copy out of the command promt so thats what i had to do.

And i added the return command and still the same thing



As far as I can see in your main() method you are not calling any of the other methods... (not included in the quote) so its no good to expect any results... otherwise your code seems OK.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3548
    
  15
todd stolz-schroeder wrote:You cant copy out of the command promt so thats what i had to do.


Right click on the text area of the command prompt and select the mark option.
Left-click and hold your mouse and drag your cursor over the area you want to cut and paste - the marked area will appear black on white.
Once you have selected the whole area you want to copy, release the mouse button.
Hit return
The selected area will now be in the copy buffer and can be pasted as usual.


Joanne
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(Or set the Quick Edit mode in the command window options, then it's enabled by default.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How would i break this down into seperate metods?