Copyright © 2012 Pearson Education, Inc. Chapter 5 STRUCTURES, ENUMERATION, GARBAGE COLLECTION & NESTED CLASSES.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Advertisements

Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (sections ,
Chapter 2: Introduction to C++.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 Pearson Education, Inc. All rights reserved Arrays.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 10 Structured Data.
Chapter 5 new The Do…Loop Statement
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
CS0004: Introduction to Programming Variables – Strings.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
ASP.NET Programming with C# and SQL Server First Edition Chapter 5 Manipulating Strings with C#
Input, Output, and Processing
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 17 – Flag Quiz Application Introducing One-Dimensional.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 8 Working.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Screen Scraping Application Introducing String Processing.
Chapter 12: String Manipulation Introduction to Programming with C++ Fourth Edition.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
Michael Olivero Microsoft Student Ambassador for FIU
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
CHAPTER 9 Text Processing and More about Wrapper Classes Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
String String Builder. System.String string is the alias for System.String A string is an object of class string in the System namespace representing.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Characters and Strings
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
1 1  Lecture 11 – Structured Data FTMK, UTeM – Sem /2014.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 7 Pointers and C-Strings.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Chapter Structured Data 11. Combining Data into Structures 11.2.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 8: Namespaces, the class string, and User-Defined Simple Data Types.
Computer Programming ||
11 Chapter Structured Data
String String Builder.
Primitive Types Vs. Reference Types, Strings, Enumerations
Chapter 12: Text Processing
Chapter 12: Text Processing
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 11: Structured Data.
Chapter 2: Introduction to C++.
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

Copyright © 2012 Pearson Education, Inc. Chapter 5 STRUCTURES, ENUMERATION, GARBAGE COLLECTION & NESTED CLASSES

Copyright © 2012 Pearson Education, Inc. Topics Introduction String and Character Processing Structures Enumerated Types The ImageList Control

8.1 Introduction This chapter discusses: –various string and character processing techniques They are useful in applications that work extensively with text –structures which allow you to encapsulate several variables into a single item –enumerated types, which are data types that you can create –the ImageList control, which is a data structure for storing and retrieving images Copyright © 2012 Pearson Education, Inc.

8.2 String and Character Processing Text is a commonly used form of data to be processed by programs –You frequently need to manipulate strings at a detailed level C# and the.NET Framework provide tools to work with: –individual characters (char) –sets of characters (string) Copyright © 2012 Pearson Education, Inc.

The char Data Type C# data type is used to store individual characters A char variable can hold only one character at a time –To declare a char variable, use: char letter; –This statement declares a char variable named letter Character literals are enclosed in single quotation marks (‘) letter = ‘g’; –This statement assigns the character g to the letter variable char and string are two incomputable data types –The following attempts to assign a string to a char variable. It will not compile. letter = “g”; –Use ToString method to convert a char literal to string literal MessageBox.Show(letter.ToString()); Copyright © 2012 Pearson Education, Inc.

Retrieve Individual Characters in a String C# allows you to access the individual characters in a string using subscript notation –Treat a string as an array of characters string name = “Jacob”; char letter; for (int index = 0; index < name.Length; index++) { letter = name[index]; MessageBox.Show(letter.ToString()); } –Elements in the string array are read-only. You cannot change their values. For example, the following will compile: name[0]=‘T’; // assign a new value Copyright © 2012 Pearson Education, Inc. // use foreach loop foreach (char letter in name) { letter = name[index]; MessageBox.Show(letter.ToString()); }

Character Testing Methods Methods for testing the value of a character are: –char.IsDigit(ch): checks if ch is a digit (0 through 9) –char.IsDigit(str, index): checks if index of str is a digit –char.IsLetter(ch): checks if ch is an alphabet (a through z or A through Z) –char.IsLetter(str, index): checks if index of str is an alphabet –char.IsLetterOrDigit(ch): checks if ch is a digit or alphabet –char.IsLetterOrDigit(str, index): checks if index of str is a digit or alphabet where ch is a character; str is a string; and index is the position of a character within str Copyright © 2012 Pearson Education, Inc. string str = “12345”; if (char.IsDigit(str[0])) { MessageBox.Show(“True”); } string str = “Hello”; if (char.IsLetter(str[0])) { MessageBox.Show(“True”); } string str = “12345”; if (char.IsDigit(str, 0)) { MessageBox.Show(“True”); }

Character Testing Methods (Cont’d) More methods for testing the value of a character are: –char.IsPunctuation(ch): checks if ch is a punctuation mark –char.IsPunctuation(str, index): checks if index of str is a punctuation mark –char.IsWhiteSpace(ch): checks if ch is a white-space –char.IsWhiteSpace(str, index): checks if index of str is a white-space where ch is a character; str is a string; and index is the position of a character within str Copyright © 2012 Pearson Education, Inc. string str = “Hello!”; if (char.IsPunctuation(str[5])) { MessageBox.Show(“True”); } string str = “Hello!”; if (char.IsPunctuation(str, 5)) { MessageBox.Show(“True”); } string str = “Hello World!”; if (char.IsWhiteSpace(str[6])) { MessageBox.Show(“True”); }

Character Testing Methods (Cont’d) Methods that check the letter’s case are: –char.IsLower(ch): checks if ch is a lowercase letter –char.IsLower(str, index): checks if index of str is a lowercase letter –char.IsUpper(ch): checks if ch is a uppercase letter –char.IsUpper(str, index): checks if index of str is a uppercase letter where ch is a character; str is a string; and index is the position of a character within str Copyright © 2012 Pearson Education, Inc. string str = “hello!”; if (char.IsLower(str[0])) { MessageBox.Show(“True”); } string str = “Hello!”; if (char.IsUpper(str, 0)) { MessageBox.Show(“True”); }

Character Case Conversion The char data type provides two methods to convert between the case of a character: –char.ToLower(ch): return lowercase equivalent of ch –char.ToUpper(ch): return uppercase equivalent of ch For example: string str1 = “abc”; string str2 = “XYZ”; char letter.ToUpper(str1[0]); char letter.ToLower(str2[0]); Copyright © 2012 Pearson Education, Inc.

Searching for Substrings Some tasks require you to search for a specific string of characters within a string. Some of the substring searching methods are: –stringVar.Contains(substring): checks if stringVar contains substring –stringVar.Contains(ch): checks if stringVar contains ch –stringVar.StartsWith(substring): checks if stringVar starts with substring –stringVar.EndsWith(substring): checks if stringVar ends with substring where stringVar is the name of a string variable; substring the string to be found; ch is a character Copyright © 2012 Pearson Education, Inc. string str = “Eat ice cream!”; if (str.Contains(“ice”)) { MessageBox.Show(“True”); } string str = “Eat ice cream!”; if (str.Contains(‘i’)) { MessageBox.Show(“True”); } string str = “Eat ice cream!”; if (str.EndsWith(“eam”)) { MessageBox.Show(“True”); }

Finding Position of Substrings Sometimes you need to know the position of the substring. You can use the IndexOf methods. –It returns the integer position of substring’s first occurrence, and returns -1 if not found. Common usages to find substrings are: stringVar.IndexOf(substring): stringVar.IndexOf(substring, start): stringVar.IndexOf(substring, start, count): –It can also find characters. It returns the integer position of ch’s first occurrence, and returns -1 if not found. Common usages are: stringVar.IndexOf(ch): stringVar.IndexOf(ch, start): stringVar.IndexOf(ch, start, count): –where start is an integer indicating the starting position for searching; count is an integer specifying the number of character positions to examine Copyright © 2012 Pearson Education, Inc.

Sample Codes (substring) Copyright © 2012 Pearson Education, Inc. // The following code display “10” string str = “chocolate ice cream”; int position = str.IndexOf(“ice”); if (position != -1) { MessageBox.Show(position.ToString()); } // The following code display “2” string str = “cocoa beans”; int position = str.IndexOf(“co”, 2); if (position != -1) { MessageBox.Show(position.ToString()); } // The following code display “6” string str = “xx oo xx oo xx”; int position = str.IndexOf(“xx”, 3, 8); if (position != -1) { MessageBox.Show(position.ToString()); }

Sample Codes (Ch) Copyright © 2012 Pearson Education, Inc. // The following code display “2” string str = “chocolate ice cream”; int position = str.IndexOf(‘o’); if (position != -1) { MessageBox.Show(position.ToString()); } // The following code display “4” string str = “chocolate ice cream”; int position = str.IndexOf(‘o’, 3); if (position != -1) { MessageBox.Show(position.ToString()); } // The following code display “12” string str = “chocolate ice cream”; int position = str.IndexOf(‘e’, 10, 4); if (position != -1) { MessageBox.Show(position.ToString()); }

Finding Position of Substrings (Backwards) When you need to search backwards to find the LAST occurrence, you can use the LastIndexOf methods –It returns the index position of the last occurrence of a specified character or substring within this instance. Common usages to find substrings are: stringVar.LastIndexOf(substring): stringVar.LastIndexOf(substring, start): stringVar.LastIndexOf(substring, start, count): –It can also find characters. It returns the integer position of ch’s first occurrence, and returns -1 if not found. Common usages are: stringVar.LastIndexOf(ch): stringVar.LastIndexOf(ch, start): stringVar.LastIndexOf(ch, start, count): –where start is an integer indicating the starting position for searching; count is an integer specifying the number of character positions to examine Copyright © 2012 Pearson Education, Inc.

Sample Codes (substring) Copyright © 2012 Pearson Education, Inc. // The following code display “11” string str = “blue green blue”; int position = str.LastIndexOf(“blue”); if (position != -1) { MessageBox.Show(position.ToString()); } // The following code display “6” string str = “xx oo xx oo xx”; int position = str.LastIndexOf(“xx”, 10); if (position != -1) { MessageBox.Show(position.ToString()); } // The following code display “6” string str = “xx oo xx oo xx”; int position = str.LastIndexOf(“xx”, 10, 8); if (position != -1) { MessageBox.Show(position.ToString()); }

Sample Codes (Ch) Copyright © 2012 Pearson Education, Inc. // The following code display “14” string str = “chocolate ice cream”; int position = str.LastIndexOf(‘c’); if (position != -1) { MessageBox.Show(position.ToString()); } // The following code display “12” string str = “chocolate ice cream”; int position = str.LastIndexOf(‘e’, 14); if (position != -1) { MessageBox.Show(position.ToString()); } // The following code display “12” string str = “chocolate ice cream”; int position = str.LastIndexOf(‘e’, 14, 8); if (position != -1) { MessageBox.Show(position.ToString()); }

The Substring Method Sometimes you need to retrieve a specific set of characters from a string. You can use the Substring method. –stringVar.Substring(start): return a string containing the characters beginning at start, continuing to the end of stringVar –stringVar.Substring(start, count): return a string containing the characters beginning at start, continuing for count characters where start is an integer indicating the starting position for searching; count is an integer specifying the number of character positions to examine Examples: Copyright © 2012 Pearson Education, Inc. // The following code displays “beans” String str = “cocoa beans”; MessageBox.Show(str.Substring(6)); // The following code displays “cocoa” String str = “cocoa beans”; MessageBox.Show(str.Substring(0, 5));

Methods for Modifying a String When you need to modify the contents of a string, you can use: –The Insert method to insert a string into another –The Remove method to remove specified characters from a string –The Trim method to remove all leading and trailing spaces from a string Leading spaces are spaces before the string: “ Hello” Trailing spaces are spaces after the string: “Hello “ –The TrimStart method to remove all leading spaces –The TrimEnd method to remove all trailing spaces –To convert cases of a string use either ToLower or ToUpper methods Copyright © 2012 Pearson Education, Inc.

Methods for Modifying a String (Cont’d) Syntaxes: –stringVar.Insert(start, strItem) For example, string str1 = “New City”; string str2 = str1.Insert(4, “York”); MessageBox.Show(str2); // display “New York City” –stringVar.Remove(start) –stringVar.Remove(start, count) For example, string str1 = “blueberry”; string str2 = str1.Remove(4); // outcome is “blue” where start specifies a position in stringVar; strItem is the string to be inserted; count specifies a number of characters Copyright © 2012 Pearson Education, Inc. // str2 will be “jelly doughnuts” string str1 = “jelly filled doughnuts”; string str2 = str1.Remove(6, 7);

Methods for Modifying a String (Cont’d) The syntax of Trim methods are: stringVar.Trim() stringVar.TrimStart() stringVar.TrimEnd() where stringVar is the name of a string variable: Copyright © 2012 Pearson Education, Inc. // The output is “>Hello<“ string str1 = “ Hello “; string str2 = str1.Trim(); MessageBox.Show(“>” + str2 + “<“); // The output is “>Hello<“ string str1 = “ Hello“; string str2 = str1.TrimStart(); MessageBox.Show(“>” + str2 + “<“); // The output is “>Hello<“ string str1 = “Hello “; string str2 = str1.TrimEnd(); MessageBox.Show(“>” + str2 + “<“);

Tokenizing Strings When a string contains a series of words or other items of data separated by spaces or other characters “apple:orange:banana” The string can be thought to contain four items of data: apple, orange, and banana –Each item is known as a token –The character that separates tokens is known as a delimiter –The process of breaking a string into tokens is known as tokenizing In C#, the Split method is used to tokenize strings –It extracts tokens from a string and returns them as an array of strings –You can pass null as an argument indicating white-spaces are delimiters –You can pass a character or a char array as arguments to specify a list of delimiters Copyright © 2012 Pearson Education, Inc. // using null (white space) string str = “one two three four”; string[] tokens = str.Split(null); // using ; as delimiter string str = “one;two;three;four”; string[] tokens = str.Split(‘;’); // using char array string str = char[] delim = { ‘.’ } string[] tokens = str.Split(deliml);

8.3 Structures You can group several variables together into a single item known as a structure –It allows you to create custom data types for your programs –Each variable in a structure is known as a field –Fields in a structure can be of different data types The generic form to declare a structure in C# is: struct StructureName { public Field Declarations } where struct is a keyword; public is the access modifier You can declare a structure: –Outside the application’s namespace –Inside the application’s namespace –Inside a class –Inside another structure Copyright © 2012 Pearson Education, Inc.

Creating Structures For example, a used-car dealer’s application needs the following variables: string make; int year; double mileage; You can organize them into a structure struct Automobile { public string make; public int year; public double mileage; } You then create one or more objects of the structure Automobile sportsCar; Automobile miniVan, pickupTruck; Copyright © 2012 Pearson Education, Inc. Or, use the new keyword to create instances Automobile sportsCar = new Automobile();

Accessing a Structure’s Fields You can access a structure’s fields using the dot (.) operator to assign values to or retrieve values from fields Automobile sportsCar = new Automobile(); sportsCar.make = “Ford Mustang”; sportsCar.year = 1965; sportsCar.mileage = ; To retrieve values of fields: MessageBox.Show(sportsCar.make); MessageBox.Show(sportsCar.make.ToString()); You can assign one structure object to another using the assignment (=) operator car2 = sportsCar; Copyright © 2012 Pearson Education, Inc.

Arrays of Structure Objects Structure objects can be stored in an array const int SIZE = 5; Automobile[] cars = new Automobile[SIZE]; To access one object in the array, use the subscript cars[2].mileage You can create loops to access the array for (int index = 0; index < cars.Length; index++) { cars[index].year = 2016; } To store structure object in a List, use: foreach (Automobile aCar in cars) { carList.Add(aCar.make); } Copyright © 2012 Pearson Education, Inc.

8.4 Enumerated Types An enumerated data type is a programmer-defined data type –It consists of predefined constants known as enumerators –Enumerators represent integer values When you create an enumerated data type, you specify a set of symbolic values that belong to the data type An enumerated data type declaration begins with the enum keyword, followed by the name of the type, followed by a list of identifiers inside the braces enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } Inside the braces, each identifier (such as Monday) is an enumerator –Enumerators are constants that represent integer values. –The value of Day.Sunday is 0, Day.Monday is 1, etc. Copyright © 2012 Pearson Education, Inc.

Enumerated Types (Cont’d) An enumerator declaration can appear: –Outside the application’s namespace –Inside the application’s namespace –Inside a class Once you have created an enumerated data type, you can declare variable of that type Day workDay; To assign the value Day.Monday to the workDay variable, use: workDay = Day.Monday; Enumerators and enum variables also support the ToString method MessageBox.Show(Day.Monday.ToString()); Copyright © 2012 Pearson Education, Inc.

Enumerated Types (Cont’d) Enumerators are integers, but you cannot directly assign it to an int variable. You need to use a cast operator. int value = (int) Day.Friday; An enum variable can be converted to int Day workDay = Day.Monday; int value = (int) workDay; You can specify default values to enumerators enum MonthDays { Janurary = 31, February = 28, March = 31, April = 30, May = 31, June = 30, July = 31, August = 31, September = 30, October = 31, November = 30, December = 31 } Copyright © 2012 Pearson Education, Inc.

8.5 The ImageList Control The ImageList control allows you to store a collection of images –It is a container that can hold multiple images –Images are organized in a list, and you can use an index to retrieve an image Guidelines to use an ImageList control in an application are: –All the images stored in an ImageList control should be the same size –The images stored in an ImageList control can be no more than 256 by 256 pixels in size –All the images stored in an ImageList control should be in the same format (.bmp,.jpg, etc.) Copyright © 2012 Pearson Education, Inc.

The ImageList Control (Cont’d) ImageList supports the Image property with which you can add images to the Image Collection Editor Images loaded to the editor are given an index (0, 1, 2, etc.) To load an image to a PictureBox control, use: pictureBox1.Image = myImageList.Images[2]; The Count property holds the number of images stored in the ImageList int numbers = myImageList.Images.Count; Copyright © 2012 Pearson Education, Inc.

Questions?