In: Computer Science
Your project MUST do exactly what I am asking for. Read very carefully.
I will take point off. Show the output that I ask for in the text file and upload the file. When I run your project, if it does not match the given out put you get Zero.
In addition, any two project that copies from each other get zero credit and report to
the SMC disciplinary department.
Advanced Java Programming
Polymorphism, Class Design, Inheritance
Assignment:
You are to create seven classes that represent a Zoo. The classes are defined as follows:
Required member variables:
private String name;
private String address;
private Enclosure[] enclosures;
private int area;
private double budget;
Required member variables:
private String biome;
private Animal[] animals;
Required member variables:
private String name;
private String genus;
private String species;
private Zoo currentZoo;
protected Animal enemy;
protected String prefferedBiome;
For classes 4-7, there are no required member variables.
All the classes must have getters and setters for each of their member variables. All of the classes must have overridden toString() and equals(...) functions. All classes must be in separate files. Crocodile, Gazelle, Lion, and Zebra are Animals. Class Animal must implement this interface:
interface Feedable {
void getFeedingInstructions();
void feed();
}
All the child classes of Animal must have its own overridden getFeedingInstructions(); and feed(); functions.
The classes need to be implemented in a way that makes this main function:
public static void main(String[] args) {
//Creating a new Zoo.
Zoo laZoo = new Zoo("Los Angeles Zoo", "5333 Zoo Dr, Los Angeles, CA 90027", 133, 0);
//Creating two different Enclosures.
Enclosure firstEnclosure = new Enclosure("Savanna");
Enclosure secondEnclosure = new Enclosure("River");
//Creating our animals.
Zebra zeb = new Zebra("Zebby");
Gazelle gaz = new Gazelle("Gaz");
Crocodile croc = new Crocodile("Gena");
Lion leo = new Lion("Leo");
//Trying to add croc to the firstEnclosure, but Crocodiles can't live in the Savannah, so it prints an error message.
firstEnclosure.addAnimal(croc);
//Adding leo to the first enclosure.
firstEnclosure.addAnimal(leo);
//Adding croc to the second enclosure.
secondEnclosure.addAnimal(croc);
//Trying to add zeb to the secondEnclosure, but Crocodiles and Zebras are enemies, so it prints an error message.
secondEnclosure.addAnimal(zeb);
//Creating a new enclosure just for the herbivores.
Enclosure thirdEnclosure = new Enclosure("Savanna");
//Adding zeb and gaz to the third enclosure.
thirdEnclosure.addAnimal(zeb);
thirdEnclosure.addAnimal(gaz);
//Adding all three enclosures to the zoo.
laZoo.addEnclosure(firstEnclosure);
laZoo.addEnclosure(secondEnclosure);
laZoo.addEnclosure(thirdEnclosure);
//Printing the Zoo:
System.out.println(laZoo);
//Getting the feeding instructions for all the animals:
laZoo.getFeedingInstructions();
//Feeding the animals:
laZoo.feed();
//However, an error message is printed because the Zoo doesn't have enough money to feed all the animals, so we add more money to the Zoo.
laZoo.setBudget(999999999);
//Successfully feeding the animals!
laZoo.feed();
}
Output this:
Error! Gena cannot live in the Savanna. addAnimal failed.
Error! Zebby cannot live with Gena, as they are enemies. addAnimal failed.
Los Angeles Zoo
==========
Address: 5333 Zoo Dr, Los Angeles, CA 90027
Area: 133
=========
Enclosures:
How to Feed:
Feeding failed! Out of funds.
Feeding completed successfully.
Submitting the assignment:
You must upload the assignment to the canvas as a set of these 9 files as follows
Main.java Feedable.java Zoo.java Enclosure.java Animal.java
Crocodile.java Gazelle.java Lion.java Zebra.java
Main.java
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Creating a new Zoo.
Zoo laZoo = new Zoo("Los Angeles Zoo", "5333 Zoo Dr, Los Angeles, CA 90027", 133, 0);
//Creating two different Enclosures.
Enclosure firstEnclosure = new Enclosure("Savanna");
Enclosure secondEnclosure = new Enclosure("River");
//Creating our animals.
Zebra zeb = new Zebra("Zebby");
Gazelle gaz = new Gazelle("Gaz");
Crocodile croc = new Crocodile("Gena");
Lion leo = new Lion("Leo");
//Trying to add croc to the firstEnclosure, but Crocodiles can't live in the Savannah, so it prints an error message.
firstEnclosure.addAnimal(croc);
//Adding leo to the first enclosure.
firstEnclosure.addAnimal(leo);
//Adding croc to the second enclosure.
secondEnclosure.addAnimal(croc);
//Trying to add zeb to the secondEnclosure, but Crocodiles and Zebras are enemies, so it prints an error message.
secondEnclosure.addAnimal(zeb);
//Creating a new enclosure just for the herbivores.
Enclosure thirdEnclosure = new Enclosure("Savanna");
//Adding zeb and gaz to the third enclosure.
thirdEnclosure.addAnimal(zeb);
thirdEnclosure.addAnimal(gaz);
//Adding all three enclosures to the zoo.
laZoo.addEnclosure(firstEnclosure);
laZoo.addEnclosure(secondEnclosure);
laZoo.addEnclosure(thirdEnclosure);
//Printing the Zoo:
System.out.println(laZoo);
//Getting the feeding instructions for all the animals:
laZoo.getFeedingInstructions();
//Feeding the animals:
laZoo.feed();
//However, an error message is printed because the Zoo doesn't have enough money to feed all the animals, so we add more money to the Zoo.
laZoo.setBudget(999999999);
//Successfully feeding the animals!
laZoo.feed();
}
}
Feedable.java
public interface Feedable {
void getFeedingInstructions();
void feed();
}
Zoo.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Zoo {
private String name;
private String address;
private List<Enclosure> enclosures;
private int area;
private double budget;
public Zoo(String name, String address, int area, double budget) {
this.name = name;
this.address = address;
this.area = area;
this.budget = budget;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public List<Enclosure> getEnclosures() {
return enclosures;
}
public void setEnclosures(List<Enclosure> enclosures) {
this.enclosures = enclosures;
}
public int getArea() {
return area;
}
public void setArea(int area) {
this.area = area;
}
public double getBudget() {
return budget;
}
public void setBudget(double budget) {
this.budget = budget;
}
public List<Enclosure> addEnclosure(Enclosure enclosure) {
if(enclosures != null) {
enclosures.add(enclosure);
}
else {
enclosures = new ArrayList<Enclosure>(Arrays.asList(enclosure));
}
return enclosures;
}
public void getFeedingInstructions() {
System.out.println("How to Feed:");
for (Enclosure enclosure : enclosures) {
System.out.println(enclosure.getBiome() + ":");
char index = 'a';
for (Animal animal : enclosure.getAnimals()) {
System.out.print( index++ + ") ");
animal.getFeedingInstructions();
}
}
}
public void feed() {
if(this.getBudget() < 999999999) {
System.out.println("Feeding failed! Out of funds.");
} else {
for (Enclosure enclosure : enclosures) {
for (Animal animal : enclosure.getAnimals()) {
animal.feed();
}
}
System.out.println("Feeding completed successfully.");
}
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + area;
long temp;
temp = Double.doubleToLongBits(budget);
result = prime * result + (int) (temp ^ (temp >>> 32));
result = prime * result + ((enclosures == null) ? 0 : enclosures.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Zoo)) {
return false;
}
Zoo other = (Zoo) obj;
if (address == null) {
if (other.address != null) {
return false;
}
} else if (!address.equals(other.address)) {
return false;
}
if (area != other.area) {
return false;
}
if (Double.doubleToLongBits(budget) != Double.doubleToLongBits(other.budget)) {
return false;
}
if (enclosures == null) {
if (other.enclosures != null) {
return false;
}
} else if (!enclosures.equals(other.enclosures)) {
return false;
}
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(name);
builder.append("\n");
builder.append("==========");
builder.append("\n");
builder.append("Address: " );
builder.append(address);
builder.append("\n");
builder.append("Area: ");
builder.append(area);
builder.append("\n");
builder.append("==========");
builder.append("\n");
builder.append("Enclosures");
for (Enclosure enclosure : enclosures) {
builder.append("\n");
builder.append(enclosure.toString());
}
return builder.toString();
}
}
Enclosure.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class Enclosure {
private String biome;
private List<Animal> animals;
public Enclosure(String biome) {
this.biome = biome;
}
public String getBiome() {
return biome;
}
public void setBiome(String biome) {
this.biome = biome;
}
public List<Animal> getAnimals() {
return animals;
}
public void setAnimals(List<Animal> animals) {
this.animals = animals;
}
// add animal to the enclosure animal list
public void addAnimal(Animal animal) {
if(animals == null) {
animals = new ArrayList<Animal>();
}
if("Savanna".equals(this.biome) && animal instanceof Crocodile) {
System.out.println("Error! " + animal.getName() + " cannot live in the " + this.biome + ". addAnimal failed.");
return;
}
if(animal instanceof Zebra) {
Optional<Animal> croc = this.animals.stream().filter( x -> x instanceof Crocodile ).findAny();
if(croc.isPresent()) {
System.out.println("Error! " + animal.getName() + " cannot live with " + croc.get().getName() + ", as they are enemies. addAnimal failed.");
return;
}
}
animals.add(animal);
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((animals == null) ? 0 : animals.hashCode());
result = prime * result + ((biome == null) ? 0 : biome.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Enclosure)) {
return false;
}
Enclosure other = (Enclosure) obj;
if (animals == null) {
if (other.animals != null) {
return false;
}
} else if (!animals.equals(other.animals)) {
return false;
}
if (biome == null) {
if (other.biome != null) {
return false;
}
} else if (!biome.equals(other.biome)) {
return false;
}
return true;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(biome);
builder.append(":");
char index = 'a';
for (Animal animal : this.getAnimals()) {
builder.append("\n");
builder.append(index++);
builder.append(") ");
builder.append(animal.toString());
}
return builder.toString();
}
}
Animal.java
public abstract class Animal implements Feedable{
private String name;
private String genus;
private String species;
private Zoo currentZoo;
protected Animal enemy;
protected String prefferedBiome;
public Animal(String name, String genus) {
this.name = name;
this.genus = genus;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGenus() {
return genus;
}
public void setGenus(String genus) {
this.genus = genus;
}
public String getSpecies() {
return species;
}
public void setSpecies(String species) {
this.species = species;
}
public Zoo getCurrentZoo() {
return currentZoo;
}
public void setCurrentZoo(Zoo currentZoo) {
this.currentZoo = currentZoo;
}
public Animal getEnemy() {
return enemy;
}
public void setEnemy(Animal enemy) {
this.enemy = enemy;
}
public String getPrefferedBiome() {
return prefferedBiome;
}
public void setPrefferedBiome(String prefferedBiome) {
this.prefferedBiome = prefferedBiome;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((currentZoo == null) ? 0 : currentZoo.hashCode());
result = prime * result + ((enemy == null) ? 0 : enemy.hashCode());
result = prime * result + ((genus == null) ? 0 : genus.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((prefferedBiome == null) ? 0 : prefferedBiome.hashCode());
result = prime * result + ((species == null) ? 0 : species.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Animal)) {
return false;
}
Animal other = (Animal) obj;
if (currentZoo == null) {
if (other.currentZoo != null) {
return false;
}
} else if (!currentZoo.equals(other.currentZoo)) {
return false;
}
if (enemy == null) {
if (other.enemy != null) {
return false;
}
} else if (!enemy.equals(other.enemy)) {
return false;
}
if (genus == null) {
if (other.genus != null) {
return false;
}
} else if (!genus.equals(other.genus)) {
return false;
}
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
if (prefferedBiome == null) {
if (other.prefferedBiome != null) {
return false;
}
} else if (!prefferedBiome.equals(other.prefferedBiome)) {
return false;
}
if (species == null) {
if (other.species != null) {
return false;
}
} else if (!species.equals(other.species)) {
return false;
}
return true;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(name);
builder.append(" (");
builder.append(genus);
builder.append(")");
return builder.toString();
}
}
Crocodile.java
public class Crocodile extends Animal{
public Crocodile(String name) {
super(name,"Crocodylus niloticus");
}
@Override
public void getFeedingInstructions() {
System.out.println("Meat 5000$");
}
@Override
public void feed() {
}
}
Gazelle.java
public class Gazelle extends Animal {
public Gazelle(String name) {
super(name, "Rhim gazelle");
}
@Override
public void getFeedingInstructions() {
System.out.println("Grass 900$");
}
@Override
public void feed() {
}
}
Lion.java
public class Lion extends Animal {
public Lion(String name) {
super(name,"Panthera leo");
}
@Override
public void getFeedingInstructions() {
System.out.println("Meat 4000$");
}
@Override
public void feed() {
}
}
Zebra.java
public class Zebra extends Animal{
public Zebra(String name) {
super(name, "Equus quagga" );
}
@Override
public void getFeedingInstructions() {
System.out.println("Grass 600$");
}
@Override
public void feed() {
}
}
Here we have made an assumption that the feed implemenation in every animal is empty as we just need the confirmation in the Zoo.java. We can have seperate impelmentation provided we have more information.