UNDERSTANDING CLASSES CMSC 150: Lecture 17. String Literals  "Wilco"  String literal: a sequence of characters flanked by "  Want to operate on the.

Slides:



Advertisements
Similar presentations
ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
Advertisements

Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.
Introduction to Programming Lecture 39. Copy Constructor.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
Programming 2 CS112- Lab 2 Java
Chapter 7: The String class We’ll go through some of this quickly!
Chapter 9 Characters and Strings. Topics Character primitives Character Wrapper class More String Methods String Comparison String Buffer String Tokenizer.
J.43 ARRAYS  A Java array is an Object that holds an ordered collection of elements.  Components of an array can be primitive types or may reference.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
Strings In Java, strings are contained in an object that is an instance of the String class. String in java is the name of a class. That’s why it starts.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Constants, Objects, Strings Lecture 4, Tue Jan
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
28-Jun-15 String and StringBuilder Part I: String.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
Primitive Types Java offers a number of primitive types eg.) int, short, long double, float char A variable which is declared as a primitive type stores.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Working with string In java Four classes are provided to work with String:1) String 2)String Buffer 3)String Tokenizer 4)String Builder Note: An object.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Characters and Strings. Characters  New primitive char  char letter; letter = ‘a’; char letter2 = ‘C’;  Because computers can only represent numbers,
String Handling StringBuffer class character class StringTokenizer class.
CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress.
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
CMSC 150 METHODS AND CLASSES CS 150: Wed 25 Jan 2012.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1.2 Primitive Data Types and Variables
17-Feb-16 String and StringBuilder Part I: String.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
02/09/2005 Introduction to Programming with Java, for Beginners Arrays (of primitives)
CMSC 150 ARRAYS CS 150: Fri 10 Feb Motivation  Consider a list of your contact addresses: String Zero = String.
Chapter 8: Loops, Arrays, Strings Loop statements –do –while –for Arrays –declaration, allocation, initialization, access –multi-dimensional –heterogeneous.
2-1 Types of data  Java has two types of data  primitive types that store one value char boolean byte int long float double  reference types to store.
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
(Dreaded) Quiz 2 Next Monday.
Strings A String is a sequence of letters
Java: Base Types All information has a type or class designation
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Java: Base Types All information has a type or class designation
Programming in Java Text Books :
Programming Languages and Paradigms
Primitive Types Vs. Reference Types, Strings, Enumerations
String Objects & its Methods
String and StringBuilder
MSIS 655 Advanced Business Applications Programming
String and StringBuilder
String Methods: length substring
String and StringBuilder
16 Strings.
Lecture 10 Strings CSE /26/2018.
String and StringBuilder
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
POINTER CONCEPT 4/15/2019.
C Programming Pointers
String methods 26-Apr-19.
String Methods.
Strings in Java.
Dr. Sampath Jayarathna Cal Poly Pomona
POINTER CONCEPT 8/3/2019.
Presentation transcript:

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  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

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) { … } … }

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

Use String Class in Other Classes public class Fancy Reader { … public Fancy Reader() { … String userHost = accountField.getText(); int index = String user = userHost.substring(0, index); String host = userHost.substring(index + 1); … }

In Memory  String myString;  Enough space to hold a memory address "myString" 0

In Memory  String myString;  myString = new String("Wilco");  Enough space to hold an object of our class type "myString" 130

In Memory  String myString;  myString = new String("Wilco"); "myString" Data 130

In Memory  String myString;  myString = new String("Wilco"); "myString" Methods 130

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

Back to Last Lab… public class IMClient { … String lineFromServer = connection.in.nextLine() … }  linefromServer contains info like "IM_IN2:Lilly:T:T: …"

Back to Last Lab… public class IMClient { … String lineFromServer = connection.in.nextLine() … }  linefromServer contains info like "IM_IN2:Lilly:T:T: …"  Want methods to pull out buddy, status, msg  String class doesn't provide such methods

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) { … } }

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) { … } }

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

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

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 !

In Memory  String line = conn.in.nextPacket();  TOCServerPacket packet;  Enough space to hold a memory address "packet" 0

In Memory  String line = conn.in.nextPacket();  TOCServerPacket packet;  packet = new TOCServerPacket(lineFromServer); "packet"

In Memory  String line = conn.in.nextPacket();  TOCServerPacket packet;  packet = new TOCServerPacket(lineFromServer); "packet" Data

In Memory  String line = conn.in.nextPacket();  TOCServerPacket packet;  packet = new TOCServerPacket(lineFromServer); "packet" Methods

Your tour guide will be…