Lecture 25, March
19, 2012
1.
ArrayLists
2.
Arrays of
Objects
1.
Up until this
point - we've considered programs as a series of statements telling the
computer what to do
2.
Our programs
have consisted of methods, which are one form of abstraction.
3.
But we still
have still been very much in the mindset of 'do this', then 'do that'
4.
We'll spend the
next couple of weeks talking about a new sort of paradigm for programming:
Object Oriented Programming
5.
Basically,
methods are a start, but we want to introduce another layer of abstraction.
1.
Let's think
back: methods allow us to box up a series of statements.
2.
When we use a
method, we don't need to know all of the details of how it works – we just need
to know the inputs and outputs. It
handles all of the complexity for us.
3.
Classes operate
in much the same way - we will focus on developing whole classes instead of
just methods.
4.
This sort of
programming is referred to as object oriented programming. - we are focusing on
the objects
1.
We've heard
this term before – all of our code up to this point has been contained in a
class. We know that a class can contain
methods, such as the main method, and static methods. But, we haven't thought too much about what exactly a class is.
2.
Classes allow
us to box up (encapsulate) both methods and data.
3.
Objects are an
instantiation of a class.
4.
"A class
describes a set of objects with the same behavior"
1.
Think of a
blueprint for a house. It tells you the
general outline of how the house will look, where the doors, windows etc
go. But it is not actually a house
itself – it's just the outline. This is
like a class.
2.
Now, think of
an actual house. It is the physical
embodiment of the blueprint – it is actually a real thing, not just a blueprint
for a real thing. This is like an
object.
1.
Note – you can
even create multiple houses from the same blueprint. Maybe there are even some things which can be different about
them, such as color – the blueprint might allow this flexibility. But they are all still houses derived from
the same blueprint. This is like multiple objects derived from the same class.
1.
Let's think of
a specific car model. How about a 2003
Kia Rio. Why? Because that's what I drive :)
2.
All 2003 Kia
Rio's follow a similar kind of blueprint – they all have four wheels, you can
drive them, etc.
3.
But, there can
be differences too. For instance: color
(blue, red, etc), transmission (manual or automatic), # of doors (2 or 4)
4.
We can think of
the model as being one class, and the different cars created from it as being
objects of that class.
1.
Java examples
we've seen so far: String, Random, Scanner.
2.
Let's focus on
Random
1.
Random is a
class. It describes objects that can
calculate random numbers. Every random
object can calculate doubles, ints, etc
2.
Question – is
there anything that is 'customizable' about Random objects?
3.
Answer - Seed
Value. This is like car/house color
1.
First - must
construct an object from a class
2.
How do we do
this? With the new operator
1.
Random rand=new
Random(int seed);
2.
We've done this
before: For instance, Scanners, Randoms, ArrayLists. Note - Strings are a special case, since we can define them with
a literal.
3.
Question - Any
other special cases that we've seen?
4.
Answer – Array
3.
Once you have
constructed the object, you can call instance methods. Instance methods are invoked on an object.
1.
What is another
type of method we've seen? Static
methods.
2. Static methods are invoked on a Class - they don't need a specific object to operate. For instance, Math.pow(double a, double b). We don't need to create a Math object before using this method – we just call it from the class. It is a static method.
3.
Similarly, the
methods we've written so far, we don't need to create an object before using
them – we can just call them directly.
4.
In order to
call an instance method, you must provide object name, method name, and
parameters.
1.
For instance,
rand.nextInt(5);
1.
We've talked
about how methods are black boxes.
Classes aren't so different.
Encapsulation refers to the way in which we abstract away the details of
a class when we're using it.
2.
All that we
need to know to use an object are the specifications of the instance methods
that you can invoke - name, params, etc.
This is referred to as the public interface for those methods.
1.
Think of
everything we've done so far in Java.
We've been using classes like Scanner, Random, etc, even though we
didn't make them ourselves.
1.
We don't know
every detail of how these classes work - Scanner in particular is actually
really complex. How does it get input
from the user?
2.
All we know is
how to construct these classes (new), and how to call their instance methods.
3.
We can also
make classes that other programmers can use, even if they don't know all of the
details. This is often referred to as
modularity – programs can be made up of distinct pieces, created by different
people, that are interchangeable.
4.
Once again,
this is kind of like how methods allow us to abstract out information. This is
just another degree of that.
2.
Let's imagine
that in order to use Random, each developer had to implement their own version.
1.
That's a lot
more work than one person implementing it for everyone.
2.
Also, imagine
something happens, and the random number generator is compromised, causing
security issues etc.
3.
If everyone had
their own version, they would all have to fix them.
4.
But, since
there is only one version, the issue only needs to be fixed in one place.
3.
Similarly, say
someone found a faster, more efficient way to implement an instance method of,
say, String
1.
For instance,
substring.
2.
They can implement
that change, but the people using the Class don't need to do anything
3.
They'll
automatically get the fix (eventually) since the public interface hasn't
changed
4.
Or, say, there
was a bug
1.
Create public
interface.
1.
This is like the
skeleton/outline of your class.
2.
Once again,
this consists of all the methods that may be useful for objects of the class
3.
Remember that
these are instance methods, not static methods.
2.
How to declare
an instance method:
1.
public
<return type> <name>(<type> <paramName>, ...){}
2.
Note – this is
the same as static methods, except without the static keyword.
3.
Instance means
they are called on a particular object.
3.
Mutators vs
Accessors
1.
Mutators -
change something about the object itself .
1.
For instance,
painting a house.
2.
Changes
something called the state of the object – we'll define that later.
2.
Accessor - get
some information about the object and return it. Does not change the object itself.
1.
For instance,
what is the color of my house?
1.
Background:
When you go to vote somewhere, there are a bunch of voting machines.
1.
Each one
performs a similar function, but also needs to remember a couple of things.
2.
How many times
has Candidate A been voted for? Candidate
B? Etc.
2.
Let's think
about what we want our voting machine to be able to do
1.
Let's say we
have two candidates -a republican and a democrat.
2.
We want to
allow someone to vote for one of the candidates.
3.
We also want to
consider the case where they screw up, and need to undo their choice.
4.
Finally, after
voting is done, we want to be able to collect the totals.
3.
What would a
public interface for this class look like?
public void voteDemocrat() { }
public void voteRepublican() {}
public void undoVote(boolean previousVoteDem) { }
public int getDemocratVotes() { }
public int getRepublicanVotes(){}
- Keep in mind – accesors and mutators