| CSC 207 | Grinnell College | Fall, 2018 |
| Algorithms and Object-Oriented Design | ||
In this lab, you will explore both interfaces and Java generics by writing a generic interface and creating classes that implement it.
In this lab, you'll be creating an interface that allows one to extract individual components from some linearly indexed object. This is exactly analagous to the behavior of arrays. However, not all objects are naturally stored as arrays. While we may want to provide that kind of access, it may not be expedient to store them linearly.
Thus, you will create an interface that indicates this kind of access and create some implementations that provide it on top of some type that are not necessarily stored in a linearly indexed fashion. In particular, this lab develops the following hierarchy:
In summary,
To get started, it will be useful to create a new project within Eclipse and define the Interface Indexable
Create a project indexableExample within Eclipse.
Create an interface called Indexable that is parameterized by
a single type, let's call it ComponentType.
length, which returns an int
indicating the number of valid indices that may be accessed.
get, which takes an int for an index
(zero-based) and returns the object
of ComponentType at that index. The function
should throw an IndexOutOfBoundsException if the
index is greater than length()
Strings are fairly naturally linearly indexed, since that is essentially how they are stored in memory (though Java shields us from this, unlike C).
Implement a simple type of indexable object, based on an underlying string. That is, an IndexableString will implement the Indexable interface by working with the individual characters in a string.
IndexableString that implements
the Indexable interface. What should the
parameterized type be (i.e., what type should be
returned by get for a string)?
0
or null)
IndexableString IS-A String"?
extends).
IndexableString does indeed have
a String field to work with in its two required
functions.
String object methods
in the API, it shouldn't be very hard!
Typically, the individual (base 10) digits of integers are not usually indexed, so it might be useful to provide a class for this in some applications.
Let's say that our convention will be that indexing starts with the
right-most digit (the "ones place"), and is zero
based. Thus, get(2) on an IndexableInteger
should return the digit in the "hundreds" place, or the third digit
from the right.
How should we handle the indexing of negative numbers? For simplicity, we'll specify that the length only includes the number of digits, and that indexing always returns positive numbers.
IndexableInteger that
implements the Indexable interface.
get for an integer)?
Integer suffers from the same issue
as String. Use similar techniques for a HAS-A
relationship. Note that you may want to use the primitive type, rather
than the Integer class wrapper to simplify your two method
implementations.
length? Algorithmically,
speaking, this is the number of times we can divide the number by
10 before we get a number whose absolute value is less than 10. Use this
technique to implement the method.
main to
the IndexableInteger class). Be sure to test on
negative numbers and numbers that are between -10 and 10.
get mathematically
straightforward, but there are a few special cases to watch out
for.
Otherwise, you can "shift" off the rightmost digits by dividing by 10index (using integer division). The digit is the quotient modulo 10.
Hint: You may want to define a method powerOf10 (int n) that raises 10 to a specified poser, where n is an integer. (Do NOT use the java.lang.Math library, because the general pow method uses power series which are computationally expensive for integral powers.)
Now that you've had some practice with declaring interfaces and implementing some classes that make use of generics (even though these implementations are not generic at all!), now you can do some very generic things. (But don't tell your parents that you came to college to do generic things in class.)
TestIndexable.
main method of your class, create
an ArrayList of Indexable objects.
ArrayList.
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/207.sp012/labs/lab-generics.shtml
|
created 15 January 2009 by Jerod Weinman revised 10 February 2011 by Jerod Weinman revised 13 February 2012 by Henry M. Walker |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |