Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.

Similar presentations


Presentation on theme: "CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases."— Presentation transcript:

1 CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases –Reading: n Lewis & Loftus, Chapter 4 and Section 5.1 n Savitch, Chapter 4 n This Lecture –Programming Concepts n Classes, Objects, and Methods --- reiterated n Accessors n Encapsulation –Java Constructs n return statement n Visibility modifiers –public –private

2 CS 100Lecture72 Class Definition A class is a named group of declarations and methods A class is a named group of declarations and methods n Class definition class class-name { declarations methods} A method is a named parameterized group of declarations and statements A method is a named parameterized group of declarations and statements n Method definition return-type method-name( parameter-list ) { declaration-and-statement-list declaration-and-statement-list }

3 CS 100Lecture73 Class Definition Example class Account { int balance; // current balance int balance; // current balance int deposits; // deposits to date int deposits; // deposits to date int withdrawals; // withdrawals to date int withdrawals; // withdrawals to date // deposit d to account // deposit d to account void deposit(int d) { balance = balance + d; deposits = deposits + d; } // withdraw w from account // withdraw w from account void withdraw(int w) void withdraw(int w) { balance = balance - w; withdrawals = withdrawals + w; }}

4 CS 100Lecture74 Object Instantiation The value of expression The value of expression new class-name() is a reference to a new object of the given class-name

5 CS 100Lecture75 Object Instance Example n An object is a collection of instance variables (also known as fields) and methods n An object can be referred to as a unit balance deposits withdrawals deposit withdraw 0 0 0 fields methods

6 CS 100Lecture76 Access to Fields and Methods n If –r refers to object o of class c, –f is a field of o –m is a method of o n then –r.f is a variable of object o –r.m is a method of o

7 CS 100Lecture77 Application Setting n Process bank account transactions: –The input consists of zero or more non-zero integers signifying “transactions”, followed by 0. –A positive value is a deposit. –A negative value is a withdrawal. n Assume the account has an initial balance of 0, process all transactions, and output: –Final balance –Total of all deposits –Total of all withdrawals n Sample input data 100 200 -50 0 n Sample output Balance: 250 Deposits: 300 Withdrawals: 50

8 CS 100Lecture78 Application Example int amount; Account account = new Account(); /* Read and process all transactions. */ amount = in.readInt(); amount = in.readInt(); while ( amount != 0 ) while ( amount != 0 ) { if ( amount >= 0 ) if ( amount >= 0 ) account.deposit(amount); account.deposit(amount); else else account.withdraw(-amount); account.withdraw(-amount); amount = in.readInt(); amount = in.readInt(); } /* Output summary information. */ System.out.println( System.out.println( ”Balance: ” + account.balance ”Balance: ” + account.balance ); ); System.out.println( System.out.println( ”Deposits: ” + account.deposits ”Deposits: ” + account.deposits ); ); System.out.println( System.out.println( ”Withdrawals: ” + account.withdrawals ”Withdrawals: ” + account.withdrawals ); );

9 CS 100Lecture79 Methods that Return Values n Method invocation object-expression. method-name(expression-list) n Method execution –initialize the parameters with the values of the argument expressions –execute the statement-list of the method –thereafter, continue where you were before Non- void methods with return-type t can return a value of type t using the statement Non- void methods with return-type t can return a value of type t using the statement return expression ;

10 CS 100Lecture710 Class Definition Example, Version 2 class Account { int balance; // current balance int deposits; // deposits to date int withdrawals; // withdrawals to date // deposit d to account void deposit(int d) { balance = balance + d; deposits = deposits + d; } // withdraw w from account // withdraw w from account void withdraw(int w) { balance = balance - w; withdrawals = withdrawals + w; } // accessors int getBalance(){ return balance; } int getDeposits(){ return deposits; } int getWithdrawals(){ return withdrawals; } }

11 CS 100Lecture711 Application Example, Version 2 int amount; Account account = new Account(); /* Read and process all transactions. */ amount = in.readInt(); amount = in.readInt(); while ( amount != 0 ) while ( amount != 0 ) { if ( amount >= 0 ) if ( amount >= 0 ) account.deposit(amount); account.deposit(amount); else else account.withdraw(-amount); account.withdraw(-amount); amount = in.readInt(); amount = in.readInt(); } /* Output summary information. */ System.out.println( System.out.println( ”Balance: ” + account.getBalance() ”Balance: ” + account.getBalance() ); ); System.out.println( System.out.println( ”Deposits: ” + account.getDeposits() ”Deposits: ” + account.getDeposits() ); ); System.out.println( System.out.println( ”Withdrawals: ” + account.getWithdrawals() ”Withdrawals: ” + account.getWithdrawals() ); );

12 CS 100Lecture712 Field Modifiers A field declarations and method definitions can have a modifier A field declarations and method definitions can have a modifier n Declarations modifier type name ; modifier type name ; n Method definitions modifier modifier return-type method-name( parameter-list ) return-type method-name( parameter-list ) { statement-list statement-list } Possible modifiers are: Possible modifiers are:publicprivate others later A private field or method is not visible from outside the class definition. A private field or method is not visible from outside the class definition.

13 CS 100Lecture713 Class Definition Example, Version 3 class Account { private int balance; // current balance private int balance; // current balance private int deposits; // deposits to date private int deposits; // deposits to date private int withdrawals; // withdrawals to date private int withdrawals; // withdrawals to date // deposit d to account void deposit(int d) { balance = balance + d; deposits = deposits + d; } // withdraw w from account // withdraw w from account void withdraw(int w) { balance = balance - w; withdrawals = withdrawals + w; } // accessors int getBalance(){ return balance; } int getDeposits(){ return deposits; } int getWithdrawals(){ return withdrawals; } }

14 CS 100Lecture714 Class Definition Example, Version 4 class Account { private int deposits; // deposits to date private int deposits; // deposits to date private int withdrawals; // withdrawals to date private int withdrawals; // withdrawals to date // deposit d to account // deposit d to account void deposit(int d) { deposits = deposits + d; } // withdraw w from account // withdraw w from account void withdraw(int w) { withdrawals = withdrawals + w; } // accessors int getBalance() { return deposits - withdrawals; } int getDeposits(){ return deposits; } int getWithdrawals(){ return withdrawals; } }

15 CS 100Lecture715 Encapsulation and Information Hiding n The users of a class and its objects are called its clients. The author of a class is called its implementor. The author of a class is called its implementor. Information hiding principle. Implementors hide implementation details inside a class definition so clients can’t see them. In particular, they make all fields private. Information hiding principle. Implementors hide implementation details inside a class definition so clients can’t see them. In particular, they make all fields private. n The methods of a class provide services to clients. The interface of a class is the contract in which the implementor commits to deliver certain services to the clients. n Implementors are free to change the implementation of a class provided the interface doesn’t change, i.e. provided clients can’t see any change in services.


Download ppt "CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases."

Similar presentations


Ads by Google