1 / 24

Understanding Classes

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.

hoai
Download Presentation

Understanding Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Understanding Classes CMSC 150: Lecture 17

  2. 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

  3. Primitives • Primitives are only for storing information • intmyInteger = 5; • booleanmyBoolean = true; • char myCharacter = 'c'; • No associated methods • myInteger.getValue(); // NOT VALID!! • So a primitive type to hold strings wouldn't help

  4. 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 intindexOf(charch) { … } public String substring(intbeginIndex) { … } … }

  5. 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);

  6. 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); … } }

  7. In Memory • String myString; • Enough space to hold a memory address 127 "myString" 128 0 129 130 131 132

  8. In Memory • String myString; • myString = new String("Wilco"); • Enough space to hold an object of our class type 127 "myString" 128 130 129 130 "Wilco" charAt() indexOf() Substring() … 131 132

  9. In Memory • String myString; • myString = new String("Wilco"); 127 "myString" 128 130 129 Data 130 "Wilco" charAt() indexOf() Substring() … 131 132

  10. In Memory • String myString; • myString = new String("Wilco"); 127 "myString" 128 130 129 130 "Wilco" Methods charAt() indexOf() Substring() … 131 132

  11. 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

  12. Back to Last Lab… public class IMClient { … String lineFromServer = connection.in.nextLine() … } • linefromServercontains info like "IM_IN2:Lilly:T:T:<HTML><BODY>…"

  13. Back to Last Lab… • linefromServercontains info like "IM_IN2:Lilly:T:T:<HTML><BODY>…" • Want methods to pull out buddy, status, msg • String class doesn't provide such methods public class IMClient { … String lineFromServer = connection.in.nextLine() … }

  14. Let's Write Our Own! public class TOCServerPacket { String contents; public TOCServerPacket(StringpacketContents) { … } public booleanisError() { … } public booleanisBuddyUpdate() { … } public booleanisIncomingIM() { … } public String getBuddyName() { … } public String getBuddyStatus() { … } public String getErrorCode() { … } public String getMessage() { … } private String getPrefix(StringuncutPacket) { … } private String getSuffix(StringuncutPacket, intcolonNumber) { … } }

  15. Let's Write Our Own! public class TOCServerPacket { String contents; public TOCServerPacket(StringpacketContents) { … } public booleanisError() { … } public booleanisBuddyUpdate() { … } public booleanisIncomingIM() { … } public String getBuddyName() { … } public String getBuddyStatus() { … } public String getErrorCode() { … } public String getMessage() { … } private String getPrefix(StringuncutPacket) { … } private String getSuffix(StringuncutPacket, intcolonNumber) { … } }

  16. Similar to the String Class • TocServerPacket class gives us a new data type • Allows us to meaningfully store info • TheString returned from the server • And have methods to operate on that info • isIncomingIM(), getBuddyName(), getBuddyStatus() • Use TocServerPacket whenever it is useful

  17. Use TOCServerPacket in Other Classes public class IMControl { … public void dataAvailable() { … String lineFromServer = connection.in.nextPacket(); // get the buddy name intfirstIndex = lineFromServer.indexOf(':'); intsecondIndex = lineFromServer.indexOf(':', firstIndex + 1); String buddy = lineFromServer.substring(firstIndex + 1, secondIndex); // pull out message while ( … ) { … } } }

  18. Use TOCServerPacket in Other Classes public class IMControl { … public void dataAvailable() { … String lineFromServer = connection.in.nextPacket(); // get the buddy name intfirstIndex = lineFromServer.indexOf(':'); intsecondIndex = lineFromServer.indexOf(':', firstIndex + 1); String buddy = lineFromServer.substring(firstIndex + 1, secondIndex); // pull out message while ( … ) { … } } } Use a TOCServerPacket!

  19. 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!

  20. In Memory • String line = conn.in.nextPacket(); • TOCServerPacket packet; • Enough space to hold a memory address 127 "packet" 128 0 129 130 131 132

  21. In Memory • String line = conn.in.nextPacket(); • TOCServerPacket packet; • packet = new TOCServerPacket(lineFromServer); 127 "packet" 128 130 129 130 "IM_IN2:Lilly:T:…" isIncomingIM() getBuddyName() getBuddyStatus() … 131 132

  22. In Memory • String line = conn.in.nextPacket(); • TOCServerPacket packet; • packet = new TOCServerPacket(lineFromServer); 127 "packet" 128 130 129 Data 130 "IM_IN2:Lilly:T:…" isIncomingIM() getBuddyName() getBuddyStatus() … 131 132

  23. In Memory • String line = conn.in.nextPacket(); • TOCServerPacket packet; • packet = new TOCServerPacket(lineFromServer); 127 "packet" 128 130 129 130 "IM_IN2:Lilly:T:…" Methods isIncomingIM() getBuddyName() getBuddyStatus() … 131 132

  24. Your tour guide will be…

More Related