Requirements In a new Eclipse project, create the following: public abstract cla
ID: 3804395 • Letter: R
Question
Requirements In a new Eclipse project, create the following: public abstract class Pet has these private attributes name : String gender : char acquired : Date has these public methods one constructor getName() getGender() getAcquired() abstract method sound() that returns a String public interface Mobility has one abstract method move() that returns a String concrete class Dog that extends Pet and implements Mobility and Comparable has these private attributes breed : String weight : int has these public methods one constructor getBreed() getWeight() sound() move() compareTo(Dog arg0) that compares dogs by weight toString() that returns a string fully describing a Dog instance concrete class Reptile that extends Pet and implements Mobility has one private attribute type : String has these public methods one constructor getType() sound() move() toString() that fully describes a Reptile instance executable class TestPet creates at least one Reptile pet and displays it creates an array of at least four Dog pets sorts the array of Dogs by weight uses a foreach loop to fully display all data for all dogs sorted by weight (see sample output) Sample Output Reptile name = Slinky, rock python, M Must be caged, crawls or slithers Sound Does not make a sound, acquired Fri Feb 03 17:06:54 EST 2017 Dog name = Pedro, chihuahua, M Walks on a leash, weight 14 Makes sound bark woof!, acquired Fri Feb 03 17:06:54 EST 2017 Dog name = Marley, pug, M Walks on a leash, weight 20 Makes sound bark woof!, acquired Fri Feb 03 17:06:54 EST 2017 Dog name = Sacha, beagle, F Walks on a leash, weight 25 Makes sound bark woof!, acquired Fri Feb 03 17:06:54 EST 2017 Dog name = Butch, Alsatian, M Walks on a leash, weight 90 Makes sound bark woof!, acquired Fri Feb 03 17:06:54 EST 2017
Explanation / Answer
Create a package called animal and copy the following files
---------------------------------------------------------------------------------------
package animal;
import java.util.Date;
public abstract class Pet {
private String name;
private char gender;
private Date acquired;
public Pet(String name, char gender, Date acquired) {
super();
this.name = name;
this.gender = gender;
this.acquired = acquired;
}
public String getName() {
return name;
}
public char getGender() {
return gender;
}
public Date getAcquired() {
return acquired;
}
public abstract String sound();
}
---------------------------------------------------------------------------------------
package animal;
public interface Mobility {
public String move();
}
----------------------------------------------------------------------------------------
package animal;
import java.util.Date;
public class Dog extends Pet implements Mobility, Comparable<Dog>{
private String breed;
private int weight;
public Dog(String name, char gender, Date acquired, String breed, int weight) {
super(name, gender, acquired);
this.breed = breed;
this.weight = weight;
}
public String getBreed() {
return this.breed;
}
public int getWeight() {
return this.weight;
}
@Override
public String move() {
return "walks on a leash";
}
@Override
public String sound() {
return "bark woof!";
}
@Override
public int compareTo(Dog dog) {
if (dog == null || this.weight > dog.weight) {
return 1;
}
else if (dog.weight == this.weight) {
return 0;
}
else {
return -1;
}
}
@Override
public String toString() {
return "Dog name = " + getName() + ", breed = " + getBreed() + ", gender = " + getGender() + ", move = " + move()
+ ", weight = "+ getWeight() + ", sound " + sound() + ", acquired " + getAcquired();
}
}
----------------------------------------------------------------------------------------
package animal;
import java.util.Date;
public class Reptile extends Pet implements Mobility{
private String type;
public Reptile(String name, char gender, Date acquired, String type) {
super(name, gender, acquired);
this.type = type;
}
@Override
public String move() {
return "crawls or slithers";
}
@Override
public String sound() {
return "none";
}
public String getType() {
return this.type;
}
@Override
public String toString() {
return "Reptile name = " + getName() + ", type = " + getType() + ", gender = " + getGender() + ", move = " + move()
+ ", sound = " + sound() + ", acquired " + getAcquired();
}
}
----------------------------------------------------------------------------------------
package animal;
import java.util.Arrays;
import java.util.Date;
public class TestPet {
public static void main(String args[]) {
Reptile reptile = new Reptile("Slinky", 'M', new Date(), "Rock Python");
System.out.println(reptile.toString());
Dog[] dogArray = new Dog[4];
dogArray[0] = new Dog("Sacha", 'F', new Date(), "beagle", 25);
dogArray[1] = new Dog("Butch", 'M', new Date(), "Alsatian", 90);
dogArray[2] = new Dog("Pedro", 'M', new Date(), "chihuahua", 14);
dogArray[3] = new Dog("Marley", 'M', new Date(), "pug", 20);
Arrays.sort(dogArray);
for (int i=0; i<dogArray.length; i++) {
System.out.println(dogArray[i].toString());
}
}
}
---------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.