cse 1301 lecture 4 using classes figures from lewis, “c# software solutions”, addison wesley...

46
CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

Upload: lenard-owen

Post on 13-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Lecture 4

Using Classes

Figures from Lewis, “C# Software Solutions”, Addison Wesley

Richard Gesick

Page 2: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Topics

• Class Basics and Benefits• Creating Objects• .NET Architecture and Base Class Libraries• Random Class• Math Class

Page 3: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Object-Oriented Programming

• Classes combine data and the methods (code) to manipulate the data

• Classes are a template used to create specific objects

• All C# programs consist of at least one class.

Page 4: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Example

• Student class– Data: name, year, and grade point average– Methods: store/get the value of each piece of

data, promote to next year, etc.• Student Object: student1

– Data: Maria Gonzales, Sophomore, 3.5

Page 5: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

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.

Page 6: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

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

Page 7: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

What’s in a Class

Class contains

Members are

Fields

Methods

Instance variables

Class variables

Page 8: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Encapsulation

• Instance variables are usually declared to be private, which means users of the class must reference the data of an object by calling methods of the class.

• Thus the methods provide a protective shell around the data. We call this encapsulation.

• Benefit: the class methods can ensure that the object data is always valid.

Page 9: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

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

Page 10: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

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

Page 11: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

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

Page 12: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Date Class APIConstructor: special method that creates an

object and assigns initial values to dataDate 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

Page 13: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Instantiation Examples

Date independenceDay;independenceDay = new Date(7,4, 1776 );

Date graduationDate = new Date(5,15,2008);

Date defaultDate = new Date( );

Page 14: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Objects After Instantiation

Object Instances

Page 15: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

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

Page 16: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Creating Aliases

Date hireDate = new Date( 2, 15, 2003 );Date promotionDate = new Date( 9, 28, 2004 );

promotionDate = hireDate;

int x = 5, y = 3;x = y;

Page 17: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Two References to an Object • After program runs, two object references point to

the same object

Page 18: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

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.

Page 19: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

NullReference Date aDate;aDate.setMonth( 5 );Date independenceDay = new Date( 7, 4, 1776 );// set object reference to nullindependenceDay = null;

// attempt to use object referenceindependenceDay.setMonth(5);

Page 20: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

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

Page 21: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

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.

Page 22: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

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

Page 23: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

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

Page 24: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Method Classifications

• Accessor methods– Gets the values of object data

• Mutator methods– Writes/changes values of object data

• Others to be defined later

Page 25: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

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!

Page 26: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Calling a Method

Page 27: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

• 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

Page 28: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Length Property

28

public int Length { get; } The number of characters in the current string.RemarksThe 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

Page 29: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

To upper and lower case

29

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”

Page 30: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

IndexOf methods

30

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= 1int or_index= myString.IndexOf(“or”); //or_index=7

Page 31: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Substring methods

31

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=“llo”

Page 32: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

.NET Architecture

• Framework • When you press F5

– source code compiled into IL– submitted to .NET engine for execution

Page 33: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Page 34: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Base Class Libraries and The C# API• This link will take you to the .Net framework

class library.• https://

msdn.microsoft.com/en-us/library/gg145045%28v=VS.110%29.aspx

• On that page most of the classes you will need are in the System namespace.

34

Page 35: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

using Declaration

• Must have using statement to use values in library:using System.Text;

• Or you can fully qualify:System.Text.StringBuilder phrase = new System.Text.StringBuilder (“Change is inevitable”);

Page 36: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Page 37: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Random Class

• To generate random numbers• Generates a pseudorandom number

(appearing to be random, but mathematically calculated based on seed value)

3-37

Page 38: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Random API

3-38

Page 39: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 13013-39

The upper bound in the Random class is exclusive

Page 40: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 13013-40

Page 41: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

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

Page 42: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

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

Page 43: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 13013-43

Page 44: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 13013-44

Page 45: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 13013-45

Page 46: CSE 1301 Lecture 4 Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick

CSE 1301

Summary

• What did you learn?

• Muddiest Point