Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** 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){
- Item mostVal=null;
- for(int i=c-1; i<=c+1; i++){
- if(isValid(r, i)&&grid[r][i].getValue()>mostVal.getValue()){
- mostVal=grid[r][i];
- }
- }
- return mostVal.getName();
- }
- public double findAverage(){
- int numElements=grid.length*grid[0].length;
- int sum=0;
- for(Item[]row:grid){
- for(Item i: row){
- sum+=i.getValue();
- }
- }
- return (double)sum/numElements;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement