Question

In: Computer Science

Java- Create a new class named Forest that has a 2D array of integers to store...

Java-

Create a new class named Forest that has a 2D array of integers to store a forest.

Create a private inner class Position to store the row and column of the current cell.

Create a Depth First Search Method that uses a stack to remove the current tree and search its neighbors based on the following pseudocode:

// given a forest "f"
// store positions on fire
Stack<Position> cellsToExplore

// the forest is aflame!
for each tree in the top row:
  add position to cellsToExplore
  mark tree as "on fire"

// continue lighting neighbors on fire
while cellsToExplore is not empty
  pop the stack into currentPosition
  return true if currentPosition is on the bottom row
  for each neighboring tree
    push location onto the stack
    mark tree as "on fire"

// must not have reached the bottom row...
return false

Solutions

Expert Solution

The solution to the above problem in java is as follows:-

Code-

import java.io.*;
import java.util.*;

public class Forest {
   int [][] array; //To store 2d array
   public Forest(int r,int c){ //Constructor to initialize 2d array
       array=new int[r][c];
       for(int i=0;i<array.length;i++)
           for(int j=0;j<array[i].length;j++)
               array[i][j]=0; //Tree not on fire
   }
   public boolean dfs(){ // Dfs implemented according to question
       // store positions on fire
       Stack<Position> cellsToExplore=new Stack<Position>();
       // the forest is aflame!
       for(int i=0;i<array[0].length;i++){ //for each tree in the top row:
           Position x=new Position(0,i);
           cellsToExplore.push(x); // add position to cellsToExplore
           array[0][i]=1; // mark Tree as "on fire"
       }


       // continue lighting neighbors on fire
       while(!cellsToExplore.empty()){
           Position top=cellsToExplore.pop(); //pop the stack into currentPosition
           if(top.row==array.length-1) // Checking if we are in the bottom row
               return true; // return true if currentPosition is on the bottom row
           int x=top.row, y=top.col;
           if(x+1<array.length){
               array[x+1][y]=1; // mark Tree as "on fire"
               Position neighbour=new Position(x+1,y);
               cellsToExplore.push(neighbour); //push location onto the stack
           }
       }
       // must not have reached the bottom row...
       return false;
   }
   //Inner Postion Class
   private class Position{
       int row;
       int col;
       public Position(int r,int c){
           row=r;
           col=c;
       }
   }
   public static void main(String[] args) throws IOException {
       int r,c; // Taking row and column of dfs as input
       System.out.println("Enter row and columns for the 2D array of Forest:");
       Scanner sc= new Scanner(System.in);
       r=sc.nextInt();
       c=sc.nextInt();
       Forest x=new Forest(r,c); //initialize forest
       System.out.println("Are all trees burnt? "+x.dfs()); //run dfs
   }
}


Code Screenshots-

Outputs-

Feel free to comment for any issues, do rate the answer positively


Related Solutions

Create in Java Create a stack class to store integers and implement following methods: 1- void...
Create in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
In java: -Create a class named Animal
In java: -Create a class named Animal
An exception with finally block lab. Create a new class named ReadArray Create simple array and...
An exception with finally block lab. Create a new class named ReadArray Create simple array and read an element using a try block Catch any exception Add a finally block and print a message that the operation if complete
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1....
Create a Java Program/Class named MonthNames that will display the Month names using an array. 1. Create an array of string named MONTHS and assign it the values "January" through "December". All 12 months need to be in the array with the first element being "January", then "February", etc. 2. Using a loop, prompt me to enter an int variable of 1-12 to display the Month of the Year. Once you have the value, the program needs to adjust the...
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT