Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

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 esch question, you may use any of the accessible methoxis that are listed in classes defined in that cuestion. Writing significant amounts of rade that can be replaced by a call to cine 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 ItenGrid class that manages a two-dimensional array of items. A delinition of the Item class is shown belan. public class Iten { private String name; private int value; public Iten(String itemName, int iterValue) name = itemName; value = itenValue; ) public String getName return name; } public int getValuci return value: } 1 The ItemGrid class w uses the two-dimensional array grid la represent group of Itemojects public class ItemGrid private Item) [] grid; // Canstructor not shown /+- Returns true if xPos is a valid row index and ypos is a valid * column index and returns false atherwise. public boolean isValid int xPos, int ypos) { /* implementation not shawn x } /** Compares the item in row r and column c to the items to its * left and to its right. Returns the name of the iter with of the greatest value, as described in part al. * Precondition: r and c are valid indices public String mastValuableNeighbar(int r, int c) { /* to be implemented in part (al */ } /** Returns the average value of all the items in grid, * as described in part 1b). public double findAverage() (/* to be implemented in part (b) */ } } (a) Write the most valuableNeighbor method, which compares the item in row r and column c to the items to its tand 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 raiteir to its lell, it is comparedi arily to the item ta is right. If the iter has noiten 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 xPosis a valid row index and ypos is a valid column index in the two-dimensional array grid, and retums false otherwise Assume that the ItenGrid object ig has been created such that the two-dimensional array grid contains the following item objects. The ItemGrid class w uses the two-dimensional array grid la represent group of Itemojects public class ItemGrid private Item) [] grid; // Canstructor not shown /+- Returns true if xPos is a valid row index and ypos is a valid * column index and returns false atherwise. public boolean isValid int xPos, int ypos) { /* implementation not shawn x } /** Compares the item in row r and column c to the items to its * left and to its right. Returns the name of the iter with of the greatest value, as described in part al. * Precondition: r and c are valid indices public String mastValuableNeighbar(int r, int c) { /* to be implemented in part (al */ } /** Returns the average value of all the items in grid, * as described in part 1b). public double findAverage() (/* to be implemented in part (b) */ } } (a) Write the most valuableNeighbor method, which compares the item in row r and column c to the items to its tand 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 raiteir to its lell, it is comparedi arily to the item ta is right. If the iter has noiten 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 xPosis a valid row index and ypos is a valid column index in the two-dimensional array grid, and retums false otherwise Assume that the ItenGrid object ig has been created such that the two-dimensional array grid contains the following item objects. 0 1 2 3 "carrot" "desk" 0 "acorn" 7 "book" 10 8 9 "egg" "flag" 1 "globe" 8 "harp" 9 5 8 2 "island" 7 "jacket" 19 "kale" 8 lunch" 16 The following table shows some examples of the behavior of the most valuableNeighbor method Return Value "haak" Method Call ig.mostValuableNeighborio, 2) ig.most valuableNeighborii, 1) ig.most valuableNeighbor(2, 0 ig.mostValuableNeighbor(2, 31 Explanation The itern at row.colurin 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) 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 (B). so either can be returned "+ laq" of "globe" "jacket" The item atrow 2. column ("island") has no item to its left, so it is only compared with the item to its nght ("jacket"). Of the two items, "jacket" has the greater value (19) "lunch" The item at row 2 column 3 ("lunch") has no item ta its right, so it is only compared with the item to its left ("kale''), of the two iterns, "lunch" has the greater value (16) Complete the mastValuanl Neighbor melted below. You must use isValid apprecipriately to receive full credit. Assume Ital grid has been initialized with at least three rows and three reilumns, and contains no null slements. Complete the mostValuableNeighbor method below. You must use is valid appropriately to receive full credit. Assume that grid has been initialized with at least three rows and three columns and contains no null elements. /x* 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 care valid indices public String most valuableNeighbori intr, int c) B 0/10000 Word Limit b) write the findAverage method, which returns the average value of all items in grid. For example, for the ItenGrid 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 med helt /** Returns the average value of all the items in grid, :: as described in part (b). public double findAverage() B 0/10000 Word Limit 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 esch question, you may use any of the accessible methoxis that are listed in classes defined in that cuestion. Writing significant amounts of rade that can be replaced by a call to cine 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 ItenGrid class that manages a two-dimensional array of items. A delinition of the Item class is shown belan. public class Iten { private String name; private int value; public Iten(String itemName, int iterValue) name = itemName; value = itenValue; ) public String getName return name; } public int getValuci return value: } 1 The ItemGrid class w uses the two-dimensional array grid la represent group of Itemojects public class ItemGrid private Item) [] grid; // Canstructor not shown /+- Returns true if xPos is a valid row index and ypos is a valid * column index and returns false atherwise. public boolean isValid int xPos, int ypos) { /* implementation not shawn x } /** Compares the item in row r and column c to the items to its * left and to its right. Returns the name of the iter with of the greatest value, as described in part al. * Precondition: r and c are valid indices public String mastValuableNeighbar(int r, int c) { /* to be implemented in part (al */ } /** Returns the average value of all the items in grid, * as described in part 1b). public double findAverage() (/* to be implemented in part (b) */ } } (a) Write the most valuableNeighbor method, which compares the item in row r and column c to the items to its tand 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 raiteir to its lell, it is comparedi arily to the item ta is right. If the iter has noiten 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 xPosis a valid row index and ypos is a valid column index in the two-dimensional array grid, and retums false otherwise Assume that the ItenGrid object ig has been created such that the two-dimensional array grid contains the following item objects. The ItemGrid class w uses the two-dimensional array grid la represent group of Itemojects public class ItemGrid private Item) [] grid; // Canstructor not shown /+- Returns true if xPos is a valid row index and ypos is a valid * column index and returns false atherwise. public boolean isValid int xPos, int ypos) { /* implementation not shawn x } /** Compares the item in row r and column c to the items to its * left and to its right. Returns the name of the iter with of the greatest value, as described in part al. * Precondition: r and c are valid indices public String mastValuableNeighbar(int r, int c) { /* to be implemented in part (al */ } /** Returns the average value of all the items in grid, * as described in part 1b). public double findAverage() (/* to be implemented in part (b) */ } } (a) Write the most valuableNeighbor method, which compares the item in row r and column c to the items to its tand 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 raiteir to its lell, it is comparedi arily to the item ta is right. If the iter has noiten 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 xPosis a valid row index and ypos is a valid column index in the two-dimensional array grid, and retums false otherwise Assume that the ItenGrid object ig has been created such that the two-dimensional array grid contains the following item objects. 0 1 2 3 "carrot" "desk" 0 "acorn" 7 "book" 10 8 9 "egg" "flag" 1 "globe" 8 "harp" 9 5 8 2 "island" 7 "jacket" 19 "kale" 8 lunch" 16 The following table shows some examples of the behavior of the most valuableNeighbor method Return Value "haak" Method Call ig.mostValuableNeighborio, 2) ig.most valuableNeighborii, 1) ig.most valuableNeighbor(2, 0 ig.mostValuableNeighbor(2, 31 Explanation The itern at row.colurin 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) 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 (B). so either can be returned "+ laq" of "globe" "jacket" The item atrow 2. column ("island") has no item to its left, so it is only compared with the item to its nght ("jacket"). Of the two items, "jacket" has the greater value (19) "lunch" The item at row 2 column 3 ("lunch") has no item ta its right, so it is only compared with the item to its left ("kale''), of the two iterns, "lunch" has the greater value (16) Complete the mastValuanl Neighbor melted below. You must use isValid apprecipriately to receive full credit. Assume Ital grid has been initialized with at least three rows and three reilumns, and contains no null slements. Complete the mostValuableNeighbor method below. You must use is valid appropriately to receive full credit. Assume that grid has been initialized with at least three rows and three columns and contains no null elements. /x* 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 care valid indices public String most valuableNeighbori intr, int c) B 0/10000 Word Limit b) write the findAverage method, which returns the average value of all items in grid. For example, for the ItenGrid 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 med helt /** Returns the average value of all the items in grid, :: as described in part (b). public double findAverage() B 0/10000 Word Limit

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

Database Systems On GPUs In Databases

Authors: Johns Paul ,Shengliang Lu ,Bingsheng He

1st Edition

1680838482, 978-1680838480

More Books

Students also viewed these Databases questions

Question

Describe how common-size statements are useful.

Answered: 1 week ago