Search code examples
javaarraysreturn

What exactly should I be returning in this code?


We are creating a program that will return a topographic map and show the user the best way to navigate the terrain without making steep climbs or descents. For the files that I am working with, I am not sure what maximum and minimum values need to be returned. The assignment is mostly complete except for 3 errors regarding an expected return value. I've submitted the primary code that we are working with and included a link to the other files the professor has given us for the assignment.

I've tried return maxValue, return minValue, return max, return min, and a few other combinations but the issue is I'm not sure if I am supposed to be returning a value from this MapDataDrawer.java file or from one of the other two files that we have to use for the assignment from the professor.

//MapDataDrawer.java
//This is the code that is returning the error
import java.util.*;
import java.io.*;
import java.awt.*;

public class MapDataDrawer
{

  private int[][] grid;

  public MapDataDrawer(String filename, int rows, int cols){
      // initialize grid 
      grid = new int[rows][cols];

      //read the data from the file into the grid
      File dataFile = new File(filename);
      try {
         Scanner dataInput = new Scanner(dataFile);
         for (int i=0; i<rows; i++) {
            for (int j=0; j<cols;j++) {
               grid[i][j] = dataInput.nextInt();

            }
         }

      } catch (Exception e) { e.printStackTrace();}

  }

  /**
   * @return the min value in the entire grid
   */
  public int findMin() {
     // Implement this method
  }
  /**
   * @return the max value in the entire grid
   */
  public int findMax(){
     // Implement this method
  }

  /**
   * @param col the column of the grid to check
   * @return the index of the row with the lowest value in the given col for the grid
   */
  public  int indexOfMinRow(int col){
     //Implement this method

  }

  /**
   * Draws the grid using the given Graphics object.
   * Colors should be grayscale values 0-255, scaled based on min/max values in grid
   */
  public void drawMap(Graphics g){
      int min = findMin();
      int max = findMax();

      for (int i=0; i<480; i++) {
         for (int j=0; j<480; j++) {
            int c = (255 * (grid[i][j] - min)) / (max - min);
            g.setColor(new Color(c, c, c));
            g.fillRect(j, i, 1, 1);
         }
      }
   }

   /**
   * Find a path from West-to-East starting at given row.
   * Choose a foward step out of 3 possible forward locations, using greedy method described in assignment.
   * @return the total change in elevation traveled from West-to-East
   */
  public int drawLowestElevPath(Graphics g, int row){
    int elevChange = 0;
      // Implement this method
      return elevChange;

  }

   private int minOfThree(int a, int b, int c) {
      if ((a > b) && (a > c)) return a;
      if ((b > a) && (b > c)) return b;
      if ((c > a) && (c > b)) return c;
      return 0;
   }

}

These are the professors submitted files, of which I am unsure if I am supposed to be returning some value from one of these files or from the actual homework file (MapDataDrawer.java) https://drive.google.com/drive/folders/1siRoY1K0ngptE2rL-wscXLK8Ct7Qo1hb?usp=sharing

It also includes the topographic data we are supposed to use.


Solution

  • As you have filled grid:

    public int findMin() {
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
               int value = grid[i][j];
               if (value < min) {
                   min = value;
               }
            }
         }
         return min;
     }