Object method can not access Object variables? - java

I am trying to master the fundamentals of Java and OOP. From my understanding, if I have an object Circle that is instantiated with the variable radius, and passes that to a double x, should methods of the Object be able to access these?
package classes;
public class Circle {
Circle(double radius) {
double x = radius;
}
double area() {
return x * x * 3.1415; // x can't be resolved to a variable
}
}

x is only available within the scope of the Circle constructor. Declare it at class level so it can be accessed by the area method
public class Circle {
private double x;
Circle(double radius) {
this.x = radius;
}
...
}

Here you have a scope problem. When you declare x inside the constructor, you are telling that it will only be accessible inside it.
You may want to declare it outside:
public class Circle {
double x;
Circle(double radius) {
x = radius;
}
...
}

After defined at class level use 'this' for readiblity.
public class Circle {
private double x =0.0;
Circle(double radius) {
this.x = radius;
}
double area() {
return this.x * this.x * 3.1415; // x can't be resolved to a variable
}
}

In your example, the double x is limited in scope to the constructor. If you move it out to the object level, it will work as you expect.
public class Circle {
private double x;
Circle(double radius) {
this.x = radius;
}
double area() {
return x * x * 3.1415;
}
}

Try this code :
public class Circle {
private double x =0.0;
Circle(double radius) {
this.x = radius;
}
double area() {
return this.x * this.x * Math.PI;
}
}

Related

creating an object for delegation in java

here is a part of code that I found while searching for delegation. I did not understand a part. Do we have to create an object for delegation? Or we can use it only with the a variable typeof the class the we want the use method of?
public class Point {
private double xCoord;
private double yCoord;
public double getXCoord(){
return xCoord;
}
public double getYCoord(){
return yCoord;
}
}
public class Circle {
/*
Is it a variable defined type of Point?
Is it possible or should it have been defined as below.
What is the difference?
*/
private Point center;
// Point center = new Point();
public double getCenterX(){
return center.getXCoord(); // Delegation
}
public double getCenterY(){
return center.getYCoord(); // Delegation
}
}
Is it a variable defined type of Point?
It's a variable, but at this point, it's not initialised (an empty variable).
Is it possible or should it have been defined as below?
You must initialise your variable (Here, create a constructor, initialise your variable and initialise the xCoord and yCoord vars.
public class Point {
private double xCoord;
private double yCoord;
public double getXCoord(){
return xCoord;
}
public double getYCoord(){
return yCoord;
}
}
public class Circle {
/*
Is it a variable defined type of Point?
Is it possible or should it have been defined as below.
What is the difference?
*/
private Point center;
// Point center = new Point();
public Circle(double x, double y) { // Constructor
center = new Point(); // Initialise variable
center.xCoord = x; // Set x
center.yCoord = y; // Set y
}
public double getCenterX(){
return center.getXCoord(); // Delegation
}
public double getCenterY(){
return center.getYCoord(); // Delegation
}
}
What is the difference?
A difference between what ?

using generics without changing the class header

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)));
}

Instantiating a class does not work, constructor parameter did not pass

I tried to initialize an circle object from my test class, but the parameter(5.5) did not pass. The result is wrong. I tried to debug and find out the radius is 0.00 in circle class, 5.5 did not pass into the Circle class.
Anyone can help me with that?
This is my output:
The area of circle is: 3.14
This is my test class:
public class ShapeTest {
public static void main(String[] args){
Circle circle = new Circle(5.5);
System.out.println(circle);
}
}
}
This is my circle class:
public class Circle extends TwoDimensionalshape {
private double radius;
public Circle(double radius){
super(radius);
}
public void setRadius(double radius){
this.radius = radius;
}
public double getRadius(){
return radius;
}
#Override
public double getArea(){
return 3.14+getRadius()+getRadius();
}
#Override
public String toString(){
return String.format("%s %,.2f%n ","The area of circle is: ",getArea());
}
}
This is my super class:
public class TwoDimensionalshape implements Area{
private double radius;
private double base;
private double height;
public TwoDimensionalshape(double radius){
this.radius = radius;
}
public TwoDimensionalshape(double base, double height){
this.base = base;
this.height = height;
}
public double getRadius() {
return radius;
}
public double getBase() {
return base;
}
public double getHeight() {
return height;
}
#Override
public double getArea(){
return 1;
}
public String toString(){
return "The area is: "+getArea();
}
}
Your radius variable in Circle hides the radius variable in TwoDimensionalshape. They are 2 different variables. Your constructor sets the one in TwoDimensionalShape, but getArea is using the one in Circle.
Remove the radius variable in Circle. Let Circle inherit getRadius by removing that method from Circle. Also in Circle, move setRadius to TwoDimensionalShape.
Also, in getArea, multiply the radius twice instead of adding it twice. You can also use Math.PI instead of 3.14.
return Math.PI * getRadius() * getRadius();
Remove private double radius; from Circle class and it should work. Also change getRadius() method body in Circle class to:
return super.getRadius();
You are missing the following line in Circle constructor:
public Circle(double radius){
super(radius);
this.radius = radius; // add this line
}
As radius is private in TwoDimensionalshape, you don't have an access to it from the subclass (from Circle in that case). You need to set value of radius of Circle class in constructor of Circle, as in your code, it calculated getArea() with the radius value of 0.
Another issue - to calculate area properly, you should change:
return 3.14 + getRadius() + getRadius();
to:
return 3.14 * getRadius() * getRadius();

How to reference object parameters in other classes in java

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.

Objects override each other

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.

Categories

Resources