Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Methods again!
Example
Consider the following deni&on of a class to represent an investment account. No&ce that Ive made the instance variables public so I can access them outside the class just for illustra&on in this example.
Example, contd
Here is a program that creates two instances of the investment account. No&ce that because the instance variables of the InvestmentAccountV1 class are public, I can ini&alize them outside the class.
Example, contd
What happens when I run this program? The entry point of a program is always the main method. The statements in the main method are executed in sequence. When a method is called within main, e.g. myAccount.printInfo(), it is as if all the code for the method was inserted directly into main at that point.
Example, contd
Remember: When a method is called within main, it is as if all the code for the method was inserted directly into main at that point. We just need to remember that if the method is called on an object, any references to instance variables inside the method refer to that objects instance variables.
Example, contd
Remember: When a method is called within main, it is as if all the code for the method was inserted directly into main at that point. Thus, our program is equivalent to this one.
Example, contd
Example, contd
This is the output of the original program. It is the same as the output of the equivalent program on the previous slide.
CONSTRUCTORS
10
Example, contd
Note that when we create an object, we are actually calling a special method the constructor of the object.
But
we
didnt
write
a
constructor!
Thats
not
a
problem
--
if
we
dont
provide
a
constructor,
Java
automa&cally
provides
a
simple
one
for
us,
so
that
objects
of
our
class
can
be
created.
G.
Ayorkor
Korsah,
Ashesi
University
College,
Programming
II
-
First
Semester
2011
11
Example, contd
The constructor that Java automa&cally provides, ini&alizes all instance variables to their default values. It is as if we wrote this default constructor for our class:
12
Example, contd
Segng the values of the instance variables of the InvestmentAccountV1 objects in the main method is cumbersome and violates the principle of encapsula&on.
Lets
create
a
constructor
of
the
InvestmentAccountV1
class,
that
will
ini&alize
the
instance
variables
inside
the
object
at
the
same
&me
the
object
is
created!
G.
Ayorkor
Korsah,
Ashesi
University
College,
Programming
II
-
First
Semester
2011
13
Example, contd
We will move these three lines of code into the constructor for the InvestmentAccountV1 class.
14
Example, contd
Hmm. We have a problem now, all instances of the InvestmentAccountV1 class will the same informa&on Ayorkor Korsah as the account holder, 1000.00 as the ini&al amount, and 0.05 as the compound interest rate. If we do this, how will we be able to create Kobbys account???
15
Example, contd
Lets modify things slightly. If we use temporary variables or placeholders to hold the desired values for the account holder, the ini&al amount and the compound interest rate, then the code for ini&alizing Ayorkors account and Kobbys account are now exactly the same. We just need to set the placeholder values correctly
16
Example, contd
We will put these three lines of code into the constructor for the InvestmentAccountV1 class. Our temporary variables, or placeholders, become the formal parameters or arguments for the constructor that is, they represent informa&on that needs to be provided by the caller when the method is called/invoked.
17
Example, contd
We can now replace the indicated sec&ons of code with calls to our new constructor, as shown below.
18
Example, contd
Note that when we call the constructor, we pass it some actual parameters (the values of our placeholder variables). The values of the actual parameters are automa&cally copied into the constructors formal parameters.
19
Example, contd
This is the resul&ng program our main method has only 4 statements!
Example, contd
Now that we have a constructor and dont need to set the values of the instance variables in the main method outside the InvestmentAccountV1 class, we can make these instance variables private.
21
Example, contd
Now, we have extended main to call the balanceAler5Years() method of the two InvestAccountV1 objects we have created.
23
Example, contd
A key dierence between the balanceAler5Years() method and the printInfo() method is that the balanceAler5Years() method has a return value (its return type is double).
24
Example, contd
If a method returns a value, you can invoke it anywhere that you would use a value of the type returned by the method. In this case, balanceAler5Years() returns a double. We thus invoke it on the right hand- side of an assignment statement which assigns the return value to a variable of type double.
Example,
contd
This
is
the
output
of
the
program.
26
Example, contd
A
problem
with
the
deni&on
of
our
InvestmentAccountV1
class
is
that
it
is
rather
inexible
we
can
gure
out
the
balance
aler
5
years,
but
not
aler
any
other
number
of
years
(e.g.
1
year,
2
years,
10
years,
etc)
G.
Ayorkor
Korsah,
Ashesi
University
College,
Programming
II
-
First
Semester
2011
28
Example, contd
We can x this by changing the balanceAler5Years method to a method called balanceAlerNYears, which takes in a parameter/argument for the number of years, n.
29
Example,
contd
This
is
the
output
of
the
program.
We can call this method in main to nd out the balance aler, say, 10 years
30
Example, contd
We can even print a table that lists the balance every 5 years, up to 30 years!
31