Lecture 26, CS 302-6, November 2
i. Example – blueprint/house
i. Public Interface
i. Accessors
ii. Mutators
i. Voting Machine
ii. Vending Machine
i. Declared for the entire class, not for just
one method
i. Need to consider the needs of each method in
determining how many/which ones to declare
i. We'll have an instance variable to keep track
of each of these.
i. Declared in the class, outside any methods.
ii. Usually at the top of the class.
iii. private
<type> <name>; …
i. Since we declare these as private, they
cannot be accessed directly by the object.
Must use accessor instead.
i. 0 or more of them
i. One and only one
i. Holds reference to the object itself (the
implicit parameter)
ii. Use to refer to instance variables from
within instance methods
1. this.<variable name>
i. For now, we won't consider the undoVotes
functionality
1. this.demVotes++;
1. return this.demVotes;
i. Consider a new method, voteDemocratXTimes
1. We won't worry about undoVote here
public void voteDemocratXTimes(int x)
{
this.demVotes+=x;
}
ii. What if, instead of calling our int x, we
called it demVotes?
public void voteDemocratXTimes(int demVotes)
{
this.demVotes+=demVotes;
}
iii. Think about variable scope…
Design a class that represents a circle. It should store how big the circle is (radius). It should have methods to set the radius,
get the value for the radius, get the value for the area of the circle, and get
the value for the circumference of the circle.