CS 112 Lecture 23: Characters and Inheritance - Static Char Methods, Slides of C programming

A part of the lecture notes for cs 112 introduction to programming at yale university. It covers the topic of characters, including the use of double-subscripted arrays for rectangular and jagged arrays, and the encapsulation of an array of characters in the string class. The document also introduces inheritance, a programming concept that allows a new class to inherit characteristics from an existing one. The example provided demonstrates the use of static char methods from the char structure to test various properties of a character. A code example of a windows forms application that takes a character input and displays its corresponding properties.

Typology: Slides

2010/2011

Uploaded on 10/05/2011

christina
christina 🇺🇸

4.6

(23)

393 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 112 Introduction to
Programming
Lecture #23:
Characters;
More on Classes and Objects
http://flint.cs.yale.edu/cs112/
2
Recap: Two Types of Double-Subscripted Arrays
rRectangular arrays
int[,] array1 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
int[,] array11 = new int[3,4];
array1[0,2] = 10;
rJagged arrays
int[][] array2 = new int[ 3][];
array2[ 0 ] = new int[] { 1, 2 };
array2[ 1 ] = new int[] { 3 };
array2[ 2 ] = new int[] { 4, 5, 6 };
array2[2][1] = 3;
3
Recap: Class String
rA string is essentially an encapsulation of an array of
characters
rSince we use string so often, C# defines the string
class and provides many methods (see link on the
schedule page), e.g.,
mConstructors, CopyTo, Format, Substring, Concat
mCompareTo, Equals, ==, !=, StartsWith,
EndsWith, IndexOf, IndexOfAny, LastIndexOf,
LastIndexOfAny
mToUpper, ToLower, Trim, Replace, Insert,
Split
rA related class: StringBuilder (linked on the
lecture-notes page)
4
Outline
rAdmin.
ØCharacters
qAn introduction to inheritance
5
Char Methods
rFor the char value type, there is a Char class
rMost methods are static
mIsLetter
mIsDigit
mIsLetterOrDigit
mIsLower
mIsUpper
mToUpper
mToLower
mIsPunctuation
mIsSymbol
mIsWhiteSpace
Outline
CharMethods.cs
1 // StaticCharMethods.cs
2 // Demonstrates static character testing methods
3 // from Char structure
4
5 using System;
6 using System.Drawing;
7 using System.Collections;
8 using System.ComponentModel;
9 using System.Windows.Forms;
10 using System.Data;
11
12 // Form displays information about specific characters.
13 public class StaticCharMethods : System.Windows.Forms.Form
14 {
15 private System.Windows.Forms.Label enterLabel;
16 private System.Windows.Forms.TextBox inputTextBox;
17 private System.Windows.Forms.Button analyzeButton;
18 private System.Windows.Forms.TextBox outputTextBox;
19
20 private System.ComponentModel.Container components = null;
21
22 // The main entry point for the application.
23 [STAThread]
24 static void Main()
25 {
26 Application.Run( new StaticCharMethods() );
27 }
28
29 // Visual Studio .NET generated code
30
pf3

Partial preview of the text

Download CS 112 Lecture 23: Characters and Inheritance - Static Char Methods and more Slides C programming in PDF only on Docsity!

CS 112 Introduction to

Programming

Lecture #23:

Characters;

More on Classes and Objects

http://flint.cs.yale.edu/cs112/

Recap: Two Types of Double-Subscripted Arrays

r Rectangular arrays

int[,] array1 = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };

int[,] array11 = new int[3,4];

array1[0,2] = 10;

r Jagged arrays

int[][] array2 = new int[ 3 ][];

array2[ 0 ] = new int[] { 1, 2 };

array2[ 1 ] = new int[] { 3 };

array2[ 2 ] = new int[] { 4, 5, 6 };

array2[2][1] = 3;

Recap: Class String

r A string is essentially an encapsulation of an array of

characters

r Since we use string so often, C# defines the string

class and provides many methods (see link on the

schedule page), e.g.,

m Constructors, CopyTo, Format, Substring, Concat

m CompareTo, Equals, ==, !=, StartsWith,

EndsWith, IndexOf, IndexOfAny, LastIndexOf,

LastIndexOfAny

m ToUpper, ToLower, Trim, Replace, Insert,

Split

r A related class: StringBuilder (linked on the

lecture-notes page)

Outline

r Admin.

ÿ Characters

q An introduction to inheritance

Char Methods

r For the char value type, there is a Char class

r Most methods are static

m IsLetter

m IsDigit

m IsLetterOrDigit

m IsLower

m IsUpper

m ToUpper

m ToLower

m IsPunctuation

m IsSymbol

m IsWhiteSpace

Outline

CharMethods.cs

1 // StaticCharMethods.cs 2 // Demonstrates static character testing methods 3 // from Char structure 4 5 using System; 6 using System.Drawing; 7 using System.Collections; 8 using System.ComponentModel; 9 using System.Windows.Forms; 10 using System.Data; 11 12 // Form displays information about specific characters. 13 public class StaticCharMethods : System.Windows.Forms.Form 14 { 15 private System.Windows.Forms.Label enterLabel; 16 private System.Windows.Forms.TextBox inputTextBox; 17 private System.Windows.Forms.Button analyzeButton; 18 private System.Windows.Forms.TextBox outputTextBox; 19 20 private System.ComponentModel.Container components = null; 21 22 // The main entry point for the application. 23 [STAThread] 24 static void Main() 25 { 26 Application.Run( new StaticCharMethods() ); 27 } 28 29 // Visual Studio .NET generated code 30

Outline

CharMethods.cs

31 // handle analyzeButton_Click 32 private void analyzeButton_Click( 33 object sender, System.EventArgs e ) 34 { 35 char character = Convert.ToChar( inputTextBox.Text ); 36 BuildOutput( character ); 37 } 38 39 // display character information in outputTextBox 40 private void BuildOutput( char inputCharacter ) 41 { 42 string output; 43 44 output = "is digit: " + 45 Char.IsDigit( inputCharacter ) + "\r\n"; 46 47 output += "is letter: " + 48 Char.IsLetter( inputCharacter ) + "\r\n"; 49 50 output += "is letter or digit: " + 51 Char.IsLetterOrDigit( inputCharacter ) + "\r\n"; 52 53 output += "is lower case: " + 54 Char.IsLower( inputCharacter ) + "\r\n"; 55 56 output += "is upper case: " + 57 Char.IsUpper( inputCharacter ) + "\r\n"; 58 59 output += "to upper case: " + 60 Char.ToUpper( inputCharacter ) + "\r\n"; 61 62 output += "to lower case: " + 63 Char.ToLower( inputCharacter ) + "\r\n"; 64

Outline

CharMethods.cs

Program Output

65 output += "is punctuation: " + 66 Char.IsPunctuation( inputCharacter ) + "\r\n"; 67 68 output += "is symbol: " + Char.IsSymbol( inputCharacter ); 69 70 outputTextBox.Text = output; 71 72 } // end method BuildOutput 73 74 } // end class StaticCharMethods

Outline

r Admin.

q Characters

ÿ An introduction to inheritance

ÿ why inheritance?

C# Classes

r C# classes play dual roles:

m program modules: contain a list of (static) method

declarations and (static) data fields

m blueprints for generating objects:

  • data and methods

r Classes support

m “data encapsulation” (e.g., private components, for

abstraction)

m “inheritance” (for code reuse)

Inheritance

r Inheritance allows a software developer to derive a

new class from an existing one

r The existing class is called the parent class, or

superclass , or base class

r The derived class is called the child class, or

subclass, or derived class.

r As the name implies, the child inherits characteristics

of the parent

r That is, the child class inherits the methods and data

defined for the parent class

Inheritance

r Inheritance relationships are often shown

graphically in a class diagram , with the arrow

pointing to the parent class

Inheritance should Inheritance should

create ancreate an^ isis--aa

relationshiprelationship ,,

meaning the childmeaning the child

is ais a more specificmore specific

version of theversion of the

parentparent

Animal

# weight : int

+ GetWeight() : int

Bird

+ Fly() : void

Animal

Bird