Create a file called
assign6.txt
. You will use this file to paste your code and answer written questions. Please clearly label each section.If you have any questions regarding the assignment or see any errors in the assignment writeup, please contact me.
🎉 Congratulations!🎉 At this point in the course, you've fully learned the four main principles of Object Oriented Programming: encapsulation, abstraction, inheritance, and polymorphism.
This week, we learned about inheritance and polymorphism. Moreover, our look into abstract classes and interfaces gives us a better understanding of abstraction and its uses. This assignment will test your comprehension of those topics and features of Java as an OOP language and help you attain a better sense of when to use each type of structure.
List of Important Concepts:
Resources:
Describe the differences between a class, an abstract class, and an interface.
What are some situations that upcasting would be useful? What about downcasting?
When is the @Override
annotation needed?
List one concrete example of when an abstract class would be used as opposed to an interface and vice versa.
Examine the following code.
xpublic abstract class Car {
int speed = 0;
String color;
Car(String color) {
this.color = color;
}
public void turnSignal(String direction) {
System.out.println("Turning " + direction + ".");
}
public abstract void drive();
}
public interface RepairableObject {
public void destroy();
public void repair();
}
public interface Commandable {
public void inputCommand(String input);
public String getLatestOutput();
}
public class Ferrari extends Car implements RepairableObject, Commandable;
For which functions is the class Ferrari
required to provide its own implementation?
Write your answers in
assign6.txt
This assignment will be much more open-ended than previous assignments so that you can fully exercise the software engineering skills you have gained over the past weeks. Although you still have to complete the required portions of the assignment, I highly recommend you to be as creative with them as possible, attempting to mold your code to something that would be truly applicable in the real world.
Something to think about while completing the assignment: most of the time, the difficulty doesn't come from writing the code itself, but being able to design an effective and useful system before you even touch the keyboard. Planning beforehand decides if your project will be organized and efficient or if it will be a heap of spaghetti code.
After this assignment, you will work on a final CSAlpha project for approximately three weeks. That project requires your utmost creativity and effort, using all the concepts that we have learned thus far. I advise you to give your best shot on this assignment in preparation for that project.
Your job this time is to create a suite of classes that represents a small hierarchy of objects branching from one main abstract object. The title of this assignment is "Zoology Classification", but you can pick any theme you would like. In the next milestones, I will give some examples for the default theme, but you can adjust those examples to your own theme. Be creative!
Example: The theme is animals.
At the top of your hierarchy is one abstract class that will help group these closely related classes and objects together. The reason why we want an abstract class here is because we want to implement some functions but want to require subclasses to implement others.
Below are some requirements for your base abstract class:
Example:
Now you will flesh out your hierarchy by adding many child classes extending the abstract parent class.
Below are some requirements for your children classes:
Example: Some child classes of Animal
could be Pig
, Horse
, etc.
Although you have already provided functions and fields that are shared among all these subclasses, perhaps you want to add more functionality that is not strictly for them. For example, the Cloneable
interface. Although you may want all animals in the zoo to share the ability to create copies of themselves, that's not the only group of classes that may want the ability to clone; it may be useful to cars, people, etc. This new functionality should be created using an interface to emphasize that very point.
Note that if your abstract parent class implements an interface, the subclasses will be required to implement the declared functions of the interface. This is helpful because the subclasses won't need to independently implement the interface.
Below are some requirements for your interface:
Example: Communicate
, an interface that allows you to send and receive messages from the object,
Please define an interface different from the example one.
After you've finished the code for all your subclasses, it's time to make a small driver to test all these parts in conjunction. If you've been doing some testing after each milestone, this section will not be difficult.
Here are the steps for the creation of your driver, which you will be placing in the Main
class main method:
Create five objects corresponding to your five subclasses and place them in an array using upcasting (i.e. in an array with a type of the abstract parent class)
Loop through the array and do three things for each iteration:
Inside of your loop, check to see if one the objects matches one of the special subclasses using the instanceof
keyword.
Example: if (animal instanceof Pig)
For this iteration, call the special function of that particular object or print out its special field.
Amazing job! You went from writing tiny programs to creating a complex system/hierarchy that has real-world application. Be proud of yourself and all that you have achieved in this course!
Paste your code in
assign6.txt
The driver surely tests if your code is working, but it doesn't make use of the hierarchy as much as it could. For this extension, you will be creating a separate class to manage a 'zoo' of all objects of your subclasses and make it easier to use your hierarchy . The Zoo
class and its requirements refer to the animals theme example, but call your management class and its fields and functions according to the theme you picked.
Here are the required attributes for this extension class:
You must maintain an array of animals of length 100 that is modified according to calls of the addAnimal
and removeAnimal
functions detailed below. You may need auxiliary variables for those functions to interact with the array properly.
addAnimal(Animal animal)
: This function appends the parameter to the array of animals. If there is no space for another animal in the array, do not add the animal and print an error message.
removeAnimal(int index)
: This function removes an animal at the specified index. You may have to shift the animals in the array after removal to ensure that all the animals are in one contiguous section at the start of the array. If there is no animal at the specified index, do not remove anything and print an error message.
printAnimalInfo(int index)
: This function prints out the info of an animal at the specified index using the following format:
xxxxxxxxxx
Animal: (name of subclass)
(name of field1): (value of field1)
(name of field2): (value of field2)
(name of field3): (value of field3)
Available Functions:
(name of function1)
(name of function2)
...
Note that the number of available functions will depend on your implementation. If there is no animal at the specified index, print an error message.
printAnimalList()
: This function prints the entire array in the following format:
xxxxxxxxxx
0: (name of subclass)
1: (name of subclass)
2: (name of subclass)
...
runAnimalFunction(int index, String functionName)
: This function runs the specified function of the animal at the specified index in the array. If there is no animal at the specified index or the function is not available, print an error message and do not run any function.
startConsole()
: This function starts a dialog within the console to manage the Zoo
class. The function must have the following properties:
Zoo
class array using the command "add (name of subclass)"Zoo
class array using the command "remove (index)".The split(String regex)
function of Strings will be helpful, along with the static function Integer.valueOf(String str)
.
You can call startDialog()
from the main method in the Main
class to test out your management class, or you can call the functions directly to test small parts at first.
You may have to hardcode much of the branching logic for identifying the animal subclass and running functions. However, if you find a way to do it in a cleaner way, props to you! That will require some knowledge of data structures and a few other topics outside the scope of this class.
Paste your code in
assign6.txt
Create another hierarchy or make a simple game that employs concepts of inheritance and polymorphism.