SlideShare ist ein Scribd-Unternehmen logo
1 von 18
The Difference Between Instance and Local Variables LIS4930 © PIC 1 2 3 Instance variables are declared inside a class but not within a method. Local variables are declared within a method. Local variables MUST be initialized before use! class AddThing { int a; intb = 12; 	public int add() { inttotal = a + b; 		return total; 	} } class foo { 	public void go() { intx; intz = x + 3; 	} } class Horse { 	private double height = 15.2; 	private String breed; 	//more  code… }
Default Values LIS4930 © PIC Instance variables always get a default value: Local variables DO NOT get a default value!
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. 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. 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 virtualboard. 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. LIS4930 © PIC
A (simple) High-Level Design 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:  LIS4930 © PIC
A (simple) High-Level Design 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. 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. LIS4930 © PIC
The Three Things! LIS4930 © PIC 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.
Prep Code Most prepcode includes three parts:  Instance variable declarations Method declarations Method logic LIS4930 © PIC PREP CODE SimpleDotCom int[] locationCells intnumOfHits String checkYourself(String guess) void setLocationCells(int[] loc) Let’s give it a try! See page 100
Test Code LIS4930 © PIC TEST CODE 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. Based on our prep code what should we test?
Test Code for the SimpleDotCom Class LIS4930 © PIC TEST CODE
The checkYourself() Method LIS4930 © PIC REAL CODE
Final code for SimpleDotCom and SimpleDotComTestDrive LIS4930 © PIC
Let’s Finish Our SimpleDotComGame Class LIS4930 © PIC Check out the prep code on page 108. Check out the main() method on page 110. Let’s Play!
More about for loops! LIS4930 © PIC for (inti = 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.
Difference Between While and For LIS4930 © PIC 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. But, if you knowhow many times to loop a forloop is cleaner. Let’s compare the two types of loops.
The Enhanced For Loop! LIS4930 © PIC for (String name : nameArray) {  } Part One: initialization variable declaration Part Two: the actual collection Let’s look at an example!
Casting Primitives LIS4930 © PIC I have already gone over this concept in class, but read page 117 in your textbook for a more in-depth explanation.  long byte can be cast to

Weitere ähnliche Inhalte

Was ist angesagt?

Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generationIffat Anjum
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design) Tasif Tanzim
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONAnil Pokhrel
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationAkshaya Arunan
 
Three address code generation
Three address code generationThree address code generation
Three address code generationRabin BK
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysisIffat Anjum
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expressionAkhil Ahuja
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 

Was ist angesagt? (19)

Ch9b
Ch9bCh9b
Ch9b
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
C programming session3
C programming  session3C programming  session3
C programming session3
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Basic Block
Basic BlockBasic Block
Basic Block
 
Assignment12
Assignment12Assignment12
Assignment12
 
Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)   Intermediate code generation (Compiler Design)
Intermediate code generation (Compiler Design)
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTION
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Three address code generation
Three address code generationThree address code generation
Three address code generation
 
Ch8b
Ch8bCh8b
Ch8b
 
Lecture 03 lexical analysis
Lecture 03 lexical analysisLecture 03 lexical analysis
Lecture 03 lexical analysis
 
Evaluation of postfix expression
Evaluation of postfix expressionEvaluation of postfix expression
Evaluation of postfix expression
 
Back patching
Back patchingBack patching
Back patching
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 

Andere mochten auch (9)

Mysocial
MysocialMysocial
Mysocial
 
Web architecture v3
Web architecture v3Web architecture v3
Web architecture v3
 
07 java api and inheritance
07 java api and inheritance07 java api and inheritance
07 java api and inheritance
 
Sdlc
SdlcSdlc
Sdlc
 
03 objects
03 objects03 objects
03 objects
 
04 variables
04 variables04 variables
04 variables
 
15a gui
15a gui15a gui
15a gui
 
14a exceptions
14a exceptions14a exceptions
14a exceptions
 
12 abstract classes
12 abstract classes12 abstract classes
12 abstract classes
 

Ähnlich wie 06a methods original

Python For Machine Learning
Python For Machine LearningPython For Machine Learning
Python For Machine LearningYounesCharfaoui
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperDeepak Singh
 
Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)Unity Technologies
 
Technical aptitude Test 1 CSE
Technical aptitude Test 1 CSETechnical aptitude Test 1 CSE
Technical aptitude Test 1 CSESujata Regoti
 
Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)ExcellenceAcadmy
 
Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)ExcellenceAcadmy
 
python presentation
python presentationpython presentation
python presentationVaibhavMawal
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in PythonSumit Satam
 
Seminar 2 coding_principles
Seminar 2 coding_principlesSeminar 2 coding_principles
Seminar 2 coding_principlesmoduledesign
 
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdfC# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdfssuserc77a341
 
Seminar 2 coding_principles
Seminar 2 coding_principlesSeminar 2 coding_principles
Seminar 2 coding_principlesmoduledesign
 
MIPS_Programming.pdf
MIPS_Programming.pdfMIPS_Programming.pdf
MIPS_Programming.pdfXxUnnathxX
 
Exactpro FinTech Webinar - Global Exchanges FIX Protocol
Exactpro FinTech Webinar - Global Exchanges FIX ProtocolExactpro FinTech Webinar - Global Exchanges FIX Protocol
Exactpro FinTech Webinar - Global Exchanges FIX ProtocolIosif Itkin
 
Gsp 215 Believe Possibilities / snaptutorial.com
Gsp 215  Believe Possibilities / snaptutorial.comGsp 215  Believe Possibilities / snaptutorial.com
Gsp 215 Believe Possibilities / snaptutorial.comStokesCope20
 

Ähnlich wie 06a methods original (20)

06b extra methods
06b extra methods06b extra methods
06b extra methods
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Python For Machine Learning
Python For Machine LearningPython For Machine Learning
Python For Machine Learning
 
Computer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paperComputer science-2010-cbse-question-paper
Computer science-2010-cbse-question-paper
 
Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)Adding Love to an API (or How to Expose C++ in Unity)
Adding Love to an API (or How to Expose C++ in Unity)
 
Technical aptitude Test 1 CSE
Technical aptitude Test 1 CSETechnical aptitude Test 1 CSE
Technical aptitude Test 1 CSE
 
Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)
 
Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)
 
python presentation
python presentationpython presentation
python presentation
 
gdscpython.pdf
gdscpython.pdfgdscpython.pdf
gdscpython.pdf
 
Basic Concepts in Python
Basic Concepts in PythonBasic Concepts in Python
Basic Concepts in Python
 
Seminar 2 coding_principles
Seminar 2 coding_principlesSeminar 2 coding_principles
Seminar 2 coding_principles
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdfC# 6Write a program that creates a Calculation ClassUse the foll.pdf
C# 6Write a program that creates a Calculation ClassUse the foll.pdf
 
Seminar 2 coding_principles
Seminar 2 coding_principlesSeminar 2 coding_principles
Seminar 2 coding_principles
 
02 prepcode
02 prepcode02 prepcode
02 prepcode
 
MIPS_Programming.pdf
MIPS_Programming.pdfMIPS_Programming.pdf
MIPS_Programming.pdf
 
Exactpro FinTech Webinar - Global Exchanges FIX Protocol
Exactpro FinTech Webinar - Global Exchanges FIX ProtocolExactpro FinTech Webinar - Global Exchanges FIX Protocol
Exactpro FinTech Webinar - Global Exchanges FIX Protocol
 
Gsp 215 Believe Possibilities / snaptutorial.com
Gsp 215  Believe Possibilities / snaptutorial.comGsp 215  Believe Possibilities / snaptutorial.com
Gsp 215 Believe Possibilities / snaptutorial.com
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 

Mehr von Program in Interdisciplinary Computing (20)

Phpmysqlcoding
PhpmysqlcodingPhpmysqlcoding
Phpmysqlcoding
 
Database basics
Database basicsDatabase basics
Database basics
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
Mysocial databasequeries
Mysocial databasequeriesMysocial databasequeries
Mysocial databasequeries
 
CGS2835 HTML5
CGS2835 HTML5CGS2835 HTML5
CGS2835 HTML5
 
01 intro tousingjava
01 intro tousingjava01 intro tousingjava
01 intro tousingjava
 
Xhtml
XhtmlXhtml
Xhtml
 
Webdev
WebdevWebdev
Webdev
 
Web architecture
Web architectureWeb architecture
Web architecture
 
Javascript
JavascriptJavascript
Javascript
 
Javascript
JavascriptJavascript
Javascript
 
Html5
Html5Html5
Html5
 
Frameworks
FrameworksFrameworks
Frameworks
 
Drupal
DrupalDrupal
Drupal
 
Database
DatabaseDatabase
Database
 
Javascript2
Javascript2Javascript2
Javascript2
 
11 polymorphism
11 polymorphism11 polymorphism
11 polymorphism
 
13 interfaces
13 interfaces13 interfaces
13 interfaces
 
15b more gui
15b more gui15b more gui
15b more gui
 

06a methods original

  • 1. The Difference Between Instance and Local Variables LIS4930 © PIC 1 2 3 Instance variables are declared inside a class but not within a method. Local variables are declared within a method. Local variables MUST be initialized before use! class AddThing { int a; intb = 12; public int add() { inttotal = a + b; return total; } } class foo { public void go() { intx; intz = x + 3; } } class Horse { private double height = 15.2; private String breed; //more code… }
  • 2. Default Values LIS4930 © PIC Instance variables always get a default value: Local variables DO NOT get a default value!
  • 3. 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. LIS4930 © PIC
  • 4. 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. LIS4930 © PIC
  • 5. 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 virtualboard. 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. LIS4930 © PIC
  • 6. A (simple) High-Level Design 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: LIS4930 © PIC
  • 7. A (simple) High-Level Design 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. 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. LIS4930 © PIC
  • 8. The Three Things! LIS4930 © PIC 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.
  • 9. Prep Code Most prepcode includes three parts: Instance variable declarations Method declarations Method logic LIS4930 © PIC PREP CODE SimpleDotCom int[] locationCells intnumOfHits String checkYourself(String guess) void setLocationCells(int[] loc) Let’s give it a try! See page 100
  • 10. Test Code LIS4930 © PIC TEST CODE 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. Based on our prep code what should we test?
  • 11. Test Code for the SimpleDotCom Class LIS4930 © PIC TEST CODE
  • 12. The checkYourself() Method LIS4930 © PIC REAL CODE
  • 13. Final code for SimpleDotCom and SimpleDotComTestDrive LIS4930 © PIC
  • 14. Let’s Finish Our SimpleDotComGame Class LIS4930 © PIC Check out the prep code on page 108. Check out the main() method on page 110. Let’s Play!
  • 15. More about for loops! LIS4930 © PIC for (inti = 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.
  • 16. Difference Between While and For LIS4930 © PIC 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. But, if you knowhow many times to loop a forloop is cleaner. Let’s compare the two types of loops.
  • 17. The Enhanced For Loop! LIS4930 © PIC for (String name : nameArray) { } Part One: initialization variable declaration Part Two: the actual collection Let’s look at an example!
  • 18. Casting Primitives LIS4930 © PIC I have already gone over this concept in class, but read page 117 in your textbook for a more in-depth explanation. long byte can be cast to