Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 4 Using Classes Richard Gesick.

Similar presentations


Presentation on theme: "Lecture 4 Using Classes Richard Gesick."— Presentation transcript:

1 Lecture 4 Using Classes Richard Gesick

2 Topics Class Basics and Benefits String class Random Class Math Class

3 Some Terminology Object reference: identifier of the object
Instantiating an object: creating an object of a class Instance of the class: the object Methods: the code to manipulate the object data Calling a method: invoking a service for an object.

4 Class Data Members of a class: the class's fields and methods
Fields: instance variables and class variables Fields can be: any primitive data type (int, double, etc.) objects Instance variables: variables defined in the class and given values in the object

5 What’s in a Class Class contains Members are Fields Instance variables
Methods Instance variables Class variables

6 Naming Conventions Class names: start with a capital letter
Object references: start with a lowercase letter In both cases, internal words start with a capital letter Example: class: Student objects: student1, student2

7 1. Declare an Object Reference
Syntax: ClassName objectReference; or ClassName objectRef1, objectRef2…; Object reference holds address of object Example: Date d1; d1 contains the address of the object, but the object hasn’t been created yet

8 2. Instantiate an Object Objects MUST be instantiated before they can be used Call a constructor using new keyword Constructor has same name as class. Syntax: objectReference = new ClassName( arg list ); Arg list (argument list) is comma-separated list of initial values to assign to object data, and may be empty

9 Date Class Constructor Summary
Date Class API Constructor: special method that creates an object and assigns initial values to data Date Class Constructor Summary Date( ) creates a Date object with initial month, day, and year values of 1, 1, 2000 Date( int mm, int dd, int yy ) creates a Date object with initial month, day, and year values of mm, dd, and yy

10 Instantiation Examples
Date independenceDay; independenceDay = new Date(7,4, 1776 ); Date graduationDate = new Date(5,15,2008); Date defaultDate = new Date( );

11 Object Reference vs. Object Data
Object references point to the location of object data. An object can have multiple object references pointing to it. Or an object can have no object references pointing to it. If so, the garbage collector will free the object's memory

12 null Object References
An object reference can point to no object. In that case, the object reference has the value null Object references have the value null when they have been declared, but have not been used to instantiate an object. Attempting to use a null object reference causes a run time exception.

13 String and StringBuilder
string is like a primitive data type but creates an immutable object Once created, cannot be changed Does not need to be instantiated Stringbuilder is a class Must be instantiated Can be changed Use StringBuilder when many concatenations are needed

14 Reusability Reuse: class code is already written and tested, so you build a new application faster and it is more reliable Example: A Date class could be used in a calendar program, appointment-scheduling program, online shopping program, etc.

15 How To Reuse A Class You don't need to know how the class is written.
You do need to know the application programming interface (API) of the class. The API is published and tells you: How to create objects What methods are available How to call the methods

16 The Argument List in an API
Pairs of “dataType variableName” Specify Order of arguments Data type of each argument Arguments can be: Any expression that evaluates to the specified data type

17 Dot Notation Use when calling method to specify which object's data to use in the method Syntax: objectReference.methodName( arg1, arg2, … ) Note: no data types are specified in the method call; arguments are values only!

18 When calling a method, include only expressions in your argument list
When calling a method, include only expressions in your argument list. Including data types in your argument list will cause a compiler error. If the method takes no arguments, remember to include the empty parentheses after the method's name. The parentheses are required even if there are no arguments. The following examples use string class properties and methods

19 Length Property public int Length { get; } The number of characters in the current string. Remarks The Length property returns the number of Char objects in this instance, not the number of Unicode characters. Example: string h= “hello”; int len = h.Length; len has a value of 5

20 To upper and lower case public string ToLower() Return Value: A string in lowercase. public string ToUpper() Return Value: A string in uppercase. Example: string myString = “good luck”; myString = myString.ToUpper(); myString now has the value “GOOD LUCK”

21 IndexOf methods public int IndexOf( char value ) The zero-based index position of value if that character is found, or -1 if it is not. public int IndexOf( string value) The zero-based index position of value if that string is found, or -1 if it is not. string myString= “hello world”; int e_index=myString.IndexOf(‘e’); // e_index= 1 int or_index= myString.IndexOf(“or”); //or_index=7

22 Substring methods public string Substring( int startIndex, int length ) A string that is equivalent to the substring of length length that begins at startIndex in this instance public string Substring( int startIndex) A string that begins at startIndex and continues to the end of the source string string h= “hello”; string s= h.Substring(1,3); //s = “ell” string t = h.Substring (2); //t=“ello”

23 Base Class Libraries and The C# API
This link will take you to the .Net framework class library. On that page most of the classes you will need are in the System namespace.

24

25 Random Class To generate random numbers
Generates a pseudorandom number (appearing to be random, but mathematically calculated based on seed value) 3-25

26 Random API 3-26

27 The upper bound in the Random class is exclusive
3-27

28 Math Class Basic mathematical functions
All methods are static methods (class methods) invoked through the name of the class no need to instantiate object Two static constants PI = the value of pi E = the base of the natural logarithm 3-28

29 Calling static Methods
Use dot syntax with class name instead of object reference Syntax: ClassName.methodName( args ) Example: int absValue = Math.Abs( -9 ); abs is a static method of the Math class that returns the absolute value of its argument (here, -9). 3-29

30 3-30

31 3-31

32 3-32


Download ppt "Lecture 4 Using Classes Richard Gesick."

Similar presentations


Ads by Google