Write methods and constructors from Javadocs
Hi everyone, I'm currently having an assignment in which my instructors want students to "understand" and interpret Javadocs, then write the methods and constructors back from them. Right now I'm stuck at these last two and I would like to ask you guys for suggestions.
Code :
[B]Constructor Detail[/B]
DogAndCat
public DogAndCat(int size)
Creates a grid of squares with height and width both equal to the given size; each square is constructed with the appropriate status (cat, dog, or a vacant spot) for starting a game.
The starting configuration consists of a vacant square in the center and the remaining square being half occupied by cats and half occupied by dogs. Each (vertical) column with a higher index than the middle column starts with all cats, and each column with an index lower than the middle column starts with all dogs. The center column has cats in the low row indices, dogs in the high row indices, and a vacant spot in the middle.
The legal range of size is odd numbers from 3 up to 9, which is assumed by the constructor as a precondition.
and another Javadocs (this time for a method called "isSolved" :
Code :
isSolved
public boolean isSolved()
Indicates whether or not all lily pads in the pond are in the solved state.
Returns:
true if the puzzle is solved and false otherwise.
He was asking us to write a cat and dog game, where you can find an example here : Toads And Frogs puzzle in two dimensions (actually the one in the example are frogs and toads, but he changes it to cats and dogs lol). I don't know what to put inside the constructor, as well as "isSolved" method. How could one know when is the problem solved? I'm really confused right now. Any help is appreciated. Thank you.
Re: Write methods and constructors from Javadocs
You need to create a size X size 2D array to hold cats or dogs. I assume the rest of the assignment gives details of what cats and dogs are. The instructions go into great detail of which cells in the 2D array will be blank, which hold dogs and which hold cats. What specific part are you having trouble with? Try writing some code. When you get stuck, post the code and any error messages you get and ask a specific question. "I don't know how to do this" is not specific.
Also, the last line mentions what valid values size can have. You should add some code at the start of the constructor to validate the size. If it is invalid, throw an IllegalArgumentException.
Re: Write methods and constructors from Javadocs
Thanks for your input ^^, I have already put if else to validate the size. However, I got lost after that because I don't know how to make that size X size 2D array you said above, as well as how to make the "play area" according to what user wants in the main app (for ex if user input 3 it creates a 3x3 area with 9 squares, 5 will be 5x5, 6 will be invalid, 10 will be invalid, and so on). Canyou give me a link on how to make that kind of array please? I got lost right at the start, so I couldn't write any code on the applet yet, except for the validation part :(
Re: Write methods and constructors from Javadocs
What happened when you typed "java 2d array" into Google?