Thread: Constructors

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0

    Constructors


    Hello all. I'm working on a program that will take two points and tell me the distance between the two. We're working with constructors right now and my professor didn't explain them very well so I'm coming here to ask for some guidance. He stated that you have instance variables, constructor, and methods. I *believe* I have the first "point" done, using the following code:
    Code:
    public class TwoDPoint {
        private int xCor;
        private int yCor;
        public TwoDPoint(){
            xCor = 0;
            yCor = 0;
        }
        public TwoDPoint(int xCor, int yCor){
        	        int x2 = xCor;
        	        int y2 = yCor;
        	    }
    Now that is before the main method. Am I correct in putting this code in this position? If so, how would I go about creating a another "point"?
  2. #2
  3. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    You're on the right track. You have two constructors in your code, one that takes no arguments (which is also known as the default constructor) and one that takes two arguments.

    Now you'll need to use this class. Have you learned about the "new" keyword yet? Basically you'll have some code somewhere (maybe in your main() method) that does something like:

    java Code:
    TwoDPoint twoDPoint = new TwoDPoint();  // default constructor
    TwoDPoint anotherTwoDPoint = new TwoDPoint( 100, 200 ); // two int constructor


    Where you put this code is mostly up to you but, again, you're well on your way.
  4. #3
  5. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,635
    Rep Power
    1945
    You are correct that you have instance variables and two constructors.
    If you need other methods, you will have to define and add those too.

    How did you create the first point? you will create the second similar to that, just with another variable name.

    Not sure what exactly you mean with this:
    Now that is before the main method
    Creating the points in the main can be fine, it all depends at your goal.
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    In the second constructor, remember the purpose is to initialize the instance variables. So you want to make sure you do that in the contructor.

    After you finish the class, you can make as many instances as you want.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    15
    Rep Power
    0
    Alright, so here is my code for my TwoDPoint class (the class with the constructors and methods). When I use it in my TestTwoDPoint class, it doesn't like it. What did I do wrong?

    TwoDPoint
    Code:
    public class TwoDPoint {
        private int xCor;
        private int yCor;
        public TwoDPoint(){
            xCor = 0;
            yCor = 0;
        }
    public TwoDPoint(int xCor, int yCor){
        	        int x2 = xCor;
        	        int y2 = yCor;
        	    }
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    
    		
    	}
    	public double distanceFrom(int xCor,
    									 int yCor,
    									 int x2,
    									 int y2){
    		//Note: I could have used int, but for simplicity I used double so
    		//I didn't have to mess with the product at the end
    		//Such as multiply by 1.0 or use (double)
    		double a = (x2 - xCor); //the first part of the distance formula
    		double b = a * a; //a squared, distance formula
    		double c = (y2 - yCor); //the second part of the distance formula
    		double d = c * c; //b squared, distance formula
    		double e = b + d;
    		double distance = Math.sqrt(e);
    			
    		return distance;
    	}
    	public double dotProduct(int xCor,
    									 int yCor,
    									 int x2,
    									 int y2){
    		double a = xCor * x2;
    		double b = yCor * y2;
    		double dotProduct = a + b;
    		return dotProduct;
    	}
    	}
    TestTwoDPoint
    Code:
    package pa5;
    
    public class TestTwoDPoint {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    		//Origin
    		TwoDPoint origin = new TwoDPoint();
    		
    		TwoDPoint p1 = new TwoDPoint(34,76);
    		TwoDPoint p2 = new TwoDPoint(-1,5);
    		
    		System.out.println(distanceFrom(p1,p2));
    		System.out.println(distanceFrom(origin, p1));
    		System.out.println(dotProduct(p1,p2));
    		
    	}
    
    }
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    One of the principles to remember when an instance method like distancefrom is called, it can only be called by an instance of the class. So one of the points in calculating distance is implicit. You will then supply as parameter the other point.

    The way you have it now, you're not taking advantage of this fact.

    Also syntactically the distancefrom method is not known in the Test2DPoint class.
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Another problem is in the constructor that defines two variables: x2,y2 locally in the constructor, assigns them values and does nothing with them. When the constructor exits, those two variables will go away.

IMN logo majestic logo threadwatch logo seochat tools logo