This project requires the use of the following Java program files:
|
Download them to your PC before doing this project.
Abide to the Emory Honor code when doing assignments.
In earlier lectures, you have also learned how to make a class Comparable by implementing the Comparable<T> interface (look in your notes or my notes if you need a refresher).
This assignment is a refresher on implementing the Comparable<T> interface and to solidify your knowlegde on inheritance.
Remember that if you want to invoke a method M() in the superclass. you must use:
super.M() |
You do so by changing the classes to implement the Comparable<T> interface
|
You are given the following Animal class:
public class Animal { private String name; private double weight; public Animal(String n, double w) { name = n; weight = w; } public double getWeight() { return weight; } } |
The Animal class has 3 subclasses: Dog. Cat and Bird. (Take a look inside their Java files, but you don't need to use Dog.java, Cat.java nor Bird.java in Phase A).
In Phase A, you must modify the Animal class so that it implements the Comparable<T> interface with a compareTo() method that compares 2 Animal objects
Furthermore, the compareTo() method must enforce the following ordering:
Bird < Cat < Dog |
In other words:
Suppose: Bird b = new Bird( ... ); Cat c = new Cat( ... ); Dog d = new Dog( ... ); Then: b.compareTo(b) returns 0 b.compareTo(c) returns a value < 0 b.compareTo(d) returns a value > 0 c.compareTo(c) returns 0 c.compareTo(b) returns a value > 0 c.compareTo(d) returns a value < 0 d.compareTo(d) returns 0 d.compareTo(b) returns a value > 0 d.compareTo(c) returns a value > 0 |
Using this compareTo() method, the sort algorithms will sort an array of Animal objects according to the type of animal
Example:
If the input is: [(dog1 6.0), (cat1 4.0), (dog2 7.0), (cat2 3.0), (bird1 5.0), (bird2 3.0)] The output can be: [(bird1 5.0), (bird2 3.0), (cat1 4.0), (cat2 3.0), (dog1 6.0), (dog2 7.0)] ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^ all birds first then all cats finally the dogs Within each type of animals, the animals need not be ordered by their weight |
When you finish with
Phase A, you can use
Test1A.java
and
Test2A.java
to test your code.
(Test1A.java
will run the BubbleSort on
the array of
Animals and
Test1A.java
will run the SelectionSort on
the array of
Animals)
Warning:
|
Modify the Cat, Dog and Bird classes so that they implement the Comparable<T> interface.
Furthermore, the compareTo() method must enforce the following ordering:
Bird,weight < Cat,weight < Dog,weight |
I.e.: the primary order is type of animal and within each type of animals, the animals must be ordered by their weight.
In other words:
Suppose: a[0] = new Dog("dog1", 6); // A gog that has weight 6 a[1] = new Cat("cat1", 4); a[2] = new Dog("dog2", 7); // A dog that has weight 7 a[3] = new Cat("cat2", 3); a[4] = new Bird("bird1", 5); a[5] = new Bird("bird2", 3); Then: For i = 0,1,2,3,4: a[5].compareTo(a[i]) returns a value < 0 For i = 0,1,2,3: a[4].compareTo(a[i]) returns a value < 0 For i = 0,1,2: a[3].compareTo(a[i]) returns a value < 0 For i = 0,2: a[1].compareTo(a[i]) returns a value < 0 For i = 2: a[0].compareTo(a[i]) returns a value < 0 |
Using this compareTo() method, the sort algorithms will sort an array of Animal objects according to the type of animal and within each type of animals, they will be ordered by their weight
Example:
If the input is: [(dog1 6.0), (cat1 4.0), (dog2 7.0), (cat2 3.0), (bird1 5.0), (bird2 3.0)] The output can be: [(bird2 3.0), (bird1 5.0), (cat2 3.0), (cat1 4.0), (dog2 6.0), (dog1 7.0)] ^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^ all birds first then all cats finally the dogs Birds ordered by weight Cats ordered by weight Dogs ordered by weight Within each type of animals, the animals must be ordered by their weight |
When you finish with
Phase B, you can use
Test1B.java
and
Test2B.java
to test your code.
(Test1B.java
will run the BubbleSort on
the array of
Animals and
Test1B.java
will run the SelectionSort on
the array of
Animals)
Warning:
|
You must be on Emory campus (e.g.: Emory unplugged) in order to turn in an assignment
Use your web browser and visit the cs171 turnin website: click here
|
Follow the instructions given in homework 1 to turn in.
You must be on Emory campus (e.g.: Emory unplugged) in order to make extension requests
Use your web browser and visit the cs171 turnin website: click here
Use assignment code hw9 to request extension for this homework.
Follow the instructions given in homework 1 to request extension.