• Post Reply Bookmark Topic Watch Topic
  • New Topic

from java prep code to test code (resolved)  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from java prep code to test code.... I can't grasp the idea how to write test code, then real code, test all previous tests, continue until done.
I see the benefits from doing it step by step. Could someone give me an idea how I could start writing test code for the following prep code?
Or give a general pointer that helps me on my way is also great.

The prep code:

DECLARE and instantiate the GameHelper instance variable, named helper.
DECLARE and instantiate an ArrayList to hold the list of DotComs (initially three) Call it dotComsList.
DECLARE an int variable to hold the number of user guesses (so that we can give the user a
score at the end of the game). Name it numOfGuesses and set it to 0.
DECLARE a setUpGame() method to create and initialize the DotCom objects with names
and locations. Display brief instructions to the user.
DECLARE a startPlaying() method that asks the player for guesses and calls the
checkUserGuess() method until all the DotCom objects are removed from play.
DECLARE a checkUserGuess() method that loops through all remaining DotCom objects and
calls each DotCom object’s checkYourself() method.
DECLARE a fi nishGame() method that prints a message about the user’s performance, based
on how many guesses it took to sink all of the DotCom objects.
METHOD: void setUpGame()
// make three DotCom objects and name them
CREATE three DotCom objects.
SET a name for each DotCom.
ADD the DotComs to the dotComsList ( the ArrayList).
REPEAT with each of the DotCom objects in the dotComsList array
CALL the placeDotCom() method on the helper object, to get a randomly-selected
location for this DotCom (three cells, vertically or horizontally aligned, on a 7 X 7 grid).
SET the location for each DotCom based on the result of the placeDotCom() call.
END REPEAT
END METHOD

METHOD: void checkUserGuess(String userGuess)
// fi nd out if there’s a hit (and kill) on any DotCom
INCREMENT the number of user guesses in the numOfGuesses variable
SET the local result variable (a String) to “miss”, assuming that the user’s guess will be a miss.
REPEAT with each of the DotObjects in the dotComsList array
EVALUATE the user’s guess by calling the DotCom object’s checkYourself() method
SET the result variable to “hit” or “kill” if appropriate
IF the result is “kill”, REMOVE the DotCom from the dotComsList
END REPEAT
DISPLAY the result value to the user
END METHOD
METHOD: void fi nishGame()
DISPLAY a generic “game over” message, then:
IF number of user guesses is small,
DISPLAY a congratulations message
ELSE
DISPLAY an insulting one
END IF
END METHOD
METHOD: void startPlaying()
REPEAT while any DotComs exist
GET user input by calling the helper getUserInput() method
EVALUATE the user’s guess by checkUserGuess() method
END REPEAT
END METHOD
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've programmed in a few languages now and the only time I need prep code is when the project/class is huge. Even then I don't do it like you've done here, a post-it note with a list of things that need to be done, or in what class does a certain function exist will suffice. Really this sort of stuff is just designed for helping beginners understand how to 'think' like a programmer. Its not necessary. Just start programming and trail by fire. Best way to learn imo.
 
chihwah li
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ok Thanks!
I was reading about a book and I thought it might be important to pick up early.
But you are right...the most important thing to learn is the language itself,
Come to think about it, it's like I am trying to ride a bicycle and I can't even walk straight yet.

But if anyone have a good pointer...please do tell... thumbs up
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that sort of pseudocode is actually good, specially for a beginner. It shows how you break down the problem into smaller and smaller pieces, which you can easily convert to real code.

And the language itself is the least important thing. It is the logic pf programming you need to learn. Once you have the logic it is relatively simple to convert it to C, Java, Pascal, Eiffel or whatever.
 
chihwah li
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ahh ok, I see the logic in this. If I can talk the code in my own language I am able to understand it at a higher lvl.
Then I truly understand it. Thanks for another good comment!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this; it's "level" not "lvl".
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chihwah li wrote: . . . Thanks . . .
You're welcome
 
Brad Dwan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:...specially for a beginner. It shows how you break down the problem into smaller and smaller pieces ...

Meaning, thinking like a programmer :P

Campbell Ritchie wrote:...And the language itself is the least important thing. It is the logic pf programming you need to learn...

Agree 100%. Programming logic is easily transposed across most languages. Differences in syntax can be great and that's where learning the actual language comes in. For example: C++ is strict (have to do substantially more coding to achieve the same result) compared to Java, and Java is strict compared to PHP, thus C++ and PHP will seem completely different although the similarities in logic are obvious when reading the source code. It's the language itself that is more complex.

That's my two cents worth anyway...
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brad Dwan wrote: . . . Meaning, thinking like a programmer :P

. . .

Agree 100%.
  • 1: Yes, I think we both mean the same thing.
  • 2: Use the button, rather than writing smilies by hand. You missed out a hyphen so you got :P rather than
  • 3: Agree with myself too
  •  
    Brad Dwan
    Ranch Hand
    Posts: 62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Brad Dwan wrote: . . . Meaning, thinking like a programmer :P

    . . .

    Agree 100%.
  • 1: Yes, I think we both mean the same thing.
  • 2: Use the button, rather than writing smilies by hand. You missed out a hyphen so you got :P rather than
  • 3: Agree with myself too


  • You can't agree with yourself. That makes you look arrogant
     
    Campbell Ritchie
    Marshal
    Posts: 56600
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Brad Dwan wrote: . . . You can't agree with yourself. . . .
    I have to. Nobody else does
     
    Brad Dwan
    Ranch Hand
    Posts: 62
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:I have to. Nobody else does


    Well really I have to agree with you, now that we've had a 'conversation' I can not feel anything but animosity toward you. bahah
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!