Download presentation
Presentation is loading. Please wait.
Published byKerrie Ferguson Modified over 9 years ago
1
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods
2
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
3
BankAccount Class (continued) // BankAccount class continued... public long getBalance( ) { return balance; } public void setBalance(long balance) { if ( balance >= 0 ) this.balance = balance; elseErrorMessage("invalid..."); } An accessor method: returns value of an attribute. A mutator method: changes value of an attribute; also performs data checking.
4
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) }
5
Invoking a Method To 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 data string s = this.txtDepositAmount.Text; decimal money = Convert.ToDecimal(s); // method: deposit the money in my account myAcct.deposit( money ); call "deposit" method of a BankAccount object
6
Writing a Method (1) A method consists of these parts. / / return the maximum of 2 double values public 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 Parameters Method Body, with returned value.
7
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 }
8
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 ); }
9
Visibility (Accessibility) of Methods You 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 */ } visibilitytype of returned value. "void" if method doesn't return any value.
10
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 ". void means this method does not return a value.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.