This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.We're giving away four copies of TensorFlow 2.0 in Action and have Thushan Ganegedara on-line!See this thread for details.
Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Paul Clapham
• Bear Bibeault
• Jeanne Boyarsky
Sheriffs:
• Ron McLeod
• Tim Cooke
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Jj Roberts
• Stephan van Hulst
• Carey Brown
Bartenders:
• salvin francis
• Scott Selikoff
• fred rosenberger

# Not sure if my method is written correctly

Ranch Hand
Posts: 65
I'm having trouble with this method that I have to write for a school assignment and I'm not sure whether the implication is that I should be incrementing color or incrementing the number of occurrences for each individual color. Any pointers to what's wrong with it? It's due tomorrow.

Saloon Keeper
Posts: 7393
66
Look at line 9. You pass in a value to the method in a parameter called 'color'. On line 9 you immediately set it to zero and lose whatever it was that was passed in.

The comment suggest that you should increment a count.

Carey Brown
Saloon Keeper
Posts: 7393
66
Where do the magic numbers -2 and 1 come from?

Andrew Bauer
Ranch Hand
Posts: 65

Carey Brown wrote:Where do the magic numbers -2 and 1 come from?

Those represent the value of the colors. 1 would indicate green, 0 would indicate white, -1 would indicate yellow, and -2 would indicate black.

Carey Brown
Saloon Keeper
Posts: 7393
66

Andrew Bauer wrote:number of occurrences for each individual color

The comment doesn't say anything like this, it says number of cells that equal 'color'.

Carey Brown
Saloon Keeper
Posts: 7393
66

Andrew Bauer wrote:

Carey Brown wrote:Where do the magic numbers -2 and 1 come from?

Those represent the value of the colors. 1 would indicate green, 0 would indicate white, -1 would indicate yellow, and -2 would indicate black.

That's a very weird scheme to use both negative and positive integers. Are you sure? Besides, instructions say if the cell equals 'color', not -2, -1, 0, or 1.

Andrew Bauer
Ranch Hand
Posts: 65

Carey Brown wrote:

Andrew Bauer wrote:

Carey Brown wrote:Where do the magic numbers -2 and 1 come from?

Those represent the value of the colors. 1 would indicate green, 0 would indicate white, -1 would indicate yellow, and -2 would indicate black.

That's a very weird scheme to use both negative and positive integers. Are you sure? Besides, instructions say if the cell equals 'color', not -2, -1, 0, or 1.

Yeah, but does this look a little bit closer to correct now?

Carey Brown
Saloon Keeper
Posts: 7393
66
Yes, that looks good. You shouldn't be needing "this." though.

Carey Brown
Saloon Keeper
Posts: 7393
66
One subtle improvement you could make is to get the number of rows and columns directly from the 'grid' variable. Remember that 2D arrays in Java are really arrays of arrays, so grid.length is the number of rows and gird[row].length is the number of columns. That gives you...

Andrew Bauer
Ranch Hand
Posts: 65

Carey Brown wrote:One subtle improvement you could make is to get the number of rows and columns directly from the 'grid' variable. Remember that 2D arrays in Java are really arrays of arrays, so grid.length is the number of rows and gird[row].length is the number of columns. That gives you...

Alright well I tried without "this" included and the type of segment that you gave me but for some odd reason the method still doesn't pass the junit test. At this point I'm so lost lol

Carey Brown
Saloon Keeper
Posts: 7393
66
Can you post the actual failure message? Do you have the source for the test itself?

Andrew Bauer
Ranch Hand
Posts: 65

Carey Brown wrote:Can you post the actual failure message? Do you have the source for the test itself?

Yeah this is the segment in the test.

CellcountTestFailure.JPG

Carey Brown
Saloon Keeper
Posts: 7393
66
Could you re-post the code for your method?

Andrew Bauer
Ranch Hand
Posts: 65

Carey Brown wrote:Could you re-post the code for your method?

Yup.

Carey Brown
Saloon Keeper
Posts: 7393
66
Well, cellCount() is very straight forward. Have you tested inoculate() or nextState()? Are there test cases for those?

Andrew Bauer
Ranch Hand
Posts: 65

Carey Brown wrote:Well, cellCount() is very straight forward. Have you tested inoculate() or nextState()? Are there test cases for those?

Yeah I've tested inoculate and it passes the junit test itself and this is what I have written for that method. I have not gotten around to the nextState method yet but these are the rules that have been established.
The Pandemic Simulation board is a rxc grid and each cell of the grid represents a person or group of people. Each cell is in one of four states

GREEN for inoculated
WHITE for normal (i.e., not sick not inoculated)
YELLOW for infected/sick
BLACK for deceased
In each phase or step of the simulation has two parts.
The Department of Health (the player) has a limited supply of vaccine and can inoculate some fixed number (currently 4) uninfected people. This will change the cell from WHITE to GREEN.
Each WHITE and YELLOW cell is updated using these rules
If the cell is WHITE and 2 or more of its neighbors are infected then the cell is infected (YELLOW).
If the cell is YELLOW and 5 or more of its neighbors are infected then the cell dies (BLACK).
If the cell is YELLOW and has between 2 and 4 infected neighbors it remains infected (YELLOW).
If the cell is YELLOW and has at most 1 infected neighbor the cell recovers (WHITE).

Inoculate-test-pass.JPG

Carey Brown
Saloon Keeper
Posts: 7393
66
Doesn't look like inoculate() follows the requirements.

Andrew Bauer
Ranch Hand
Posts: 65

Carey Brown wrote:Doesn't look like inoculate() follows the requirements.

Even though it passes the test?

Carey Brown
Saloon Keeper
Posts: 7393
66
IF cell inoc.grid[i][j] is GREEN
THEN the cell this.grid[i][j] is set to GREEN.

Carey Brown
Saloon Keeper
Posts: 7393
66
You are testing for YELLOW (-1), not GREEN (1).

Carey Brown
Saloon Keeper
Posts: 7393
66
Instead of magic numbers like '1', you could use PandemicModel.GREEN which would make the code more readable.

Andrew Bauer
Ranch Hand
Posts: 65

Carey Brown wrote:Instead of magic numbers like '1', you could use PandemicModel.GREEN which would make the code more readable.

Okay so like this then.

Carey Brown
Saloon Keeper
Posts: 7393
66
Magic numbers are very bad. It is best to replace them with named constants that describes them. This makes the code more readable and the coding less error-prone.

Anyway, I'm closing down for the night. Good luck.

Carey Brown
Saloon Keeper
Posts: 7393
66
Remove line 17. Redundant.

Marshal
Posts: 70684
288
Please check carefully what the if in line 14 in the original version is doing. I think it degenerates to if (true).

Carey Brown wrote:One subtle improvement . . .

What about replacing the ordinary for loops with for‑each loops? After all, you are iterating the entire array in the normal direction.