Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How would i break this down into seperate metods?

 
todd stolz-schroeder
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Bert Wilkinson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2542
9
 
Ritesh Pareek
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Hope it will work for.

 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im trying to do it but when its like this why does it return nothing??

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does *what* return nothing? And do you mean *return*, or display?
 
todd stolz-schroeder
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it looks like this





instead of this

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where do you *call* the methods you've written?
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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... ...
 
Anupam Jain
Ranch Hand
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Or set the Quick Edit mode in the command window options, then it's enabled by default.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic