Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write the Java implementation of the following assignment. Please actually write the code. After creating the Java code use the driver code to test

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedPlease write the Java implementation of the following assignment. Please actually write the code. After creating the Java code use the driver code to test it. In addition to the Java code please upload an image of the output and fully comment the code. Thank you.

Unit 2 Task: Mountain Paths Algorithm Background: In this task you will read a set of topographic (land elevation) data into a 2D array and write methods to compute paths through the mountains as well as display them visually. There are many contexts in which you want to know the most efficient way to travel over land. When traveling through mountains, it's often easiest to walk in the valleys than climb over the summits. Given some topographic data it should be possible to calculate an "easiest path" from one side of a map to the other. Typically, this is quantified by considering the least total elevation change. Curriculum Ties: you have researched and learned greedy, divide and conquer, and dynamic programming approaches to solving problems. You may choose any approach, but greedy is highly recommended. Using your technique, you will develop a solution to the "easiest path" problem. Since our map is in a 2D grid, we can envision a "walk" as starting in some in some cell at the leftmost edge of the map (column 0) and proceeding forward by taking a "step" into one of the three adjacent cells in the next column over (column 1). This might mean walking uphill or downhill depending on the elevation difference. The diagrams below show a few scenarios for choosing where to take the next step from the grey square. where to take the next step trom the grey square. Assignment Requirements: The minimum requirements for the assignment are that you write code to produce something like the map shown in the picture below. To do that you need to complete the following 5 steps: Step 1: Read the data into an appropriate data structure (a 2D array will suffice). Step 2: Find min, max elevations, and other calculations on the data. Step 3: Draw the map using a scaled color scheme (more on this later). Step 4: Draw the "easiest path" from each starting point on the west side (shown in red). Step 5: Draw the most absolute easiest route found by your algorithm (shown in green). The red paths represent lowest-elevation-change routes through various starting points on the west side of the map, whereas the green path represents the globally lowest-elevation-change route for the entire map using a particular algorithm. Technical Requirements: To fulfill the requirements for this task, you will be required to complete the provided MapDataDrawer class. You should open that file and understand the intentions of each method stub. The DrawingPanel class is used to host graphical content and should not be modified. The Driver class is to be used for testing purposes. You may modify it. The drawMap() method of the MapDataDrawer class interprets the data and draws the map in a scaled grayscale representation. The method will be passed the Graphics object you should use to do the drawing (this was demonstrated in the ICS 3U assignment). The method should "draw" the 2D data with the given Graphics object as a series of filled 11 rectangles with the color set to a grayscale value between white and black. Scaling \& Assigning Colors: The shade of gray should be scaled to the elevation of the map. Recall, in RGB color, if each of the RGB values are the same, you'll get a shade of gray. Thus, there are 256 possible shades of gray from black (0,0,0) to middle gray (128,128,128), to white (255,255,255). However, there are far more than 256 possible elevations! To choose the shade of gray for a given cell, you should use the min and max elevations to scale each elevation to a value between 0 and 255 inclusive. A quick Google Search should assist you! Try search terms such as scaling values to a range. Once you have found your value between 0 and 255 , you need to set the fill color just before drawing the rectangle. See the code below: int c=;// calculated grayscale value g.setColor(new Color(c, c, c )); g.fillRect (x,y,1,1); Test Data: Colorado_844x480.dat is a plain ascii text file. Each number represents the elevation of a 700700 patch of land in Colorado. The data comes as one large, spaceseparated list of integers. There are 403,200 integers representing a 480-row by 844-column grid. Each integer is the average elevation in meters of each cell of the grid. Read the documentation provided on the course website about how to read from a text file. import java.awt.*; // A class that contains the main method (driver) for the Unit 2 Task. public class Driver \{ public static void main(String[] args) \{ //construct DrawingPanel, and get its Graphics context DrawingPanel panel = new DrawingPanel (844, 480); Graphics g= panel.getGraphics(); //Test Step 1 - construct mountain map data 844)i //Test Step 2 - min, max, int min = map.findMinValue(); System. out. println("Min value in map: "+min); int max= map.findMaxValue ( ); System.out.println("Max value in map:"+max) //Test Step 3 - draw the map map.drawMap (g); //Test Step 4 - draw lowest- elevation path starting from row 5 (chosen randomly). Returns the total elevation change. g.setColor(Color.RED); //can set the color of the 'brush' before drawing, then method doesn't need to worry about it int change = map.drawLeastElevChangePath (g,5); //map.drawMap(g); //use this to get rid of all red lines previously drawn. // Draw all paths and obtain the row of the path with least elevation change. int bestRow = map.indexOfLeastElevChangePath(g); g.setColor(Color.GREEN); //set brush to green for drawing best path int leastChange = map.drawLeastElevChangePath(g, bestRow); System. out.println("The Lowest- Elevation-Change Path starts at row: "+bestRow+" and gives a total change of: "+leastChange); \} import java.io.*; import java.util.Scanner; public class FilelnputExample \{ public static void main(String[] args) throws FileNotFoundException \{ // Import your data file into the Java PROJECT folder, NOT the src folder, in Eclipse. File file = new File("InputFile.txt"); // Create a Scanner using the above file. Scanner sc = new Scanner(file); // Now use sc.nextlnt() to read an integer, sc.next() to read a String, etc. // No prompting for data (i.e asking a question) is required. \}

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

Students also viewed these Databases questions