# Point constructor

• October 4th, 2010, 04:29 PM
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,

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

Code java:

```  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)```
• October 4th, 2010, 04:53 PM
aussiemcgr
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?
• October 4th, 2010, 04:57 PM
copeg
Re: Point constructor
How are the values of the object being printed out? Based upon the code you posted:
Code :

`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?
• October 4th, 2010, 05:28 PM
Norm
Re: Point constructor
Another way to round vs using the awkward if tests:
(int)(dblVal + 0.5)
• October 4th, 2010, 08:50 PM
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,

Code java:

```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 + ")"); }     }```

Code java:

```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); } }```
• October 4th, 2010, 08:53 PM
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
• October 4th, 2010, 09:01 PM
Norm
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?
• October 5th, 2010, 01:17 AM
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
• October 5th, 2010, 07:13 AM
Norm
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.
• October 5th, 2010, 07:18 AM
aussiemcgr
Re: Point constructor
Quote:

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:
Code java:

```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.
• October 5th, 2010, 08:51 AM
copeg
Re: Point constructor
When passing an object to the System.out.println function the toString() function of that object is used to print the object. You should take into account all variables in the toString() function (eg when you create the object with doubles, you still only print out the int values). This isn't the only problem however, as aussiemcgr pointed out.
• October 5th, 2010, 07:34 PM