Advertisement
cgorrillaha

Untitled

Feb 3rd, 2022
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.65 KB | None | 0 0
  1. /** Compares the item in row r and column c to the items to its
  2.  
  3. * left and to its right. Returns the name of the item with
  4.  
  5. * the greatest value, as described in part (a).
  6.  
  7. * Precondition: r and c are valid indices
  8.  
  9. */
  10.  
  11. public String mostValuableNeighbor(int r, int c){
  12.     Item mostVal=null;
  13.     for(int i=c-1; i<=c+1; i++){
  14.         if(isValid(r, i)&&grid[r][i].getValue()>mostVal.getValue()){
  15.             mostVal=grid[r][i];
  16.         }
  17.     }
  18.     return mostVal.getName();
  19. }
  20.  
  21. public double findAverage(){
  22.     int numElements=grid.length*grid[0].length;
  23.     int sum=0;
  24.     for(Item[]row:grid){
  25.         for(Item i: row){
  26.             sum+=i.getValue();
  27.         }
  28.     }
  29.     return (double)sum/numElements;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement