Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Point constructor

Hi,

I'm a newbie in programming, trying to work on a practice problem in classes and objects. I tried the following to construct Point of type double, but when I print Point C, it prints 0,0; can someone take a look and guide where I'm getting this wrong..

thanks,

-----------------------------------------------------------------------------------

```
public class Point
{
private int x;
private int y;
private double a;
private double b;

//constructs a new point with (0,0) as coordinates
public Point()
{
x=0;
y=0;
}

//constructs a new point with the given (x,y) location
public Point(int initialX, int initialY)
{
x = initialX;
y = initialY;
}

//constructs a new point with coordinates of type double
public Point(double initialX, double initialY)
{

if((initialX-(int) initialX)>0.5)
a=(int)initialX + 1;
//else
//	initialX=initialX;

if((initialY-(int) initialY)>0.5)
b=(int)initialY + 1;
//else
//	initialY=initialY;
}

//returns the x-coordinate of this point
int getX()
{
return x;
}

//returns the y-coordinate of this point
public int getY()
{
return y;
}
}
---------------------------------------------

public class ReferenceMystery3 {

public static void main(String[] args) {

double x = 3.1;

double y = 3.9;

Point A = new Point();

Point B = new Point(3,6);

Point C = new Point(x,y);

System.out.println(A);

System.out.println(B);

System.out.println(C);

}

}

------------------------------------------------

Output:

(0,0)

(3,6)

(0,0)```

2. ## Re: Point constructor

I found your issue. Let me ask you a question: Where do you set the x and y variables in the constructor that takes in two doubles?

3. ## Re: Point constructor

How are the values of the object being printed out? Based upon the code you posted:
`System.out.println(A)`
should print something entirely different. Did you override the toString method and neglect to post that code? If so what values are being printed? Are your doubles being set correctly in the constructor? Are they being read correctly in the println?

4. ## Re: Point constructor

Another way to round vs using the awkward if tests:
(int)(dblVal + 0.5)

5. ## Re: Point constructor

Sorry,

I skipped the toString method iin the above code (but yes, I did include that in my code).

I expect the below main method to print Points of data types: int as well as double.therefore, the code should print:
(0,0)
(3,6)
(3.1,4.0)

Kindly find the complete code here again:

thanks,

```public class Point
{
private int x;
private int y;
private double a;
private double b;

//constructs a new point with (0,0) as coordinates
public Point()
{
x=0;
y=0;
}

//constructs a new point with the given (x,y) location
public Point(int initialX, int initialY)
{
x = initialX;
y = initialY;
}

//constructs a new point with coordinates of type double
public Point(double initialX, double initialY)
{

if((initialX-(int) initialX)>0.5)
a=(int)initialX + 1;

if((initialY-(int) initialY)>0.5)
b=(int)initialY + 1;

}

//returns the x-coordinate of this point
int getX()
{
return x;
}

//returns the y-coordinate of this point
public int getY()
{
return y;
}

public String toString()
{

return("(" + x + ", " + y + ")");
}

}```

```public class ReferenceMystery3 {

public static void main(String[] args) {
double x = 3.1;
double y = 3.9;
Point A = new Point();
Point B = new Point(3,6);
Point C = new Point(x,y);

System.out.println(A);
System.out.println(B);
System.out.println(C);
}
}```

6. ## Re: Point constructor

As I said, I am just learning about classes and objects, and am not sure how to define constructors to handle different data types.kindly point me in the right direction..

thanks,
Sarita

7. ## Re: Point constructor

Does the newly posted code work correctly? If not please show its output and describe what is wrong with it.

In your code you have 4 class variables. What are they all used for? When are they used?
Are all of them always set in the constructor?

8. ## Re: Point constructor

My code does not work as expected which is:
(0,0)
(3,6)
(3.1,4.0)

Actual result:
(0,0)
(3,6)
(0,0)

the reason for 4 class vaiables is to include different data types i.e int and double, but probably this is wrong and I am not sure how to do this

9. ## Re: Point constructor

Look closely at your use of the 4 variables. You need to think through why you have those variables and what each are used for.

10. ## Re: Point constructor

My code does not work as expected which is:
(0,0)
(3,6)
(3.1,4.0)

Actual result:
(0,0)
(3,6)
(0,0)

the reason for 4 class vaiables is to include different data types i.e int and double, but probably this is wrong and I am not sure how to do this
Ok, so you are not wanting to cast your doubles as ints, like you have here:
```if((initialX-(int) initialX)>0.5)
a=(int)initialX + 1;

if((initialY-(int) initialY)>0.5)
b=(int)initialY + 1;```

So, since you want to be able to return double coordinates when you give double coordinates, it is true we want to handle the two cases different. What you need to do though is somehow tell the program if you use the double constructor or not.

I'm sure there is an easier way, but I would modify it to have a boolean. That boolean will be true if the object was constructed with the double constructor, and false if with the other two constructors. I would then make the variables x and y doubles instead of ints. You can then set the x and y variables to ints for the two constructors that do not use doubles and you can still set the double constructor with the same variable. Then you need to make the getX() and getY() methods to return doubles. Lastly, you need to do a check for your boolean in your toString method. Basically, if the boolean is false, cast the x and y variables as ints before printing them out.

I wanted to explain how to do this, instead of coding it for you. Tell me if you understand everything and if you need help I'll provide what I can.