CS110- Lecture 7 Feb 28, 2005 - PowerPoint PPT Presentation

About This Presentation
Title:

CS110- Lecture 7 Feb 28, 2005

Description:

Account acc2 = new Account('James Gosling',72312,112) ... 'James Gosling' 'Lewis Loftus' 8/10/09. CS110-Spring 2005, Lecture 7. 7. UML Diagram. Account ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 21
Provided by: Robert1138
Learn more at: http://www.cs.umb.edu
Category:
Tags: cs110 | feb | gosling | lecture

less

Transcript and Presenter's Notes

Title: CS110- Lecture 7 Feb 28, 2005


1
CS110- Lecture 7Feb 28, 2005
  • Agenda
  • Anatomy of a Class
  • Anatomy of a Method
  • The while Statement
  • Project 1 questions

2
Anatomy of a Class
  • So far weve written single classes containing a
    single main method.
  • In general, a Java program consists of objects
    from various classes interacting with one
    another.
  • So far we instantiated objects using predefined
    classes in the Java Class Library and interacted
    with those objects for the services.
  • Now we will write classes that will instantiate
    objects of our own classes.

3
Anatomy of a Class
  • public class Banking
  • //Execution always starts from main
  • public static void main(String args)
  • Account acc1 new Account(Ted
    Murphy,72345,100)
  • Account acc2 new Account(James
    Gosling,72312,112)
  • Account acc3 new Account(Lewis
    Loftus,72332,200)
  • double newBal acc1.deposit(23.37)
  • System.out.println(After deposit newBal)
  • System.out.println(After withdrawal
    acc1.withdraw(12.37))

4
A Class as an Outline
Class name Account Fields (Instance
data) owner account number balance Methods
(actions) initialize data (Constructor)
deposit money How balance
balance amount withdraw money
How balance balance amount get the
balance How return the balance
Data determine the state. It is called instance
data because memory is determined for each
instance of the class that is created. Scope of
instance data is the whole class
Same name as of class and is called when the
new operator is used to create an instance of
class
Methods determine the behavior
5
Account Class
public class Account private String owner
private long acctNumber private double
balance public Account(String o, long a,
double b) owner o acctNumber a
balance b public double deposit(double
amount) balance balance amount return
balance public double withdraw(double
amount) balance balance amount
return balance public double
getBalance() return balance
6
Instantiations of the Class Account
owner acctNumber balance
Ted Murphy
72345
acc1
100
owner acctNumber balance
James Gosling
acc2
72312
112
owner acctNumber balance
Lewis Loftus
acc3
72332
200
7
UML Diagram
Class Name
Account
owner String acctNumber long balance double
Data
deposit (double amount) double withdraw
(double amount) double getBalance() double
Methods
8
Anatomy of a Method
  • A method declaration specifies the code that is
    executed when the method is invoked.
  • When a method is called control of flow transfers
    to that method
  • When that method is done, control returns to the
    location where the call was made and execution
    continues.

9
Anatomy of a Method
Account
Banking
main (Calling method)
Account (Called method)
  • Account acc1
  • new Account(Ted Murphy,72345,100)
  • double newBal acc1.deposit(23.37)

Deposit (Called method)
10
Anatomy of a Method
  • There are two types of methods
  • Those that return a single value
  • Those that perform some action other than return
    a value. These methods are called void methods.
  • For e.g. We can add setBalance method to
    Account class.

public void setBalance (double b) balance
b
11
Method Definitions
  • Void method definition
  • public void Method_name(parameters)
  • Statement_1
  • Statement_2
  • ..
  • Statement_Last

Heading
Body
12
Method Definitions
  • Definition of a method that return value
  • public Type_Returned Method_name(parameters)
  • List of statements at least one of which
  • must contain a return statement that
  • returns some value of type Type_Returned

Heading
Body
13
Return statement
  • return Expression
  • The Expression can be any expression that
    produces a value of the type specified in the
    heading of the method definition.
  • public int getFemales(int population)
  • int males population/2
  • return (population males)

14
Parameters
  • A parameter is a value that is passed into a
    method when it is invoked.
  • The parameter list in the header of a method
    specifies the types of the values that are passed
    and the names by which the called method will
    refer to these values.
  • public double deposit (double amount)
  • balance balance amount
  • return balance

15
Parameters
  • Formal Parameters The names of the parameters in
    the header of the method declaration.
  • public double deposit (double amount)
  • balance balance amount
  • return balance
  • Actual Parameters The actual values passed into
    a method. Also called arguments.
  • acc1.deposit(23.37)

Formal parameter
Actual parameter
16
Local Data
  • A scope of a variable is the part of the program
    in which a valid reference to that variable can
    be made.
  • A variable can be declared inside a method,
    making it local data as opposed to instance data
    which is declared inside the class.
  • Local data has scope limited to only the method
    in which it is declared.
  • Instance data has scope of the entire class any
    method of the class can refer to it.

17
The while Statement
  • The while statement repeats its action again and
    again until a controlling boolean expression
    becomes false.
  • The loop is repeated while the controlling
    boolean expression is true.

18
The while Statement
  • public class WhileDemo
  • //Execution always starts from main
  • public static void main(String args)
  • System.out.println(Enter a number)
  • Scanner keyboard new Scanner(System.in)
  • int num keyboard.nextInt()
  • int count 1
  • while(count lt num)
  • System.out.print(count ,)
  • count

while(Boolean_Expression) Loop_Body
The loop body may be either a single statement or
more likely a compound statement
19
The while Statement
while(Boolean_Expression) Loop_Body
Start
Evaluate Boolean_Expression
false
End loop
true
Execute Loop_Body
20
The while Statement
while(count lt num) System.out.print(count
,) count
Start
Evaluate count lt num
false
true
End loop
Execute System.out.print(count ,)
count
Write a Comment
User Comments (0)
About PowerShow.com