CS100J%20Lecture%207 - PowerPoint PPT Presentation

About This Presentation
Title:

CS100J%20Lecture%207

Description:

A method is a named parameterized group of declarations and statements. Method definition ... Class Definition Example. class Account. int balance; // current balance ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 17
Provided by: Mill1
Category:

less

Transcript and Presenter's Notes

Title: CS100J%20Lecture%207


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

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

3
Class Definition Example
  • 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
  • void withdraw(int w)
  • balance balance - w
  • withdrawals withdrawals w

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

5
Object Instance Example
  • An object is a collection of instance variables
    (also known as fields) and methods
  • An object can be referred to as a unit

6
Access to Fields and Methods
  • If
  • r is an expression whose value refers to an
    object o of class c
  • f is a field of o
  • m is a method of o
  • then
  • r.f is a variable of object o
  • r.m is a method of o

7
Application Setting
  • 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.
  • Assume the account has an initial balance of 0,
    process all transactions, and output
  • Final balance
  • Total of all deposits
  • Total of all withdrawals
  • Sample input data
  • 100 200 -50 0
  • Sample output
  • Balance 250
  • Deposits 300
  • Withdrawals 50

8
Application Example
  • int amount // current transaction
  • Account account new Account()
  • / Read and process all transactions. /
  • amount in.readInt()
  • while ( amount ! 0 )
  • if ( amount gt 0 )
  • account.deposit(amount)
  • else
  • account.withdraw(-amount)
  • amount in.readInt()
  • / Output summary information. /
  • System.out.println(
  • Balance account.balance
  • )

9
Application Example, cont.
  • The code for the application would be in a
    different class from Account, say
  • / Process banking stransactions. /
  • import java.io.
  • public class TrivialApplication
  • public static void main(String args)
  • declarations
  • // Initialize Text object in to read
  • // from standard input.
  • TokenReader in new TokenReader(System.in)
  • statements

10
Methods that Return Values
  • Method invocation
  • object-expression . method-name(expression-list)
  • 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
  • return expression
  • A method that just returns the value of a field
    is called an field accessor.

11
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
  • void withdraw(int w)
  • balance balance - w
  • withdrawals withdrawals w
  • // field accessors

methods for accessing field values
12
Application Example, Version 2
  • int amount // current transaction
  • Account account new Account()
  • / Read and process all transactions. /
  • amount in.readInt()
  • while ( amount ! 0 )
  • if ( amount gt 0 )
  • account.deposit(amount)
  • else
  • account.withdraw(-amount)
  • amount in.readInt()
  • / Output summary information. /
  • System.out.println(
  • Balance account.getBalance()
  • )
  • System.out.println(

field values accessed via methods
field values accessed via methods
field values accessed via methods
13
Field Modifiers
  • A field declarations and method definitions can
    have a modifier
  • Declarations
  • modifier type name
  • Method definitions
  • modifier
  • return-type method-name( parameter-list )
  • statement-list
  • Possible modifiers are
  • public
  • private
  • others later

14
Class Definition Example, Version 3
fields hidden from clients
  • class Account
  • private int balance // current balance
  • private int deposits // deposits 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
  • void withdraw(int w)
  • balance balance - w
  • withdrawals withdrawals w
  • // field accessors

15
Class Definition Example, Version 4
balance field deleted
  • class Account
  • private int deposits // deposits to date
  • private int withdrawals // withdrawals to date
  • // deposit d to account
  • void deposit(int d)
  • deposits deposits d
  • // withdraw w from account
  • void withdraw(int w)
  • withdrawals withdrawals w
  • // return computed balance
  • int getBalance()
  • return deposits - withdrawals

balance computed
16
Encapsulation and Information Hiding
  • The users of a class and its objects are called
    its clients.
  • The author of a class is called its implementor.
  • Information hiding principle. Implementors hide
    implementation details inside a class definition
    so clients cant see them. In particular, they
    make all fields private.
  • 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.
  • Implementors are free to change the
    implementation of a class provided the interface
    doesnt change, i.e. provided clients cant see
    any change in services.
Write a Comment
User Comments (0)
About PowerShow.com