06a methods original
DESCRIPTION
TRANSCRIPT
![Page 1: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/1.jpg)
LIS4930 © PIC
The Difference Between Instance and Local
Variables1. Instance variables are declared inside a class but not within a
method.2. Local variables are declared within a method.3. Local variables MUST be initialized before use!1 2 3
class Horse {private double
height = 15.2;private String
breed;//more code…
}
class AddThing {int a;int b = 12;
public int add() {
int total = a + b;
return total;
}}
class foo {public void
go() {int
x;int
z = x + 3;}
}
![Page 2: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/2.jpg)
LIS4930 © PIC
Default ValuesInstance variables always get a default value:
integers 0
floating points 0.0
booleans false
references null
Local variables DO NOT get a default value!
![Page 3: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/3.jpg)
LIS4930 © PIC
Comparing Variables
To compare two primitives, use the equality ( == ) operator.
To see if two references are the same (which means they refer to the same object on the heap) use the == operator.
To see if two objects are the same use the equals() method.
![Page 4: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/4.jpg)
LIS4930 © PIC
Developing A Class
Figure out what the class is supposed to do.
List the instance variables and methods.
Write prepcode for the methods.
Write test code for the methods.
Implement the class.
Test the methods.
![Page 5: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/5.jpg)
LIS4930 © PIC
The “Simple Dot Com Game”
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).
Setup: When the game program is launched, the computer places three Dot Coms on a virtual board. When that’s complete, the game asks for your first guess.
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.
![Page 6: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/6.jpg)
LIS4930 © PIC
A (simple) High-Level Design
1. 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:
0 1 2 3 4 5 6
![Page 7: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/7.jpg)
LIS4930 © PIC
A (simple) High-Level Design
2. 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.
3. 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.
![Page 8: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/8.jpg)
LIS4930 © PIC
The Three Things!PREP CODE TEST CODE REAL CODE
Prep code: A form of pseudocode, to help you focus on the logic without stressing about syntax.
Test code: A class or methods that will test the real code and validate that it’s doing the right thing.
Real code: The actual implementation of the class. This is where you put the real Java code.
![Page 9: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/9.jpg)
LIS4930 © PIC
Prep Code
Most prepcode includes three parts: Instance variable declarations
Method declarations
Method logicSimpleDotCo
mint[] locationCellsint numOfHits
String checkYourself(String guess)
void setLocationCells(int[] loc)
Let’s give it a try!
PR
EP
CO
DE See page 100
![Page 10: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/10.jpg)
LIS4930 © PIC
Test Code
Based on our prep code what should we test?
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.
TES
T C
OD
E
![Page 11: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/11.jpg)
LIS4930 © PIC
Test Code for the SimpleDotCom Class
TES
T C
OD
E
![Page 12: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/12.jpg)
LIS4930 © PIC
The checkYourself() Method
REA
L C
OD
E
![Page 13: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/13.jpg)
LIS4930 © PIC
Final code for SimpleDotCom and SimpleDotComTestDrive
![Page 14: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/14.jpg)
LIS4930 © PIC
Let’s Finish Our SimpleDotComGame Class
Check out the prep code on page 108.
Check out the main() method on page 110.
Let’s Play!
![Page 15: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/15.jpg)
LIS4930 © PIC
More about for loops!
for (int i = 0; i < 100; i++) { }
Part One: initialization
Part Two: boolean test
Part Three: iteration expression
Take a trip through a for() loop on page 115.
![Page 16: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/16.jpg)
LIS4930 © PIC
Difference Between While and For
The while loop has only the Boolean test; it doesn’t have a built-in initialization or iteration expression.
A while loop is good when you don’t know how many times to loop and just want to keep going while some condition is true.
But, if you know how many times to loop a for loop is cleaner.
Let’s compare the two types of loops.
![Page 17: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/17.jpg)
LIS4930 © PIC
The Enhanced For Loop!
for (String name : nameArray) { }Part One: initialization variable declaration
Part Two: the actual collection
Let’s look at an example!
![Page 18: 06a methods original](https://reader033.vdocument.in/reader033/viewer/2022061213/549704f8b47959c06d8b4870/html5/thumbnails/18.jpg)
LIS4930 © PIC
Casting PrimitivesI have already gone over this concept in class, but read page 117 in your textbook for a more in-depth explanation.
long
bytecan be
cast to