Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Flood Map You are given a two-dimensional array of values that give the height o

ID: 670535 • Letter: F

Question

Flood Map You are given a two-dimensional array of values that give the height of a terrain at different points in a square. Create a class called Terrain with a constructor public Terrain (double | | | | heights) and a method public void printFloodMap ( double waterLevel) that prints out a flood map, showing which of the points in the terrain would be flooded if the water level was the given value. In the flood map, print a * for each flooded point and a space for each point that is not flooded. Here is a sample map:

Explanation / Answer

Program:


public class Terrain {
double terrain[][];
public Terrain(double[][] heights)
{
terrain=heights;
}
  
public void printFloodMap(double waterLevel)
{
System.out.println("Flood Map");
for(int i=0;i<terrain.length;i++)
{
System.out.println("");
for(int j=0;j<terrain.length;j++)
{
if(terrain[i][j]>waterLevel)
System.out.print("*");
else
System.out.print(" ");
}
}
}
public static void main(String args[])
{
double heights[][]={ // user can input
{12.1,10,14.0,5.2,3.2,11.6,8.2,6.9,7.9,10},
{11.1,4,7.0,12.2,11.2,11.4,1.2,7.0,7.2,5.4},
{5.7,8,6.0,12.2,11.2,11.4,1.2,7.0,7.2,5.4},
{9.8,5,14.0,12.2,11.2,11.4,1.2,7.0,7.2,5.4},
{31,1,15.0,12.2,11.2,71.4,7.2,3.0,5.2,5.4},
{6.1,9,6.0,17.2,11.2,19.4,1.2,8.0,9.2,56.4},
{9.3,10,9.8,0,14.2,19.2,16.4,45.2,30.0,44.2},
{4.1,45.9,7.0,62.2,61.2,1.4,7.2,8.0,9.2,4.4},
{3.2,7,7.0,11.2,91.2,1.4,11.2,72.0,67.2,8.4},
{11.9,67,7.0,8.2,19.2,31.4,11.2,77.0,2.2,7.4}
};
Terrain T=new Terrain(heights);
T.printFloodMap(5.2); // user can input
}
}

Output:


Flood Map

*** *****
* **** ***
****** ***
* **** ***
* ***** *
****** ***
*** ******
**** ***
**** ****
******** *