Published on

Published in: Technology, News & Politics
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Random Creating simple games with Java Methods and Parameters
  2. 2. Agenda <ul><li>In this lecture, we will see that a program is a set of classes, a class is a set of methods and a method is a collection of statements . </li></ul><ul><li>We will discover how a message expression invokes a particular method . </li></ul>
  3. 3. Outline <ul><li>Program </li></ul><ul><li>Classes </li></ul><ul><li>Kinds of Java Methods </li></ul><ul><li>Invoking instance methods </li></ul><ul><li>Passing values by parameter </li></ul>
  4. 4. The Structure of a Java Program <ul><li>There are four major structural components of Java programs: </li></ul><ul><ul><li>the program itself </li></ul></ul><ul><ul><li>classes </li></ul></ul><ul><ul><li>methods </li></ul></ul><ul><ul><li>statements </li></ul></ul>
  5. 5. A Java Program Class1 Class 2 Class N A Java Program static var instance var static var instance var static var instance var Method 1 statement1; statement2; statementN; Method N statement1; statement2; statementN; Method 1 statement1; statement2; statementN; Method N statement1; statement2; statementN; Method 1 statement1; statement2; statementN; Method N statement1; statement2; statementN;
  6. 6. Classes <ul><li>A class is: </li></ul><ul><ul><li>Conceptually: a category of objects </li></ul></ul><ul><ul><li>In a Java program: A block of code that describes what objects in this category are like ( state ), and how they can behave ( message protocol ) </li></ul></ul><ul><li>Example: we could create different classes to model different kinds of vehicles: </li></ul><ul><ul><li>Car class – 4 wheels, fuel powered, doors, steering wheel etc. </li></ul></ul><ul><ul><li>Bicycle class – 2 wheels, manually powered, handle bars etc. </li></ul></ul>
  7. 7. A Java Program - a Set of Classes <ul><li>A Java program consists of one or more classes </li></ul><ul><li>A class is like a blueprint that describes what objects of that class are like </li></ul><ul><li>We can use these classes to create the objects that our program manipulates </li></ul>
  8. 8. Syntax for a Java Class <ul><li>public class Game { </li></ul><ul><li>/* Version 1 </li></ul><ul><li>This program is a number guessing game where the user tries to guess an integer randomly picked by the computer </li></ul><ul><li>*/ </li></ul><ul><li>} </li></ul>class end delimiter class name class start delimiter class comment body of the class goes here start comment delimiter end comment delimiter visibility modifier class keyword
  9. 9. A Java Class - a Set of Methods <ul><li>The body of each Java class includes a set of methods </li></ul><ul><li>A method is some code that performs a single, well defined task. </li></ul>One Java Class A Java Method A Java Method A Java Method A Java Method
  10. 10. Two Kinds of Java Methods <ul><li>An instance method </li></ul><ul><ul><li>implements a message that is sent to an instance of the class. </li></ul></ul><ul><li>A static method </li></ul><ul><ul><li>implements a task that is independent of any particular object. </li></ul></ul><ul><li>In either case, some code is run and (optionally) a result is returned </li></ul><ul><li>We will learn about static methods in a later lecture </li></ul>
  11. 11. Syntax for a Java Method <ul><li>public static void main(String args[]) { </li></ul><ul><li>/* </li></ul><ul><li>Starting point for a program. </li></ul><ul><li>*/ </li></ul><ul><li>} </li></ul>method end delimiter method name method start delimiter method comment visibility modifier static keyword return type parameter list body of the method goes here
  12. 12. A Java Method - Statements <ul><li>The body of a method includes a sequence of statements </li></ul><ul><li>These statements specify what happens when the method is executed (or “invoked” or “called”) </li></ul>A Java Method A Java Statement A Java Statement A Java Statement A Java Statement
  13. 13. Java Statements <ul><li>There are many kinds of Java statements </li></ul><ul><li>We can use many different kinds of statements: </li></ul><ul><ul><li>variable declarations </li></ul></ul><ul><ul><li>message expressions </li></ul></ul><ul><ul><li>assignment statements </li></ul></ul><ul><ul><li>imports (we don’t put import statements inside methods) </li></ul></ul><ul><li>Each statement ends with a semi-colon ; </li></ul>
  14. 14. Invoking an instance method <ul><li>When we execute a piece of code that sends a message to a receiver, the class of the receiver object is searched for an instance method with the same signature as the message expression </li></ul><ul><li>Once located, the method starts to execute (we say that the method has been “ called ” or “ invoked ”) </li></ul><ul><li>When the method is invoked, any parameters declared in the method signature get created </li></ul><ul><li>This is sometimes called “ method dispatch ” </li></ul>
  15. 15. Example 1 <ul><li>Consider the following message expression: </li></ul><ul><li>&quot; Hello &quot; .charAt(1); </li></ul><ul><li>Returns the character ' e ' (at location 1) </li></ul><ul><li>When we execute this code, the charAt(int) method is located in the String class, and begins to execute </li></ul><ul><li>The code that contains the message expression gets suspended while the charAt() method executes </li></ul><ul><li>Any parameters, local variables get created when the charAt( ) method starts to execute </li></ul>
  16. 16. Example 2 <ul><li>&quot;Hello&quot;.toUpperCase(); </li></ul>String Class public String toUpperCase() { /* … class of receiver is String empty argument list empty parameter list “ HELLO” Note: returns a String – see method signature! message name is toUpperCase
  17. 17. Example 3 <ul><li>System.out.print(&quot;Hello&quot;); </li></ul>Note: does not return anything – see method signature! PrintStream Class public void print(String aString) { /* … class of receiver is PrintStream message name is print one argument class String one parameter class String
  18. 18. Parameters <ul><li>Q: Why do we need parameters? </li></ul><ul><li>A: Because sometimes a method needs some (previously existing) information to be “fed in” from another part of the program so that it can do its job. </li></ul><ul><li>Parameters are like interfaces between methods </li></ul>
  19. 19. Declaring Parameters <ul><li>Parameters are declared in the signature (1 st line) of a method </li></ul><ul><li>Consider our first example: there is a method in the String class which has this signature </li></ul><ul><ul><li>public char charAt(int index){ </li></ul></ul>Parameter declaration
  20. 20. Parameters & Local Variables <ul><li>Parameters are very much like local variables in that: </li></ul><ul><ul><li>Lifetime: the same as the method in which they are declared </li></ul></ul><ul><ul><li>Scope: the same as the method in which they are declared </li></ul></ul><ul><li>But parameters are declared in the first line of the method, not inside the method. </li></ul><ul><li>When the method is invoked, they are bound to the arguments. </li></ul>
  21. 21. Initializing Parameters <ul><li>// in the main program </li></ul><ul><li>int number; </li></ul><ul><li>number = 1; </li></ul><ul><li>&quot; Hello &quot; .charAt(number); </li></ul><ul><li>public char charAt(int index){ // method header </li></ul>1 index 1 number 1 number
  22. 22. An Example using Primitive Data Types <ul><li>public class Example { </li></ul><ul><li>public Example ( ) { </li></ul><ul><li>} </li></ul><ul><li>private void aMethod( int param) { </li></ul><ul><li>param = 1; // notice param is re-bound </li></ul><ul><li>System.out.println(param); </li></ul><ul><li>} </li></ul><ul><li>public static void main(String args[ ] ) { </li></ul><ul><li>int argument; Example anObj; </li></ul><ul><li>anObj = new Example( ); </li></ul><ul><li>argument = 6; </li></ul><ul><li>anObj.aMethod(argument); </li></ul><ul><li>System.out.print(argument); … </li></ul>
  23. 23. Summary <ul><li>Methods </li></ul><ul><li>Parameters (formal and actual) </li></ul><ul><li>Local variables </li></ul><ul><li>Return values vs. void </li></ul><ul><li>Overloading </li></ul>
  24. 24. “ Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25” --Andrew Rutherford