Display two array instances

1

Good morning, I am involved in designing two boards for a game (Sink the Fleet), in small steps, I have a class MyDesk where I have created two instances of the board, however, when calling both from a main class ( MyDesk barco = new MyDesk() EnemyDesk enemigo = new EnemyDesk() ), only the first one is displayed, is it because both are included in the same class?

import java.util.Arrays;

class MyDesk {

    int [][] barcosvivos;

    public MyDesk(){
        this.barcosvivos = new int[5][5];
        barcosvivos [1][0] = 1; barcosvivos [2][0] = 1; barcosvivos[3][0]=1;
        barcosvivos [1][2]= 1; barcosvivos [1][3]= 1; barcosvivos [1][4]= 1;
        for (int []interno: this.barcosvivos){System.out.println(Arrays.toString(interno));}    
    }

    public MyDesk (int [][] EnemyDesk){
        EnemyDesk = new int [5][5];
        EnemyDesk [0][2] = 3; EnemyDesk [0][3] = 3; EnemyDesk [0][4] = 3;
        EnemyDesk [2][0] = 3; EnemyDesk [2][1] = 3; EnemyDesk [2][2] = 3;
        for (int []interno2: EnemyDesk){System.out.println(Arrays.toString(interno2));}
    }
}

The code I use in Main is:

public class Main {
    public static void main (String [] args){
        MyDesk barco = new MyDesk();
        MyDesk prueba = new MyDesk();
    } 
}
    
asked by Pablo León 22.04.2016 в 12:21
source

2 answers

4

I do not know that you have EnemyDesk in the class, but you are overloading the MyDesk class constructor incorrectly.

import java.util.Arrays;

class MyDesk {

    int [][] barcosvivos;
    int [][] barcosenemigos;

    public MyDesk(){
        barcosvivos = new int[5][5];
        barcosvivos [1][0] = 1; barcosvivos [2][0] = 1; barcosvivos[3][0]=1;
        barcosvivos [1][2]= 1; barcosvivos [1][3]= 1; barcosvivos [1][4]= 1;

        barcosenemigos = new int [5][5];
        barcosenemigos[0][2] = 3; barcosenemigos[0][3] = 3; barcosenemigos[0][4] = 3;
        barcosenemigos[2][0] = 3; barcosenemigos[2][1] = 3; barcosenemigos[2][2] = 3;
    }

    public void showAllyDesk () {
        for (int []interno: this.barcosvivos){System.out.println(Arrays.toString(interno));}    
    }

    public void showEnemyDesk () {
        for (int []interno2: this.barcosenemigos){System.out.println(Arrays.toString(interno2));}
   }
}

And then in your main

MyDesk tablero = new MyDesk();
tablero.showAllyDesk();
tablero.showEnemyDesk();

Of course, if you want the boards to vary the position of the boats every time you execute them, you will have to remove them from the constructor, or put together something that generates them in a random way, I really do not know where your question is pointing.

    
answered by 22.04.2016 / 16:22
source
4

I assume that in its main it has something like this:

MyDesk barco = new MyDesk();
EnemyDesk enemigo = new EnemyDesk();

If the above compiles, you have a class of type EnemyDesk, but in this part public MyDesk (int [][] EnemyDesk){ use the same word I do not know if what I wanted is to be able to pass an EnemyDesk instance or simply pass the convention, I think and I think it is the second one, because inside the constructor you treat EnemyDesk as an array of integrated types of some kind, among other things but I think that at first glance it is confused by the simple fact of seeing int [][] EnemyDesk and previously putting EnemyDesk enemigo = new EnemyDesk(); .

Now if I understand your question well, maybe what I'm looking for is something like this:

public static void main (String[] args) throws java.lang.Exception {

        MyDesk barco = new MyDesk();
        //EnemyDesk enemigo = new EnemyDesk();// es confusa esta parte

        int [][] enemyDesk = null;
        MyDesk barco1 = new MyDesk(enemyDesk);
}
class MyDesk {
int [][] barcosvivos;
    public MyDesk(){
        this.barcosvivos = new int[5][5];
        barcosvivos [1][0] = 1; barcosvivos [2][0] = 1; barcosvivos[3][0]=1;
        barcosvivos [1][2]= 1; barcosvivos [1][3]= 1; barcosvivos [1][4]= 1;
        for (int []interno: this.barcosvivos){System.out.println(Arrays.toString(interno));}    
    }
    public MyDesk (int [][] enemyDesk){ 
        enemyDesk = new int [5][5];
        enemyDesk [0][2] = 3; enemyDesk [0][3] = 3; enemyDesk [0][4] = 3;
        enemyDesk [2][0] = 3; enemyDesk [2][1] = 3; enemyDesk [2][2] = 3;
        for (int []interno2: enemyDesk){System.out.println(Arrays.toString(interno2));}
    }
}

Test ideone

PS: to understand what you say about the convention, you can look at this: link

I do not know if this is the behavior I expected when I design it, according to this you have to call the two constructors to show the two outputs, for example:

  • MyDesk barco = new MyDesk(); --> llama a -> public MyDesk(){
  • MyDesk barco1 = new MyDesk(enemyDesk); --> llama a -> public MyDesk (int [][] enemyDesk){

If you want to call both independently of the constructor that is called initially you can use this if you do not know how to work with them or how they work you can look at these answers in some way how to apply it:

Using Extends in Java

Update by comment:

  

Hello Angel, I have tried with your correction and I effectively achieve that   two boards appear, but both are the same,

I do not understand what he says with both are the same:

ship:

[0, 0, 0, 0, 0]
[1, 0, 1, 1, 1]
[1, 0, 0, 0, 0]
[1, 0, 0, 0, 0]
[0, 0, 0, 0, 0]

barco1:

[0, 0, 3, 3, 3]
[0, 0, 0, 0, 0]
[3, 3, 3, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
  

that is, in both cases the MyDesk () dashboard is displayed instead of   that in the second one it appears with the variations of MyDesk (int [] []   enemyDesk)

If you want to see what happens to you, you have to make some changes:

Sample main:

    int [][] EnemyDesk = null;
    EnemyDesk = new int [5][5];

    EnemyDesk [0][2] = 1; 
    EnemyDesk [0][3] = 1; 
    EnemyDesk [0][4] = 1;
    EnemyDesk [2][0] = 3; 
    EnemyDesk [2][1] = 3; 
    EnemyDesk [2][2] = 3;

    MyDesk barco1 = new MyDesk(EnemyDesk); //<- le pasas EnemyDesk

Class MyDesk, change this part:

 public MyDesk (int [][] EnemyDesk){

    for (int []interno2: EnemyDesk){System.out.println(Arrays.toString(interno2));}
}

Notes: I recommend that you analyze the answer of @ MarceloZárate because if it is functional for what you are looking for, it has a better design in my opinion than the one you are showing in your question (but I do not know if that is suitable for what search) if so, I could add something like the following to the aforementioned reply:

public MyDesk(int [][] EnemyDesk){

    barcosvivos = new int[5][5];
    barcosvivos [1][0] = 1; barcosvivos [2][0] = 1; barcosvivos[3][0]=1;
    barcosvivos [1][2]= 1; barcosvivos [1][3]= 1; barcosvivos [1][4]= 1;

    barcosenemigos = EnemyDesk;  
}
    
answered by 22.04.2016 в 15:12