Dinosaurs! Design a class called Dinosaur. A dinosaur has a time when it lived,
ID: 3906904 • 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
import java.util.*;
class Dinosaur implements Comparable<Dinosaur>
{
private String time;
private int id;
public Dinosaur(String time,int id)
{
this.time = time;
this.id = id;
}
public int compareTo(Dinosaur d) // compare two Dinosaurs using their ids
{
if(this.id < d.id)
return -1;
else
return 0;
}
public String toString()
{
return "Id : "+id+ " Time Period : "+time;
}
}
class Saurischia extends Dinosaur
{
public Saurischia(String time,int id)
{
super(time,id);// send arguments to base class constructor
}
public String toString()
{
return "Saurischia -----"+super.toString();
}
}
class Theropod extends Saurischia
{
public Theropod(String time,int id)
{
super(time,id);
}
public String toString()
{
return "Theropod -----hollow bones and three-toed limbs ,insectivores to herbivores and carnivores "+super.toString();
}
}
class Sauropod extends Saurischia
{
public Sauropod(String time,int id)
{
super(time,id);
}
public String toString()
{
return "Sauropod -----long necks, long tails, small heads, and four thick, pillar-like legs feeding on plants "+super.toString();
}
}
class Ornithiscia extends Dinosaur
{
public Ornithiscia(String time,int id)
{
super(time,id);
}
public String toString()
{
return "Ornithiscia -----"+super.toString();
}
}
class Thyreophora extends Ornithiscia
{
public Thyreophora(String time,int id)
{
super(time,id);
}
public String toString()
{
return "Thyreophora -----armored dinosaurs , carnivorous "+super.toString();
}
}
class Cerapod extends Ornithiscia
{
public Cerapod(String time,int id)
{
super(time,id);
}
public String toString()
{
return "Cerapod -----bird-foot or fringed heads ,herbivores and carnivores "+super.toString();
}
}
class Test
{
public static void main (String[] args)
{
Dinosaur[] dinos = {
new Theropod("231.4 million years ago", 238),
new Sauropod("late Triassic Period", 339),
new Thyreophora("early Jurassic until the end of the Cretaceous", 158),
new Cerapod("early Jurassic", 139)
};
Arrays.sort(dinos); // sort all objects of dinos
for(Dinosaur d : dinos)
{
System.out.println(d );
}
}
}
Output:
Cerapod -----bird-foot or fringed heads ,herbivores and carnivores Ornithiscia -----Id : 139 Time Period : early Jurassic
Thyreophora -----armored dinosaurs , carnivorous Ornithiscia -----Id : 158 Time Period : early Jurassic until the end of the Cretaceous
Theropod -----hollow bones and three-toed limbs ,insectivores to herbivores and carnivores Saurischia -----Id : 238 Time Period : 231.4 million years ago
Sauropod -----long necks, long tails, small heads, and four thick, pillar-like legs feeding on plants Saurischia -----Id : 339 Time Period : late Triassic Period
Do ask if any doubt. Please upvote
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.