programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Help with sections in code

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
Hi, I am have been stuck on this code for a while now, and I don't know what to do to finish it. I thought I completed the TODO section, but apparently, I did not.

Bartender
Posts: 1845
10
• Number of slices to send:
Optional 'thank-you' note:
Not really very clear code.
From the TODO comments, I guess that the individual entries in the two-dimensional array are blood donors - representing how much people have donated.
The first index represents the state (in the US ? )
The second index the number of the donor in each state?

public int findTopDonor() {
/*** TODO: Read each value in donations, noting the index of the maximum
(maybe keep a counter to know how many donors have come before it)
***/

public int findTopState() {
/*** TODO: Sum all values in a row of donations, noting the row of the maximum
Return the maximum row ***/

Anyways, the first thing is to re-read the questions and make sure you are returning what they want.

Find Top Donor - you are not keeping track of the maximum amount seen so far anywhere.  I would suggest

Find top state.  They want the index in the array which has the highest sum.
Not the actual sum...
I would suggest printing out the value of rowSum after each "row" is completed, and then checking to see if it matches what you calculate.

One final  suggestion - for testing purposes change the line at the top:
Random r = new Random();
Make it
Random r = new Random(1);
Just remember to change it back before you submit the assignment :-)

That change means you will get the same "random" numbers every time which makes it easier to debug your program

Alex D'Angelo
Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
How would I keep track of the maximum amount in findTopDonor()?

Marshal
Posts: 79233
377
• Number of slices to send:
Optional 'thank-you' note:
Have you got a donor class? That class should record number of donations (and maybe type, too). It looks very bad design to me to have arrays of donation numbers; if you have a donor class, you can return a reference to it and there will be no messing around with int indices. I hope your questions don't require you to use such poor design

Don't use /** comments */ for TODOs. Use // comments short enough to fit on one line. Using the correct comments is part of the art of formatting code.

Marshal
Posts: 8857
637
• Number of slices to send:
Optional 'thank-you' note:
Welcome to the Ranch.

It seems you know the coding from the syntax point of view and can manage implementing logical parts.
However, you seem to be squashed everything to one class and added bunch of unrelated to the class methods. And that may be a problem.

Java is object-oriented language, so you'd be better off trying to follow that line, so you could achieve easier maintainable and extendable code.

Even though you are using methods, in my opinion they aren't enough decomposed as I can see some places with 3 and even 4 nested blocks. As a primitive rule, you can think, one nested block makes one method, because outer loop does something broader, while inner loop does something more narrowed (and that needs to be a method).

What Campbell rightly suggested, by introducing extra classes you could assign responsibilities better, which would result in a better design, which would be easier to understand.

Have you covered objects in your learning material?