Holodeck Design Project
From Huben's Wiki
(Created page with "It is a little known fact that the Holodeck is programmed in Java. You need to develop a new holodeck simulation that has 5 different classes with particular methods. {| border...") |
|||
Line 1: | Line 1: | ||
It is a little known fact that the Holodeck is programmed in Java. You need to develop a new holodeck simulation that has 5 different classes with particular methods. | It is a little known fact that the Holodeck is programmed in Java. You need to develop a new holodeck simulation that has 5 different classes with particular methods. | ||
− | {| border="1" align="center" | + | {| border="1" |
− | + | |- align="center" | |
! class | ! class | ||
! graze() | ! graze() | ||
! fly() | ! fly() | ||
− | ! | + | ! ears() |
! hooves() | ! hooves() | ||
! flame() | ! flame() | ||
− | |- | + | |- align="center" |
− | ! Horse | + | ! Horse |
− | |- | + | | X || || || X || |
− | ! Elephant | + | |- align="center" |
− | |- | + | ! Elephant |
− | ! Pegasus | + | | X || || X || || |
− | |- | + | |- align="center" |
− | ! Dumbo | + | ! Pegasus |
− | |- | + | | X || X || || X || |
− | ! Dragon | + | |- align="center" |
+ | ! Dumbo | ||
+ | | X || X || X || || | ||
+ | |- align="center" | ||
+ | ! Dragon | ||
+ | | || X || || || X | ||
|} | |} | ||
+ | |||
+ | * You will create these 5 classes. | ||
+ | * You will create super classes and/or interfaces as needed so that you could use polymorphism for the methods that are present in more than one class. | ||
+ | * To keep it simple, all methods have no arguments and return strings with the class name and the method name, such as "Dragon flame". If a method is inherited, you can just use the inherited method: no need to override the method. | ||
+ | * None of your classes should have methods that are not checked: Dragon should not have a graze() method. | ||
+ | * Use the following class to see your results. | ||
+ | |||
+ | <pre> | ||
+ | public class Holodeck | ||
+ | { | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | Horse h = new Horse(); | ||
+ | Elephant e = new Elephant(); | ||
+ | Pegasus p = new Pegasus(); | ||
+ | Dumbo d = new Dumbo(); | ||
+ | Dragon r = new Dragon(); | ||
+ | |||
+ | // Replace the XXX with the appropriate class or interface for each line. | ||
+ | // The line should compile and the print() later on should not have errors either. | ||
+ | // XXX should be a type that gives a method such as fly(). | ||
+ | // This should demonstrate polymorphism: each object knows its own methods. | ||
+ | XXX[] eared = {e, d}; // classes with ears() | ||
+ | XXX[] hooved = {h, p}; // classes with hooves() | ||
+ | XXX[] grazers = {h, e, p, d}; // classes with graze() | ||
+ | XXX[] fliers = {p, d, r}; // classes with fly() | ||
+ | |||
+ | System.out.print("ears(): "); | ||
+ | for (int i = 0; i < eared.length; i++) | ||
+ | System.out.print(eared[i].ears() + " "); | ||
+ | System.out.println(); | ||
+ | |||
+ | System.out.print("hooves(): "); | ||
+ | for (int i = 0; i < hooved.length; i++) | ||
+ | System.out.print(hooved[i].hooves() + " "); | ||
+ | System.out.println(); | ||
+ | |||
+ | System.out.print("graze(): "); | ||
+ | for (int i = 0; i < grazers.length; i++) | ||
+ | System.out.print(grazers[i].graze() + " "); | ||
+ | System.out.println(); | ||
+ | |||
+ | System.out.print("fly(): "); | ||
+ | for (int i = 0; i < fliers.length; i++) | ||
+ | System.out.print(fliers[i].fly() + " "); | ||
+ | System.out.println(); | ||
+ | } | ||
+ | } | ||
+ | </pre> |