cmsc 150 classes cs 150: mon 6 feb 2012 images: library of congress

49
CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

Upload: scott-hutchinson

Post on 13-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

CMSC 150CLASSES

CS 150: Mon 6 Feb 2012 Images: Library of Congress

Page 2: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

First: quick review of using methods

Remember types of methods:

Function: creates and “returns” a value

int score = game.getScore();

System.out.println( “You get “ + game.numPointsForWin() );

int newScore = game.getScore() – game.numPointsPerMove();

Room playerRoom = game.getRoom( playerRow, playerCol );

Remember to either: Save the returned value in a variable of the

appropriate type, orUse the returned value in an expression.

Page 3: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

First: quick review of using methods

Remember types of methods:

Procedure: does work & returns no value (i.e., void return type)

game.updateScore(newScore);

game.updateMessage( “It’s the StayPuft Marshmallow Man!”);

roomForSlimer.addSlimer();

Remember: methods with void return type don’t create a value illogical to use them in an assignment or expression

Page 4: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

Anatomy of a method call

game.updateScore( newScore );

An object reference is always needed to call a method

Object to call themethod with.

Page 5: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

Anatomy of a method call

game.updateScore( newScore );

An object reference is always needed to call a method

Name of the methodto call.

Page 6: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

Anatomy of a method call

game.updateScore( newScore );

An object reference is always needed to call a method

Parameters – input values forthe method.

Page 7: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

One Exception

public class GhostBusters {

public boolean handleMove( ) {

boolean result = checkForLoss( );

}

public boolean checkForLoss( ) { … }

}

EXCEPT: if the method definition and the method call are in the same class, the object reference is “implicit”

Supplied automatically by Java

Page 8: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

One other exception

Static methods are a little different int jenny = Integer.parseInt(“8675309”);

This is the name of a class, not a referenceto an object.

Page 9: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

What is a Class?

A “blueprint” of your entity to be implemented

Defines and encapsulates your entity’s Characteristics (data) Behaviors (methods)

Example:

Page 10: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

Example: Dog

What characteristics do dogs have in general? Your class’s data (instance variables)

What behaviors do dogs exhibit? Your class’s methods

But do these identify a specific dog?

Page 11: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

Example: Dog

Do these identify a specific dog?

Page 12: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

Example: Dog

Page 13: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

Example: Dog

Class: Dog Characteristics: breed, eye color, weight Behaviors: fetch(), lick(), sit(), stay(), poop()

Science Lab Lilly: an instance (object) of class Dog chocolate lab, golden eyes, 65 lbs. can perform Dog behaviors

Darth Bailey: an instance (object) of class Dog black lab, brown eyes, 50 lbs. can perform Dog behaviors

Page 14: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

In Summary

Write the class “blueprint” once

Create a specific object of that class Create another object of that class And another…

Consider a familiar example

Page 15: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

Write Our Own String Class

public class SimpleString

{

// instance variables (data)

// methods

}

Notice no main() method Only when you want to directly execute that

class

Page 16: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

Writing Your Own String Classpublic class SimpleString

{

// instance variables (data)

private char myFirstCharacter;

private char mySecondCharacter;

private int myLength;

// methods

}

Page 17: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

Methods

Constructor must have same name as class syntax: public ClassName( parameters… ) creates the object of this class type SimpleString str = new SimpleString(‘Z’,’a’); no return type in method definition (implicit)

All other methods

Page 18: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

Writing Your Own String Class

public class SimpleString

{

// instance variables (data)

private char myFirstCharacter;

private char mySecondCharacter;

private int myLength;

// constructor

public SimpleString( char charOne, char charTwo )

{

myFirstCharacter = charOne;

mySecondCharacter = charTwo;

myLength = 2;

}

}

Page 19: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

In Another Class…

public class Tester

{

public static void main( String[] args )

{

SimpleString str1 = new SimpleString( ‘O’, ‘y’ );

SimpleString str2 = new SimpleString( ‘Z’, ‘a’ );

}

}

Page 20: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

Page 21: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

Variables are of the class type SimpleString

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

Page 22: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

Objects will be constructed using the constructor from that class

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

Page 23: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

The Two Programs Together

SimpleString.java: defines blueprint for a generic "SimpleString" object we do not directly run this

Tester.java: we run this (because it contains main()) uses SimpleString as a variable type creates objects of the class SimpleString

Let's see what happens in memory as we run Tester…

Page 24: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

str1

declare…

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

127

128

129

130

131

132

133

134

135

136

137

138

Page 25: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java‘O’

‘y’

character literals stored

elsewhere automatically

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

str1 127

128

129

130

131

132

133

134

135

136

137

138

Page 26: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

reserve enough space for a

SimpleString object…

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

‘O’

‘y’

str1 127

128

129

130

131

132

133

134

135

136

137

138

Page 27: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

reserve enough space for a

SimpleString object…

mySecondChar

myLengthDATA

METHOD

STUFF

myFirstChar

‘O’

‘y’

str1 127

128

129

130

131

132

133

134

135

136

137

138

Page 28: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

mySecondChar

myLength

METHOD

STUFF

myFirstChar

‘O’

‘y’

str1 127

128

129

130

131

132

133

134

135

136

137

138

invoke the constructor…

Page 29: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

mySecondChar

myLength

METHOD

STUFF

myFirstChar

‘O’

‘y’

str1 127

128

129

130

131

132

133

134

135

136

137

138

which reserves spacefor the

parameters(like variables)…

char1

char2

Page 30: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

mySecondChar

myLength

METHOD

STUFF

myFirstChar

‘O’

‘y’

str1 127

128

129

130

131

132

133

134

135

136

137

138

char1

char2

parameters

arguments

‘O’

Page 31: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

mySecondChar

myLength

METHOD

STUFF

myFirstChar

‘O’

‘y’

str1 127

128

129

130

131

132

133

134

135

136

137

138

char1

char2

parameters

arguments

‘O’

‘y’

Page 32: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

mySecondChar

myLength

METHOD

STUFF

myFirstChar

‘O’

‘y’

str1 127

128

129

130

131

132

133

134

135

136

137

138

char1

char2

‘O’

‘y’

execute the first statement in the

constructor…

‘O’

Page 33: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

mySecondChar

myLength

METHOD

STUFF

myFirstChar

‘O’

‘y’

str1 127

128

129

130

131

132

133

134

135

136

137

138

char1

char2

‘O’

‘y’

execute the next statement in the

constructor…

‘O’

‘y’

Page 34: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

mySecondChar

myLength

METHOD

STUFF

myFirstChar

‘O’

‘y’

str1 127

128

129

130

131

132

133

134

135

136

137

138

char1

char2

‘O’

‘y’

execute the next statement in the

constructor…

‘O’

‘y’

2

Page 35: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

mySecondChar

myLength

METHOD

STUFF

myFirstChar

‘O’

‘y’

str1 127

128

129

130

131

132

133

134

135

136

137

138

as the constructor finishes, space for the parameters is

reclaimed…

‘O’

‘y’

2

Page 36: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

mySecondChar

myLength

METHOD

STUFF

myFirstChar

‘O’

‘y’

str1 127

128

129

130

131

132

133

134

135

136

137

138

constructor call returns the memory address of the new

object, which is assigned to str1…

‘O’

‘y’

2

130

Page 37: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

mySecondChar

myLength

METHOD

STUFF

myFirstChar

‘O’

‘y’

str1 127

128

129

130

131

132

133

134

135

136

137

138

str1 variable now “references” the

SimpleString object

‘O’

‘y’

2

130

Page 38: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

mySecondChar

myLength

METHOD

STUFF

myFirstChar

‘O’

‘y’

str1 127

128

129

130

131

132

133

134

135

136

137

138

‘O’

‘y’

2

130

object (instance) of class SimpleString

variable of type SimpleString(containing reference)

Page 39: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

private char myFirstChar;

private char mySecondChar;

private int myLength;

public SimpleString(char char1, char char2)

{

myFirstChar = char1;

mySecondChar = char2;

myLength = 2;

}

}

public class Tester{

public static void main( String[] args ){

SimpleString str1 = new SimpleString(‘O’,’y’);

SimpleString str2 = new SimpleString(‘Z’,’a’);

}}

SimpleString.java

Tester.java

mySecondChar

myLength

METHOD

STUFF

myFirstChar

str2 337

338

339

340

341

342

343

344

345

346

347

348

‘Z’

‘a’

2

340

next statement results in similar

picture elsewhere in memory

Page 40: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class MyString

{

// instance variables (data)

private char myFirstCharacter;

private char mySecondCharacter;

private int myLength;

// default constructor

public SimpleString()

{

myFirstCharacter = ‘ ‘;

mySecondCharacter = ‘ ‘;

myLength = 0;

}

// constructor with two parameters

public SimpleString( char charOne, char charTwo )

{

myFirstCharacter = charOne;

mySecondCharacter = charTwo;

myLength = 2;

}

}

Page 41: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

Methods

Constructor must have same name as class syntax: public ClassName( arguments… ) creates the object of this class type MyString str = new MyString(); no return type (implicit)

All other methods use any non-reserved name two types: perform action, return a value

Page 42: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

Methods

All other methods use any non-reserved name two types: perform action, return a value

1. perform an action: don't return anything public void doSomething() { …

public void doSomethingElse() { …2. return a value: provide a specific return

type public int length() { … public String substring( int …

Page 43: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

All other methods use any non-reserved name two types: perform action, return a value

1. perform an action: don't return anything public void doSomething( … )

{ … public void doSomethingElse( … ) { …2. return a value: provide a specific return type public int length() { … public String substring( int …

Methods

Page 44: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

// these methods do something without

// needing to return any sort of value

public void printTheString()

{

System.out.println( “” + myFirstCharacter

+ mySecondCharacter );

}

public void clearTheString()

{

myFirstCharacter = ‘ ‘;

mySecondCharacter = ‘ ‘;

myLength = 0;

}

}

Page 45: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

return type of method

public class SimpleString

{

// these methods do something without

// needing to return any sort of value

public void printTheString()

{

System.out.println( “” + myFirstCharacter

+ mySecondCharacter );

}

public void clearTheString()

{

myFirstCharacter = ‘ ‘;

mySecondCharacter = ‘ ‘;

myLength = 0;

}

}

Page 46: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

// these methods do something without

// needing to return any sort of value

public void printTheString()

{

System.out.println( “” + myFirstCharacter

+ mySecondCharacter );

}

public void clearTheString()

{

myFirstCharacter = ‘ ‘;

mySecondCharacter = ‘ ‘;

myLength = 0;

}

}

void: no return

statement

Page 47: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

// these methods return some sort of info

public int length()

{

return myLength; // return instance variable

}

public char charAt(int index)

{

char returnChar = ‘ ‘;

if (index == 0) {

returnChar = myFirstCharacter;

} else if (index == 1) {

returnChar = mySecondCharacter;

}

return returnChar;

}

}

Page 48: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

// these methods return some sort of info

public int length()

{

return myLength; // return instance variable

}

public char charAt(int index)

{

char returnChar = ‘ ‘;

if (index == 0) {

returnChar = myFirstCharacter;

} else if (index == 1) {

returnChar = mySecondCharacter;

}

return returnChar;

}

}

return type of method

Page 49: CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress

public class SimpleString

{

// these methods return some sort of info

public int length()

{

return myLength; // return instance variable

}

public char charAt(int index)

{

char returnChar = ‘ ‘;

if (index == 0) {

returnChar = myFirstCharacter;

} else if (index == 1) {

returnChar = mySecondCharacter;

}

return returnChar;

}

}

return statement using a

variable of type int

return statement using a

variable of type char