Java - The left-hand side of an assignment must be a variable - java

I'm trying to make a small program that locates different cities as my first Java project.
I want to access the variables of my class 'GPS' from the class 'City' but I keep getting this error: The left-hand side of an assignment must be a variable. Anyone can explain to me what am I am doing wrong here and how to avoid such mistake in the future?
public class Gps {
private int x;
private int y;
private int z;
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public int getZ() {
return this.z;
}
}
(I want to leave the variables as private)
And this class 'Citiy' should have the coordinates:
class City {
Gps where;
Location(int x, int y, int z) {
where.getX() = x;
where.getY() = y; //The Error Here
where.getZ() = z;
}
}

The error speaks for itself: you cannot assign a value to something that is not a field or a variable. Getters are for getting values stored in the class. Java uses setters to deal with storing the values back:
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
Now you can set values by invoking the setter:
City(int x, int y, int z) {
where.setX(x);
...
}
However, this solution is not ideal, because it makes Gps mutable. You can keep it immutable by adding a constructor:
public Gps(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
Now City can set where in one shot:
City(int x, int y, int z) {
where = new Gps(x, y, z);
}

Don't set properties with getter. It should be done like that:
public class Gps {
private int x;
private int y;
private int z;
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public int getZ() {
return this.z;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setZ(int z) {
this.z = z;
}
}
class City {
Gps where;
City(int x, int y, int z) {
this.where = new Gps();
where.setX(x);
where.setY(y);
where.setZ(z);
}
}

Related

Trying To Implement Player Gravity For My Test Block for Player Class (RealtutsGml Platformer Tutorial)

I am following realTutsgml Tutorial in attempt to implement gravity for the player class which I am using a Linklist to refer to my Gameobject class.
public class Player extends GameObject {
private float width = 32, height = 64;
private float gravity = 0.05f;
public Player(float x, float y, ObjectId id) {
super(x, y, id);
}
public void tick(LinkedList<GameObject> object) {
x += velX;
y += velY;
if(falling||jumping)
{
velY += gravity;
}
}
I was able to draw the test block onto the screen which an instance was created in the game class in the init method
handler.addObject(new Player(100, 100, ObjectId.Player));
GameObject Class
public abstract class GameObject
{
protected float x, y;
protected ObjectId id;
protected float velX = 0, velY = 0;
protected boolean falling = true;
protected boolean jumping = false;
public GameObject(float x, float y, ObjectId id)
{
this.x = x;
this.y = y;
this.id = id;
}
public abstract void tick(LinkedList<GameObject> object);
public abstract void render(Graphics g);
public abstract Rectangle getBounds();
public float getX() {
return x;
}
public float getY() {
return y;
}
public void setX(float x) {
this.x = x;
}
public void setY(float y) {
this.y = y;
}
public float getvelX() {
return velX;
}
public float getvelY() {
return velY;
}
public void setvelX(float velX) {
this.velX = velX;
}
public void setvelY(float velY) {
this.velY = velY;
}
public boolean isFalling() {
return falling;
}
public void setFalling(boolean falling) {
this.falling = falling;
}
public boolean isJumping() {
return jumping;
}
public void setJumping(boolean jumping) {
this.jumping = jumping;
}
public ObjectId getId() {
return id;
}
}
I added the getters and setters to generate the return value for the jumping and falling boolean values.

Wait for two change listeners to change both

I have a short question regarding PropertyChangeListeners.
I have a Person class which has 3 attributes. Two of them are the x and y coordinates which the person has on a map.
I want to update the map whenever the coordinates of a person change.
So my problem is to detect when not only x or y change, but both do.
That's how far I got. I hope you can help me.
Person.java:
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
public class Person {
private int x, y, hp;
private PropertyChangeSupport changes = new PropertyChangeSupport( this );
public Person (int x, int y) {
this.x = x;
this.y = y;
}
public void setX( int x) {
int oldX = x;
this.x = x;
changes.firePropertyChange( "x", oldX, x );
}
public void setY( int y) {
int oldY = y;
this.y = y;
changes.firePropertyChange( "y", oldY, y );
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void addPropertyChangeListener( PropertyChangeListener l ) {
changes.addPropertyChangeListener( l );
}
public void removePropertyChangeListener( PropertyChangeListener l ) {
changes.removePropertyChangeListener( l );
}
}
Map.java:
private void initPeople() {
people = new Person[amount];
PropertyChangeSupport
for (Person p : people) {
person.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent e) {
// IF Person x AND y changed DO something
}
});
}
}
You can store the boolean xChanged and yChanged properties in person's property change listener class, and then in callback:
xChanged | ="x". equals(e.source);
yChanged |="y". equals(e.source);
if(xChanged & & yChanged ) {
}
You can instead of have two fields for People coordinate (x and y) merge them to other object (you can call it Coordinate). That allow you to work with coordinate of People like entire thing. And of course you have fire changing of it in one call.

Inheritance casting

I have two class.
class Vector{
........
Vector(int x, int y, int z){...........}
public Vector sum(Vector vc){
Vector result;
...........//all working and store to Vector result.
return result;
}
public Vector subtract(Vector vc){................//codes}
}
class Velocity extends Vector{
Velocity(int x, int y, int z){......}
................
}
class Test{
public static void main(String args){
Velocity v1=new Velocity(14,14,14);
Velocity v2=new Velocity(14,14,14);
Vector result=v1.sum(v2); //here I want to get this result as Velocity
//I don't know how to get it....
}
}
In similar manner I have Acceleration, Force, Momentum, Displacement class which extends Vector class....And all have same problem......besides this
Vector.java:
public interface Vector<T extends Vector> {
public int getX();
public int getY();
public int getZ();
public T sum(T other);
}
BaseVector.java:
public abstract class BaseVector<T extends Vector> implements Vector<T> {
private final int x;
private final int y;
private final int z;
public BaseVector(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
protected abstract T createNew(int x, int y, int z);
#Override
public T sum(T other) {
return createNew(x + other.getX(), y + other.getY(), other.getZ());
}
#Override
public int getX() {
return x;
}
#Override
public int getY() {
return y;
}
#Override
public int getZ() {
return z;
}
#Override
public String toString() {
return "BaseVector [x=" + x + ", y=" + y + ", z=" + z + "]";
}
}
Velocity.java:
public class Velocity extends BaseVector<Velocity> {
public Velocity(int x, int y, int z) {
super(x, y, z);
}
#Override
protected Velocity createNew(int x, int y, int z) {
return new Velocity(x, y, z);
}
}
Test.java:
public static void main(String[] args) {
Velocity v1 = new Velocity(14, 14, 14);
Velocity v2 = new Velocity(14, 14, 14);
Velocity result = v1.sum(v2);
System.out.println(result);
}
}
Vector result = ((Velocity)v1).sum(v2);
=========================================================
This will not help, because you defined sum in Vector, returning Vector. To make result a Velocity you would need a sum method returning type Velocity, rather than the one in Vector that returns a Vector.
A better alternative might be to provide a Velocity constructor that takes a Vector
Velocity result = new Velocity(v1.sum(v2));

Java how to implement and design an abstract class

I've run into a design problem in my java code. My application uses missiles, and there are different types of missiles that all work identical except they have 3 unique attributes. The constructor of a missile must know these attributes. I decided to make missile an abstract class, but I can't assign values to protected variables in a subclass outside of a method/constructor. Also I can't declare the variables in the constructor, because I must make the call to the super-constructor first thing.
How can I be smart about this problem?
public abstract class Missile {
private int x, y;
private Image image;
boolean visible;
private final int BOARD_WIDTH = 390;
protected final int MISSILE_SPEED;
protected final int MISSILE_HEIGHT;
protected String file;
public Missile(int x, int y) {
ImageIcon ii =
new ImageIcon(this.getClass().getResource(file));
image = ii.getImage();
visible = true;
this.x = x;
this.y = y - Math.floor(MISSILE_HEIGHT/2);
}
public Image getImage() {
return image;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isVisible() {
return visible;
}
public void move() {
x += MISSILE_SPEED;
if (x > BOARD_WIDTH)
visible = false;
}
}
And there is an ideal implementation of a subclass, except it doesn't work. (it can't recognize the protected variables). What do I do?
public class Laser extends Missile {
MISSILE_SPEED = 2;
MISSILE_HEIGHT = 5;
file = "laser.jpg";
public Laser(int x, int y) {
super(x, y);
}
}
I think the best way to do what you want it to do is make abstract methods in Missile that the subclasses have to implement. For example, add these to Missile:
public abstract int getMissileSpeed();
public abstract int getMissileHeight();
public abstract int getFileName();
Then your subclass has to implement it, and you can make it constant like so:
public class Laser extends Missile {
public Laser(int x, int y) {
super(x, y);
}
public int getMissileSpeed() {
return 2;
}
public int getMissileHeight() {
return 5;
}
public String getFileName() {
return "laser.jpg";
}
}
edit: And then of course anywhere that you want to retrieve the constant value you just call those methods.
Change the base class fields and constructors to
protected final int speed;
protected final int height;
public Missile(int x, int y, int speed, int height, String file) {
ImageIcon ii =
new ImageIcon(this.getClass().getResource(file));
image = ii.getImage();
visible = true;
this.speed = speed;
this.height = height;
this.x = x;
this.y = y - Math.floor(height/2);
}
And the subclass to:
public class Laser extends Missile {
public Laser(int x, int y) {
super(x, y, 2, 5, "laser.jpg");
}
...
}
The attributes are already in the base class, so they must not be redefined in the subclass. All-uppercase naming is reserved to constants in Java.
I'm not sure if missile needs to be an abstract class, but I think something like this might be what you're going for:
public abstract class Missile {
private int x, y;
private Image image;
boolean visible;
private final int BOARD_WIDTH = 390;
protected final int MISSILE_SPEED;
protected final int MISSILE_HEIGHT;
public Missile(int x, int y, int speed, int height, String file) {
MISSILE_SPEED = speed;
MISSILE_HEIGHT = height;
ImageIcon ii = new ImageIcon(this.getClass().getResource(file));
image = ii.getImage();
visible = true;
this.x = x;
this.y = y - Math.floor(MISSILE_HEIGHT/2);
}
}
public class Laser extends Missile {
public Laser(int x, int y) {
super(x, y, 2, 5, "laser.jpg");
}
}
Create an interface and put all your final fields in it.
Now implement this interface within Missile and Laser both. At least that would solve the issue of access.

Java: When I Instantiate a Subclass of an Abstract Class It Doesn't Recognize the Constructor of its Superclass

I do not have very much Java experience but I see codes where there is an abstract class with a certain constructor and then a subclass of that abstract class without a constructor. Then when the subclass is instantiated it is constructed with its superclass constructor. Is that right?
I have this abstract class:
public abstract class Tile{
public int x;
public int y;
public int z;
protected Color color;
protected float friction;
protected float bounce;
protected boolean liquid;
public void Tile(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
init();
}
abstract protected void init();
And this subclass:
public class TestTile extends Tile{
protected void init(){
color = Color.RED;
friction = 0.1f;
bounce = 0.2f;
liquid = false;
}
}
But when I instantiate a TestTile with this:
Tile tile = new TestTile(0, 0, 0);
the init() method never runs. All of the values defined inside it are null. I tried making what I though might be a redundant constructor in the subclass which just called super with the exact same parameters, but when I did that, even with super(x, y, z) the only statement inside it, it said this:
TestTile.java:27: call to super must be first statement in constructor
I want to make a bunch of subclasses of Tile which implement the properties of a Tile. If this is not the correct way to do that, what is a better way?
I am using 32-bit Ubuntu Linux 11.04 if it has to do with anything.
Thanks.
Your constructor is not in propert constructor format, it's void, make it:
public Tile(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
init();
}
I don't see a constructor for TestTime that takes three arguments. I don't see any ctor at all, which means that all you have is the default that the compiler gives you. Did I go too fast and miss it?
I'd recommend paying careful attention to this. I'd rethink this design:
What's wrong with overridable method calls in constructors?
Try this - it includes the fix for your constructor and avoids the issue that the other thread points out:
public abstract class Tile{
public int x;
public int y;
public int z;
protected Color color;
protected float friction;
protected float bounce;
protected boolean liquid;
public Tile(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
}
public class TestTile extends Tile{
// You're missing this.
public TestTile(int x, int y, int z)
{
super(x, y, z);
this.init();
}
protected void init(){
color = Color.RED;
friction = 0.1f;
bounce = 0.2f;
liquid = false;
}
}
First of all, Tile has only one constructor with the x, y, z parameters, no default constructor, so you have to call super(x, y, z) in the TestTile constructor. As slandau said, the "constructor" has a wrong void return type.
The TestTile needs to declare the parameters or pass default values:
public TestTile(int x, int y, int z) {
super(x, y, z);
}
public TestTile() {
super(0, 0, 0);
}
In Java, there are many riscs to call an abstract method in a constructor, see also here, the instance is not initialized properly. You can only call static methods safe (which will not work here).
public TestTile(int x, int y, int z) {
super(x, y, z);
color = Color.RED;
friction = 0.1f;
bounce = 0.2f;
liquid = false;
}
or you need to call a private method in the derived class (remove the abstract init() from Tile):
public TestTile(int x, int y, int z) {
super(x, y, z);
init();
}
private void init() {
color = Color.RED;
friction = 0.1f;
bounce = 0.2f;
liquid = false;
}
Are you sure members are the right implementation here? Maybe abstract methods (getters) may be better here to declare a behavior and implement it in the subclass?
public abstract class Tile {
public int x;
public int y;
public int z;
public Tile(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public abstract Color getColor();
public abstract float getFriction();
public abstract float getBounce();
public abstract boolean isLiquid();
}
public class TestTile extends Tile {
public TestTile(int x, int y, int z) {
super(x, y, z);
}
public Color getColor() {
return Color.RED;
}
public float getFriction() {
return 0.1f;
}
public float getBounce() {
return 0.2f;
}
public boolean isLiquid() {
return false;
}
}
Constructors are not inherited, so the three-parameter constructor of Tile is not invoked when you create your TestTile object. You need to explicitly call the three-parameter Tile constructor from a TestTile constructor, like you said you did try, but that call to super(x,x,x) must be the first statement of the TestTile constructor.
And like Matt Ball said, your Tile "constructor" isn't really a constructor until you remove the void return type.

Categories

Resources