Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In a new Eclipse project, create the following: public abstract class Pet has th

ID: 3804411 • Letter: I

Question

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<Dog>
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

Pet.java:

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) {
       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();
}



Mobility.java:


public interface Mobility {
   public String move();
}



Dog.java:

import java.util.Date;

public class Dog extends Pet implements Mobility, Comparable<Dog> {
   private String breed;
   private int weight;

   public Dog(String breed, int weight, String name, char gender, Date acquired) {
       super(name, gender, acquired);
       this.breed = breed;
       this.weight = weight;
   }

   @Override
   public String move() {
       return "Dog Move";
   }

   @Override
   public String sound() {
       return "Dog Sound";
   }

   public String getBreed() {
       return breed;
   }

   public int getWeight() {
       return weight;
   }

   @Override
   public int compareTo(Dog dog) {
       return (dog.getWeight() > weight) ? -1
               : ((dog.getWeight() == weight ? 0 : 1));
   }

   public String toString() {
       return "Dog: " + getName() + ", Breeed: " + breed + ", Weight: "
               + weight + " Move: " + move() + " Sound: " + sound()
               + " Acquired On: " + getAcquired();
   }
}


Reptile.java:

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;
   }
  
   public String getType() {
       return type;
   }

   @Override
   public String move() {
       return "Reptile Move";
   }

   @Override
   public String sound() {
       return "Reptile Sound";
   }

   public String toString() {
       return "Reptile name = " + getName() + ", " + type + ", " + getGender() + " Move: " + move() + " Sound: " + sound()
               + " Acquired On: " + getAcquired();
   }

}


TestPet.java:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Random;


public class TestPet {

   public static void main(String[] args) throws ParseException {
       DateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
       Reptile reptile = new Reptile("Reptile", 'M', formatter.parse("12-02-2011"), "ReptileType");
       System.out.println(reptile);
      
       Random r = new Random();
       Dog[] dogs = new Dog[5];
       System.out.println(" Before Sorting");
       for(int i=0; i<5; i++) {
           dogs[i] = new Dog("Breed" + i, r.nextInt(100), "Dog " + i, 'M',formatter.parse("12-09-2006"));
           System.out.println(dogs[i]);
       }

       Arrays.sort(dogs);
       System.out.println(" After Sorting");
      
       for(int i=0; i<5; i++) {
           System.out.println(dogs[i]);
       }
      
   }

}


SampleOutput:

Reptile name = Reptile, ReptileType, M
Move: Reptile Move
Sound: Reptile Sound
Acquired On: Wed Jan 12 00:02:00 IST 2011

Before Sorting
Dog: Dog 0, Breeed: Breed0, Weight: 82
Move: Dog Move
Sound: Dog Sound
Acquired On: Thu Jan 12 00:09:00 IST 2006
Dog: Dog 1, Breeed: Breed1, Weight: 48
Move: Dog Move
Sound: Dog Sound
Acquired On: Thu Jan 12 00:09:00 IST 2006
Dog: Dog 2, Breeed: Breed2, Weight: 15
Move: Dog Move
Sound: Dog Sound
Acquired On: Thu Jan 12 00:09:00 IST 2006
Dog: Dog 3, Breeed: Breed3, Weight: 28
Move: Dog Move
Sound: Dog Sound
Acquired On: Thu Jan 12 00:09:00 IST 2006
Dog: Dog 4, Breeed: Breed4, Weight: 27
Move: Dog Move
Sound: Dog Sound
Acquired On: Thu Jan 12 00:09:00 IST 2006


After Sorting
Dog: Dog 2, Breeed: Breed2, Weight: 15
Move: Dog Move
Sound: Dog Sound
Acquired On: Thu Jan 12 00:09:00 IST 2006
Dog: Dog 4, Breeed: Breed4, Weight: 27
Move: Dog Move
Sound: Dog Sound
Acquired On: Thu Jan 12 00:09:00 IST 2006
Dog: Dog 3, Breeed: Breed3, Weight: 28
Move: Dog Move
Sound: Dog Sound
Acquired On: Thu Jan 12 00:09:00 IST 2006
Dog: Dog 1, Breeed: Breed1, Weight: 48
Move: Dog Move
Sound: Dog Sound
Acquired On: Thu Jan 12 00:09:00 IST 2006
Dog: Dog 0, Breeed: Breed0, Weight: 82
Move: Dog Move
Sound: Dog Sound
Acquired On: Thu Jan 12 00:09:00 IST 2006

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote