Docsity

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

Toggle sidebar
bg1
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
pf3
pf4
pf5

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-

Topics

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

CSE 1301 (^) 3A-

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-

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: student
    • Data: Maria Gonzales, Sophomore, 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.

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-

What’s in a Class

Class contains Members are Fields

Methods

Instance variables Class variables

CSE 1301 (^) 3A-

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-

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, student

CSE 1301 (^) 3A-

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

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

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-

NullReference

Date aDate; aDate.setMonth( 5 );

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

CSE 1301 (^) 3A-

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.

CSE 1301 (^) 3A-

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 CSE 1301 (^) 3A-

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

Insert .NET example here

Method Classifications

  • Accessor methods
    • Gets the values of object data
  • Mutator methods
    • Writes/changes values of object data
  • Others to be defined later

CSE 1301 (^) 3A-

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-

Calling a

Method

CSE 1301 (^) 3A-

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

.NET Architecture

  • Framework
  • When you press F
    • source code compiled into IL
    • submitted to .NET engine for execution

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-

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-

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-

CSE 1301 3-393A-39^ CSE 1301 3-403A-

CSE 1301 3-413A-41^ CSE 1301 (^) 3A-

Summary

  • What did you learn?
  • Muddiest Point