Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Item { private String name; private int value; public Item(String itemName, int itemValue) { name = itemName; value = itemValue; } public String

public class Item

{

private String name;

private int value;

public Item(String itemName, int itemValue)

{

name = itemName;

value = itemValue;

}

public String getName()

{

return name;

}

public int getValue()

{

return value;

}

}

The ItemGrid class below uses the two-dimensional array grid to represent a group of Item objects.

public class ItemGrid

{

private Item[][] grid;

// Constructor not shown

/** Returns true if xPos is a valid row index and yPos is a valid

* column index and returns false otherwise.

*/

public boolean isValid(int xPos, int yPos)

{ /* implementation not shown */ }

/** Compares the item in row r and column c to the items to its

* left and to its right. Returns the name of the item with

* the greatest value, as described in part (a).

* Precondition: r and c are valid indices

*/

public String mostValuableNeighbor(int r, int c)

{ /* to be implemented in part (a) */ }

/** Returns the average value of all the items in grid,

* as described in part (b).

*/

public double findAverage()

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

}

(a) Write the mostValuableNeighbor method, which compares the item in row r and column c to the items to its left and to its right. The method determines which of the three items has the greatest value and returns its name. If more than one of the values have a value that is greatest, then any of their names can be returned. If the item has no item to its left, it is compared only to the item to its right. If the item has no item to its right, it is compared only to the item to its left.

The helper method isValid has been provided. The isValid method returns true if xPos is a valid row index and yPos is a valid column index in the two-dimensional array grid, and returns false otherwise.

Assume that the ItemGrid object ig has been created such that the two-dimensional array grid contains the following item objects.

The following table shows some examples of the behavior of the mostValuableNeighbor method.

Method Call Return Value Explanation
ig.mostValuableNeighbor(0, 2) "book" The item at row 0, column 2 ("carrot") is compared with the items to its left and right ("book" and "desk"). Of the three items, "book" has the greatest value (10).
ig.mostValuableNeighbor(1, 1) "flag" or "globe" The item at row 1, column 1 ("flag") is compared with the items to its left and right ("egg" and "globe"). Of the three items, both "flag" and "globe" have the greatest value (8), so either can be returned.
ig.mostValuableNeighbor(2, 0) "jacket" The item at row 2, column 0 ("island") has no item to its left, so it is only compared with the item to its right ("jacket"). Of the two items, "jacket" has the greater value (19).
ig.mostValuableNeighbor(2, 3) "lunch" The item at row 2, column 3 ("lunch") has no item to its right, so it is only compared with the item to its left ("kale"). Of the two items, "lunch" has the greater value (16).

Complete the mostValuableNeighbor method below. You must use isValid appropriately to receive full credit. Assume that grid has been initialized with at least three rows and three columns, and contains no null elements.

/** Compares the item in row r and column c to the items to its

* left and to its right. Returns the name of the item with

* the greatest value, as described in part (a).

* Precondition: r and c are valid indices

*/

public String mostValuableNeighbor(int r, int c)

BoldItalicUnderline

(b) Write the findAverage method, which returns the average value of all items in grid. For example, for the ItemGrid object ig shown in part (a), the findAverage method should return 9.5, which is the average value of the twelve items in the 2D array.

Complete the findAverage method below.

/** Returns the average value of all the items in grid,

* as described in part (b).

*/

public double findAverage()

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

Hands On Database

Authors: Steve Conger

1st Edition

013610827X, 978-0136108276

More Books

Students also viewed these Databases questions

Question

Explain the strength of acid and alkali solutions with examples

Answered: 1 week ago

Question

Introduce and define metals and nonmetals and explain with examples

Answered: 1 week ago

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago