Can you help me implement the getHighestYield method, which returns …

Computers and Technology Questions

The ExperimentalFarm class represents crops grown on an experimental farm. An experimental farm is a rectangular tract of land that is divided into a grid of equal-sized plots. Each plot in the grid contains one type of crop. The crop yield of each plot is measured in bushels per acre. A farm plot is represented by the Plot class. A partial definition of the Plot class is shown below. public class Plot { private String cropType; private int cropYield; public Plot(String crop, int yield) { /* implementation not shown */ } public String getCropType() { return cropType; } public int getCropYield() { return cropYield; } } The grid of equal-sized plots is represented by a two-dimensional array of Plot objects named farmPlots, declared in the ExperimentalFarm class. A partial definition of the ExperimentalFarm class is shown below. public class ExperimentalFarm { private Plot[][] farmPlots; public ExperimentalFarm(Plot[][] p) { /* implementation not shown */ } /** Returns the plot with the highest yield for a given crop type, as described in part (a). */ public Plot getHighestYield(String c) { /* to be implemented in part (a) */ } /** Returns true if all plots in a given column in the two-dimensional array farmPlots * contain the same type of crop, or false otherwise, as described in part (b). */ public boolean sameCrop(int col) { /* to be implemented in part (b) */ } } (a) Write the getHighestYield method, which returns the Plot object with the highest yield among the plots in farmPlots with the crop type specified by the parameter c. If more than one plot has the highest yield, any of these plots may be returned. If no plot exists containing the specified type of crop, the method returns null. Assume that the ExperimentalFarm object f has been created such that its farmPlots array contains the following cropType and cropYield values. The figure presents a two-dimensional array of Plot objects with 3 columns and 4 rows. The columns are labeled from 0 to 2, and the rows are labeled from 0 to 3. Each plot is labeled with a crop name and crop yield as follows. Row 0. Column 0, “Corn” 20. Column 1, “Corn” 30. Column 2, “Peas” 10. Row 1. Column 0, “Peas” 30. Column 1, “Corn” 40. Column 2, “Corn” 62. Row 2. Column 0, “Wheat” 10. Column 1, “Corn” 50. Column 2, “Rice” 30. Row 3. Column 0, “Corn” 55, Column 1, “Corn” 30. Column 2, “Peas” 30. The following are some examples of the behavior of the getHighestYield method. Method Call Return Value f.getHighestYield(“corn”) √¢¬Ä¬ãfarmPlots[1][3] f.getHighestYield(“peas”) farmPlots[1][0] or farmPlots[3][2]√¢¬Ä¬ã f.getHighestYield(“bananas”) null Write the getHighestYield method below. /** Returns the plot with the highest yield for a given crop type, as described in part (a). */ public Plot getHighestYield(String c)

Short Answer

The answer outlines a three-step process for creating a system to manage crop plots. Step 1 involves defining a Plot class with attributes like crop type and yield. Step 2 focuses on the ExperimentalFarm class, which manages a 2D array of Plot objects and includes methods for yield analysis and crop type consistency. Finally, Step 3 explains how to implement the Main class to instantiate Plot objects, populate the array, and test the functionalities using the methods from ExperimentalFarm.

Step-by-Step Solution

Step 1: Create the Plot Class

Define the Plot class to represent individual crop plots on the farm. This class should include attributes such as cropType (String) and cropYield (int). Use constructors to initialize these attributes, and provide getter methods to access the values.

  • Attributes: cropType, cropYield
  • Constructor: Initializes cropType and cropYield
  • Methods: getCropType(), getCropYield(), toString()

Step 2: Implement the ExperimentalFarm Class

The ExperimentalFarm class holds a two-dimensional array of Plot objects. This class will include methods to find the plot with the highest yield for a specified crop type and to check if all plots in a specific column have the same crop type. Use nested loops to iterate through the array and check the conditions.

  • Attributes: farmPlots (2D array of Plot)
  • Method 1: getHighestYield(String c) uses loops to find the plot with the highest yield.
  • Method 2: sameCrop(int col) checks if all crops in a column are the same.

Step 3: Create the Main Class Implementation

In the Main class, instantiate Plot objects and populate a 2D array with them. Then, create an instance of ExperimentalFarm using this array. Call the methods to test their functionality: retrieving the highest yield for different crops and checking if plots in specific columns are the same.

  • Instantiate multiple Plot objects with various crop types and yields.
  • Add Plot objects to a 2D array and create an ExperimentalFarm instance.
  • Call methods getHighestYield() and sameCrop() to display results.

Related Concepts

Plot Class

A class designed to represent individual crop plots, including attributes like crop type and yield, with methods to access these values.

Experimentalfarm Class

A class that manages a two-dimensional array of plot objects, providing functionality to find the highest yielding plot for a specific crop type and to verify uniformity of crop types in a column.

Methods

Functions defined within a class that perform specific operations, such as retrieving crop information or analyzing yield data.

Table Of Contents
  1. The ExperimentalFarm class represents crops grown on an experimental farm. An experimental farm is a rectangular tract of land that is divided into a grid of equal-sized plots. Each plot in the grid contains one type of crop. The crop yield of each plot is measured in bushels per acre. A farm plot is represented by the Plot class. A partial definition of the Plot class is shown below. public class Plot { private String cropType; private int cropYield; public Plot(String crop, int yield) { /* implementation not shown */ } public String getCropType() { return cropType; } public int getCropYield() { return cropYield; } } The grid of equal-sized plots is represented by a two-dimensional array of Plot objects named farmPlots, declared in the ExperimentalFarm class. A partial definition of the ExperimentalFarm class is shown below. public class ExperimentalFarm { private Plot[][] farmPlots; public ExperimentalFarm(Plot[][] p) { /* implementation not shown */ } /** Returns the plot with the highest yield for a given crop type, as described in part (a). */ public Plot getHighestYield(String c) { /* to be implemented in part (a) */ } /** Returns true if all plots in a given column in the two-dimensional array farmPlots * contain the same type of crop, or false otherwise, as described in part (b). */ public boolean sameCrop(int col) { /* to be implemented in part (b) */ } } (a) Write the getHighestYield method, which returns the Plot object with the highest yield among the plots in farmPlots with the crop type specified by the parameter c. If more than one plot has the highest yield, any of these plots may be returned. If no plot exists containing the specified type of crop, the method returns null. Assume that the ExperimentalFarm object f has been created such that its farmPlots array contains the following cropType and cropYield values. The figure presents a two-dimensional array of Plot objects with 3 columns and 4 rows. The columns are labeled from 0 to 2, and the rows are labeled from 0 to 3. Each plot is labeled with a crop name and crop yield as follows. Row 0. Column 0, "Corn" 20. Column 1, "Corn" 30. Column 2, "Peas" 10. Row 1. Column 0, "Peas" 30. Column 1, "Corn" 40. Column 2, "Corn" 62. Row 2. Column 0, "Wheat" 10. Column 1, "Corn" 50. Column 2, "Rice" 30. Row 3. Column 0, "Corn" 55, Column 1, "Corn" 30. Column 2, "Peas" 30. The following are some examples of the behavior of the getHighestYield method. Method Call Return Value f.getHighestYield("corn") ​farmPlots[1][3] f.getHighestYield("peas") farmPlots[1][0] or farmPlots[3][2]​ f.getHighestYield("bananas") null Write the getHighestYield method below. /** Returns the plot with the highest yield for a given crop type, as described in part (a). */ public Plot getHighestYield(String c)
Scroll to Top