145k views
3 votes
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.
This question uses two classes: an Item class that represents an item that has a name and value and an ItemGrid class that manages a two-dimensional array of items. A definition of the Item class is shown below.

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

}

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.

1 Answer

1 vote

Final answer:

The mostValuableNeighbor method in the ItemGrid class is written in Java to find the highest valued item compared to its immediate left and right neighbors in the grid, returning the name of the item with the greatest value.

Step-by-step explanation:

The task is to complete the mostValuableNeighbor method in the ItemGrid class in Java. This method checks the value of the current item in the grid and compares it to the values of its immediate neighbors to the left and right. Depending on boolean checks to ensure valid grid positions, the method uses conditional statements to compare and determine the highest value amongst the adjacent items.

Here is an implementation example of the mostValuableNeighbor method:

public String mostValuableNeighbor(int r, int c) {
Item current = grid[r][c];
Item maxItem = current; // Assumes current is the max

if (isValid(r, c - 1) && grid[r][c - 1].getValue() > maxItem.getValue()) {
maxItem = grid[r][c - 1];
}
if (isValid(r, c + 1) && grid[r][c + 1].getValue() > maxItem.getValue()) {
maxItem = grid[r][c + 1];
}
return maxItem.getName();
}

This method begins by assuming the current item has the maximum value, then checks adjacent items and updates the reference to maxItem if a neighboring item has a greater value. The method finally returns the name of the item with the highest value.

User Azamantes
by
6.5k points