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

Dinosaurs! Design a class called Dinosaur. A dinosaur has a time when it lived,

ID: 3906876 • Letter: D

Question

Dinosaurs! Design a class called Dinosaur. A dinosaur has a time when it lived, and an ID (an integer that represents each dinosaur..  Dinosaur should implement comparable and compare their IDs.

Saurischia  and Ornithiscia  are subclasses of Dinosaur. Theropod and Sauropod are subclasses of Saurischia. Thyreophora and Cerapod are subclasses of Ornithiscia. Override the toString method to give a description of the dinosaur (its parent classes and a notable fact).

Create a driver program that instantiates an array of at least one Theropod, Sauropod, Thyreophora, and Ceropod, sort and output the array so each dinosaur is sorted by ID and outputs their ID, description, notable fact and what they eat.

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

// Dinosaur.java

public class Dinosaur implements Comparable<Dinosaur> {

                //attributes

                private String timeLived;

                private int ID;

                //constructor

                public Dinosaur(String timeLived, int iD) {

                                this.timeLived = timeLived;

                                ID = iD;

                }

               

                //getters and setters

               

               

                public String getTimeLived() {

                                return timeLived;

                }

                public void setTimeLived(String timeLived) {

                                this.timeLived = timeLived;

                }

                public int getID() {

                                return ID;

                }

                public void setID(int iD) {

                                ID = iD;

                }

                @Override

                public int compareTo(Dinosaur d) {

                                //compares by id

                                if (this.ID < d.ID) {

                                                return -1;

                                } else {

                                                return 1;

                                }

                }

                @Override

                public String toString() {

                                //returning id and time lived

                                return "ID: " + ID + ", time lived: " + timeLived;

                }

}

// Saurischia.java

public class Saurischia extends Dinosaur {

                public Saurischia(String timeLived, int iD) {

                                //passing values to super class

                                super(timeLived, iD);

                }

                @Override

                public String toString() {

                                return "ID: "

                                                                + getID()

                                                                + ", time lived: "

                                                                + getTimeLived()

                                                                + ", ["

                                                                + getClass().getName()

                                                                + "], notable fact: 'reptile-hipped', food type: carnivorous, parent class: "

                                                                + getClass().getSuperclass().getName();

                }

}

// Ornithiscia.java

public class Ornithiscia extends Dinosaur {

                public Ornithiscia(String timeLived, int iD) {

                                //passing values to super class

                                super(timeLived, iD);

                }

                @Override

                public String toString() {

                                return "ID: "

                                                                + getID()

                                                                + ", time lived: "

                                                                + getTimeLived()

                                                                + ", ["

                                                                + getClass().getName()

                                                                + "], notable fact: 'bird-hipped', food type: Omnivorous, parent class: "

                                                                + getClass().getSuperclass().getName();

                }

}

// Theropod.java

public class Theropod extends Saurischia {

                public Theropod(String timeLived, int iD) {

                                //passing values to super class

                                super(timeLived, iD);

                }

                @Override

                public String toString() {

                                return "ID: " + getID() + ", time lived: " + getTimeLived() + ", ["

                                                                + getClass().getName()

                                                                + "], notable fact: 'hollow bones and three-toed limbs'"

                                                                + ", food type: carnivorous, parent class: "

                                                                + getClass().getSuperclass().getName();

                }

}

// SauroPod.java

public class SauroPod extends Saurischia {

                public SauroPod(String timeLived, int iD) {

                                //passing values to super class

                                super(timeLived, iD);

                }

                @Override

                public String toString() {

                                return "ID: " + getID() + ", time lived: " + getTimeLived() + ", ["

                                                                + getClass().getName() + "], notable fact: 'very long necks', "

                                                                + "food type: herbivorous, parent class: "

                                                                + getClass().getSuperclass().getName();

                }

}

// Thyreophora.java

public class Thyreophora extends Ornithiscia {

                public Thyreophora(String timeLived, int iD) {

                                //passing values to super class

                                super(timeLived, iD);

                }

                @Override

                public String toString() {

                                return "ID: " + getID() + ", time lived: " + getTimeLived() + ", ["

                                                                + getClass().getName()

                                                                + "], notable fact: 'presence of body armor', "

                                                                + "food type: herbivorous, parent class: "

                                                                + getClass().getSuperclass().getName();

                }

}

// Cerapod.java

public class Cerapod extends Ornithiscia {

                public Cerapod(String timeLived, int iD) {

                                //passing values to super class

                                super(timeLived, iD);

                }

                @Override

                public String toString() {

                                return "ID: "

                                                                + getID()

                                                                + ", time lived: "

                                                                + getTimeLived()

                                                                + ", ["

                                                                + getClass().getName()

                                                                + "], notable fact: 'thicker enamel on the inside of their lower teeth',"

                                                                + " food type: herbivorous, parent class: " + getClass().getSuperclass().getName();

                }

}

// Driver.java

import java.util.Arrays;

public class Driver {

                public static void main(String[] args) {

                                /**

                                * Creating an array of Dinosaurs and initializing with different types

                                * of dinos

                                */

                                Dinosaur dinos[] = new Dinosaur[4];

                                dinos[0] = new Theropod("250 million years ago", 72);

                                dinos[1] = new Cerapod("240 million years ago", 12);

                                dinos[2] = new SauroPod("300 million years ago", 34);

                                dinos[3] = new Thyreophora("200 million years ago", 99);

                                /**

                                * sorting by id

                                */

                                Arrays.sort(dinos);

                               

                                /**

                                * displaying each of them

                                */

                                for(int i=0;i<dinos.length;i++){

                                                System.out.println(dinos[i]);

                                }

                }

}

/*OUTPUT*/

ID: 12, time lived: 240 million years ago, [Cerapod], notable fact: 'thicker enamel on the inside of their lower teeth', food type: herbivorous, parent class: Ornithiscia

ID: 34, time lived: 300 million years ago, [SauroPod], notable fact: 'very long necks', food type: herbivorous, parent class: Saurischia

ID: 72, time lived: 250 million years ago, [Theropod], notable fact: 'hollow bones and three-toed limbs', food type: carnivorous, parent class: Saurischia

ID: 99, time lived: 200 million years ago, [Thyreophora], notable fact: 'presence of body armor', food type: herbivorous, parent class: Ornithiscia

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