Lecture 25, CS 302-6, October 31
i. 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. Objects
derived from the same class.
i. Random is a class. It describes objects that can calculate random numbers. Every random object can calculate doubles,
ints, etc
ii. Question – is there anything that is
'customizable' about Random objects?
iii. Answer - Seed Value. This is like car/house color
i. Random rand=new Random(int seed);
ii. We've done this before: For instance,
Scanners, Randoms, ArrayLists. Note -
Strings are a special case, since we can define them with a literal.
iii. Question - Any other special cases that we've
seen?
iv. Answer – Array
i. What is another type of method we've
seen? Static methods.
ii. 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.
iii. 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.
i. For instance, rand.nextInt(5);
i. Note - this is the sort of info that you'll
see in the javadoc for a method
i. 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?
ii. All we know is how to construct these classes
(new), and how to call their instance methods.
iii. 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.
i. That's a lot more work than one person
implementing it for everyone.
ii. Also, imagine something happens, and the
random number generator is compromised, causing security issues etc.
iii. If everyone had their own version, they would
all have to fix them.
iv. But, since there is only one version, the
issue only needs to be fixed in one place.
i. For instance, substring.
ii. They can implement that change, but the
people using the Class don't need to do anything
iii. They'll automatically get the fix
(eventually) since the public interface hasn't changed
i. This is like the skeleton/outline of your
class.
ii. Once again, this consists of all the methods
that may be useful for objects of the class
iii. Remember that these are instance methods, not
static methods.
i. public <return type>
<name>(<type> <paramName>, ...){}
ii. Note – this is the same as static methods,
except without the static keyword.
iii. Instance means they are called on a
particular object.
i. 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.
ii. 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?
i. Each one performs a similar function, but
also needs to remember a couple of things.
ii. How many times has Candidate A been voted
for? Candidate B? Etc.
i. Let's say we have two candidates -a
republican and a democrat.
ii. We want to allow someone to vote for one of
the candidates.
iii. We also want to consider the case where they
screw up, and need to undo their choice.
iv. Finally, after voting is done, we want to be
able to collect the totals.
public void voteDemocrat() { }
public void voteRepublican() {}
public void undoVote() { }
public int getDemocratVotes() { }
public int getRepublicanVotes(){}
i. Add money
ii. Display balance
iii. Select item
public void putInMoney(double amount) {}
public double displayBalance() {}
public String selectItem(int column, int row) {}