Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given a 2D array, an hourglass is a selection of values shaped like this: a b c d e f g For example, if I

Given a 2D array, an hourglass is a selection of values shaped like this:

a b c d e f g 

For example, if I create an hourglass with ones within a 2D array of zeros it may look like this:

1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

There are actually 16 hourglasses in the 6x6 2D array above. The three leftmost hourglasses at the top of the 2D array are the following:

1 1 1 1 1 0 1 0 0 1 0 0 1 1 1 1 1 0 1 0 0 

The sum of an hourglass is the sum of all the numbers within it. The sum for the hourglasses above are 7, 4, and 2, respectively.

In this problem you have to print the largest sum among all the hourglasses in the array.

Directions

Download the starter code (Runner.java and Grid.java) and the .txt files (grid1.txt, grid2.txt, and grid3.txt). Then, ADD your implementation for the largestHourglass() method and COMPLETE the Runner class. THERE ARE TWO CLASSES!

Read the comments of the provided methods carefully so you know what they do and how to use them.

Test Outputs

grid1.txt: 13

grid2.txt: 19

grid3.txt: 52

Here are the attached files. Unfortunately I cannot upload files so I will be pasting the contents here.

RUNNER.JAVA

/** * * @author * @version */

import java.util.Scanner;

public class Runner { public static void main(String[] args) { Scanner in = new Scanner(System.in); // take input for file name // create object // print hourglass sum } }

GRID.JAVA

/** * This class represents a 6x6 grid of integers * which holds "hourglasses" as shown below. * * EXAMPLE * * 1 3 2 1 1 1 * 2 3 3 -4 5 5 * 3 -2 11 12 3 3 * 2 2 1 1 2 2 * 1 1 2 1 1 11 * 2 3 22 21 1 1 1 * * There are 16 hourglasses that follow the pattern below: * * a b c * d * e f g * * So few from above (top left and bottom left) include: * * 1 3 2 * 3 * 3 -2 11 * * 2 2 1 * 1 * 2 3 22 * * @author * @version */

import java.util.Scanner; import java.io.IOException; import java.io.File;

public class Grid { private int[][] grid; private final int NUM_ROWS = 6; private final int NUM_COLS = 6; // Constructor // initialize instance variable // fileName is name of file to be read public Grid(String fileName) { grid = new int[NUM_ROWS][NUM_COLS]; readFile(fileName); } // pre: none // post: returns value of largest (by sum) hourglass in grid public int largestHourglass() { // TODO implement this method to find the largest sum } // pre: fileName to read as a String // post: populates grid with values from text file public void readFile(String fileName) { int r = 0; int c = 0; Scanner inFile = null; try { inFile = new Scanner(new File(fileName)); } catch(IOException e) { e.printStackTrace(); return; } while(inFile.hasNext()) // stop parsing file when there are no more values { grid[r][c++] = inFile.nextInt(); // populate our 2D array with integers from file if(c > NUM_COLS - 1) { r++; c = 0; } } } }

GRID1.TXT

1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 9 2 -4 -4 0 0 0 0 -2 0 0 0 0 -1 -2 -4 0

GRID2.TXT

1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 2 4 4 0 0 0 0 2 0 0 0 0 1 2 4 0

GRID3.TXT

1 3 2 1 1 1 2 3 3 -4 5 5 3 -2 11 12 3 3 2 2 1 1 2 2 1 1 2 1 1 11 2 3 22 21 1 1

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

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Carry out an interview and review its success.

Answered: 1 week ago