PhoneNumber
class of Lab 4 named ExtendedPhoneNumber
. The
subclass should add a description (String
) for the phone number, e.g., "mobile", "home", etc (the description
is arbitrary.
PhoneNumber
class as well as a sample app for testing (and the output). (see link at bottom of page)
getDescription
method
read
method:
read
method of PhoneNumber
,
that doesn't quite work. (If you are wondering why, think of leveraging the toString
method of Counter
in the
toString
of UpperBoundedCounter
… the same issue arises).
read
method read in the description as well as the number. Both are passed to the constructor
for ExtendedPhoneNumber
which in turn passes the number up to 's constructor (using super
).
toString
method should call the toString
of the superclass (PhoneNumber
ans prefix it with the description (see the output below --
it's the phone number:
line).
phone number: mobile: (550)831-9463 description: mobile area code: 550 exchange: 831 line number: 9463 is toll free: false phone number: home: (775)087-0947 description: home area code: 775 exchange: 087 line number: 0947 is toll free: false phone number: work: (766)294-2999 description: work area code: 766 exchange: 294 line number: 2999 is toll free: false phone number: other: (342)775-1584 description: other area code: 342 exchange: 775 line number: 1584 is toll free: false --- 4 phone numbers processed.
ExtendedPhoneNumber.java
ExtendedPhoneNumber
(and PhoneNumber
) classes of the previous lab, code the following app:
PhoneNumber
(100 elements is more than enough)
ExtendedPhoneNumber
s read in from the file "extended_numbers.text" (using the
read
method). Print the objects out as you read them in .
PhoneNumer
s read in from the file "numbers.text".
Again, print the objects out as you read them in.
toString
method that took the array and size and built a String
representing the array (the logic was just like a regular toString; but this one is not the 'official' toString
(this is an app class after all, not an object class; in fact, there's no reason not to declare this
method static
there are not instance variables used … the atrray and size are passed in as arguments).
prnt
method that goes straight out to System.out
(though
that is not reallt 'the Java way').
compareTo
medthod to the PhoneNumber
class for the purpose of sorting
mobile: (550)831-9463 home: (775)087-0947 work: (766)294-2999 other: (342)775-1584 (718)123-4567 (347)987-6543 Before sorting: {mobile: (550)831-9463, home: (775)087-0947, work: (766)294-2999, other: (342)775-1584, (718)123-4567, (347)987-6543} After sorting: {other: (342)775-1584, (347)987-6543, mobile: (550)831-9463, (718)123-4567, work: (766)294-2999, home: (775)087-0947}I've provided the sample input files as well as the expected out here as well. If you develop in your IDE, use your own
ExtendedPhoneNumber
class
from the previous lab.
PhoneNumber
entries print out differently than
the ExtendedPhoneNumber
entries. This is because they are each using their own (overrided) toString
method.
PhoneNumber
, but its the (runtime/dynamic) type of the
actual object referenced by each array element (PhoneNumber
or ExtendedPhoneNumber
) that determines
which method is called.
PhoneNumber
(the phone number) which is common to both classes.
PhoneNumber
s state.