Methods Classes And Objects, Lecture Slide - Computer Science, Slides of C programming

Classes, Methods, Example, Provide Abstract and Reuse, Header, Body, The return Statement, Calling a Method, MessageBox Buttons, Icons, Motivation, Objects, Creating and Accessing Objects

Typology: Slides

2010/2011

Uploaded on 10/05/2011

christina
christina 🇺🇸

4.6

(23)

393 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 112 Introduction to
Programming
Lecture #12:
Methods, Classes, and Objects
http://flint.cs.yale.edu/cs112/
2
Recap
rExamples
mReverseNumber
mGamePlay
mIntLargest
mIntSnd
mTriangle
rDesigning a loop
mLook for a pattern/state
mDetermine termination condition
mSet correct initial state
3
Outline
rAdmin. and review
ØClasses and Methods
rObjects: a first introduction
4
Classes/Methods
rC# Framework Class Library (FCL) defines many classes, e.g.,
mConsole
mMessageBox
mInt32
mMath
rThe definition of a class includes both methods and data
properties:
mmethods, e.g.,
Console.Write( )
Console.WriteLine( )
Int32.Parse( )
mproperties, e.g.,
Int32.MinValue
Int32.MaxValue
Math.PI
Math.E
5
Methods
rA method should provide a well-defined, easy-to-
understand functionality
mA method takes input (parameters), performs some actions,
and (sometime) returns a value
rExample: invoking the WriteLine method of the
Console class:
Console.WriteLine ( "Whatever you are, be a good one.“ );
class method Information provided to the method
(parameters)
dot
6
Example: Math Class Methods
Method Description Example
Abs( x ) absolute value of x Abs( 23.7 ) is 23.7
Abs( 0 ) is 0
Abs( -23.7 ) is 23.7
Ceiling( x )
rounds x to the smallest
integer not less than x Ceiling( 9.2 ) is 10.0
Ceiling( -9.8 ) is -9.0
Cos( x ) trigonometric cosine of x
(x in radians) Cos( 0.0 ) is 1.0
Exp( x ) exponential method ex Exp( 1.0 ) is approximately
2.7182818284590451
Exp( 2.0 ) is approximately
7.3890560989306504
Floor( x ) rounds x to the largest integer
not greater than x Floor( 9.2 ) is 9.0
Floor( -9.8 ) is -10.0
Log( x ) natural logarithm of x (base e)
Log( 2.7182818284590451 )
is approximately 1.0
Log( 7.3890560989306504 )
is approximately 2.0
Max( x, y ) larger value of x and y
(also has versions for float,
int and long values)
Max( 2.3, 12.7 ) is 12.7
Max( -2.3, -12.7 ) is -2.3
Min( x, y ) smaller value of x and y
(also has versions for float,
int and long values)
Min( 2.3, 12.7 ) is 2.3
Min( -2.3, -12.7 ) is -12.7
Pow( x, y ) x raised to power y (xy) Pow( 2.0, 7.0 ) is 128.0
Pow( 9.0, .5 ) is 3.0
Sin( x ) trigonometric sine of x
(x in radians) Sin( 0.0 ) is 0.0
Sqrt( x ) square root of x Sqrt( 900.0 ) is 30.0
Sqrt( 9.0 ) is 3.0
Tan( x ) trigonometric tangent of x
(x in radians) Tan( 0.0 ) is 0.0
Commonly used Math class methods.
pf3
pf4

Partial preview of the text

Download Methods Classes And Objects, Lecture Slide - Computer Science and more Slides C programming in PDF only on Docsity!

CS 112 Introduction to

Programming

Lecture #12:

Methods, Classes, and Objects

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

Recap

r Examples

m ReverseNumber

m GamePlay

m IntLargest

m IntSnd

m Triangle

r Designing a loop

m Look for a pattern/state

m Determine termination condition

m Set correct initial state

Outline

r Admin. and review

ÿ Classes and Methods

r Objects: a first introduction

Classes/Methods

r C# Framework Class Library (FCL) defines many classes, e.g.,

m Console

m MessageBox

m Int

m Math

r The definition of a class includes both methods and data

properties:

m methods, e.g.,

  • Console.Write( )
  • Console.WriteLine( )
  • Int32.Parse( )

m properties, e.g.,

  • Int32.MinValue
  • Int32.MaxValue
  • Math.PI
  • Math.E

Methods

r A method should provide a well-defined, easy-to-

understand functionality

m A method takes input (parameters), performs some actions,

and (sometime) returns a value

r Example: invoking the WriteLine method of the

Console class:

Console.WriteLine ( "Whatever you are, be a good one.“ );

class method

Information provided to the method

(parameters)

dot

Example: Math Class Methods

Method Description Example Abs( x ) absolute value of x Abs( 23.7 ) is 23. Abs( 0 ) is 0 Abs( -23.7 ) is 23. Ceiling( x ) rounds x to the smallest integer not less than x

Ceiling( 9.2 ) is 10. Ceiling( -9.8 ) is -9. Cos( x ) trigonometric cosine of x ( x in radians)

Cos( 0.0 ) is 1.

Exp( x ) exponential method ex Exp( 1.0 ) is approximately

Exp( 2.0 ) is approximately

Floor( x ) (^) rounds x to the largest integer not greater than x

Floor( 9.2 ) is 9. Floor( -9.8 ) is -10. Log( x ) natural logarithm of x (base e ) Log( 2.7182818284590451 ) is approximately 1. Log( 7.3890560989306504 ) is approximately 2. Max( x, y ) larger value of x and y (also has versions for float, int and long values)

Max( 2.3, 12.7 ) is 12. Max( -2.3, -12.7 ) is -2.

Min( x, y ) smaller value of x and y (also has versions for float, int and long values)

Min( 2.3, 12.7 ) is 2. Min( -2.3, -12.7 ) is -12. Pow( x, y ) (^) x raised to power y (xy) Pow( 2.0, 7.0 ) is 128. Pow( 9.0, .5 ) is 3. Sin( x ) trigonometric sine of x ( x in radians)

Sin( 0.0 ) is 0.

Sqrt( x ) (^) square root of x Sqrt( 900.0 ) is 30. Sqrt( 9.0 ) is 3. Tan( x ) trigonometric tangent of x ( x in radians)

Tan( 0.0 ) is 0.

Commonly used Math class methods.

Methods Provide Abstraction and Reuse

r An abstraction hides (or ignores) the right

details at the right time

r A method is abstract in that we don't really

have to think about its internal details in order

to use it

m e.g., we don't have to know how the WriteLine

method works in order to invoke it

r Why abstraction?

m Divide and conquer

m Reuse

m Easier to understand

Method Declaration: Header

r A method declaration begins with a method header

method method

namename

returnreturn

typetype

parameter listparameter list

The parameter list specifies the typeThe parameter list specifies the type

and name of each parameterand name of each parameter

The name of a parameter in the methodThe name of a parameter in the method

declaration is called adeclaration is called a^ formal argumentformal argument

class MyClass

static int SquareSum( int num1, int num2 )

propertiesproperties

Method Declaration: Body

r The method header is followed by the method body

static int SquareSum(int num1, int num2)

int sum = num1 + num2;

return sum * sum;

class MyClass

The return Statement

r The return type of a method indicates the

type of value that the method sends back to

the calling location

r A method that does not return a value has a

void return type

r The return statement specifies the value that

will be returned

r Its expression must conform to the return type

Calling a Method

r Each time a method is called, the actual arguments in

the invocation are copied into the formal arguments

static int SquareSum (int num1, int num2)

int sum = num1 + num2;

return sum * sum;

int num = SquareSum (2, 3);

See SquareSum.cs

More Examples of Classes/Methods

r The Console class

m The Write and WriteLine methods

r The String class

m The Format method: e.g.,

String.Format( “{0:C}”, 2.0 )

See TestMethods.cs

Outline

r Admin. and review

r Classes and Methods

ÿ Objects

A brief introduction to make you feel comfortable

about using such methods. We will explore the full

glory later.

Class Methods (a.k.a. Static Methods)

r Previously we use class mainly to define

related methods together:

m For example all math related methods are defined in

the Math class

r The methods we have seen are defined as

static (or class) methods

r To make a method static, a programmer

applies the static modifier to the method

definition

The result of each invocation of a class (static) method is

completely determined by the actual parameters

Motivation for Objects

r But sometime we want a method to keep (internal)

state after each call

r For example, random number generator

m A computer may generate a sequence of random numbers

using the following formula:

X

n

X

n-

mod (

m To write a method called GetNextRandom() we have to give

the method the previous number:

int GetNextRandom( int previous )

return 7^5 * previous % ( 2^31 – 1 );

Objects

r Class: a concept, e.g.,

m the dog concept

r Object

m An instance of a concept, e.g., the dog at my home

m An object has internal state

• This is called encapsulation

m Thus the action (method) may depend on both

parameters and internal state.

Creating and Accessing Objects

r We use the new operator to create an object

Random myRandom;

myRandom = new Random();

This calls theThis calls the RandomRandom constructorconstructor , which is, which is

a special method that sets up the objecta special method that sets up the object

r Creating an object is called instantiation

m An object is an instance of a particular class

r To call a method on an object, we use the

variable (not the class), e.g.,

Random generator1 = new Random();

int num = generate1.Next();

Example: the Random class

Random Random ()

Random methods

int Next ()

// returns an integer from 0 to Int32.MaxValue

int Next (int max)

// returns an integer from 0 upto but not including max

int Next (int min, int max)

// returns an integer from min upto but not including max

double NextDouble ( )

// returns a double number from 0 to 1

See RandomNumbers.cs