toString() method not printing the correct values of object - java

I'm following a book and I have a Point class which defines a point and I am trying to display the values. I've been looking around for a while now, whatever I do it always displays [0,0] here's my code.
class Main {
public static void main(String []args) {
Point point = new Point(10, 20);
System.out.println(point.toString());
}
}
class Point {
private int x, y;
public Point(int x, int y) {
x = x;
y = y;
}
public String toString() {
return "[" + x + ", " + y + "]";
}

You're never initializing class members x and y actually. So they're initialized as 0 automatically hence the [0,0] output.
x = x means parameter x = parameter x which does nothing.
use this.x = x; instead so Point class member x will be set.

Look at your constructor:
public Point(int x, int y) {
x = x;
y = y;
}
In your case you do not initialize class fields. To do that, use this keyword and it should look like:
public Point(int x, int y) {
this.x = x;
this.y = y;
}
Otherwise you are dealing with parameter x and y variables only.

Use this in Constructor as below :-
class Point {
private int x, y; // These x and y are member of Point class
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "[" + x + ", " + y + "]";
}
You are not initializing x and y of Point class.
In toString() method you are printing x and y of Point class which are not initialized and hence taking default value of 0 i.e. 0 is default value of integer.

use the this keyword for the separation of local variable and instance variable. in your parameterized constructor jvm is not able to initialize the instance variable because it gets an ambiguity problem between local and instance variable. And instance variable in java gets implicitly initialized if u we are not initializing that's why you are getting [0,0] as the output.
here is the code..
class Main {
public static void main(String []args) {
Point point = new Point(10, 20);
System.out.println(point.toString());
}
}
class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "[" + x + ", " + y + "]";
}

In your Constructor for the Point object:
public Point(int x, int y) {
this.x = x;
this.y = y;
}
You will need to put this.x to indicate you are setting the value of the Point object's x variable, not the x argument passed into the constructor.
The updated, functional code is below:
class Main {
public static void main(String []args) {
Point point = new Point(10, 20);
System.out.println(point.toString());
}
}
class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return "[" + x + ", " + y + "]";
}

Related

Constructor undefined while using multiple classes

I have a question. For the first time, I am working with multiple classes in JAVA. I have some trouble doing this. I created a class which I will call from another class. I want to make a type Coordinate, which, as the name suggests, holds coordinates. then, I want to shift those coordinates. So far the code looks as follows:
public class Coordinate {
double x;
double y;
Coordinate(){
x=0;
y=0;
}
public Coordinate(int x, int y){
this.x = x;
this.y = y;
System.out.print(x);//TO TEST WHETHER IT DOES SOMETHING
}
Coordinate shiftCoordinate(int z, int w){
this.x = x + z;
this.y = y+ w;
return new Coordinate(x,y);//ERROR: The constructor Coordinate(double, double) is undefined
}
}
It throws an error where stated. I do not understand this error. In my 'main' class, I did the following:
void start() {
Coordinate coordinate = new Coordinate();
coordinate.x=3;
coordinate.y=4;
}
I would expect this to print 3, but it does not. Where am I wrong?
First you don't work with mutiples class, only one : Coordinate but you want multiple constructors.
As your attributs are double make a constructor that needs that type, it'll be used when you write new Coordinate(5,6)
public Coordinate(double x, double y) {
this.x = x;
this.y = y;
}
If you want a default constructor (no args) it'll be used when you call new Coordinate()
You want a way to shift from a Coordinate instance you have 2 ways : modify the current instance or create a new one, but don't do both together (like your code) it's not usefull to get at the end 2 objects whith the same values
// modify current instance
void shiftCoordinate(double z, double w) {
this.x = x + z;
this.y = y + w;
}
// return a new object
Coordinate shiftCoordinate(double z, double w) {
return new Coordinate(this.x + z, this.y + w);
}
Also you last code uses the default constructor with no args so that's normal you don't see any printing use new Coordinate(3,4) to see it
A classic constructor is also, the constructor to clone an instance, it takes an instance and create a new one with the same values :
public Coordinate(Coordinate clone) {
this.x = clone.x;
this.y = clone.y;
}
public Coordinate(int x, int y){
this.x = x;
this.y = y;
System.out.print(x);
}
This constructor has arguments that are integers, and you're trying to pass in doubles. Change the constructor to this:
public Coordinate(double x, double y){
this.x = x;
this.y = y;
System.out.print(x);
}

Cannot find symbol Error p1.getX() [duplicate]

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 4 years ago.
And the code I typed for it is given below:
class MyPoint {
public int x;
public int y;
public MyPoint(){
x = 0;
y = 0;
}
public MyPoint(int x, int y) {
this.x = x;
this.y = y;
}
public void setXY(int newx, int newy) {
x = newx;
y = newy;
}
public int[] getXY() {
int [] getXYarray = new int[2];
getXYarray[0] = x;
getXYarray[1] = y;
return getXYarray;
}
public String toString() {
return "(" + x + "," + y + ")";
}
But i cannot see what is actually wrong here.
Please tell me where i am going wrong with this as i am very lost.
The third bullet point from the bottom says you need "Getter and setter for the instance variables x and y". Implement those and I suspect your Tester will pass.
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
Your instructions also call for an overloaded constructor that takes a MyPoint (a copy constructor)
public MyPoint(MyPoint that) {
this.x = that.x;
this.y = that.y;
}
The test code tries to use the methods getX() and getY().
You do not define those methods, you only define getXY().
(Please provide textual information as text in the future, it would have made answering this easier for me.)

Initialization of Class fields

public class InitializationTest {
private int x;
private int y;
private int sumOFXandY = x + y;
public InitializationTest(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSumOFXandY() {
return sumOFXandY;
}
}
class Tester {
public static void main(String[] args) {
InitializationTest initializationTest = new InitializationTest(5, 6);
System.out.println(initializationTest.getX());
System.out.println(initializationTest.getY());
System.out.println(initializationTest.getSumOFXandY());
System.out.println(initializationTest.getX() + initializationTest.getY());
}
}
Output:
5
6
0 //Why not 11?
11
In the example above my brain cannot understand such simple thing - a revelation.
When the class is created, its fields are initialized with default values. In this case those are 0.
Upon calling the constructor x is initialized with 5 and y is initialized with 6. Why then sumOFXandY is somewhere aside and still is initialized with 0 even after calling the constructor. Why is not it initialized with 5 + 6 = 11 ?
Because it(sumOFXandY) cannot be re-initialized after the constructor gets executed. When you create an object, it initializes the instance variables that are initialized inline (here it is sumOFXandY = x + y). Then the constructor block gets executed.
To solve this, move sumOFXandY = x + y inside the constructor.

Java super set private fields

I'm learning java inheritance and encapsulation. Here is sample code
class Base {
private int x;
private int y;
Base(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
class Child extends Base {
Child(int x, int y) {
super(x, y);
}
}
Child c = new Child(1, 2);
System.out.println(c.getX());
Why have I access to x and y (private) in Child class? Does super change anything?
You don't have access to x and y in Child class. When you write
Child(int x, int y) {
super(x, y);
}
the x and y are formal parameters of Child's constructor, that happen to be called the same names as private fields x and y of the superclass. They might as well be called something else - say, a and b, and the effect would be exactly the same:
Child(int a, int b) {
super(a, b);
}
Why have I access to x and y (private) in Child class?
you dont, if you would you could do without problem:
System.out.println(c.x);
System.out.println(c.y);
you wrote instead public setters/getters that are giving you access (capsulation) to the fields..

method issue in java

i'm learning to create custom classes and can't figure out where I've gone wrong
From main class...
MyPoint p1 = new MyPoint(317, 10);
the error says:
constructor MyPoint in class MyPoint cannot be applied to given types;
required: no arguments
found: int, int
reason: actual and formal argument lists differ in length
this is from my MyPoint class:
private int x, y;
public void MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
Why isn't MyPoint(317, 10) being fed into the relevant class along with the x and y values?
Any help would be appreciated, thank you.
Constructors don't have return type. This is just a normal method you just made.
Solution: Remove the void from the method. It should look like
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
remove return type from
public void MyPoint(int x, int y)
constructor cannot have return type not even void
make it
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
You need to declare parameterized constructor in MyPoint class.
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
Constructors must not have return type. So change your code as
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}

Categories