home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs
lect05b
practice implementing classes
We continue from our Song example,
from last time,
where we developed
SongTester-v1.java/
and completed the stub-methods to get
Song-v2.java/
-
The original homework obtained info from the user
by putting up dialog boxes.
We can do this, too:
we'll make a second constructor which takes no arguments,
and that constructor will do the work of propmting the user.
[solution]
Observe how this is a big win, compared with hw02:
Even though we have a test class plus a primary class with
several methods,
our code isn't much longer than before.
-
Why is it no longer?
Mostly because we didn't have to have two copies
of the code for getting song-info via dialog boxes.
-
If we wanted to modify the program do prompt for five songs
instead of just two,
our program here makes that trivial:
our SongTester would just include
Song s3 = new Song();
Song s4 = new Song();
Song s5 = new Song();
|
and voila!, we have dialog boxes which
gather info about all the songs.
(Our hw02 approach didn't scale -- it would have required
about 15 more variables, and more than 20 lines of code
dealing with finding where the “:” was,
converting Strings into ints, etc..
-
Our program, as written, has more functionality
than hw02, since it also lets us use the 3-arg constructor
to make Song objects with a known band&title
without needing more than a single (Song) variable.
-
Our latest version is easier to maintain:
If we decide to modify the details of how the user is prompted
for information,
we only need to modify a single method,
instead of multiple copies of pasted code.
Problems for self-practice
-
What are some additional Song behaviors
which are more convenient to people using Songs:
[solution]
-
Okay, now we're ready to
complete the method-bodies.
After each one, see which tests we pass.
[solution]
-
Hey look, we pass our test suite!
home—info—lects—labs—exams—hws
textbook—tutor/PIs—java.lang docs—java.util docs