Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 1 SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA. Assume that the classes listed in the Java Quick

Question 1 SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA. Assume that the classes listed in the Java Quick Reference have been imported where appropriate. Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied. In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit. 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.

image text in transcribed

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)

{

Question 2

(b) Write the sameCrop method, which returns true if all the plots in a given column of farmPlots grow the same crop and returns false otherwise.

Assume that the ExperimentalFarm object f has been created such that its farmPlots array contains the following cropType and cropYield values.

image text in transcribed

The following are two examples of the behavior of sameCrop.

  • The method call f.sameCrop(0) returns false because the values of cropType for the elements of column 0 ("corn", "peas", "wheat", and "corn") are not all the same.
  • The method call f.sameCrop(1) returns true because the values of cropType for all elements of column 1 are the same ("corn").

Write the sameCrop method below.

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

0 2 "corn" "corn" 30 "peas" 10 20 "peas" 30 "corn" 40 "corn" 62 "corn" "wheat" 10 "rice" 30 50 "corn". 55 "corn" 30 "peas" 30 0 2 "corn" "corn" 30 "peas" 10 20 "peas" 30 "corn" 40 "corn" 62 "corn" "wheat" 10 "rice" 30 50 "corn". 55 "corn" 30 "peas" 30

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Ai And The Lottery Defying Odds With Intelligent Prediction

Authors: Gary Covella Ph D

1st Edition

B0CND1ZB98, 979-8223302568

More Books

Students also viewed these Databases questions