06a methods original


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

06a methods original

  1. 1. The Difference Between Instance and Local Variables<br />LIS4930 © PIC<br />1<br />2<br />3<br />Instance variables are declared inside a class but not within a method.<br />Local variables are declared within a method.<br />Local variables MUST be initialized before use!<br />class AddThing {<br />int a;<br />intb = 12;<br /> public int add() {<br />inttotal = a + b;<br /> return total;<br /> }<br />}<br />class foo {<br /> public void go() {<br />intx;<br />intz = x + 3;<br /> }<br />}<br />class Horse {<br /> private double height = 15.2;<br /> private String breed;<br /> //more code…<br />}<br />
  2. 2. Default Values<br />LIS4930 © PIC<br />Instance variables always get a default value:<br />Local variables DO NOT get a default value!<br />
  3. 3. Comparing Variables<br />To compare two primitives, use the equality ( == ) operator.<br />To see if two references are the same (which means they refer to the same object on the heap) use the == operator.<br />To see if two objects are the same use the equals() method.<br />LIS4930 © PIC<br />
  4. 4. Developing A Class<br />Figure out what the class is supposed to do.<br />List the instance variables and methods.<br />Write prepcode for the methods.<br />Write test code for the methods.<br />Implement the class.<br />Test the methods.<br />LIS4930 © PIC<br />
  5. 5. The “Simple Dot Com Game”<br />Goal: Sink all of the computer’s Dot Coms in the fewest number of guesses.(This is like battle ship, but with little dot-com companies).<br />Setup: When the game program is launched, the computer places three Dot Coms on a virtualboard. 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 />
  6. 6. A (simple) High-Level Design<br />Game starts, and creates ONE DotCom and gives it a location on three cells in the single row of seven cells.Instead of “A2”, “C4”, and so on, the locations are just integers (for example 1,2,3 are the cell locations in this picture: <br />LIS4930 © PIC<br />
  7. 7. A (simple) High-Level Design<br />Game play begins. Prompt user for a guess, then check to see if it hit any of the DotCom’s three cells. If a hit, increment the numOfHits variable.<br />Game finishes when all three cells have been hit (the numOfHits variable value is 3), and tells the user how many guesses it took to sink the DotCom.<br />LIS4930 © PIC<br />
  8. 8. 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 the right thing.<br />Real code: The actual implementation of the class. This is where you put the real Java code.<br />
  9. 9. 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 />See page 100<br />
  10. 10. 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 />
  11. 11. Test Code for the SimpleDotCom Class<br />LIS4930 © PIC<br />TEST CODE<br />
  12. 12. The checkYourself() Method<br />LIS4930 © PIC<br />REAL CODE<br />
  13. 13. Final code for SimpleDotCom and SimpleDotComTestDrive<br />LIS4930 © PIC<br />
  14. 14. 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 />
  15. 15. 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 />
  16. 16. 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 />
  17. 17. 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 />
  18. 18. 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 />