This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Cattle Drive and the fly likes Say 4b - completely stumped Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Say 4b - completely stumped" Watch "Say 4b - completely stumped" New topic
Author

Say 4b - completely stumped

Nick White
Ranch Hand

Joined: Oct 09, 2006
Posts: 94
I seem to have reached the limit of my ability on this one.

I will try and describe the problem, without giving anything away to those following in my dust...

My code works.

Trouble is the nitpicker doesn't like it.

I move some code from the 'main' method, and then i get told I am repeating myself. So... I move some logic back into main, and get told it would be better off back in the method, and just pass parameters.

OK - this I did. Then I get told that the arguments I pass could be handled in the method. Then I am told I could return a value instead of void.

Now so far this will make little sense unless you have been there or you are the nitpicker.

It seems that it would all work fine, but I will end up doubling the amount of code, or moving some if statements to another method, just because of the variable I need (which keeps track of how big the number I have left is), doesn't like to be returned from one method to another.

Could I have some pointers on:
1) Not using FOR statements when the process is only repeated 4 times
2) Why having the IF statements in main is not good
3) Keeping track of a variable that needs to be accessed by ALL methods in a class, and the latest value used!

I have been advised that I am close, but to me I seem to be told to do one thing, and then advised to do it the way I had previously, just better!!

Any pointers gratefully received - DO NOT POST the code and get this pulled, please!

Regards

Nick
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1347
    
  13
Hey there, Nick,

I'm behind you on this, so don't trust a word I say. That said...

I would suggest staying in main just long enough to call a method that orchestrates the whole show.

The method that orchestrates the whole show can call other methods. Sometimes a little method is handy for something that the show-orchestrating method needs to do several times.

i.e.


A static variable is visible to the whole class. This is usually used if you have several objects of the same class that need to access the same information (please excuse my imprecise explanation - I am sure someone else will come along with a better description). So a static variable might be a way of keeping track of whatever it is you need to keep track of.



Once again - I have no idea how you have approached this (and since I'm still a couple of assignments behind you I really don't want to know <grin>). Everything I say should be taken with a cow-lick of salt!

Katrina

[ November 13, 2006: Message edited by: Katrina Owen ]
[ November 13, 2006: Message edited by: Katrina Owen ]
Nick White
Ranch Hand

Joined: Oct 09, 2006
Posts: 94
Katrina

Thank-you!

I have an idea, and as you point out I can do it by calling a method that does what I want, and that could call another method.

Thing is....

This increases my number of methods to five - not sure that really matters (?).

Secondly, I will basically be repeating my code in different methods, and as far as readability goes, I can't see a benefit.

I will see if I get any further help, and failing that I will just submit my 6th (!!!) attempt as described above.

Regards

Nick
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1347
    
  13
Is there some way of extracting out the similarities between similar methods and making one method that does everything that is identical? Then -- uhm -- you could change the conditions by passing a parameter or something.
Nick White
Ranch Hand

Joined: Oct 09, 2006
Posts: 94
What I did was this...

In main, work out the size of the number, and based on that passed 3 parameters to a method that printed out the 'first' part of the number, and passed the 'remainder' back to me to be re-processed, i.e. work out the size of the number etc etc.

I was advised that I could pass the working of the number into a method and then expect a return (not sure what).

So what I thought was, move the working out of the number into a new method and process as before, using the 'remainder' to carry on...(see where I need a variable to be usable in different methods?)

Not sure if this helps readability and the benefit. My last submission had 4 methods, 109 lines, 16 of which are blank.

To me (untrained) it seems OK.

Nick
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1347
    
  13
Hmm...

How about holding both a current "working portion" number and the remainder in static variables.



Not at all sure if it would be legal, nor whether it is at all applicable to your situation.

Katrina
[ November 13, 2006: Message edited by: Katrina Owen ]
Nick White
Ranch Hand

Joined: Oct 09, 2006
Posts: 94
Katrina

Thanks again.

I have submitted a solution, basically still 4 methods and all the stuff moved out of main...

I found your examples helpful, although while.... can't be used as far as I understand the style guide, but I got the point.

I still think there is little difference in what I have submitted this time compared to try number 5, so I am not overly confident it will pass, but if not hopefully the nitpick will push me further along.

Having looked at the new code, though I think perhaps it is tidier

Regards

Nick
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1347
    
  13
Another thought...

Could you split off all the bits of your number at the start and then feed each bit sequentially to the method that you use to work on your three digits?


[ November 13, 2006: Message edited by: Katrina Owen ]
Nick White
Ranch Hand

Joined: Oct 09, 2006
Posts: 94
The only trouble with thoughts like this are that when you actually try and code the first part, second part etc, suddenly the neat pseudocode doesn't look quite the same.

Think about how you would determine each part.

Would you take the number of digits and deduce the number from that?
Perhaps take the length of the string?

Ultimately, I worked on the basis of was the number bigger than a billion? If so, what do you then do to the number to; a) work out the number of billions, and b) what are you left with? etc etc.

Not quite as easy as the pseudocode implies, although it is sort of what I have done!

Nick
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1347
    
  13
Yeah, since I'm not there yet it is hard for me to say.

How about lopping off numbers using division and modulus? That would leave you with 0 if the number you are dividing by is larger than the number you are inputting.
Nick White
Ranch Hand

Joined: Oct 09, 2006
Posts: 94
That's what I have gone with. Seems the best way - still we will see what Pauline thinks.....

Nick
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1347
    
  13
Fingers crossed
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1347
    
  13
So Nick,

Any news from Pauline yet?
Nick White
Ranch Hand

Joined: Oct 09, 2006
Posts: 94
Yes!

Twice in fact!

Maybe I will complete this one soon....

Thing is, I am trying too many things at once.

I run a laptop with Windows XP, and for a long time I have wanted to try linux. So today I decided to download a distribution and actually use it rather than just install and go back to windows, which I am familiar with.

I did all that - took ages and now I find myself trying to configure my new linux system to do what I know I can do easily in windows!! :roll:

However, this is my first post using my linux distribution (www.ubuntu.com).

I even now have my email setup - this again is a first.

Whilst doing all this I notice Pauline has responded - and I have briefly read her response. By the way she also responded last night and gave me a clearer idea of what was required.

I hope to get off my eighth!!! reply tonight, and hopefully I will be onto the next one before the week is out.

By the way - any hints and tips on converting from XP to ubuntu would not go amiss. I have read all the wiki stuff on their site - but personal experience is always appreciated!

I have achieved the following:

1) install
2) wireless up and running! phew.
3) email ok
4) internet ok!
5) need help reading my existing windows file system - briefly tried and can mount the volume, but no files!

Regards

Nick
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
Nick,
You might want to ask your ubuntu question in the Linux forum.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Katrina Owen
Sheriff

Joined: Nov 03, 2006
Posts: 1347
    
  13
Hi Nick,

You could try mounting your XP partition as root (or sudo), but I would second Marylin on asking at the ubuntu forums

Sounds like there is a lot of learning going on with this assignment.

Enjoy,
Katrina
Nick White
Ranch Hand

Joined: Oct 09, 2006
Posts: 94
A couple of things...

Many thanks to you all for replying. Sorry for introducing ubuntu - you are correct not the correct forum

Since then I have got the from Pauline!

My linux is progressing

Closing topic now - so email if you wish to carry on!! // edited bit follows: (I can't!! - have asked a moderator to do so).

Regards

Nick
nick.white@btinternet.com
[ November 16, 2006: Message edited by: Nick White ]
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Nick: Closing topic now - so email if you wish to carry on!!

Okey doke, on one condition: if you promise that we're not missing out on any great conversations happening via email...


Pauline
Nick White
Ranch Hand

Joined: Oct 09, 2006
Posts: 94
I promise

Nick
nick.white@btinternet.com

Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Well OK then, we'll put an end to this round of stumping.
 
Don't get me started about those stupid light bulbs.
 
subject: Say 4b - completely stumped
 
Similar Threads
References between classes
4b structure
Initial Page
Returning a value from a pop-up window
Passed By Reference?