CS-2510 Lab 7

Purpose:

To practice the design of 'stateful' classes with mutation, void methods and testing, and using ArrayLists.




Part 1: Insertion Sort in Java

You all remember insertion-sort from last semester right? Well, as practice designing methods we're going to practice sorting some more.

Exercise 1:

First we need an interface to define how to order our elements (in Racket we would just pass a function, right?).

Design an interface, IComp<X> that contains a comprison method (e.g., boolean lessthan(X a, X b);)

Make sure you understand what the function-interface means and what it represents; give it a good purpose statement.

Note: This is almost exactly how the Java library implements functions used to sort. The only difference is that they use an integer output (-1 for less than, 0 for equal, 1 for greater than). You should be able to use this later for sorting arbitrary objects.

Exercise 2:

Design a class that implements IComp<Integer>, comparing Integers in some order (e.g., using <=)

Test it to make sure you know what it's supposed to do.

Exercise 3:

Design parameterized classes to represent arbitrary Lists. You've been working on these for HW, so feel free to use those... but more practice is always better. Or you can look at the Java standard library way to do this, with LinkedLists. (If you do it this way, you will still need to extend the LinkedList for the next Exercise).

Exercise 4:

Design a method insert that inserts a given X into this List, in the order defined by a given IComp<X>. Hint: if the comparison says true, then the first argument belongs before the second.

Proceed case by case within your List classes.

Exercise 5:

Using your insert method, implement a sort method that sorts this List, in the order defined by a given IComp<X>.

Look at one of the implementations in HtDP if you get confused. You can return a new list if you need, but you can also try to do this destructively (i.e. by modifying the current list).

Exercise 6:

Test your sort method by sorting a List<Posn> by x. (Hint: You'll need a new class that implements IComp).

Then make another test that sorts a List<Posn> by y.




Part 3: More on Using ArrayLists; Using VoidWorld (SpongeBob)

We'll probably be using ArrayList throughout the rest of the semester, so it's good to familiarize yourself with the documentation (JavaDoc) site. All the standard Java classes are included from the main site. Feel free to poke around for interesting stuff.

We also introduce a new world, VoidWorld, which is a version of our World that allows for mutation. For VoidWorld, there's documentation on the JavaWorld site, and the documentation for all the world/image libraries is located here.

By the way, you're welcome to use any other (class appropriate) image you find from the internet if you'd like: puppies, pancakes, unicorns, transformers... You could even have a random number component that decides between a few different images.

Exercise 7:

Design a class to represent SpongeBobs. As is typical with the labs, each SpongeBob needs a location (x/y) and (vx/vy). In addition (this one will be cooler), we need to track the angle (ang) and rotational velocity (vr).

Just so I can give you suggestions later, your constructor should take 6 doubles:
SpongeBob(double x, double y, double vx, double vy, double ang, double vr){ ... }
            
It's long, but the end result will be worth it!

Exercise 8:

Create a convenient constructor that initializes a SpongeBob given only x and y. Give SpongeBob random x/y velocities (make the vy start negative, so we get a nice toss effect). Starts him at an angle of 0, with a random rotational velocity (+ or -).

You can use Math.random() to create random numbers in [0..1], which you can multiply by some scale. If you want a balance of neg/pos numbers, you can subtract 0.5 first.

Here are some values that will make it look pretty cool:

SpongeBob(double x, double y){
    this(x, y,
         10*(Math.random()-0.5),
         -15*Math.random()-10,
         0,
         20*(Math.random()-0.5));
}
            

Exercise 9:

Design a void step() method for your SpongeBob class that moves and rotates him in the direction of his velocities, and applies gravity to the vy component (subtracting 2 or 3 should do).

All this is done using assignment statments. Test this method.

Exercise 10:

Design a class that extends VoidWorld containing an ArrayList of SpongeBobs. Instead of passing the list to the constructor, make only a default (no argument) constructor that initializes the list to an empty ArrayList.

Exercise 11:

Design a method stepAll that goes through the ArrayList and calls step on all the SpongeBobs. How you implement it is up to you; just make it work correctly.

Exercise 12:

Design a method drawAll that goes through the ArrayList and accumulates the images of each SpongeBob into the given Scene. You can use the image below:

You'll want a helper in the SpongeBob class that places him into the given Scene... Don't forget to rotate the image by his current angle before placing it!

Exercise 13:

Design a void method to remove SpongeBobs from the list if they fall below the height of the screen (above, left, or right is fine).

Hint: you'll want to use remove, just be careful when you recur through the list, since removing one element causes all the later indexes to be shifted (E.g. removing element #5 means that the new 5th element is what used to be the 6th, if it existed).

Note: We discussed in class how inefficient this is. Think about what would be a more efficient way to manage your Bobs. You don't actually have to change your code, but for now, just be aware of problems like this.

Exercise 14:

Hook up a public Scene onDraw(), a public void onTick() method, and add the following mouse handler:

public void onMouse(int x, int y, String me){
   if(me.equals("button-down")){
      this.bobs.add(new SpongeBob(x, y));
   }
}
              
Assuming that your ArrayList field is called bobs.

Exercise 15:

Run your program, click the mouse, and watch Sponge bob spin off the screen!!

If you really want to have fun, change the "button-down" to "drag". Here's my example:

You could also modify your step function to make him bounce off the floor (note that you'll need knowledge of the canvas size). You would also want to delete your bottom check from the remove method. Now you have Sponge bob on a trampoline (or your unicorns and transformers).