15.2k views
4 votes
Please help!

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)

1 Answer

2 votes

Answer:

See explanation. I divided it up into part a and b.

Step-by-step explanation:

PLOT CLASS CODE:

public class Plot

{

private String cropType;

private int cropYield;

public Plot(String crop, int yield)

{

this.cropType = crop;

this.cropYield = yield;

}

public String getCropType()

{

return cropType;

}

public int getCropYield()

{

return cropYield;

}

public String toString() {

return this.cropType+", "+this.getCropYield();

}

}

EXPERIMENTAL FARM CLASS CODE:

public class ExperimentalFarm

{

private Plot[][] farmPlots;

public ExperimentalFarm(Plot[][] p)

{

this.farmPlots = p;

}

/** Returns the plot with the highest yield for a given crop type, as described in part (a). */

public Plot getHighestYield(String c)

{

Plot plot = null;

int highest = this.farmPlots[0][0].getCropYield();

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

{

for(int j=0;j<3;j++)

{

if(farmPlots[i][j].getCropType().equalsIgnoreCase(c) && farmPlots[i][j].getCropYield()>highest)

{

highest = farmPlots[i][j].getCropYield();

plot = farmPlots[i][j];

}

}

}

if(plot != null)

return plot;

else

return null;

/* 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)

{

boolean check = true;;

String crop = farmPlots[0][col].getCropType();

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

{

if(!farmPlots[i][col].getCropType().equalsIgnoreCase(crop))

{

check = false;

break;

}

}

return check;

/* to be implemented in part (b) */

}

}

MAIN CLASS CODE:

public class Main {

public static void main(String[] args)

{

Plot p1 = new Plot("corn",20);

Plot p2 = new Plot("corn",30);

Plot p3 = new Plot("peas",10);

Plot p4 = new Plot("peas",30);

Plot p5 = new Plot("corn",40);

Plot p6 = new Plot("corn",62);

Plot p7 = new Plot("wheat",10);

Plot p8 = new Plot("corn",50);

Plot p9 = new Plot("rice",30);

Plot p10 = new Plot("corn",55);

Plot p11 = new Plot("corn",30);

Plot p12 = new Plot("peas",30);

Plot[][] plots = {{p1,p2,p3},

{p4,p5,p6},

{p7,p8,p9},

{p10,p11,p12}};

ExperimentalFarm f = new ExperimentalFarm(plots);

Plot highestYield = f.getHighestYield("corn");

Plot highestYield1 = f.getHighestYield("peas");

Plot highestYield2 = f.getHighestYield("bananas");

try {

System.out.println(highestYield.toString());

System.out.println(highestYield1.toString());

System.out.println(highestYield2.toString());

}

catch(Exception e)

{

System.out.println("null");

}

System.out.println("The method call f.sameCrop(0)");

System.out.println(f.sameCrop(0));

System.out.println("The method call f.sameCrop(1)");

System.out.println(f.sameCrop(1));

}

}

User Ravi Gehlot
by
3.5k points