Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class A9 {
  5.  
  6.     public static void main(String[] args) {
  7.    
  8.         int trees;
  9.         double density;
  10.         int treecount=0;
  11.         int burncount=0;
  12.         int burncount2=0;
  13.        
  14.         Scanner scan=new Scanner(System.in);
  15.    
  16.                 //Error checking for number of trees
  17.                 do
  18.                 {   System.out.print("Enter number of trees: ");
  19.                     trees=scan.nextInt();  
  20.    
  21.                 } while (trees<=10 || trees>=30);
  22.                
  23.                 //Error checking for proper density amount
  24.                 do
  25.                 {
  26.                     System.out.print("Enter tree density: ");
  27.                     density=scan.nextDouble();
  28.                 } while (density<=.20 || density >=.80);
  29.                 scan.close();
  30.                
  31.                 double [][] forest= new double [trees][trees];
  32.  
  33.                
  34.                 //fill the grid
  35.                 for (int row = 0; row < forest.length; row++) {
  36.                     for (int col = 0; col < forest[row].length; col++) {
  37.                         forest[row][col] = Math.random();
  38.                     }
  39.                 }
  40.                
  41.                     System.out.println("");
  42.                     System.out.println("The original forest:");
  43.                     print2D(forest,density,treecount,trees);
  44.                    
  45.                     System.out.println("The final forest:");
  46.                     burn2D(forest,density,burncount,trees,burncount2);
  47.                 }
  48.  
  49.              
  50.    
  51.    
  52.    
  53.                 public static void print2D (double [][] a,double d,int c,int t ) {
  54.                    
  55.                     //Prints the original forest
  56.                     for(int i = 0; i <a.length; i++) {
  57.                         for(int j = 0; j < a[i].length; j++) {
  58.                            
  59.                             if (a[i][j]<d) {
  60.                                 System.out.print('T'+" ");
  61.                                 c++;}
  62.                             else
  63.                                 System.out.print('.'+" ");
  64.                
  65.                         }
  66.                         System.out.println();
  67.                         }
  68.                         System.out.println("Percent populated: "+(double)c/Math.pow(t, 2));
  69.                         System.out.println("");
  70.                        
  71.                     }
  72.                
  73.                
  74.                 public static void burn2D (double[][] a,double d, int c, int t,int c2) {
  75.                    
  76.                     //Ignites the first row of 2D array on fire
  77.                     for(int q = 0; q <1; q++) {
  78.                         for(int p = 0; p < t; p++) {
  79.                            
  80.                             if (a[q][p]<d) {
  81.                                 System.out.print('B'+" ");
  82.                                 c2++;}
  83.                             else
  84.                                 System.out.print('.'+" ");
  85.              
  86.                         }
  87.                          System.out.println();
  88.                    
  89.                              
  90.         //Problems are with this nested for loop
  91.                     for(int i = 1; i <a.length; i++) {
  92.                         for(int j = 0; j < a[i].length; j++) {
  93.                            
  94.                                 if (a[i][j-1]=='B') //checks row above for 'B'
  95.                                 System.out.print('B'+" ");
  96.                            
  97.                                 else if (a[i+1][j-1]=='B') { //checks top right for 'B'
  98.                                     System.out.print('B'+" ");
  99.                                 }
  100.                                
  101.                                 else if (a[i-1][j-1]=='B') { //checks top left for 'B'
  102.                                     System.out.print('B'+" ");
  103.                                 }
  104.                                
  105.                                
  106.                                 else if (a[i][j]<d) {
  107.                                 System.out.print('T'+" ");
  108.                                 c++;}
  109.                            
  110.                                 else
  111.                                 System.out.print('.'+" ");
  112.              
  113.                         }
  114.                          System.out.println();
  115.            
  116.             }
  117.                    
  118.                    
  119.         }
  120.     }  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement