Question: CAN SOMEONE SOLVE THIS IN JAVA,I'VE ALSO ADDED THREE FILES AT THE END FOR YOUR HELP. THE CODE NAMES ARE: LEAFPILE.JAVA, GROUND.JAVA, POSITION.JAVA...... No recursion
CAN SOMEONE SOLVE THIS IN JAVA,I'VE ALSO ADDED THREE FILES AT THE END FOR YOUR HELP. THE CODE NAMES ARE: LEAFPILE.JAVA, GROUND.JAVA, POSITION.JAVA......No recursion is allowed. The algorithm must also utilize either a stack or a queue or a deque.
you are provided with a main class (LeafPile.java), Position class (Position.java) and the Ground enum (Ground.java). Do NOT change the Ground enum or the provided methods inside LeafPile.java except any you create and the body of the provided largestLeafPile method. You may add all necessary methods to solve the problem that you need to inside main. Modifying the enum or the provided methods will cost you points. You may change the map[][]array in the main method to different sizes for testing purposes.
The LeafPile Problem
Suppose you have an grid in which either grass or a leaf is in each cell of the grid (but not both). Your assignment is to determine the largest leaf pile on the map. Your algorithm may not utilize recursion to solve the problem. It must be purely iterative using either a stack or a queue.
The grid internally is represented like this
In which GRASS and LEAF are the enum values from Ground.java and this is a array.
For the purposes of shortness, GRASS will be represented by the period symbol . and LEAF will be represented by the tilde symbol ~ for the rest of the matrices in this document.
What constitutes a leaf pile?
A leaf is part of a pile if it touches another leaf in the four cardinal directions of north, east, south and west. Leaves that touch diagonally are not part of the pile. So, from the matrix above the piles are
Each pile is represented as a list of tuples that contain the coordinates of a leaf in the form of (row, column) or (y, x) such that y starts at the top and moves down while x starts at the left of the row and moves right.
As you can see, the pile with the most leaves is pile 2.
Provided Files
You are given three files, Ground.java, Position.java and LeafPile.java.
Ground.java contains a public enum which contains two types, LEAF and GRASS. A LEAF is represented by the symbol tilde while grass is a period. You may change these to your liking.
The main class (LeafPile.java) contains the following utility methods
printMap(Ground map[][]) - Which prints a 2 dimensional Ground type array.
generateRandomGround(Ground map[][]) - Takes in a 2 dimensional Ground array and generates a random map
largestLeafPile(Ground map[][]) - This is the method you need to implement. It should return the pile with the most leaves.
Along with the largestLeafPile method you may also add any additional methods to help you solve the problem.
The position class (Position.java) contains the following
A public final int row.
A public final int column.
A constructor that takes a row and a column and sets the above variables.
Requirements:
No recursion is allowed. The algorithm must also utilize either a stack or a queue or a deque.
You must write your own stack, queue, or deque class. This problem can be solved with either of the three so pick one and stick with it.
Your algorithm must return the correct leaf pile size and must be able to work with different sized arrays. Rows and columns may not be the same, so you should not assume that they are.
LEAFPILE.JAVA:
public class LeafPile { public static void main(String[] args) { Ground map[][] = new Ground[7][11]; generateRandomGround(map); printMap(map); System.out.println(largestLeafPile(map)); } /**********Student code here***************************/ private static int largestLeafPile(Ground map[][]) { } /***************End Student Code************************/ private static void printMap(Ground map[][]) { for (int r = 0; r < map.length; r++) { for (int c = 0; c < map[r].length; c++) { System.out.print(map[r][c]); if (c < map[r].length - 1) { System.out.print(' '); } } System.out.println(); } } private static void generateRandomGround(Ground map[][]) { double randGround; double randMax = 100; double percentGrass = .70; for (int r = 0; r < map.length; r++) { for (int c = 0; c < map[r].length; c++) { randGround = (Math.random() * randMax) + 1; map[r][c] = randGround <= randMax * percentGrass ? Ground.GRASS : Ground.LEAF; } } } } GROUND.JAVA:
public enum Ground { GRASS('-'), LEAF('~'); private char symbol; private Ground(char value) { symbol = value; } public String toString() { return "" + symbol; } } POSITION.JAVA:
public class Position { public final int row; public final int column; public Position(int row, int column) { this.row = row; this.column = column; } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
