0% found this document useful (0 votes)
10 views

04 ImplementingClasses

Uploaded by

Maxwell Mabhikwa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

04 ImplementingClasses

Uploaded by

Maxwell Mabhikwa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Implementing Classes in Java, using

• Documented Stubs
• Test-First Programming

Check out BankAccount and WordGames from SVN


 Encapsulation
 Java classes:
◦ Implementation details
◦ “How To” example
◦ Practice in WordGames project
 Encapsulation—separating implementation
details from how an object is used
◦ Client code sees a black box with a known interface
◦ Implementation can change without changing client

Functions Objects
Black box Function Constructor and
exposes signature method
signatures
Encapsulated Operation Data storage and
inside the box implementation operation
implementation
Q1, 2
 Essentially based on Big Java
◦ But using explicit this references
◦ And putting fields at the top of the class
 Comparing and contrasting with Python
◦ Source code with Python examples is in SVN for
reference
 Next slide shows the entire class
◦ Subsequent slides discuss it piece by piece
The BankAccount
class

A class has 3 parts after its


header: fields, constructors
and methods.
Javadoc comment precedes
the class definition Name of class, follows
the class keyword

/** javadoc… */ class BankAccount:


public class BankAccount { """docstring..."""
... …
}

Access specifier (aka visibility), one of:


• public,
• protected,
• private, or
• default (i.e., no specifier, called package visibility)

Java classes are usually declared public

Java Python
Q3
Javadoc comment precedes the method definition (always if
the method is public, optionally if the method is private)

/** javadoc… */ def deposit(self, amount):


public void deposit(double amount) { """docstring..."""
... ...
}
Return type Parameters with types
Access • Do not list “self” as in Python
• void means
specifier nothing returned

Java methods usually are a mix of


public (when used by objects of
other classes) and private (when
used only within this class).

Java Python

Q4-6
Javadoc comment precedes
the constructor definition

/** javadoc… */ def __init__(self,


initAmt=0.0):
public BankAccount() { """docstring..."""
... ...
Access
}
specifier Parameters with types
• Do not list “self” as in Python
/** javadoc… */
public BankAccount(double initAmount) Use overloading
{ to handle default
... argument values
Constructor
} No explicit return type name is always
• If you accidentally put a return the same as the
type, it is a weirdly named class name
method, not a constructor!

Java Python
Java constructors are
almost always public Q7-9
 The public interface of an BankAccount
object:
◦ Is the inputs and outputs of
the black box BankAccount()
◦ Defines how we access the BankAccount(double initAmount)
object as a user
◦ Consists of:
void deposit(double amount)
 public constructors of its
class, plus void withdraw(double amount)
 public methods of its class double getBalance()
 The private implementation
of an object consists of:
◦ Its (private) instance fields
◦ Definitions of its constructors
and methods
The above shows the public interface of BankAccount objects.
The next slides show their private implementation.
Q10
Generally no Javadoc here, since you should
choose variable names that are self-documenting.

/** javadoc as needed… */ No instance field


private double balance; definitions in
Python
Name
Access Type
specifier

When do you need


Java instance fields An object is a field?
should almost
always be private
an instance
Answer: Whenever you
of a class have data that is
associated with the
object, that needs to
Java Python remain alive as long as
the object remains alive.

Q11
/** javadoc… */ def __init__(self,
initAmt=0.0):
public BankAccount(double initAmount) { """docstring..."""
this.balance = initAmount; self.balance = initAmt

Use the this keyword


inside constructors and
methods to refer to the
implicit argument

Java Python

Q12
/** javadoc… */ def getBalance(self):
"""docstring..."""
public double getBalance() { return self.balance
return this.balance;
}

/** javadoc… */ def deposit(self, amount):


public void deposit(double amount) { """docstring..."""
newBal =
double newBalance = self.balance
this.balance + amount; + amount
self.balance = newBal
this.balance = newBalance;
} The deposit method has a parameter variable (amount), a local
variable (newBalance), and a reference to a field (this.balance).
• Do you see the difference between these types of variables?

Java Can omit return Python


for void methods
Q13,14
The BankAccount
class (summary)

deposit method. Note the use of a


parameter, local variable and field.

private field

Constructor Withdraw method

Reference to the field,


using the this keyword

A getter method that


preserves the encapsulation
of the private field.

Another constructor. Note overloading.


But surely I owe you an accurate answer!
1. Create the (initially empty) class
◦ File ⇒ New ⇒ Class
2. Write documented stubs for the public interface of the class
◦ Find out which methods you are asked to supply
 If the class implements an interface, then the interface tells
you exactly which methods you must implement
 And Eclipse volunteers to type their stubs for you!
◦ Documented stubs means that you write the documentation at this
step (BEFORE fully implementing the constructors and methods, that
is, while they are only stubs)
3. Implement the class:

3. Test and implement each
Determine and implement instance fields
◦ constructor
Implement constructors and adding
and methods, method private methods and
additional instance
• fieldsthe
Write as test
needed
cases BEFORE
4. Test the class implementing the constructor/method
The BankAccount project that you checked out of SVN has the
code that we just discussed. Examine it at your leisure.

Turn now to the WordGames project that you checked out of


SVN. Let’s together:
• Study the StringTransformable interface.
• Write a Shouter class that implements StringTransformable.
Its transform method should return its given String
transformed into all UPPER-CASE (“shouting”).
1. Create the (initially empty) class
2. Write documented stubs (use Quick Fix!)
3. Write tests, then implement and test the class
4. Commit your work
• When you are done with Shouter, continue per the
WordGames instructions (linked from Homework 4).
Step 1: Create the (initially empty) class
◦ File ⇒ New ⇒ Class
Step 2: Write documented stubs for the public interface of the class

Do you understand what it means to implement an interface ?


Do you see what a stub is?
Did you see how Eclipse offered to write the stubs for you?
Note the TODO’s: The above is not yet a documented stub – see the next slide for that.
Do you see the form for Javadoc
comments? For their tags?

The form for a class?

Step 1: Create the (initially empty) class


◦ File ⇒ New ⇒ Class
Step 2: Write documented stubs for the public interface of the class
Do you understand what it means to use documented stubs ?
Do you know what you must document? (Answer: anything public.)
Do you understand why you
write tests before
implementing ?
Do you see what a field is?
Why one is used here? (Answer:
so the Shouter can be reused in all the
tests. It would also be OK to construct a
new Shouter for each test.)
Did you see how the
assertEquals method works?
How you specify a test? How the
@Before and @Test annotations work?

Look at the (many)


tests we supplied in
ShouterTest. Are they
a good set of tests, with
good coverage? Could
we test how fast Shouter’s
Step 1: Create the (initially empty) class transform runs?
Step 2: Write documented stubs for the public interface of the class
Step 3a: We provided some JUnit tests for the transform method of each class.
Do you understand how Eclipse helps you find the right method to apply
to the stringToTransform? (Pause after typing the dot.)
Do you see why you don’t need a local variable?
Do you know Java’s 1st dirty little secret about constructors? (Namely, that
Java inserted a do-nothing constructor for you! More on this later.)
 Censor: given blah, produces the result of replacing each
occurrence of the character (not string) foo in blah with an
asterisk, where foo is the character that the particular
Censor censors.
 How do you deal with foo?
◦ Can it be a parameter of transform?
 No, that violates the StringTransformable interface
◦ Can it be a local variable of transform?
 No, it needs to live for the entire lifetime of the Censor.
◦ What’s left?
 Answer: It is a field ! (What is a sensible name for the field?)
 How do you initialize the field for foo?
◦ Answer: by using Censor’s constructors!
Let’s together:
• Write a Censor class that implements StringTransformable.
Its transform method should return the result of replacing
each occurrence of the character (not string) foo in blah
with an asterisk, where foo is the character that the
particular Censor censors.
1. Create the (initially empty) class
2. Write documented stubs (use Quick Fix!)
3. Write tests, then implement and test the class
4. Commit your work
• When you are done with Censor, continue per the
WordGames instructions (linked from Homework 4).
Step 1: Create the (initially empty) class
Step 2: Write documented stubs for
the public interface of the class

Do you see why you need stubs for


the two Censor constructors? (See
the calls to them in the CensorTest class.)

Do you understand what it means to


implement an interface ?
Do you see what a stub is? Did you see how
Eclipse offered to write the stubs for you?
Note the TODO’s: The above is not yet a
documented stub – see the next slide for that.
private char characterToCensor;

this.characterToCensor = ‘e’;
Step 1: Create the
(initially empty) class
Step 2: Write
documented stubs
this.characterToCensor = for the public
characterToCensure; interface of the class

Do you understand
what it means to use
documented stubs ?
Do you know what
you must document?
(Answer: anything public.)
Do you see why Censor
needs a field? How the
field is initialized? How
Censor the field is referenced
final version (using this)?
How Censor has two
constructors? How those
constructors are called in
CensorTest?
Should we have made a field for the
‘*’ constant? (Probably.)

You might also like