Question
Lab: Dairy Barn - max milk production (arrays) A dairy barn consists of numbered spaces. Each space holds one cow. The spaces are indexed starting
Lab: Dairy Barn - max milk production (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
Cow maxCowProduction() which finds and returns the Cow with the highest average daily milk production. If there are multiple Cows with the same average, return the Cow in the lowest numbered space.
DailyBarnDriver.java
ublic 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); Cow maxCow = dairy.maxCowProduction(); System.out.println(maxCow);
} }
Cow.java:
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 + "]"; } }
DailyBarn.java:
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]; } } /** * Returns the Cow with the highest average daily milk production. * If there are multiple Cows with the same average, return the Cow in the lowest numbered space. * @returns Cow - the Cow with the highest average daily milk production */ public Cow maxCowProduction() { // TODO Complete this method return null; } 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