CS51 - Spring 2010 - Lecture 17
exercise 14.2.2
- provide a declaration for diamond, an array of four Lines needed to make the drawing. In your declaration, include an array construction.
- write the four assignment statements needed to create the diamond drawing and to associate these with the appropriate array entries. Assume the diamond has to just fit within a 200 by 200 pixel window.
- add additional lines to change the top two lines of the drawing red.
Write a function that given an array of ints, returns the largest number in the array
public int largestNumber(int[] numbers){
int largest = numbers[0];
for( int number: numbers ){
if( number > largest ){
largest = number;
}
}
}
show
TextStat demo
- String member method: split
- public String[] split(String splitString);
- We're going to write the four supporting methods to make this class
- private String getLongestWord(String[] words)
- private String getShortestWord(String[] words)
- private int getNumberOfWords(String[] words)
- private int getNumberOfCharacters(String[] words)
Show
DrawingPanelWithShapeArray demo
- Note how we can move things around and different objects are "in front" of other objects
- Hard to get this behavior with recursion
- We'll do this using arrays:
- we'll construct an array with some maximum size, call it MAX_OBJECTS
- private DrawableInterface[] shapes = new DrawableInterface[MAX_OBJECTS];
- initially, the array will be empty
- as we add things, add the objects to the array
- how can we do this?
- keep an integer (int numShapes) that tells us how many things are in the array so far
- How do we add a new item?
- construct the shap
- figure out the shape from the menu and create a new shape
- figure out the color from the menu and set color
- how do we add it to the shapes array?
- remember, we're using numShapes to keep track of how many items are already in the array
- set shapes[numShapes] to the new object
- increment numShapes
- how do we delete an item?
- figure out which object we clicked on
- remove it from the canvas
- remove it from our shapes array
- let's look at each of these in more detail
- how do we find out which object we clicked on?
// returns the index into shapes of the object that contains point
// or -1 if none contain that point
public int getIndexOf(Location point)
- how do we deal with overlapping items, i.e. what if we click and two items contain the point?
- where are we adding more recent items?
- to the end of the array
- therefore, items towards the end of the array are more recent
- look at getIndexOf method in
DrawingPanelWithShapeArray code
- start from the back not the front
- return the first one we find
- how do we remove something at a particular index from the shapes array?
- starting at that index, shift everything down
- set the last thing to null
- decrement numShapes
- look at removeEltWithIndex method
- How do we implement move?
- how do we normally move an object?
- onMousePress
- check if we clicked on the object
- set a boolean variable that we're dragging
- remember where the mouse was pressed
- onMouseDrag
- move the object the difference between where the mouse was previously and currently
- remember where the mouse was pressed
- how is this going to change?
- onMousePress
- iterate through all of the things in our array to see if any of them were clicked on
- rather than a boolean, we'll use a variable of type DrawableInterface to keep track of whether or not something is selected
- if null, then nothing was clicked on
- if non-null, then the variable has the object that was clicked on
- onMouseDrag
- doesn't change
- how does moving affect overlapping items?
- the challenge is that when we move something, we bring it to the front
- if we don't change our shapes array ordering, will it work properly?
- we're assuming things later on in the array are on top of those previous
- if we don't change the ordering, then this will not be true
- so, what do we need to do when we move an item?
- we need to move it to the end of the array!
- how?
- delete it using removeEltWithIndex
- add it back on to the end of the array
- look at selectShapeAt method
- How do we implement Recolor? (should be easy now)
- getIndexOf(point)
- recolor that objects
Other misc. array things
- what happens here?
int[] numbers = new array[10];
int currentIndex = 0;
...
currentIndex = 10;
numbers[currentIndex]++;
** ArrayOutOfBounds exception
- what happens here?
FramedRect[] rects = new FramedRect[10];
rect[0].move(10, 10);
** null pointer exception
- we've just created an array of FramedRect variables
- as like any other FramedRect variable, their initial value is null until you assign something to them