ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.

10
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods

Transcript of ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.

Page 1: ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.

ACO 101:Introduction to Computer Science

Anatomy Part 2: Methods

Page 2: ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.

Structure of a Class// A Bank Account with deposit/withdraw

public class BankAccount {private long balance;private String accountNumber;private String accountName;private int homeBranch;

//create a new bank account object

public BankAccount( String number, String name ) { }

/** deposit or credit to account */public void credit( long amount ) {

assert amount>=0 : "Error: negative

deposit";balance += amount;

}

comment describes the class

Beginning of class definition, including accessibility

Attributes of the class

A constructor

A method with a parameter

Page 3: ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.

BankAccount Class (continued)// BankAccount class continued...

public long getBalance( ) {return balance;

}public void setBalance(long balance) {

if ( balance >= 0 )this.balance =

balance;else

ErrorMessage("invalid ...");}

An accessor method: returns value of an attribute.

A mutator method: changes value of an attribute; also performs data checking.

Page 4: ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.

What is a Method?Programming view: a method is a function. It can return a value or not.

Design view: methods define the behavior of objects. Methods are the way by which objects communicate

// define a "deposit" behavior for a bank account.public class BankAccount {

private long balance; // acct balance attribute public void deposit( long amount ) {

balance = balance + amount;}

//... more methods and variables (attributes)}

Page 5: ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.

Invoking a MethodTo invoke the deposit method, you must use is as a behavior of a particular BankAccount object.Example:

// define a "deposit" behavior for a bank account.BankAccount myAcct = new BankAccount(“1111111");

// read some deposit datastring s = this.txtDepositAmount.Text;decimal money = Convert.ToDecimal(s);

// method: deposit the money in my accountmyAcct.deposit( money );

call "deposit" method of a BankAccount object

Page 6: ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.

Writing a Method (1)A method consists of these parts.

// return the maximum of 2 double valuespublic double Max ( double a, double b ) {

if ( a > b ) return a;else return b;

}

Access Control:who can use this method?

Type of value returned by this method. "void" if nothing is returned.

Method Name

ParametersMethod

Body, with returned

value.

Page 7: ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.

Scope of Methods• All methods (functions) must be part of a class. • This avoids name conflicts. The max( ) method of the MyMath class

does not conflict with the max( ) method of an other class.

/** My own Math library */public class MyMath {

/** a max method */public static double max( double x, double y)

{ if ( x >= y ) return x;else return y;

}//... more methods and attributes

}

Page 8: ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.

Scope of Methods (2)• From inside of the class, you can refer to a method using just its name.• From outside of the class, you must use a class name (static methods)

or object name (instance method) to call a method. (We will go over the difference between static and instance methods later)

public class TestMyMath {public static void main( String [] args ) {

Scanner console = new Scanner(System.in);double x = console.nextDouble( );double y = console.nextDouble( );// call "max" of MyMath class:double r1 = MyMath.Max( x, y );// call "max" of C# Math class:double r2 = Math.Max( x, y );

}

Page 9: ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.

Visibility (Accessibility) of MethodsYou determine what objects or parts of the

program can access an object's methods.• private: method can only be invoked by objects of this class.• protected: method can be invoked by other code in the same package,

and by objects of this class and its subclasses.• public: method can be invoked by any program.

public void deposit( long amount ) { /* body of the method */

}

visibility type of returned value. "void" if method doesn't return any value.

Page 10: ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.

Returned Value of a Method

class BankAccount {

public void deposit(long amount) {balance += amount;

}public long getBalance( ) { return balance; }

A method may return a value. The type of returned value must be declared in the method header.

A method which doesn't return any value should have a return type of "void".

In the method body, use "return <expression>".

void means this method does not return a value.