|
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
Signatures are covered in §3.1, 3.2. The mantra for a signature is:
typereturn name( parameter-type-and-name )
The problem with just writing, say,
public double getBalance() { ... } |
public double getBalance() { return -12.345; } |
Of course, if a method's signature says it returns a String, then our stub function must comply:
public String getOwnerName() { return "Otto von Schnitzelpusskrankengescheitmeyer"; } |
What about void methods, that don't return a value at all? You can leave those empty:
public void deposit() { } // This compiles as-is. |
(Constructors are like void methods, in this respect: they don't explicitly return a value, even though conceptually they are returning a whole new object.)
Problem 1: Our boss wants us to build GPSUnits for people to put in their car.
Solution 1: We can already write code for this:
public class GPSUnit { } |
Problem 2: Our boss tells us what sort of things our GPSUnit can do: it can
Give you directions to any given destination address.
(That is, the user has to give a destination to the GPSUnit.
Question: Does the user have to tell the GPSUnit where it currently is?)
We start to wonder: How should we represent the information going on?
In particular -- how should we represent the address: do we use
int or double or String or
Rectangle?
A: An address, like "1001 E. Main St, Radford VA, 24142"
might well be represented by Strings.
We also wonder: How should we represent the directions? If it's a voice interface, it might be an mp3 file. For now, we'll have a more primitive system which just returns the directions as a String (for example: "Turn left now, go 1 mile, turn right, go 2 blocks."). We might later add other methods which can take this text and embed it in a web page, or can take this text and convert it into a mp3 using a speech-synthesizer, or can take this text and create a Braille printout :-).
Once we've decided that this method takes in a String and returns a String, then we can write its signature:
String getDirections( String destination ) |
Finally, we can flesh out the method-body with a stub, and write valid code (which goes inside the class GPSUnit { ... }):
public String getDirections( String destination ) { return "First star on the left; straight on 'til morning."; } |
double getLatitude() |
public double getLatitude() { return -1000.0; } |
Suppose the boss asks also wants the user to be able to set a the destination just once, and then (later) ask for directions to the most-recently-set-destination.
This is two different behaviors (set the destination, and get directions to the destination), so we'll need two methods. What information is involved with each? What are your signatures?
lect03b-soln.html
home—info—labs—hws—exams
textbook—java.lang docs—java.util docs—archive
©2008, Ian Barland, Radford University Last modified 2008.Sep.17 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |