CISC181 S2015 Lab7

From class_wiki
Jump to: navigation, search

Preliminaries

  • Make a new project with n = 7 (following these instructions)
  • Name your main class "Lab7" (when creating a new module in the instructions above, in the Java class name field)
  • Modify Lab7.java by adding your name and section number in a comment before the Lab7 class body.

Part 1

Suppose you have a Pet class defined (in Lab7.java but outside of the Lab7 class) as follows:

class Pet {
  String name;
  Pet() { name = "Pet X"; }
  Pet(String name) { this.name = name; }
  void eat(String food) { System.out.println(name + " eats " + food); }
  void speak(String utterance) { System.out.println(name + " says " + utterance); }
}

Paste this into your Lab7.java, then write Dog and Fish subclasses of Pet (also outside Lab7). Do the following:

  • Have the Dog class override both the eat() and speak() methods by replacing "eats" and "says" with more species-appropriate (or at least different) verbs
  • Have the Fish class simply overload eat() and speak() with no-parameter versions.

Test by pasting the following method into the Lab7 class and calling it from main():

static void part1_test() {
    Fish f = new Fish();
    Dog d = new Dog("Rover");
    f.speak("glug glug");
    f.eat();
    d.speak("woof");
    d.eat("some chow");
 }

You will get an error on the line where the Dog object is initialized. Fix it by writing a constructor that uses super to invoke the correct Pet constructor.

Part 2

In Lab7.java (but outside your Lab7 class) define an abstract class Object3D containing the following:

  • An abstract method, computeVolume() that returns a double
  • A double field volume
  • A String field name
  • Override the toString() method to return the object name and volume with a space in between, followed by " cm^3"

Derive two concrete classes, Sphere and Box, from Object3D. Sphere should have a double field radius and Box should have double fields length, width, and height. Each derived class should have the following methods:

  • A constructor that takes the name of the object and the relevant dimensions as parameters, and sets the fields appropriately, including volume. Make sure that the constructor calls in the test function below work
  • computeVolume() with correct formulas

Now add the following method to your Lab7 class and call it from main():

static void part2_test_basic() {
    ArrayList<Object3D> objArray = new ArrayList<Object3D>();
    objArray.add(new Sphere("soccer ball", 22));
    objArray.add(new Sphere("basketball", 24.1));
    objArray.add(new Sphere("baseball", 7.4));
    objArray.add(new Sphere("cantaloupe", 10));
    objArray.add(new Sphere("golf ball", 2.1));
    objArray.add(new Sphere("zorb", 150));
    objArray.add(new Box("cheerios", 19, 7, 28));
    objArray.add(new Box("shoe", 29.5, 18, 9.5));
    objArray.add(new Box("tv", 154, 43, 104));
    objArray.add(new Box("shed", 304, 244, 274));
    objArray.add(new Box("jewelry", 29.2, 16.5, 8.9));
    for (Object3D obj : objArray) {
        System.out.println(obj);
    }
}

Create and call another test function part2_test_by_name() which is identical to the above, but just before the enhanced for loop add this line:

Collections.sort(objArray, new CompareByName());

Create and call another test function part2_test_by_volume() which is identical to the above, but just before the enhanced for loop add this line:

Collections.sort(objArray, new CompareByVolume());

Write classes CompareByName and CompareByVolume that implement the Comparator interface such that the Object3D objects in objArray are sorted from smallest to largest by name (aka alphabetically) and by volume, respectively. Add all import statements necessary for ArrayList, Comparator, Collections, etc.

Expected output

There are some "creative" choices here on how the various animals eat and speak, so feel free to substitute your own verbs and nouns.

Pet X says glug glug
Pet X eats tetramin
Rover barks woof
Rover gulps some chow

Part 2 basic (you don't have to have the 3-digit precision)

soccer ball 44602.238 cm^3
basketball 58632.679 cm^3
baseball 1697.398 cm^3
cantaloupe 4188.790 cm^3
golf ball 38.792 cm^3
zorb 14137166.941 cm^3
cheerios 3724.000 cm^3
shoe 5044.500 cm^3
tv 688688.000 cm^3
shed 20324224.000 cm^3
jewelry 4288.020 cm^3

Part 2 by volume

golf ball 38.792 cm^3
baseball 1697.398 cm^3
cheerios 3724.000 cm^3
cantaloupe 4188.790 cm^3
jewelry 4288.020 cm^3
shoe 5044.500 cm^3
soccer ball 44602.238 cm^3
basketball 58632.679 cm^3
tv 688688.000 cm^3
zorb 14137166.941 cm^3
shed 20324224.000 cm^3

Part 2 by name

baseball 1697.398 cm^3
basketball 58632.679 cm^3
cantaloupe 4188.790 cm^3
cheerios 3724.000 cm^3
golf ball 38.792 cm^3
jewelry 4288.020 cm^3
shed 20324224.000 cm^3
shoe 5044.500 cm^3
soccer ball 44602.238 cm^3
tv 688688.000 cm^3
zorb 14137166.941 cm^3

Submission

Submit your Lab7.java on Sakai. Be sure to add your name and section number.