Any help would be appreciated! Thanks, in advance! Here\'s what I have so far in
ID: 3589731 • Letter: A
Question
Any help would be appreciated! Thanks, in advance!
Here's what I have so far in my main class. There should be 5 total classes (Main class, Animal superclass, and classes for dog, cat and cow)
// Enum type for food
enum FoodType { BONE, SALMON, GRASS }
public class Farm_Client
{
// Constants for food costs
final static double grassCost = 1.0;
final static double boneCost = 3.0;
final static double salmonCost = 5.0;
// Random number generator
private static Random r = new Random();
public static void main(String[] args)
{
// For autograding purposes - setting seed (read in from command line)
// so random always generates same numbers
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;
// It is used to reproduce the output in the Project writeup
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);
}
// TODO: Insert your 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;
}
}
Description Unfortunately, Old MacDonald has begun to lose his vision! However, he still has a keen sense of hearing and would like to keep working on the farm. Every few days, he gets called in to feed the animals at a new farm. Old MacDonald must visit each stall in the barn, call out at the animal in the stall, and discern what type of animal he hears. Then, when he identifies the animal as a cat, dog or cow, he'll feed that animal its favorite food until it is no longer hungry. Old MacDonald knows that cats especially like salmon, dogs love a good bone to chew on, and cows enjoy fresh grass. Each feeding costs money (salmon-S5, bone-S3, grass-S1) and an animal can require multiple feedings. Thus, Old MacDonald will leave a bill for the farm keeper at the end of his visit.Explanation / Answer
static double foodCost;
public Cat(int feedingsRequired) {
super(feedingsRequired);
}
@Override
public void feed(String foodType) {
if(foodType.equals("salmon"))
{
super.numberOfRequiredFeedings--;
super.numberOfFeedings++;
foodCost=foodCost+5;
}
}
@Override
public String speak() {
return "MEOW";
}
@Override
public String toString() {
return "Cat [numberOfFeedings=" + numberOfFeedings
+ ", numberOfRequiredFeedings=" + numberOfRequiredFeedings
+ " "+foodCost+"]";
}
***************************************************************************************************************
package com.as.test;
public class Cow extends Animal {
static double foodCost=0;
public Cow(int feedingsRequired) {
super(feedingsRequired);
}
@Override
public void feed(String foodType) {
if(foodType.equals("grass"))
{
super.numberOfRequiredFeedings--;
super.numberOfFeedings++;
foodCost=foodCost+1;
}
}
@Override
public String speak() {
return "MOO";
}
@Override
public String toString() {
return "Cow [numberOfFeedings=" + numberOfFeedings
+ ", numberOfRequiredFeedings=" + numberOfRequiredFeedings
+ " "+foodCost+"]";
}
}
***********************************************************************************************************
package com.as.test;
public class Cat extends Animal {
static double foodCost;
public Cat(int feedingsRequired) {
super(feedingsRequired);
}
@Override
public void feed(String foodType) {
if (foodType.equals("salmon")) {
super.numberOfRequiredFeedings--;
super.numberOfFeedings++;
foodCost = foodCost + 5;
}
}
@Override
public String speak() {
return "MEOW";
}
@Override
public String toString() {
return "Cat [numberOfFeedings=" + numberOfFeedings
+ ", numberOfRequiredFeedings=" + numberOfRequiredFeedings
+ " " + foodCost + "]";
}
}
******************************************************************************************************************
package com.as.test;
public class Dog extends Animal {
static double foodCost;
public Dog(int feedingsRequired) {
super(feedingsRequired);
}
@Override
public void feed(String foodType) {
if (foodType.equals("salmon")) {
super.numberOfRequiredFeedings--;
super.numberOfFeedings++;
foodCost = foodCost + 3;
}
}
@Override
public String speak() {
return "WOOF";
}
@Override
public String toString() {
return "DOG [numberOfFeedings=" + numberOfFeedings
+ ", numberOfRequiredFeedings=" + numberOfRequiredFeedings
+ " " + foodCost + "]";
}
}
**************************************************************************************************
enum FoodType { BONE, SALMON, GRASS }
public class Farm_Client
{
// Constants for food costs
final static double grassCost = 1.0;
final static double boneCost = 3.0;
final static double salmonCost = 5.0;
// Random number generator
private static Random r = new Random();
public static void main(String[] args)
{
// For autograding purposes - setting seed (read in from command line)
// so random always generates same numbers
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;
// It is used to reproduce the output in the Project writeup
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);
}
// TODO: Insert your 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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.