Question

In: Computer Science

Your project MUST do exactly what I am asking for. Read very carefully. I will take...

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:

  1. Zoo
  2. Enclosure
  3. Animal
  4. Crocodile
  5. Gazelle
  6. Lion
  7. Zebra
  1. Zoo:

Required member variables:

private String name;

private String address;

private Enclosure[] enclosures;

private int area;

private double budget;

  1. Enclosure:

Required member variables:

private String biome;

private Animal[] animals;

  1. Animal

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:

  1. Savanna:
  1. a) Leo (Panthera leo)
  1. River:
  1. a) Gena (Crocodylus niloticus)
  1. Savanna:
  1. a) Zebby (Equus quagga)
  2. b) Gaz (Rhim gazelle)

How to Feed:

  1. Savanna:
  1. a) Meat 4000$
  1. River:
  1. a) Meat 5000$
  2. Savanna:
  3. a) Grass 600$
  4. b) Grass 900$

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

Solutions

Expert Solution

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.


Related Solutions

I am not quite sure what this question is asking, nor do I understand how to...
I am not quite sure what this question is asking, nor do I understand how to apply the lower-of-cost-or-NRV. The controller of Alt Company is applying the lower-of-cost-or-net realizable value basis of valuing its ending inventory. The following information is available: Cost Net Realizable Value Lawnmowers: Self-propelled $14,800 $17,000 Push type 19,000 18,000 Total 33,800 35,000 Snowblowers: Manual 29,800 31,000 Self-start 19,000 21,000 Total 48,800 52,000 Total inventory $82,600 $87,000 Compute the value of the ending inventory by applying the...
I am posting this for the third time. PLEASE READ EVERYTHING CAREFULLY AND THEN ANSWER I...
I am posting this for the third time. PLEASE READ EVERYTHING CAREFULLY AND THEN ANSWER I am m designing a questionnaire for the topic: FACTORS INFLUENCING CONSUMER BUYING BEHAVIOR IN PURCHASE OF MILK IN ORGANIZED AND UNORGANIZED SECTOR. the study is basically to study the factors that influence consumer decisions in purchase of milk in organized and unorganized sector of milk. For this i need variables that influence purchase of milk in organized and unorganized sector. \ Note: I need...
I am posting this for the third time. PLEASE READ EVERYTHING CAREFULLY AND THEN ANSWER I...
I am posting this for the third time. PLEASE READ EVERYTHING CAREFULLY AND THEN ANSWER I am m designing a questionnaire for the topic: FACTORS INFLUENCING CONSUMER BUYING BEHAVIOR IN PURCHASE OF MILK IN ORGANIZED AND UNORGANIZED SECTOR. the study is basically to study the factors that influence consumer decisions in purchase of milk in organized and unorganized sector of milk. For this i need variables that influence purchase of milk in organized and unorganized sector. Note: I need just...
I am in this electrical engineering project course in which an assignment is asking me this...
I am in this electrical engineering project course in which an assignment is asking me this and I cannot make heads or tails of what it means by this... Discuss the tradeoffs involved between using patents and trade secrets to protect intellectual property. Please be as descriptive as possible, THANKS
PLEASE READ CAREFULLY BEFORE STARTING THE ASSIGNMENT!!! AS YOU CAN SEE I AM DONE WITH ALMOST...
PLEASE READ CAREFULLY BEFORE STARTING THE ASSIGNMENT!!! AS YOU CAN SEE I AM DONE WITH ALMOST HALF THE TABLE. I DIDN'T FIND A SINGLE AMOUNT FOR THE COLUMNS: RENT EXPENSE, SALARIES EXPENSE, SUPPLIES EXPENSE, AUTO EXPENSE, MISC EXPENSE. THE SAME QUESTIONS HAS BEEN ASKED MANY TIMES ON CHEGG, BUT ALL THE AMOUNTS POSTED FOR THOSE MISSING COLUMNS ARE WRONG IN EACH AND EVERY SINGLE POST, SO PLEASE DON'T COPY AND PASTE FROM ANY OF THEM. I LITERALLY CHEKCED EACH AND...
Read the questions carefully and do not look for a quick answer. There are none. Your...
Read the questions carefully and do not look for a quick answer. There are none. Your answers should reflect your ability to think about complexity using multiple disciplines to explore murky answers. Your answers must be typed, and it should be more than 4 citations. Please explain in 3-4 full paragraph and please add the site that you used. It should be more than 4 sources. 1)How will space exploration and travel be changed with the private sector playing a...
JAVA - I am asking for the user to input their firstName and lastName but I...
JAVA - I am asking for the user to input their firstName and lastName but I want the method myMethod() to be able to print it when it is called. Is that possible? I'm new to Java and i'm not too sure what I should do to fix this. Also if I were to create a IF statement would I have to declare int amountDeposited; , int accountBalance; , int newBalance; in the new if statement. import java.util.Scanner; import java.util.Arrays;...
Very carefully read the following data requirements for a prospective document translation database: i. Document translation...
Very carefully read the following data requirements for a prospective document translation database: i. Document translation initially relies on a source document. The source document is a text provided in the originally recorded, valid language (see below, and assume only one language for the original document), and has an associated author and publication date. Source document authors are not related to translators in any manner. ii. Translation of a document also relies on a translator, who is capable of translating...
I want to design a project for fun, since I am planning to take electromagnetics next...
I want to design a project for fun, since I am planning to take electromagnetics next semester. I need help with this design Design Construct and demonstrate An arc generator solely driven by electrostatics, magnetostatics, and/or electromagnetics Techniques using only household materials and a 9V battery.
What I want to do is to digitally sign a document. I am new to cryptography and was wondering what issues to take into account.
What I want to do is to digitally sign a document. I am new to cryptography and was wondering what issues to take into account. The only criteria is that a textual document signed should produce a code that is different if lines in the same document are shifted. I heard that MD5 is broken and that one should avoid SHA1 (because of NSA etc).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT