Question
LAB^: Dairy Barn - find Cow (arrays) A dairy barn consists of numbered spaces. Each space holds one cow. The spaces are indexed starting from
LAB^: Dairy Barn - find Cow (arrays) A dairy barn consists of numbered spaces. Each space holds one cow. The spaces are indexed starting from 0. No two cows in the barn have the same name. An object of the Cow class has a name, a weight (in pounds) and an average daily milk production (in gallons). The Cow class has been written for you. Write the following method in the DairyBarn class: int findSpace(String name) which finds the number of the space the given cow is in. If the Cow is not found, returns -1. The DairyBarnDriver class has been provided to test your methods. Hint: Remember to use .equals() when comparing Strings.
DairyBarnDriver.java;
public class DairyBarnDriver { public static void main(String[] args) {
String[] names = {"Annabelle", "Bossy", "Daisy", "Buttercup", "Bessie", "Domino", "Dot", "Millie", "Patches", "Rosie"}; int[] weights = {1300, 1325, 1275, 1310, 1290, 1340, 1280, 1295, 1310, 1305}; double[] productionAverages = {7.4, 7.5, 7.6, 7.55, 7.45, 7.7, 7.6, 7.45, 7.8, 7.4};
Cow[] cows = new Cow[10]; // Create 10 Cows for (int i = 0; i < 10; i++) { cows[i] = new Cow(names[i], weights[i], productionAverages[i]); } // Print the 10 cows for (Cow cow : cows) { System.out.println(cow); } System.out.println(); // Create a DairyBarn DairyBarn dairy = new DairyBarn (cows); // Find some spaces int space = dairy.findSpace("Daisy"); System.out.println("Daisy should be in space 2: " + space); space = dairy.findSpace("Rosie"); System.out.println("Rosie should be in space 9: " + space); space = dairy.findSpace("Annabelle"); System.out.println("Annabelle should be in space 0: " + space);
} }
Cow:
public class Cow { private String name; private int weight; private double avgDailyProduction; public Cow (String name, int weight, double production) { this.name = name; this.weight = weight; this.avgDailyProduction = production; } public String getName() { return name; } public int getWeight() { return weight; } public double getAvgDailyProduction() { return avgDailyProduction; } public String toString() { return "Cow [name=" + name + ", weight=" + weight + ", avg daily production=" + avgDailyProduction + "]"; } }
DairyBarn.Java (needs editing);
public class DairyBarn { private Cow[] cows; /** * Constructor for CowBarn * @param cows - an Array of Cows */ public DairyBarn (Cow[] cows) { // Instantiate instance variable with same length as parameter this.cows = new Cow[cows.length]; // Copy parameter array to instance variable for (int i = 0; i < cows.length; i ++) { this.cows[i] = cows[i]; } } /** * Iterates through all Cows and finds the space with the named Cow * If the Cow is not found in the CowBarn, returns -1 * @param name - The name of the Cow to find * @returns an int representing the space the Cow is found in */ public int findSpace(String name) { // TODO Complete this method return 0; } public String toString() { String s = "DairyBarn[ "; for (Cow cow : cows) { s += cow + " "; } return s + "]"; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started