0

I have this code which gets info from an other class but I have to add another line other code for every instance object.

public static int giveShipData(String shipName,int Data){
    if(shipName.equals("Player")){i = Player.getData(Data);}
    return i;
}

Is it possible to have something like:

public static int giveShipData(String shipName,int Data){
    i = shipName.getData(Data);
    return i;
}

Sorry if I am using the wrong terminology I am self taught and new.

2
  • is Player#getData a static class method that receives an int and returns another int? Commented Apr 8, 2012 at 6:18
  • i = shipName.getData(Data); is wrong. shipName is instance of String class and haven't getData method. Commented Apr 8, 2012 at 6:20

3 Answers 3

2

I think you'd better to reconsider your design. If you have a ship name and ship data I assume you must have a Ship class which looks something like this:

public class Ship {
    private String name;
    private int data;

    public Ship(String name, int data) {
        this.name = name;
        this.data = data;
    }

    public String getName() {
        return name;
    }

    public int getData() {
        return data;
    }
}

Besides this class there should be a class like Shipyard:

 public class Shipyard {
    private Map<String, Ship> shipsByNameMap = new HashMap<String, Ship>();

    public void registerShipByName(String name, Ship ship){
        shipsByNameMap.put(name, ship);
    }

    public Ship getShipByName(String name){
        return shipsByNameMap.get(name);
    }
}

So, at first you invoke shipyard.getShip("Player") method to get ship object, than you can invoke ship.getData() method to retrieve ship data.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks this seems to do exactly what I want.
+1 for Shipyard. :-) And for an excellent example of OOP. Suggestion: if registerShipByName takes two params, then the second could be the data. That way it could create a new Ship and then register it. If the Ship already exists, then registerShipByName needs only the Ship param, and it can get the name from the Ship.
0

You might be able to do something like this...

int[] ships = new int[3]; // 3 ships

public void populateShips(){
  for (int i=0;i<ships.length;i++){
    ships[i] = giveShipData(shipname,data);
    }
  }

public int giveShipData(String shipName,int data){
    return shipName.getData(data);
}

This would allow you to create any number of ships, just increase the size of the ships[] array to be however many ships you want.

Is this kinda what you're after?

1 Comment

I would be nice if I didn't have to do this but this will work thank you.
0

As per your code "shipName" is a string...and it does not have getData() method. And why are you passing Data to the getData()... You could instead write something like this-

 i = ClassObj.getData(shipname);

and in the method getData return the info regarding the "shipname".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.