understanding classes

24
UNDERSTANDING CLASSES CMSC 150: Lecture 17

Upload: hoai

Post on 24-Feb-2016

26 views

Category:

Documents


0 download

DESCRIPTION

Understanding Classes. CMSC 150: Lecture 17 . String Literals. " Wilco " String literal : a sequence of characters flanked by " Want to operate on the sequence E.g., pull out substrings, locate characters, etc. No primitive data type directly handles such sequences. Primitives. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Understanding Classes

UNDERSTANDING CLASSES

CMSC 150: Lecture 17

Page 2: Understanding Classes

String Literals "Wilco"

String literal: a sequence of characters flanked by "

Want to operate on the sequence E.g., pull out substrings, locate characters, etc.

No primitive data type directly handles such sequences

Page 3: Understanding Classes

Primitives Primitives are only for storing information

int myInteger = 5; boolean myBoolean = true; char myCharacter = 'c';

No associated methods myInteger.getValue(); // NOT VALID!!

So a primitive type to hold strings wouldn't help

Page 4: Understanding Classes

Let's Write Our Own Class!public class String { // instance variables private char[] data;

// constructor public String(char[] value) { … }

// some useful methods public char charAt(int index) { … } public int indexOf(char ch) { … } public String substring(int beginIndex) { … }

…}

Page 5: Understanding Classes

Our String Class We now have a new class named String

Stores data Provides useful methods

Create new objects of this class type: String myString = new String("Wilco"); String ourString = new String("Son Volt");

Use methods we wrote: int index = ourString.indexOf(' '); String firstWord = ourString.substring(0, index);

Page 6: Understanding Classes

Use String Class in Other Classes

public class FancyEmailReader { …

public FancyEmailReader() { … String userHost = accountField.getText(); int index = userHost.indexOf('@');

String user = userHost.substring(0, index); String host = userHost.substring(index + 1);

… }

}

Page 7: Understanding Classes

In Memory String myString;

Enough space to hold a memory address

127

128

129

130

131

132

"myString"

0

Page 8: Understanding Classes

In Memory String myString; myString = new String("Wilco");

Enough space to hold an object of our class type

127

128

129

130

131

132

"myString"

"Wilco"

charAt()indexOf()Substring()…

130

Page 9: Understanding Classes

In Memory String myString; myString = new String("Wilco");

127

128

129

130

131

132

"myString"

"Wilco"

charAt()indexOf()Substring()…

Data

130

Page 10: Understanding Classes

In Memory String myString; myString = new String("Wilco");

127

128

129

130

131

132

"myString"

"Wilco"

charAt()indexOf()Substring()…

Methods

130

Page 11: Understanding Classes

The String Class Gives us a new data type

Allows us to meaningfully store info And have methods to operate on that

info

Use String whenever it is useful

Page 12: Understanding Classes

Back to Last Lab…public class IMClient { …

String lineFromServer = connection.in.nextLine()

}

linefromServer contains info like "IM_IN2:Lilly:T:T:<HTML><BODY>…"

Page 13: Understanding Classes

Back to Last Lab…public class IMClient { …

String lineFromServer = connection.in.nextLine()

} linefromServer contains info like "IM_IN2:Lilly:T:T:<HTML><BODY>…"

Want methods to pull out buddy, status, msg String class doesn't provide such methods

Page 14: Understanding Classes

Let's Write Our Own!public class TOCServerPacket { String contents; public TOCServerPacket(String packetContents) { … }

public boolean isError() { … } public boolean isBuddyUpdate() { … } public boolean isIncomingIM() { … }

public String getBuddyName() { … } public String getBuddyStatus() { … } public String getErrorCode() { … } public String getMessage() { … }

private String getPrefix(String uncutPacket) { … } private String getSuffix(String uncutPacket, int colonNumber) { … }

}

Page 15: Understanding Classes

Let's Write Our Own!public class TOCServerPacket { String contents; public TOCServerPacket(String packetContents) { … }

public boolean isError() { … } public boolean isBuddyUpdate() { … } public boolean isIncomingIM() { … }

public String getBuddyName() { … } public String getBuddyStatus() { … } public String getErrorCode() { … } public String getMessage() { … }

private String getPrefix(String uncutPacket) { … } private String getSuffix(String uncutPacket, int colonNumber) { … }

}

Page 16: Understanding Classes

Similar to the String Class TocServerPacket class gives us a new data

type

Allows us to meaningfully store info The String returned from the server

And have methods to operate on that info isIncomingIM(), getBuddyName(), getBuddyStatus()

Use TocServerPacket whenever it is useful

Page 17: Understanding Classes

Use TOCServerPacket in Other Classes

public class IMControl { …

public void dataAvailable() { … String lineFromServer = connection.in.nextPacket(); // get the buddy name int firstIndex = lineFromServer.indexOf(':'); int secondIndex = lineFromServer.indexOf(':', firstIndex + 1); String buddy = lineFromServer.substring(firstIndex + 1, secondIndex);

// pull out message while ( … ) { … } }

}

Page 18: Understanding Classes

Use TOCServerPacket in Other Classes

public class IMControl { …

public void dataAvailable() { … String lineFromServer = connection.in.nextPacket(); // get the buddy name int firstIndex = lineFromServer.indexOf(':'); int secondIndex = lineFromServer.indexOf(':', firstIndex + 1); String buddy = lineFromServer.substring(firstIndex + 1, secondIndex);

// pull out message while ( … ) { … } }

}

Use a TOCServerPacket!

Page 19: Understanding Classes

Use TOCServerPacket in Other Classes

public class IMControl { …

public void dataAvailable() {

… String lineFromServer = connection.in.nextPacket();

TOCServerPacket packet = new TOCServerPacket(lineFromServer);

String buddy = packet.getBuddyName(); String message = packet.getMessage();

}

}

Use a TOCServerPacket!

Page 20: Understanding Classes

In Memory String line =

conn.in.nextPacket(); TOCServerPacket packet;

Enough space to hold a memory address

127

128

129

130

131

132

"packet"

0

Page 21: Understanding Classes

In Memory

127

128

129

130

131

132

"IM_IN2:Lilly:T:…"

isIncomingIM()getBuddyName()getBuddyStatus()…

130

String line = conn.in.nextPacket(); TOCServerPacket packet; packet = new

TOCServerPacket(lineFromServer);"packet"

Page 22: Understanding Classes

In Memory

127

128

129

130

131

132

"IM_IN2:Lilly:T:…"

isIncomingIM()getBuddyName()getBuddyStatus()…

130

String line = conn.in.nextPacket(); TOCServerPacket packet; packet = new

TOCServerPacket(lineFromServer);"packet"

Data

Page 23: Understanding Classes

In Memory

127

128

129

130

131

132

"IM_IN2:Lilly:T:…"

isIncomingIM()getBuddyName()getBuddyStatus()…

130

String line = conn.in.nextPacket(); TOCServerPacket packet; packet = new

TOCServerPacket(lineFromServer);"packet"

Methods

Page 24: Understanding Classes

Your tour guide will be…