Top Banner
1 Random Creating simple games with Java Methods and Parameters
24
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: M C6java3

1

Random

Creating simple games with JavaMethods and Parameters

Page 2: M C6java3

Agenda

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.

We will discover how a message expression invokes a particular method.

Page 3: M C6java3

Outline

Program

Classes

Kinds of Java Methods

Invoking instance methods

Passing values by parameter

Page 4: M C6java3

The Structure of a Java Program

There are four major structural components of Java programs:

the program itself classes methods statements

Page 5: M C6java3

A Java Program

Class1

Method 1statement1;

statement2;

statementN;

Method Nstatement1;

statement2;

statementN;

Class 2

Method 1statement1;

statement2;

statementN;

Method Nstatement1;

statement2;

statementN;

Class N

Method 1statement1;

statement2;

statementN;

Method Nstatement1;

statement2;

statementN;

A Java Program

static var instance var

static var instance var

static var instance var

Page 6: M C6java3

Classes

A class is: Conceptually: a category of objects 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)

Example: we could create different classes to model different kinds of vehicles: Car class – 4 wheels, fuel powered, doors, steering wheel etc. Bicycle class – 2 wheels, manually powered, handle bars etc.

Page 7: M C6java3

A Java Program - a Set of Classes

A Java program consists of one or more classes

A class is like a blueprint that describes what objects of that class are like

We can use these classes to create the objects that our program manipulates

Page 8: M C6java3

Syntax for a Java Class

public class Game {

/* Version 1

This program is a number guessing game where the user tries to guess an integer randomly picked by the computer

*/

} 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

Page 9: M C6java3

A Java Class - a Set of Methods

The body of each Java class includes a set of methods

A method is some code that performs a single, well defined task.

One Java ClassA Java MethodA Java MethodA Java MethodA Java Method

Page 10: M C6java3

Two Kinds of Java MethodsAn instance method implements a message that is sent to an instance of the

class.

A static method implements a task that is independent of any particular

object.

In either case, some code is run and (optionally) a result is returned

We will learn about static methods in a later lecture

Page 11: M C6java3

Syntax for a Java Method

public static void main(String args[]) {/*

Starting point for a program.*/

}

method end delimiter

method name

method start delimiter

method comment

visibility modifier

static keyword

return type

parameter list

body of the method goes here

Page 12: M C6java3

A Java Method - Statements

The body of a method includes a sequence of statementsThese statements specify what happens when the method is executed (or “invoked” or “called”)

A Java Method

A Java StatementA Java StatementA Java StatementA Java Statement

Page 13: M C6java3

Java StatementsThere are many kinds of Java statements

We can use many different kinds of statements: variable declarations message expressions assignment statements imports (we don’t put import statements inside methods)

Each statement ends with a semi-colon ;

Page 14: M C6java3

Invoking an instance methodWhen 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

Once located, the method starts to execute (we say that the method has been “called” or “invoked”)

When the method is invoked, any parameters declared in the method signature get created

This is sometimes called “method dispatch”

Page 15: M C6java3

Example 1Consider the following message expression:

"Hello".charAt(1);

Returns the character 'e' (at location 1)

When we execute this code, the charAt(int) method is located in the String class, and begins to execute

The code that contains the message expression gets suspended while the charAt() method executes

Any parameters, local variables get created when the charAt( ) method starts to execute

Page 16: M C6java3

String Classpublic String toUpperCase() {/* …

Example 2

"Hello".toUpperCase();

class of receiver is String

empty argument list

empty parameter list

“HELLO”

Note: returns a String – see method signature!

message name is toUpperCase

Page 17: M C6java3

PrintStream Class

public void print(String aString) { /* …

Example 3

System.out.print("Hello");

class of receiver is PrintStreammessage name is print

one argument class Stringone parameter class String

Note: does not return anything – see method signature!

Page 18: M C6java3

Parameters

Q: Why do we need parameters?

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.

Parameters are like interfaces between methods

Page 19: M C6java3

Declaring Parameters

Parameters are declared in the signature (1st line) of a method

Consider our first example: there is a method in the String class which has this signature

public char charAt(int index){Parameter declaration

Page 20: M C6java3

Parameters & Local VariablesParameters are very much like local variables in that: Lifetime: the same as the method in which they are declared Scope: the same as the method in which they are declared

But parameters are declared in the first line of the method, not inside the method.

When the method is invoked, they are bound to the arguments.

Page 21: M C6java3

Initializing Parameters

// in the main programint number;number = 1;"Hello".charAt(number);

public char charAt(int index){ // method header

1

number

1 index1number

Page 22: M C6java3

An Example using Primitive Data Typespublic class Example {

public Example ( ) {}

private void aMethod( int param) {param = 1; // notice param is re-boundSystem.out.println(param);

}public static void main(String args[ ] ) {

int argument; Example anObj;anObj = new Example( );argument = 6;anObj.aMethod(argument);System.out.print(argument); …

Page 23: M C6java3

Summary

MethodsParameters (formal and actual)Local variablesReturn values vs. voidOverloading

Page 24: M C6java3

“Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25”

--Andrew Rutherford