0

Ok so I need to get the variables from one class's method and update it in another class.

Class which updates method.

public abstract class MovableObject{
    protected int speed;
    protected int heading;

    public void move(){
        setX(finalX);
        setY(finalY);
    }

Class which needs updating:

public class Car extends MoveableObject{
    private int height;
    private int width;

    public Car(){
        super.setX(200);
        super.setY(2);
    }

I have an iterator that goes through a list and checks the X and Y coordinates, currently it keeps printing out (200, 2) constantly but the Car is moving. So the class MovableObject has the updated coordinates but since I'm calling it from Car it's not getting the right coordinates to the moving car. I need to pass the variables from move() to more than one class also, that should be the same since the Iterator takes care of what is updated right or is this way very complicated?

listObject = new GameObjectCollection();
car = new Car();
listObject.add(car);

System.out.println("CAR: " + ((Car)gameObj).getX() + " " + ((Car)gameObj).getY());
user2318083
  • 567
  • 1
  • 8
  • 27
  • How do you create the list of Cars? And how do you print the list items values? Please post that code. – Averroes Apr 29 '14 at 15:52
  • Not sure what you exactly want but that's how I make the list, not only `Car` is added, there are other objects on the list also so I don't know if that's gonna be an issue with finding the coordinates for the others. – user2318083 Apr 29 '14 at 15:58

2 Answers2

2

That constructor of Car class initialize those values so if you create a Car it will have by default those values.

Solution 1: Create another constructor

public class Car extends MoveableObject{
    private int height;
    private int width;

    public Car(){
        super.setX(200);
        super.setY(2);
    }

    public Car(int x, int y){
       super.setX(x);
       super.setY(y);
    } 

}

If you use the second constructor you can define the values you want for that instance of Car.

Car c = new Car(100, 3);

This really does:

 public Car(int x, int y){
           super(); //Call the creation of the superclass
           super.setX(x); //Modify superclass X and Y values.
           super.setY(y);
        } 

Solution 2: Use getters and setter methods (provide they are accesible and they are in Class or Superclasses)

Once you create a Car you can change the x and y values before adding them to the list.

Car c = new Car();
c.setX(100);
c.setY(3);
Averroes
  • 4,168
  • 6
  • 50
  • 63
  • But you can't create a constructor with an abstract class can you? – user2318083 Apr 29 '14 at 16:02
  • 2
    Abstract classes cannot be instantiated. But `Car` isn't an abstract class. You crate a new Constructor for Class `Car`. Check the update of the answer. – Averroes Apr 29 '14 at 16:04
  • 2
    @user2318083 Check this for constructor in abstract classes question: http://stackoverflow.com/questions/5404168/constructors-in-abstract-classes – Averroes Apr 29 '14 at 16:08
  • Ok I kinda understand it, lets say there's also a `Box` class that also uses the same class `MovableObject` to move and that needs to be updated with different X and Y coordinates to it's movement, will there be an issue adding another constructor to it? It's not going to take in the same values as the car is it? It's in a list that iterates through later. – user2318083 Apr 29 '14 at 16:10
  • 1
    You can create as many constructor for your classes provide they doesn't have the same method signature (in this case, parameter types). So for instance if you have a Box class that extends MovableObject you can have a `public Box (){ super.setX(400); super.setY(1)}`. – Averroes Apr 29 '14 at 16:14
1

You want to build a get method into your class (usually you also want a set method for anything you want the outside world to be able to change).

something like:

 public int getx(){
      return this.x;  // where x is the variable you are returning
 }

because this is public, other classes can access it. If you keep your variables private, then no outside sources can access or update them except by the public methods you define.

Similarly, if you wanted outside sources to be able to update this, then you could define a setter method:

 public void setx(int input){
      this.x = input;
 }

If x is private, then outside sources can only modify it through this method (or other methods you define for the class).

With these methods, you could do something like:

 SomeClass example = new SomeClass();
 example.setx(10);  // sets example's x variable to 10
 System.out.println(example.getx());  // will print 10
Marshall Tigerus
  • 3,675
  • 10
  • 37
  • 67
  • You can't do that with abstract classes can you? – user2318083 Apr 29 '14 at 16:01
  • 1
    Your car is not an abstract class, and since it is an extension of MoveableObject, and you call its constructor, your car has those variables and you can define the methods there – Marshall Tigerus Apr 29 '14 at 16:02
  • But wouldn't I need to make a constructor for MovableObject since I'm getting the values from there and putting it into the Car? – user2318083 Apr 29 '14 at 16:04
  • 1
    your Car inherits all methods and variables from its parent class. So, its not a MoveableObject that has those values, but a Car that happens to be a MoveableObject. – Marshall Tigerus Apr 29 '14 at 16:08
  • 1
    Averroes answer has the correct information to fix this I believe – Marshall Tigerus Apr 29 '14 at 16:09
  • Yes both of these cleared things up, thanks. Now if there's another class with different x and y coordinates than the car class can I pass in the constructor the same way as and it'll grab a different x and y coordinate? All these objects are put on a list and iterated through. – user2318083 Apr 29 '14 at 16:12
  • 1
    I'm not sure exactly what you mean. With the public get and set methods I described you could send variables between isntances of the Car class – Marshall Tigerus Apr 29 '14 at 16:26