Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have all of the subclasses done. I\'m looking for help with the output code. T

ID: 3591140 • Letter: I

Question

I have all of the subclasses done. I'm looking for help with the output code. The last two pics show what the output should look like. All relevant code is listed below. The spot I need help with is bolded below (under second test case) and should utilize an ArrayList. Let me know if you need any addition information. Thanks for any help you can give!

public static void main(String[] args)

{

if (args.length > 0)

r.setSeed(Long.parseLong(args[0]));

// Decimal format class for printing out any prices

DecimalFormat df = new DecimalFormat("$0.00");

for (int i = 0; i < 2; i++)

{

// An ArrayList of animals in the stalls

ArrayList<Animal> stalls;

// This test case will be executed during FIRST iteration;

if (i == 0)

{

System.out.println("----------CASE 1: generateDogCatCow5ServingArrangement() Output----------");

stalls = generateDogCatCow5ServingArrangement();

}

else

{

int numAnimals = 10;

// This test case will be executed during SECOND iteration;

// It is used to generate a random test case

System.out.println("-------------CASE 2: generateRandomStallArrangement() Output-------------");

stalls = generateRandomStallArrangement(numAnimals);

}

// Insert code here which causes Old MacDonald (this client code) to

// visit each of the stalls. Old MacDonald should speak to each animal to determine

// what type of food to feed it. All along the way, he keeps track of what types

// of animals and how much of each type of food he is using.

// End of test

System.out.println("E-I-E-I-O! ");

}

}

/////////////////////////////////////////////////////////////////////////////////

// DO NOT EDIT

// This method generates a stall with numAnimals which are generated randomly.

// In addition, the number of feedings required per animal are set randomly as

// a number between 1 and 10.

/////////////////////////////////////////////////////////////////////////////////

private static ArrayList<Animal> generateRandomStallArrangement(int numAnimals)

{

// Create new stall (ArrayList) of animals

ArrayList<Animal> newStallArrangement = new ArrayList<Animal>();

// Generate numAnimals new animals

for (int i = 0; i < numAnimals; i++)

{

int randAnimal = r.nextInt(3); // (Dog = 0, Cat = 1, Dog = 2)

int numFeedings = r.nextInt(10)+1; // 1-10 feedings required per animal

if (randAnimal == 0)

newStallArrangement.add(new Dog(numFeedings));

else if (randAnimal == 1)

newStallArrangement.add(new Cat(numFeedings));

else

newStallArrangement.add(new Cow(numFeedings));

}

return newStallArrangement;

}

/////////////////////////////////////////////////////////////////////////////////

// DO NOT EDIT

// This method generates a stall with a dog, cat and cow, which each need five

// servings of food.

/////////////////////////////////////////////////////////////////////////////////

private static ArrayList<Animal> generateDogCatCow5ServingArrangement()

{

// Create new stall (ArrayList) of animals containing a Dog, Cat & Cow, each with a requirement of 5 servings

ArrayList<Animal> newStallArrangement = new ArrayList<Animal>();

newStallArrangement.add(new Dog(5));

newStallArrangement.add(new Cat(5));

newStallArrangement.add(new Cow(5));

return newStallArrangement;

}

}

Requirements The main objective in this project is to gain experience with and demonstrate an ability to use inheritance and polymorphism. Your project MUST contain the following 5 classes (yes, you will be turning in 5 files, but they are pretty simple) which adhere to the following requirements: NOTE: Templates for each of the classes can be found online. All classes should remain in the default package, like you've been doing (do NOT separate the client and animals into different packages or change any package names) Class 1: Animal This is the animal super class. It MUST contain the following instance variables: Instance Variable Description protected int numberOfFeedings; A protected variable (can be accessed directly by subclasses) which indicates how many feedings the animal has had private int numberOfRequiredFeedings; A private variable (only the animal supelass can directly access) which indicates how many feedings the animal needs to no longer be hun In addition, the animal super class MUST have the following methods Method Header Description Animal(int feedingsRequired) public void feed (FoodType ft) public String speak() Animal constructor. Sets numberOfRequiredFeedings to feedingsRequired and sets numberOfFeedings to 0 These methods are mostly left unimplemented by the super class and should always be overridden. Print out an error message in case it is called. (Try using System.err.printlnO) Returns TRUE if animal has received a number of feedings less than its required number of feedings and FALSE otherwise public boolean isStillHungry()

Explanation / Answer

Here is the solution to your problem:

Whenever we want to map anything, we will take advantages of Map interface. But Only one object can be mapped with single value at a time. Here we want to map two prperties with a single Animal type, number of animals feed for a particular type and the quantity for each type. So i have created a FeedResult class that contains these properties. To run this Program correctly you have to implement equals() and HashCode() method for each type of Animal. Please let me know in case you face any issue.

Map<Animal, FeedResult> map=new HashMap<>();

for(Animal animal:stalls){

FoodType foodType=FoodType.valueOf(animal.speak());

if(map.get(animal)!=null){

FeedResult feedResult=new FeedResult();

feedResult.setNumberofAnilmals(1);

}else{

FeedResult feedResult=map.get(animal);

feedResult.setNumberofAnilmals(feedResult.getNumberofAnilmals()+1);

}

while(animal.isStillHungry()){

animal.feed(foodType);

FeedResult feedResult=map.get(animal);

feedResult.setFeedQuantity(feedResult.getFeedQuantity()+1);

}

}

public class FeedResult {

private int numberofAnilmals;

private int feedQuantity;

public int getNumberofAnilmals() {

return numberofAnilmals;

}

public void setNumberofAnilmals(int numberofAnilmals) {

this.numberofAnilmals = numberofAnilmals;

}

public int getFeedQuantity() {

return feedQuantity;

}

public void setFeedQuantity(int feedQuantity) {

this.feedQuantity = feedQuantity;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote