06b extra methods


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

06b extra methods

  1. 1. The “Simple Dot Com Game”<br />Goal: Sink all of the computer’s Dot Coms in the fewest number of guesses. You’re given a taring of level, based on how well you perform.<br />Setup: When the game program is launched, the computer places three Dot Coms on a virtual 7x7 grid. When that’s complete, the game asks for your first guess.<br />How you play: The computer will prompt the user to enter a guess. In response to the guess you’ll see a response at the command line: “Hit”, “Miss”, or “You sunk Pets.com” (or whatever the name of the company was). When all companies are sunk the game ends by reporting your rating.<br />LIS4930 © PIC<br />
  2. 2. A High-Level Design <br />See you textbooks on page 97.<br />LIS4930 © PIC<br />
  3. 3. The Three Things!<br />LIS4930 © PIC<br />PREP CODE<br />TEST CODE<br />REAL CODE<br />Prep code: A form of pseudocode, to help you focus on the logic without stressing about syntax.<br />Test code: A class or methods that will test the real code and validate that it’s doing that right thing.<br />Real code: The actual implementation of the class. This is where you put the real Java code.<br />
  4. 4. Prep Code<br />Most prepcode includes three parts: <br />Instance variable declarations<br />Method declarations<br />Method logic<br />LIS4930 © PIC<br />PREP CODE<br />SimpleDotCom<br />int[] locationCells<br />intnumOfHits<br />String checkYourself(String guess)<br />void setLocationCells(int[] loc)<br />Let’s give it a try!<br />
  5. 5. Test Code<br />LIS4930 © PIC<br />TEST CODE<br />The concept of writing the test code first is one of the practices of Extreme Programming (XP), and it can make it easier (and faster) for you to write your code.<br />Based on our prep code what should we test?<br />
  6. 6. Test Code for the SimpleDotCom Class<br />LIS4930 © PIC<br />TEST CODE<br />
  7. 7. The checkYourself() Method<br />LIS4930 © PIC<br />REAL CODE<br />
  8. 8. Final code for SimpleDotCom and SimpleDotComTestDrive<br />LIS4930 © PIC<br />
  9. 9. Let’s Finish Our SimpleDotComGame Class<br />LIS4930 © PIC<br />Check out the prep code on page 108.<br />Check out the main() method on page 110.<br />Let’s Play!<br />
  10. 10. More about for loops!<br />LIS4930 © PIC<br />for (inti = 0; i < 100; i++) { }<br />Part One: initialization<br />Part Two: boolean test<br />Part Three: iteration expression<br />Take a trip through a for() loop on page 115.<br />
  11. 11. Difference Between While and For<br />LIS4930 © PIC<br />The whileloop has only the Boolean test; it doesn’t have a built-in initialization or iteration expression.A whileloop is good when you don’t know how many times to loop and just want to keep going while some condition is true.<br />But, if you knowhow many times to loop a forloop is cleaner.<br />Let’s compare the two types of loops.<br />
  12. 12. The Enhanced For Loop!<br />LIS4930 © PIC<br />for (String name : nameArray) { }<br />Part One: initialization variable declaration<br />Part Two: the actual collection<br />Let’s look at an example!<br />
  13. 13. Casting Primitives<br />LIS4930 © PIC<br />I have already gone over this concept in class, but read page 117 in your textbook for a more in-depth explanation. <br />long<br />byte<br />can be cast to<br />