 |
Defining Classes, Part III
|
Implementing methods and testing
-
Do not need to implement methods in order
-
pick easiest ones first
-
you can start implementing a class before finishing the implementation
of another
-
recall testing bits at a time - implement one method and then test it
-
Ask this question: are there any obvious values that need to be stored
as instance variables?
-
if so, what methods access them?
-
the describe method returns a string to access the location, price and
style of the house. Can we write the method without these? No. It looks
like location, price and style should be stored in instance variables
-
declare these instance variables in the skeleton:
private String location;
private String price;
private String style;
declaring these instance variables doesn't give them a value, need methods
to set them, i.e., setStyle, setLocation, setPrice. These seem the easiest
to implement:
public void setStyle (String theStyle) {
style = theStyle;
}
A more interesting version might check to see that the style is valid,
e.g., allow ranch, georgian, but not castle or beer.
the next method, describe, is slightly more difficult. It should take the
instance variables and join them together. It should also return this joined
description.
Java provides a special statement, called the return statement, to return
a value from a method:
return value;
When the return statement is executed, the method terminates and the sender
resumes execution; the value indicated in the return statement is returned
to the sender.
public String describe () {
String description;
description = location.concat (" ");
description = description.concat (price);
description = description.concat (" ");
description = description.concat (style);
return description;
}
Once all methods are implemented, we can test the whole class (using the
tester from before), by compiling both the class and the tester program
and running the tester program.
State and behavior
If we are modeling a real estate company, it is likely that there would
be more than one house.
Each house would share the same methods but have different instance variables
class TaplinRealEstate {
public static void main (String [] args) {
House house1 = new House ();
House house2 = new House ();
house1.setPrice ("$100000");
house1.setLocation ("Monaco estates");
house1.setStyle ("Ranch");
house2.setPrice ("$150000");
house2.setLocation ("Clemson");
house2.setStyle ("Bungalow");
System.out.println (house1.describe ());
System.out.println (house2.describe ());
}
}
Java stuff
Declaration order
-
Any order of declaration is valid between the left and right braces ({..})
of the class.
-
Convention in book is to declare constructors first, methods next, followed
by variable declarations
The return statement
-
The sender of a message cannot continue until the method returns.
-
by using the return statement, in which case it receives a value
-
for methods that return nothing, by reaching the end of the method.
-
Methods can only return one value at a time (per call)
-
we can't write a method that returns two Strings e.g., "Tony" and "Modra".
Access control: public vs. private
-
By placing public or private at the beginning of a method or instance variable
declaration, we can control access to the method or instance variable.
-
Using public allows other objects to have access to the method/variable
-
Using private prevents other objects having access to the method/variable
-
RULE: if something must be made accessible outside a class, then we make
it public, otherwise we make it private.
-
Why would you want to restrict access?
-
Hiding information
-
Preventing incorrect values being assigned (c.f., making sure style is
set only to particular values)
-
hiding helper methods that make no sense outside the context of the class
Variables and lifetimes
-
In defining House, variables are encountered in three different places
-
parameters, local variables, instance variables
-
How long do these variables exist, how long can the name be accessed?
-
Parameters are automatically created and assigned when a method
is invoked, and disappear when method terminates.
-
last as long as the method invocation
-
get values from the arguments used in the method call
-
arguments must correspond in number and in type, on a one-to-one basis
-
i.e., it is not valid to send a String and a PrintStream in that order
to a method expecting a PrintStream and a String.
-
are not visible outside the method
-
Local variables exist from their declaration to the end of the method.
-
are destroyed when the method terminates
-
must be assigned a value before they can be used
-
are not visibile outside the method
-
if three methods each have a local variable called s, in essence there
are three different variables that exist in different contexts.
String getGenre () {
String s = "classic rock/".concat (this.getFormat ());
return s;
}
String getFormat () {
String s;
s = "no commercials";
return s;
}
Why don't we need public or private?
Instance variables live as long as the object to which they belong.
-
is created when the object is created
-
goes away when the object is destroyed
-
cannot have two instance variables with the same name
A default identifier: this
-
this can be used in a method to refer to the object that was sent the message.
-
e.g., this.getCode (this.location)
Last modified: 2/14/99