Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Why arent both my arrays printing the exact same thing? I am using Java. I am ju

ID: 3879686 • Letter: W

Question

Why arent both my arrays printing the exact same thing? I am using Java. I am just trying to test so I can get the rest of my code working, and now im realizing that my first part(the code I pasted here) is not working.

Here is the output:

Enter how big you want the 2D array with spaces. M followed by N (array size MxN):
5
5

Enter the probability of existance of termites in decimal (.10 equals 10 percent):
.4
..**.
....*
..***
.*.*.
***.*
Neighbor array
***.*
*.*.*
....*
..***
..*..

///////////////////////////////CODE/////////////////////////////////////////////////////////////////////////////

import java.util.Random;
import java.util.Scanner;

public class DUBOCOR_Assign1B {

   public static void main(String args[]) {
      
       int m = 0;
       int n = 0;
         double p = 0.0;
      
       
       System.out.println("Enter how big you want the 2D array with spaces. M followed by N (array size MxN): ");
       Scanner mainInputs = new Scanner(System.in);
      

       m = mainInputs.nextInt();
       n = mainInputs.nextInt();
      
       System.out.println("Enter the probability of existance of termites in decimal (.10 equals 10 percent): ");
       p = mainInputs.nextDouble();
      

          mainInputs.close();
      
       termiteNest(m,n,p);
       showTermites(m,n,p);
       neighbors(m,n,p);
       // printNeighbors(m,n,p);

   }

  
   public static boolean[][] termiteNest(int m, int n, double p) {
      

      
      
           Random random = new Random();
      
           boolean [][]Mites = new boolean[n][m];
      
           for (int i = 0; i < n; i++) {   //rows
               for (int j = 0; j                    double newRandom = random.nextDouble();
                   if(newRandom >= p) {
                       Mites[i][j] = false;              // stays false (no termites)
                       }
                   if (newRandom < p) {                 // if new random # is less than the probability, make that cell true
                       Mites[i][j] = true;
                       }
                  
               }
           }
  
       return Mites;               // my array of mites
      
   }

  
   public static void showTermites(int m, int n, double p) {
  
           
      
      
               boolean [][]termiteNest = termiteNest(m,n,p);

               for (int i = 0; i < n; i++) {   //rows
                   for (int j = 0; j                        if (termiteNest[i][j] == true){
                           System.out.print("*");         //occupied cell
                       }
                       if (termiteNest[i][j] == false){
                           System.out.print(".");        //safe cell
                         }
                      
                     }
                   System.out.println();

               }

       }
  
  
  
       public static void neighbors(int m, int n, double p){
          
          
             
              System.out.println("Neighbor array");
          
          
          
               boolean [][]termiteNest = termiteNest(m,n,p);

               for (int i = 0; i < n; i++) {   //rows
                   for (int j = 0; j                        if (termiteNest[i][j] == true){
                           System.out.print("*");         //occupied cell
                       }
                       if (termiteNest[i][j] == false){
                           System.out.print(".");        //safe cell
                         }
                      
                     }
                   System.out.println();

               }

}

}

Explanation / Answer

Hello, I don’t really know what exactly you are trying to achieve making both methods showTermites() and neighbors() same. In case you are just testing it, and wondering why the methods print different arrays even though they have the same code, the answer is simple, inside both methods, you make a call to the termiteNest() method which will randomly create a boolean array as per the probability, so in both showTermites() and neighbors() methods, these calls will generate two random arrays, which is why they’re different.

See the highlighted code, which is creating a completely random 2d array each time it is executed.

public static void showTermites(int m, int n, double p) {

                        boolean[][] termiteNest = termiteNest(m, n, p);

                        for (int i = 0; i < n; i++) { // rows

                                    for (int j = 0; j < m; j++) {

                                                if (termiteNest[i][j] == true) {

                                                            System.out.print("*"); // occupied cell

                                                }

                                                if (termiteNest[i][j] == false) {

                                                            System.out.print("."); // safe cell

                                                }

                                    }

                                    System.out.println();

                        }

            }

            public static void neighbors(int m, int n, double p) {

                        System.out.println("Neighbor array");

                        boolean[][] termiteNest = termiteNest(m, n, p);

                        for (int i = 0; i < n; i++) { // rows

                                    for (int j = 0; j < m; j++) {

                                                if (termiteNest[i][j] == true) {

                                                            System.out.print("*"); // occupied cell

                                                }

                                                if (termiteNest[i][j] == false) {

                                                            System.out.print("."); // safe cell

                                                }

                                    }

                                    System.out.println();

                        }

            }

So, if you are planning to work on one single array, I suggest you to change method signatures of both, to allow passing an array as argument. So that we can create a 2d array outside the method, and then pass to it, and then it will print the same output.

Following is the modified code that accepts an array as argument.

public static void showTermites(int m, int n, double p, boolean[][] termiteNest) {

                        for (int i = 0; i < n; i++) { // rows

                                    for (int j = 0; j < m; j++) {

                                                if (termiteNest[i][j] == true) {

                                                            System.out.print("*"); // occupied cell

                                                }

                                                if (termiteNest[i][j] == false) {

                                                            System.out.print("."); // safe cell

                                                }

                                    }

                                    System.out.println();

                        }

            }

            public static void neighbors(int m, int n, double p,boolean[][] termiteNest) {

                        System.out.println("Neighbor array");

                        for (int i = 0; i < n; i++) { // rows

                                    for (int j = 0; j < m; j++) {

                                                if (termiteNest[i][j] == true) {

                                                            System.out.print("*"); // occupied cell

                                                }

                                                if (termiteNest[i][j] == false) {

                                                            System.out.print("."); // safe cell

                                                }

                                    }

                                    System.out.println();

                        }

            }

And along with this, change the calls to these methods in the main method to the following way.

boolean[][] array=termiteNest(m, n, p);

showTermites(m, n, p,array);

neighbors(m, n, p,array);

This will print the same output.

Complete code:

//DUBOCOR_Assign1B.java

import java.util.Random;

import java.util.Scanner;

public class DUBOCOR_Assign1B {

      public static void main(String args[]) {

            int m = 0;

            int n = 0;

            double p = 0.0;

            System.out

                        .println("Enter how big you want the 2D array with spaces. M followed by N (array size MxN): ");

            Scanner mainInputs = new Scanner(System.in);

            m = mainInputs.nextInt();

            n = mainInputs.nextInt();

            System.out

                        .println("Enter the probability of existance of termites in decimal (.10 equals 10 percent): ");

            p = mainInputs.nextDouble();

            mainInputs.close();

            boolean[][] array=termiteNest(m, n, p);

            showTermites(m, n, p,array);

            neighbors(m, n, p,array);

            // printNeighbors(m,n,p);

      }

      public static boolean[][] termiteNest(int m, int n, double p) {

            Random random = new Random();

            boolean[][] Mites = new boolean[n][m];

            for (int i = 0; i < n; i++) { // rows

                  for (int j = 0; j < m; j++) {

                        double newRandom = random.nextDouble();

                        if (newRandom >= p) {

                              Mites[i][j] = false; // stays false (no termites)

                        }

                        if (newRandom < p) { // if new random # is less than the

                                                            // probability, make that cell true

                              Mites[i][j] = true;

                        }

                  }

            }

            return Mites; // my array of mites

      }

      public static void showTermites(int m, int n, double p, boolean[][] termiteNest) {

            for (int i = 0; i < n; i++) { // rows

                  for (int j = 0; j < m; j++) {

                        if (termiteNest[i][j] == true) {

                              System.out.print("*"); // occupied cell

                        }

                        if (termiteNest[i][j] == false) {

                              System.out.print("."); // safe cell

                        }

                  }

                  System.out.println();

            }

      }

      public static void neighbors(int m, int n, double p,boolean[][] termiteNest) {

            System.out.println("Neighbor array");

            for (int i = 0; i < n; i++) { // rows

                  for (int j = 0; j < m; j++) {

                        if (termiteNest[i][j] == true) {

                              System.out.print("*"); // occupied cell

                        }

                        if (termiteNest[i][j] == false) {

                              System.out.print("."); // safe cell

                        }

                  }

                  System.out.println();

            }

      }

}

/*output*/

Enter how big you want the 2D array with spaces. M followed by N (array size MxN):

5

5

Enter the probability of existance of termites in decimal (.10 equals 10 percent):

.2

.**..

...*.

.....

....*

**...

Neighbor array

.**..

...*.

.....

....*

**...

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote