If your group is joined by a fourth person, split into two groups of two.
svn+ssh://XXXXX@grid.cec.wustl.edu/project/cec/class/cse131/fall09/studios/studio7-ZZZZZZAfter pasting:
import lab4.Point;at the top of your ListOfPoints class, just after the package statement.
You can implement this class by deciding that it has-an instance of the LinkedList structure we discussed in class, which you are also using in lab 6. You will declare an instance variable of type LinkedList<Point> and implement your ADT using the LinkedList.
Is your instance variable of type LinkedList final or not? What do you expect Java to do if you make the list final? Try it and see if you're right. You won't know the answer to that until you add the methods described below.
Delegation makes your life easy using the CSE131 principle of reduction. You reduce the problem at hand to an implementation already provided. The two implementations are so similar, that you need only call the other to implement your version of the functionality.
toString
takes no parameters returns a String
representation of the list of points. For example, you might return
something like "( (0,4) (25,0) (50,8) )" as your result.
OK this one is on me, so I can illustrate delegation:
public String toString() { return list.toString(); // if list is the LinkedList instance variable }
size
takes no parameters and returns the
size of the list.
Now it is your turn to delegate. On the other hand, discuss as a group: how would you implement this method without using delegation? Take a look at the other methods that are in this ADT. How is the list of Points affected by those methods? How does the size of the list change? What would be required to keep track of the size of the list yourself?
isEmpty
returns true
if and only if the list contains no points.
What representation invariant should be true about this method and the size() method? In other words, no matter how you implement this class, what should the relationship be between those methods?
append
takes a Point as its parameter
and inserts the point at the end of the list.
If a method would otherwise bevoid
, it's a good idea sometimes to return this from that method. You should do that forappend
, but you should first understand why that's a convenient thing to do. Discuss this and ask the TAs if you're unclear.You can still delegate for this method, but you'll have to do some snooping, since LinkedList lacks an append method.
Try out your ADT by constructing an instance of it and appending some points. You can do this from the JUnit test you're developing. Which methods can you test at this point? Print out some of the lists and see if they look reasonable.
From this point forward, print and test as you go to make sure your methods do what they should.
iterator
returns an Iterator<Point> for
the list of points.
LinkedList already has an iterator that will do this job, so you
can just return it.
toArray
takes no parameters and returns
an array of Point objects, one for each Point in your list.
You are not allowed to use the LinkedList's toArray() method!
Instead,
review the lecture material on how to instantiate an array.
What is in the array after it's instantiated? How do you populate
the array with the contents of your list
(try to use your iterator
)?
ListOfPoints(Point[] points)
creates
a ListOfPoints using the supplied
an array of Point objects. The list is formed from the array, assuming
a Point object exists at each valid index.
Here Comes the List---(0,1)###(4,3)###---End of the listby not delegating. Instead, use the iterator to visit each Point in the list to build up the String you want to return.
minX
returns the least value of x in the
list. Use your iterator to do this.
maxX
returns the greatest value of x in the
list. Use your iterator to do this.
minY
returns the least value of y in the
list. Use your iterator to do this.
maxY
returns the greatest value of y in the
list. Use your iterator to do this.
If you got this far, that's awesome. The methods below are optional but please do them if you have time.
First a question: why did we bother to develop this ADT when we could have used LinkedList directly? To answer that question, think about the functionality ListOfPoints has that is lacking in LinkedList, and vica versa.
rescale
takes four doubles (x1,
y1, width, height)
.
The method should not modify the existing list, but instead
should create and return a new
ListOfPoints that results from rescaling all of the points in this
list so that they fit exactly within
the rectangle defined by the parameters to the method.
(If you do the extra-credit lab graphing option,
you will use this method to rescale the points to "fit" the size of
the canvas for graphing.) Create new rescaled points.
Don't modify the original points.
Hint: To find the x coordinates in the new list,
first subtract minX from the x value in the old list,
then multiply by the ratio (width / (maxX - minX)), and finally
add x1.
Treat each y coordinate similarly.
For example, suppose the list is
( (3,8) (4,5) (5,11) ).
Then rescale(0.0, 0.0, 50.0, 8.0)
would return the list
( (0,4) (25,0) (50,8) ).
What will happen in your implementation of this method
if the minimum and maximum x are equal?
clip
takes four integers (minX,
minY, maxX, maxY)
and returns a new ListOfPoints that
contains only those points in this list that fall within the rectangle
defined by the given parameters.