Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CSE 1301 Lecture 3A: Using Classes and Object-Oriented Programming in C# - Prof. Briana Mo, Study notes of Computer Science

A lecture note from cse 1301 class at the university, focusing on using classes, object-oriented programming, and creating objects in c#. It covers topics such as class basics, creating objects, .net architecture, and base class libraries. The document also explains the concept of encapsulation, naming conventions, object references, and instantiating objects. It includes examples of the date class and its constructor, as well as the difference between object references and object data.

Typology: Study notes

Pre 2010

Uploaded on 08/03/2009

koofers-user-ais-2
koofers-user-ais-2 🇺🇸

10 documents

1 / 7

Toggle sidebar

Related documents


Partial preview of the text

Download CSE 1301 Lecture 3A: Using Classes and Object-Oriented Programming in C# - Prof. Briana Mo and more Study notes Computer Science in PDF only on Docsity! CSE 1301 Lecture 3A Using Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison CSE 1301 3A-2 Topics • Class Basics and Benefits • Creating Objects • .NET Architecture and Base Class Libraries • Random Class • Math Class CSE 1301 3A-3 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. CSE 1301 3A-4 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 CSE 1301 3A-5 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. CSE 1301 3A-6 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 CSE 1301 3A-7 What’s in a Class Class contains Members are Fields Methods Instance variables Class variables CSE 1301 3A-8 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. CSE 1301 3A-9 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 CSE 1301 3A-10 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 CSE 1301 3A-11 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 CSE 1301 3A-12 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 CSE 1301 3A-25 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! CSE 1301 3A-26 Calling a Method CSE 1301 3A-27 • 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. CSE 1301 3A-28 .NET Architecture • Framework • When you press F5 – source code compiled into IL – submitted to .NET engine for execution CSE 1301 3A-29 CSE 1301 3A-30 Base Class Libraries • Need to become familiar with libraries to use most efficiently • Group services under Namespace • Most commonly accessed Namespaces are grouped under System Namespace CSE 1301 3A-31 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”); CSE 1301 3A-32 CSE 1301 3A-33 Random Class • To generate random numbers • Generates a pseudorandom number (appearing to be random, but mathematically calculated based on seed value) 3-33 CSE 1301 3A-34 Random API 3-34 CSE 1301 3A-35 3-35 To generate a random integer between a and up to, but not including, b: CSE 1301 3A-36 3-36 CSE 1301 3A-37 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-37 CSE 1301 3A-38 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-38 CSE 1301 3A-39 3-39 CSE 1301 3A-40 3-40 CSE 1301 3A-41 3-41 CSE 1301 3A-42 Summary • What did you learn? • Muddiest Point