Question: JAVA Programming The second argument to the constructors is the ordering key. For Dog use the positive value, for Cat use the negative value. This

JAVA Programming

The second argument to the constructors is the ordering key. For Dog use the positive value, for Cat use the negative value. This defines your compareTo()

I'm struggling with adding a compareTo() into dog and cat, dog needs to return 1 and cat needs to return -1

the orderingKey defines the compareTo()

This is the Desired output:

JAVA Programming The second argument to the constructors is the ordering key.

ABSOLUTELY DO NOT MODIFY THE AssignmentTwo Class, this class must be used as is

AssignmentTwo:

import java.util.ArrayList; import java.util.Collections; import java.util.List;

public class AssignmentTwo {

public static void main(String[] args) { Cat cat1 = new Cat("Fluffy", 150); Cat cat2 = new Cat("Sweety", 200, "yellow"); Dog dog1 = new Dog("Duke", 10, 500); Dog dog2 = new Dog("Jason", 5); List animals = new ArrayList(); animals.add(cat1); animals.add(cat2); animals.add(dog1); animals.add(dog2); printAll("Before Sort", animals); Collections.sort(animals); printAll("After Sort", animals); System.out.println(""); for(Animal animal : animals) { System.out.println(" Animal " + animal + " is a " + animal.getType() + " speaks by " + animal.speaksBy()); animal.store(); animal.load(); } } private static void printAll(String title, List animals) { System.out.println(" " + title); for(Animal animal : animals) System.out.println(animal); }

}

Animal:

public abstract class Animal implements Storable, Talkable {

String name;

int orderingKey; public int getKey() { return orderingKey; }

Animal(String name, int orderingKey) {

this.name = name;

this.orderingKey = orderingKey;

} }

Dog:

class Dog extends Animal implements Talkable, Storable { double weight; Dog(String name, int orderingKey) { super(name, orderingKey); this.weight = 100.0; } Dog(String name, int orderingKey, float weight) { super(name, orderingKey); this.weight = weight; }

public String getType() { return "Doggie"; }

public String speaksBy() { return "barking"; }

public void store() { System.out.println(this + "being stored in coach"); }

public void load() { System.out.println(this + "being loaded from kennel"); }

}

Cat:

class Cat extends Animal implements Talkable, Storable {

String color;

Cat(String name, int orderingKey) { super(name, orderingKey); this.color = "pink"; } Cat(String name, int orderingKey, String color) { super(name, orderingKey); this.color = color; }

public void store() { System.out.println(this + "being stored in first class"); }

public void load() { System.out.println(this + "being loaded into wooden box"); }

public String getType() { return "Kitty"; }

public String speaksBy() { return "meowing"; }

}

Storable:

interface Storable {

void store();

void load();

}

Talkable:

interface Talkable {

String getType();

String speaksBy();

}

output v Before Sort (Fluffy, 150), color = pink (Sweety, 200), color = yellow (Duke, 10), weight = 500.0 (Jason,5), weight = 100.0 After Sort (Sweety, 200), color = yellow (Fluffy, 150), color = pink (Jason,5), weight = 100.0 (Duke, 10), weight = 500.0 Animal (Sweety, 200), color = yellow is a kitty speaks by meowing (Sweety, 200), color = yellow being stored in first class (Sweety, 200), color = yellow being loaded into wooden box Animal (Fluffy, 150), color = pink is a kitty speaks by meowing (Fluffy, 150), color = pink being stored in first class (Fluffy, 150), color = pink being loaded into wooden box Animal (Jason,5), weight = 100.0 is a Doggie speaks by barking (Jason,5), weight = 100.0 being stored in coach (Jason,5), weight = 100.0 being loaded from kennel Animal (Duke, 10), weight = 500.0 is a Doggie speaks by barking (Duke, 10), weight = 500.0 being stored in coach (Duke, 10), weight = 500.0 being loaded from kennel output v Before Sort (Fluffy, 150), color = pink (Sweety, 200), color = yellow (Duke, 10), weight = 500.0 (Jason,5), weight = 100.0 After Sort (Sweety, 200), color = yellow (Fluffy, 150), color = pink (Jason,5), weight = 100.0 (Duke, 10), weight = 500.0 Animal (Sweety, 200), color = yellow is a kitty speaks by meowing (Sweety, 200), color = yellow being stored in first class (Sweety, 200), color = yellow being loaded into wooden box Animal (Fluffy, 150), color = pink is a kitty speaks by meowing (Fluffy, 150), color = pink being stored in first class (Fluffy, 150), color = pink being loaded into wooden box Animal (Jason,5), weight = 100.0 is a Doggie speaks by barking (Jason,5), weight = 100.0 being stored in coach (Jason,5), weight = 100.0 being loaded from kennel Animal (Duke, 10), weight = 500.0 is a Doggie speaks by barking (Duke, 10), weight = 500.0 being stored in coach (Duke, 10), weight = 500.0 being loaded from kennel

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!