I have the class GameObject:
public class GameObject{
private Coordinate coordinates;
public GameObject(){
coordinates = new Coordinate();
}
public void setCoordinates(int x, int y){
coordinates.x = x;
coordinates.y = y;
}
//More methods here
}
public class Coordinate{
public int x, y;
public Coordinate(){
}
public Coordinate(int x, int y){
this.x = x;
this.y = y;
}
public void setCoordinate(int x, int y){
this.x = x;
this.y = y;
}
And two classes Champion and Spell:
public class Spell extends GameObject{
//Some methods
}
public class Champion extends GameObject{
//Some methods
public Spell fireBall = new Spell();
}
And in my main class:
Champion character = new Champion();
If I call character.setCoordinates(200, 300); (just random numbers), the character goes to these exact coordinates. But the Spell fireBall also goes to (200, 300). So the coordinates in Spell are overriden by the setCoordinates(int x, int y) call to character. How is this possible?
TL;DR - Two classes from GameObject, Spell extends GameObject and Champion extends GameObject, override eachother coordinates. Why?
For full source code:
GameObject.java
Spell.java
Champion.java
Coordinate.java
Looking at your code in gitHub you have 2 methods:
//Set the coordinates for this GameObject
public void setCoordinates(int x, int y){
this.coordinates.x = x;
this.coordinates.y = y;
}
public void setCoordinates(Coordinate coordinates){
this.coordinates = coordinates;
}
If you ever use the 2nd one, then you are sharing the same instance of Coordinates so changing one will change the other
The solution is to copy the values instead
public void setCoordinates(Coordinate coordinates){
this.coordinates.x = coordinates.x;
this.coordinates.y = coordinates.y;
}
In the class Spell you set the coordinates:
this.startCoordinates = startCoordinates;
setCoordinates(this.startCoordinates);
Subsequently this code
if (getCoordinates().x - startCoordinates.x < range) {
is equivalent to
if (getCoordinates().x - getCoordinates().x < range) {
because getCoordinates() references the same object as startCoordinates does.
Your setter method just sets the reference, but it does not copy the object.
Related
I'm newbie in Java. I'm not sure why line my2.print_list(); is printing only object my2. I want to print every time whole list of objects. I'm adding every object to list in constructor. I'm 90% sure that for loop in function print_list is good. The compiler shows no problems. I'll appreciate all help.
public class Main {
public static void main(String[] args) {
// write your code here
rectangle my = new rectangle(5);
rectangle my1 = new rectangle(3,6);
rectangle my2 = new rectangle(10,7);
System.out.println( my2.getCounter());
my2.print_list(); ////////////////////<- described line
}
}
/// my class rectangle
import java.util.ArrayList;
import java.util.List;
public class rectangle {
public static int counter =0;
public int x;
public int y;
public List<rectangle> List = new ArrayList<rectangle>();
public rectangle(int x, int y) {
this.x = x;
this.y = y;
System.out.println("Rec");
List.add(this);
counter++;
}
public rectangle(int x) {
this.x = x;
this.y = x;
System.out.println("Sqr");
List.add(this);
counter++;
}
#Override
public String toString() {
return "x->"+x+" y->"+y+" Field: "+(x*y);
}
public void print_list()
{
for(rectangle x : List)
{
System.out.println(x);
}
}
Every instance of your class has its' own instance of List. Make it static if it should be shared (which is the only way your List will be populated). Also, please rename the variable List (it looks exactly like the interface java.util.List). Also, there's no reason to make it public.
private static List<rectangle> myList = new ArrayList<rectangle>();
And then change print_list like
public void printList()
{
for(rectangle x : myList)
{
System.out.println(x);
}
}
Also, the class name should be Rectangle (to follow Java naming conventions).
Change
public List<rectangle> List = new ArrayList<rectangle>();
to
public static List<rectangle> List = new ArrayList<rectangle>();
So there's only one instance of List.
Lets say I have two classes. Pair:
public class Pair<X, Y> {
public X x;
public Y y;
public Pair(X x , Y y) {
this.x = x;
this.y = y;
}
}
and the class Triple:
public class Triple<X, Y, Z> {
public X x;
public Y y;
public Z z;
public Triple(X x , Y y, Z z) {
this.x = x;
this.y = y;
this.z = z;
}
}
And I want to create a class Test without changing the class header (can't do Test<X, Y, Z>):
public class Test {
...
}
In this class should be a method, that takes a list of Triples and should return a Map with the x-value of the triple as a key and the y and z-values of the triple as the value of the map.
How can I do this without changing the class header?
You can do it. You need to make the method generic rather than the class it's in.
class Test {
static <X, Y, Z> Map<X, Pair<Y, Z>> makeMap(List<Triple<X, Y, Z>> triples) {
// your implementation
}
}
The method could be static or non-static. In either case, the generic parameters <X, Y, Z> appear immediately before the return type.
From your description, here an implementation:
public static <X, Y, Z> Map<X, Pair<Y, Z>> makeMap(List<Triple<X, Y, Z>> arg) {
return arg.stream().collect(Collectors.toMap(e -> e.x, e -> new Pair<>(e.y, e.z)));
}
Wish I could say that I'm new, but alas I'm just extremely rusty. I'm trying to make a few simple programs to get back into the basics I learned a couple of years ago. At the moment I have two separate classes: Entity and Game. I have made a player entity object and I would like to access it's x and y parameters in different methods, and eventually different classes as well.
My first instinct was to just use 'player.x' but unfortunately that only works within the same class and only with void methods. If I try using that anywhere else I keep getting a 'NullPointerException' error on the lines where I try to reference any parameter from player. Any advice on how to reference the x and y positions without that error being thrown, or even to just know why its only being thrown in non void methods (as ideally I want to use them in a calculation in a float method) would be greatly appreciated.
This is my entity class:
public class Entity {
public float x; //x position
public float y; //y position
public Entity(float x, float y){
this.x = x;
this.y = y;
}
//entity methods
}
This is my game class:
public class Game{
public static Entity player;
public static float posX = 2f;
public static float posY = 2f;
public Game(){
player = new Entity(posX, posY);
}
public static float test(){
float newX = player.x - 2f; //I would get the error here for example
return newX;
}
//Game methods
}
Thanks!
EDIT
Changed the Game class as suggested, still getting the same error.
public class Game {
public Entity player;
public float posX = 2f;
public float posY = 2f;
public float y = test();
public Game() {
player = new Entity(posX, posY);
}
public float test() {
float newX = player.x - 2f; //I would get the error here for example
return newX;
}
public void print() {
System.out.println(y);
}
public static void main(String[] args) {
Game game = new Game();
game.print();
}
}
Reason is simple. You are creating the player object in constructor. But using it in static method. So, your constructor is never called.
Try to make your method non-static
EDIT
You can do it two ways,
1 : make your test() method non-static and everything will work as charm.
public float test(){
float newX = player.x -2f;
return newX
}
and make your Entity player non-static.
2 : make your fields static and try to initialize them, before calling the test() method.
public class Entity {
public static float x; //x position
public static float y; //y position
public Entity(float x, float y){
this.x = x;
this.y = y;
}
//entity methods
public static void initialize(float tx, float ty){
x = tx;
y = ty;
}
public static float test(){
float newX = Player.x - 2f;
return newX;
}
Ofcourse, second one is not a great solution. But a workaround.
I'm writing most of my immutable data objects in the following style, which is somtimes described as 'next generation' or 'functional':
public class Point {
public final int x;
public final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
I would like to use the same style for data objects specified by interfaces:
public interface Point {
public final int x;
public final int y;
}
public class MyPoint {
public MyPoint(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Origin {
public Origin() {
this.x = 0;
this.y = 0;
}
}
But this is not allowed by java, which gives an error in the interface code as well as the implementations.
I can change my code to
public interface Point {
public int x();
public int y();
}
public class MyPoint {
private int mx, my;
pulic MyPoint(int x, int y) {
mx = x;
my = y;
}
public int x() {return mx;}
public int y() {return my;}
}
public class Origin {
public int x() {return 0;}
public int y() {return 0;}
}
But it is more code, and I don't think it gives nearly the same feeling of simplicity in the API.
Can you see a path out of my dilemma? Or do you personally use a third, even simpler style?
(I'm not really interested in a discussion of mutable/immutable, getterSetter/new-style or private/public fields.)
I would rather switch to use inheritance or delegation
public class Point {
public final int x;
public final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Inheritance
public class MyPoint extends Point {
public MyPoint (int x, int y) {
super (x, y);
}
....
}
public class Origin extends Point {
public Origin () {
super (0, 0);
}
}
I have a class in Java describing a parameter (name: Param) and another class in which I declare and initialize around 100 of such parameters as:
private static final Param param_name_1 = new Param(x, y, z);
I would like to put all these objects/instances in an enum and initialize them there. What is the best method to do that?
===UPDATE===
I asked for the syntax of the enum but not like that.
I my case Param is another java class which has its own parameters, getters and setters and a constructor with the 3 parameters between the paranthesis:
public Param(intx, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
I my other class I declare and initialize 100 instances of the class Param as written above. Each x, y and z for each instance are different.
And the enum should contain the declaration of the instances and possibly also initialize them.
Assuming that you're asking about the enum syntax then you can do something like this (assuming that x, y and z are compile time constants).
public enum Param {
param_name_1(1,2,3),
param_name_2(3,4,5);
private int x;
private int y;
private int z;
private Param(int x, int y, int z) {
this.x=x;
this.y=y;
this.z=z;
}
}
An enum is a special type of class, so you can declare constructors, fields and methods as well as implement interfaces. However, they can not extend other classes.
In this case, I'd suggest final fields and getters, like this:
public enum Param {
PARAM_A(1, 2, 3),
PARAM_B(4, 5, 6),
PARAM_C(1, 3, 5);
private final int x;
private final int y;
private final int z;
private Param(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
}
I think this is what you want
class Param
{
private int x, y, z;
public Param(int x, int y, int z)
{
this.x = x; this.y = y; this.z = z;
}
}
public class t
{
private enum ParamVals
{
VAL1(new Param(0,0,0)),
VAL2(new Param(1,1,1));
private Param paramVal;
private ParamVals(Param paramVal)
{
this.paramVal = paramVal;
}
public Param getVal()
{
return paramVal;
}
}
}