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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Point constructor

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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,
    upad

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

     
    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)
    Last edited by copeg; October 4th, 2010 at 04:52 PM. Reason: Please use the code tags


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Point constructor

    Please fix your post by reading the directions in my signature.

    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?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Point constructor

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

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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,
    upad

    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. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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. #8
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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. #10
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Point constructor

    Quote Originally Posted by upad View Post
    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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  11. The Following User Says Thank You to aussiemcgr For This Useful Post:

    upad (October 5th, 2010)

  12. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

  13. #12
    Junior Member
    Join Date
    Oct 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Point constructor

    thank you,

    I should be able to take it from here, will mail back if I am stuck again..

Similar Threads

  1. Projecting a 3d point onto a plane
    By mingming8888 in forum Algorithms & Recursion
    Replies: 8
    Last Post: August 25th, 2010, 11:40 AM
  2. how to initialise set in a constructor
    By davie in forum Collections and Generics
    Replies: 3
    Last Post: March 12th, 2010, 05:35 PM
  3. how do i use point 3d?
    By antrax in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 6th, 2010, 05:13 PM
  4. Private Constructor
    By Ganezan in forum Object Oriented Programming
    Replies: 4
    Last Post: November 7th, 2009, 04:02 PM
  5. How can i point the mouse over a html element within the web browser?
    By bobomonkey in forum Java Theory & Questions
    Replies: 2
    Last Post: October 18th, 2009, 12:37 PM