138k views
3 votes
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) */ }

}

(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.

User Winfield
by
8.3k points

1 Answer

3 votes

The `mostValuableNeighbor` method in the `ItemGrid` class compares the value of an item at position (r, c) with its left and right neighbors, returning the name of the item with the highest value.

Below is the implementation of the `mostValuableNeighbor` method in the `ItemGrid` class:

```java

public String mostValuableNeighbor(int r, int c) {

// Get the item at the specified position

Item currentItem = grid[r][c];

// Get the item to the left (if it exists)

Item leftNeighbor = (c > 0) ? grid[r][c - 1] : null;

// Get the item to the right (if it exists)

Item rightNeighbor = (c < grid[0].length - 1) ? grid[r][c + 1] : null;

// Compare values and determine the name of the most valuable neighbor

if (leftNeighbor != null && rightNeighbor != null) {

if (leftNeighbor.getValue() >= rightNeighbor.getValue() && leftNeighbor.getValue() >= currentItem.getValue()) {

return leftNeighbor.getName();

} else if (rightNeighbor.getValue() >= leftNeighbor.getValue() && rightNeighbor.getValue() >= currentItem.getValue()) {

return rightNeighbor.getName();

}

} else if (leftNeighbor != null && leftNeighbor.getValue() >= currentItem.getValue()) {

return leftNeighbor.getName();

} else if (rightNeighbor != null && rightNeighbor.getValue() >= currentItem.getValue()) {

return rightNeighbor.getName();

}

// If the current item is the most valuable or has no neighbors, return its name

return currentItem.getName();

}

```

This method compares the value of the current item with its neighbors to the left and right, considering edge cases where the item may not have a neighbor on one side. The name of the item with the greatest value (or one of them if there's a tie) is then returned.

User Wheat Wizard
by
7.2k points