-2

I have the method Mappa() that creates the array settore (using the coordinates of the method Settore() of the class Settore) and the numeric Matrix Matrice. The class Settore has several subclasses (i'm using the subclass Sicuro to explain the code) that set the attribute SettoreNome from the enum Nome for an specific set of coordinates. I'm creating the method MappaCreator() that specifies the SettoreNome of each Settore(x,y) according to the value of the Matrice[x][y] and it seems that i'm not calling the method correctly because i get the error that the method is undefined for the subclass Sicuro.

    package mappa;


    public class Settore {
            private Nome settoreNome;
            private char letteraX;
            private final int coordinataX;
            private final int coordinataY;
            private int giocatoriPresenti;



    public int getGiocatoriPresenti() {
        return giocatoriPresenti;
    }
    public void setGiocatoriPresenti(int giocatoriPresenti) {
        this.giocatoriPresenti = giocatoriPresenti;
    }
    public char getLetteraX() {
        return letteraX;
    }

    public void setLetteraX(char letteraX){
        this.letteraX = letteraX;
    }

    public Settore (int coordinataX, int coordinataY){
        this.coordinataX=coordinataX;
        this.coordinataY=coordinataY;
    }
    public int getX(){
        return coordinataX;
    }
    public int getY(){
        return coordinataY;
    }
    public Nome getSettoreNome() {
        return settoreNome;
    }
    public void setSettoreNome(Nome settoreNome) {
        this.settoreNome = settoreNome;
    }
    public static void add(Settore[][] settore) {
        // TODO Auto-generated method stub

    }
}
package mappa.product;

import mappa.Settore;

public class Mappa {
    private Name mappaName;
    private final Settore [][] settore;
    private int Matrice [][];
    private static final int X=23;
    private static final int Y=14;
    public Mappa (){
        settore = new Settore[X][Y];
        for (int i=0; i < X; i++){
            for (int j=0; j<Y; j++) {
                settore[i][j] = new Settore (i,j);
            }
        }
        Matrice = new int[23][14];
    }



    public int[][] getMatrice() {
        return Matrice;
    }

    public void setMatrice(int matrice[][]) {
        Matrice = matrice;
    }

    public Name getMappaName() {
        return mappaName;
    }

    public void setMappaName(Name mappaName) {
        this.mappaName = mappaName;
    }
    public Settore[][] getSettori(){
        return settore;
    }
    public void addSettori(){
        Settore.add(settore);
    }


}
package mappa.creator;
import mappa.product.*;
import mappa.*;
public class MappaCreator {

    protected Mappa mappa;
    protected Settore settore;
    protected int x;
    protected int y;

        public MappaCreator(){

            Mappa mappa = new Mappa();
            this.mappa=mappa;
            int x; int y;
            if (mappa.getMatrice()[x][y]==1){
                Sicuro.Sicuro(x,y); //this is where i get the error
            }


        }
        public void createMappa(){
            Mappa mappa = new Mappa();
            this.mappa=mappa;
        }
        public void createSettore(){
            Settore settore = new Settore(x,y);
            this.settore=settore;
        }


}
package mappa;

public class Sicuro extends Settore {

    public Sicuro(int coordinataX, int coordinataY) {
        super(coordinataX, coordinataY);
        setSettoreNome(Nome.SICURO);
    }


}
package mappa;

public enum Nome {
SICURO, PERICOLOSO, SCIALUPPA, ALIENI, UMANI
}
  • 1
    On which line does the error occur, and could you paste the exact error message? (Btw, it would help a lot if you had english names on your variables.) – aioobe May 21 '15 at 15:53

3 Answers3

2

change

        Sicuro.Sicuro(x,y); //this is where i get the error

to

        new Sicuro(x,y); //this should not produce any error

new Sicuro() is the call of a constructor.

Constructors do not specify any return type and have to have the exact same name as the class.

slartidan
  • 20,403
  • 15
  • 83
  • 131
0

See the below post for Better understanding about method vs constructor

Methods vs Constructors in Java

Sicuro(x,y) is a constructor. 

If it is static method then only we can call this with Classname.

But in your code it is a constructor. While creating object to your class then constructor automatically invoked like

  new Sicuro(x,y);

Now compiler will execute Sicuro(x,y) constructor.

Community
  • 1
  • 1
0

I think you want to create an object from class Sicuro not to call one of its methods. The creation of an object invokes the constructor method of the class.

Replace:

Sicuro.Sicuro(x,y);

With:

Sicuro mysicuro = new Sicuro(x,y);

//then do stuff with mysicuro

cesarluis
  • 897
  • 6
  • 14