Results 1 to 14 of 14
  1. #1
    kyameron is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default Only want to read variables declared within certain constructors

    For a programming problem, my textbook asks me to create four constructors to represent 4 different ways in which a line in a plane can be specified. Those 4 ways being, by giving a point and the slope, by giving two points, as an equation in slope-intercept form and an equation x = a if the line is vertical.

    Here is my code:

    Java Code:
    public class Line 
    {
    	private double x;
    	private double y;
    	private double slope;
    	private double x1;
    	private double x2;
    	private double y1;
    	private double y2;
    	private double m;
    	private double yInt;
    	private double a;
    	private double aSlope;
    	
    	public Line(double xVal, double yVal, double slopeVal)
    	{
    		x = xVal;
    		y = yVal;
    		slope = slopeVal;
    	}
    	public Line(double x1Val, double y1Val, double x2Val, double y2Val)
    	{
    		x1 = x1Val;
    		x2 = x2Val;
    		y1 = y1Val;
    		y2 = y2Val;
    		
    		
    	}
    	public Line(double mVal, double yIntVal)
    	{
    		m = mVal;
    		yInt = yIntVal;
    	}
    	public Line(double aVal)
    	{
    		a = aVal;
    		aSlope = 0;
    	}
    	//Need to find a way to only read, or use, values from two lines that are being compared
    	public boolean isParallel(Line other)
    	{
    		boolean parallel = false;
    		
    		if(slope == other.m || (other.y2-other.y1)/(other.x2-other.x1) == slope || m == (other.y2-other.y1)/(other.x2-other.x1)
    				)
    		{
    			parallel = true;
    		}
    		return parallel;
    	}
    	public boolean equals(Line other)
    	{
    		boolean equal = false;
    		
    		if(isParallel(other) && (yInt == other.a || ((-(other.y2-other.y1)/(other.x2-other.x1) * other.x1)+other.y1) == ((-slope * x)+ y) 
    				|| yInt == ((-other.slope * other.x)+ other.y)) || ((-(other.y2-other.y1)/(other.x2-other.x1) * other.x1)+other.y1) == yInt)
    		{
    			equal = true;
    		}
    		return equal;
    	}
    	public boolean intersects(Line other)
    	{
    		boolean intersect = false;
    		
    		if(equals(other) || !isParallel(other))
    		{
    			intersect = true;
    		}
    		return intersect;
    	}
    	
    }
    And the tester class:


    Java Code:
    public class LineTester 
    {
    	public static void main(String[] args)
    	{
    		Line line1 = new Line(1, 1, 0.5);
    		Line line2 = new Line(1, 1, 1, 2);
    		Line line3 = new Line(0.5, -1);
    		Line line4 = new Line(1);
    		
    		System.out.println(line1.equals(line2));
    		System.out.println("Expected: false");
    		System.out.println(line2.equals(line4));
    		System.out.println("Expected: true");
    		System.out.println(line1.intersects(line2));
    		System.out.println("Expected: true");
    		System.out.println(line1.intersects(line3));
    		System.out.println("Expected: false");
    		System.out.println(line1.isParallel(line3));
    		System.out.println("Expected: true");
    		System.out.println(line1.isParallel(line4));
    		System.out.println("Expected: true");
    		System.out.println(line1.isParallel(line2));
    		System.out.println("Expected: false");
    	}
    }
    So, it's buggy and I think I know the reason. When my methods run, they aren't comparing just the variables of the lines that are called to check if the lines are parallel, equal, etc. (i.e. only slope and m if comparing lines where one is constructed using three explicit parameters and the other is constructed using two explicit parameters), but they are checking if any of the conditions are true, regardless of whether the two lines being compared include variables that are defined in the other constructors or not.

    I can't change certain parts of the code because the book says that I must use them. Those are:

    Implement a class Line with four constructors, corresponding to the four cases above (4 different ways to specify a line in a plane).
    Implement methods
    boolean intersects(Line other)
    boolean equals(Line other)
    boolean isParallel (Line other)
    Use the following class as your tester class:
    public class LineTester
    {
    public static void main(String[] args)
    {
    Line line1 = new Line(1, 1, 0.5);
    Line line2 = new Line(1, 1, 1, 2);
    Line line3 = new Line(0.5, -1);
    Line line4 = new Line(1);
    System.out.println(line1.equals(line2)) ;
    System.out.println("Expected: false");
    System.out.println(line2.equals(line4)) ;
    System.out.println("Expected: true") ;
    System.out.println(line1.intersects(line2)) ;
    System.out.println("Expected: true") ;
    System.out.println(line1.intersects(line3)) ;
    System.out.println("Expected: false");
    System.out.println(line1.isParallel(line3)) ;
    System.out.println("Expected: true") ;
    System.out.println(line2.isParallel(line4)) ;
    System.out.println("Expected: true") ;
    System.out.println(line1.isParallel(line2)) ;
    System.out.println("Expected: false");
    }
    }

    Any help would be greatly appreciated.
    Last edited by kyameron; 02-02-2011 at 02:31 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    I think you are missing the point.

    Let's say that a class has only 4 instance variables: a,b,c & d. If only a single parameter is passed then a gets set. If 2 parameters then a and c get set. If three parameters a b and d get set. All four variables get set if 4 parameters are passed. For those constructors that do not have all 4 parameters then the other variables get their default values or more applicable to your situation their values are calculated from the values that are passed.

  3. #3
    kyameron is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    I'm still very confused. Are you saying that I only need 4 instance variables? So, if I have one parameter then it would be a and would represent a vertical line, if I had two parameters then I could choose a and c and they would represent slope and y-intercept, etc. Then how would java know the difference between the a that represents a vertical line and the a that represents slope? Am I even understanding you right?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    No, 4 was just the example. The point I was trying to make is... maybe some code will show it.
    Java Code:
    // what you are doing
    int x;
    int y;
    int x1;
    int y1
    int x2;
    int y2;
    
    Line(int xVal, int yVal) {
        x = xVal;
        y = yVal;
    }
    
    Line (int xValOne, int yValOne, int xValTwo, int yValTwo) {
        x1 = xValOne;
        y1 = yValOne;
        x2 = xValTwo;
        y2 = xValTwo;
    }
    
    // what you should be doing
    int x;
    int y;
    int x1;
    int y1
    // x2 and y2 not needed
    
    Line(int xVal, int yVal) {
        x = xVal;
        y = yVal;
    }
    
    Line (int xValOne, int yValOne, int xValTwo, int yValTwo) {
        x = xValOne;  // here the same x variable is used as in the above constructor
        y = yValOne; // here the same y variable is used as in the above constructor
        x1 = xValTwo;
        y1 = xValTwo;
    }

  5. #5
    kyameron is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Right, and because xVal and xValOne have two different values but they both refer to the x instance variable, then how does java know the different between the two when I try to compare them?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    No, you are still missing the point. Another example, lets say we have a Person class and each Person has a first and lastname.
    Java Code:
    //your version
    class Person {
        String firstName;
        String firstName2;
        String lastName2;
    
        Person(String s) {
            // we only get a first name
            firstName = s;
        }
    
        Person(String s1, String s2) {
            // we get a first and last name
            firstName2 = s1; // why do this when a Person only has one first name
            lastName2 = s2;
        }
    }
    
    // should be
    class Person {
        String firstName;
        String lastName;
    
        Person(String s) {
            // we only get a first name
            firstName = s;
        }
    
        Person(String s1, String s2) {
            // we get a first and last name
            firstName = s1; //here we use the same instance variable.
            lastName = s2;
        }
    }
    what you are doing wrong is declaring a different set of variables for each constructor. What you should be doing is using the same variables in the different constructors depending upon what values are passed.

    To further illustrate your misunderstanding, lets add a phone number to the Person class.
    Java Code:
    class Person {
        String firstName;
        String firstName2;
        String lastName2;
        String firstName3;
        String lastName3;
        String phoneNumber3;
    
        Person(String s) {
            // we only get a first name
            firstName = s;
        }
    
        Person(String s1, String s2) {
            // we get a first and last name
            firstName2 = s1; // why do this when a Person only has one first name
            lastName2 = s2;
        }
    
        Person(String s1, String s2, String s3) {
            firstname3 = s1;
            lastName3 = s2;
            phoneNumber2 = s3;
        }
    }
    Now what happens if you add an address? Will you declare yet another firstName, lastName and phoneNumber variables?

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Approach this from the other direction -- look at the methods you need to implement, and think about how you're going to implement them:

    Java Code:
            public boolean intersects(Line other) {
                    // if the lines aren't equal, and they aren't parallel, they intersect
            }
    
            public boolean equals(Line other) {
                    // if the lines have the same slope and share a point, they are equal
            }
    
            public boolean isParallel (Line other) {
                    // if the lines have the same slope, but they don't share a point, they are parallel
            }
    So it seems like we're going to want to compare the slopes of the two lines, so in our constructor, we're going to want to store the slope. And we'll want a point to compare, and it seems like a good candidate is the y intercept.
    Java Code:
    public class Line {
    [COLOR="Blue"]        private double slope;
            private double yIntercept;
    [/COLOR]
            ...
    }
    But what if we get an x = a constructor? In that case the slope and the y intercept are undefined! Since there's no way to store those facts in the doubles we already have, we need a couple more instance variables:

    Java Code:
    public class Line {
            private double slope;
            private double yIntercept;
    [COLOR="Blue"]        private boolean slopeUndefined = false;
            private boolean yInterceptUndefined = false;
    [/COLOR]
            ...
    }
    Now if we don't have a yIntercept, we'll need a different point -- how about the xIntercept? But that might be undefined as well (slope = 0, yIntercept = 4, for example), so we'll need two more instance variables:

    Java Code:
    public class Line {
            private double slope;
            private double yIntercept;
    [COLOR="Blue"]        private double xIntercept;
    [/COLOR]        private boolean slopeUndefined = false;
            private boolean yInterceptUndefined = false;
    [COLOR="Blue"]        private boolean xInterceptUndefined = false;
    [/COLOR]        ...
    }
    So now that you know what your instance variables need to be, you should be able to write your constructors and your methods.

    -Gary-

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gcalvin View Post
    Java Code:
    public class Line {
            private double slope;
            private double yIntercept;
    [COLOR="Blue"]        private double xIntercept;
    [/COLOR]        private boolean slopeUndefined = false;
            private boolean yInterceptUndefined = false;
    [COLOR="Blue"]        private boolean xInterceptUndefined = false;
    [/COLOR]        ...
    }
    So now that you know what your instance variables need to be, you should be able to write your constructors and your methods.
    All you have to do is think about the internal representation of the line; y == a*x+b will do fine so we only need two intstance variables, a and b. If we want to be able to represent a vertical line we have to set a= Double.POSITIVE_INFINITE (NEGATIVE_INFINITE would've been fine as well).

    The different constructors have to find the values for a and b (math 101) and store those values in member variables a and b. Skip all those booleans, they only complicate matters.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    All you have to do is think about the internal representation of the line; y == a*x+b will do fine so we only need two intstance variables, a and b. If we want to be able to represent a vertical line we have to set a= Double.POSITIVE_INFINITE (NEGATIVE_INFINITE would've been fine as well).

    The different constructors have to find the values for a and b (math 101) and store those values in member variables a and b. Skip all those booleans, they only complicate matters.

    kind regards,

    Jos
    I didn't know about Double.POSITIVE_INFINITE -- yes, this is even better. Note that a is the slope, and b is the y intercept, so we're basically talking about the same thing.

    -Gary-

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gcalvin View Post
    I didn't know about Double.POSITIVE_INFINITE -- yes, this is even better. Note that a is the slope, and b is the y intercept, so we're basically talking about the same thing.
    Yep; so we can sit back a watch the OP create a beautiful little class; I'll go make some coffee, you bring in the cookies and I'll put some beer in the fridge so we can enjoy the show ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    By the way...
    Java Code:
    public class LineTester
    {
        public static void main(String[] args)
        {
            Line line1 = new Line(1, 1, 0.5);
            Line line2 = new Line(1, 1, 1, 2);
            Line line3 = new Line(0.5, -1);
            Line line4 = new Line(1);
            System.out.println(line1.equals(line2)) ;
            System.out.println("Expected: false");
            System.out.println(line2.equals(line4)) ;
            System.out.println("Expected: true") ;
            System.out.println(line1.intersects(line2)) ;
            System.out.println("Expected: true") ;
            System.out.println(line1.intersects(line3)) ;
            System.out.println("Expected: false");
            System.out.println(line1.isParallel(line3)) ;
            System.out.println("Expected: true") ;
    [COLOR="Red"]        System.out.println(line2.isParallel(line4)) ;
            System.out.println("Expected: true") ;
    [/COLOR]        System.out.println(line1.isParallel(line2)) ;
            System.out.println("Expected: false");
        }
    }
    I don't like this. They're the same line, so they're not parallel. Seems to me that a corollary of two lines being parallel is that they share zero points. These "two lines" share an infinite number of points. I would return false on that, and I would defend it to my instructor.


    -Gary-

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gcalvin View Post
    By the way...

    I don't like this. They're the same line, so they're not parallel. Seems to me that a corollary of two lines being parallel is that they share zero points. These "two lines" share an infinite number of points. I would return false on that, and I would defend it to my instructor.
    That depends on the definition of two lines being "parallel"; if the definition was two lines are parallel if they maintain a constant separation between points closest to each other on the two lines, nothing is said about the distance; it may be zero.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    kyameron is offline Member
    Join Date
    Nov 2010
    Posts
    9
    Rep Power
    0

    Default

    Here's the working class:

    Java Code:
    public class Line 
    {
    	private double theSlope;
    	private double theYInt;
    	private boolean undefinedSlope;
    	
    	public Line(double x, double y, double slope)
    	{
    		theSlope = slope;
    		theYInt = (-slope * x) + y;
    	}
    	public Line(double x1, double y1, double x2, double y2)
    	{	
    		if(x2-x1 == 0)
    		{
    			undefinedSlope = true;
    			theYInt = Double.POSITIVE_INFINITY;
    		}
    		else
    		{
    			theSlope = (y2-y1)/(x2-x1);
    			theYInt = (-(y2-y1)/(x2-x1) * x1) + y1;
    		}
    	}
    	public Line(double m, double yInt)
    	{
    		theSlope = m;
    		theYInt = yInt;
    	}
    	public Line(double a)
    	{
    		undefinedSlope = true;
    		theYInt = Double.POSITIVE_INFINITY;
    	}
    	public boolean isParallel(Line other)
    	{
    		boolean parallel = false;
    		
    		if((theSlope == other.theSlope && undefinedSlope == false) || (undefinedSlope == true && other.undefinedSlope == true))		
    		{
    			parallel = true;
    		}
    		return parallel;
    	}
    	public boolean equals(Line other)
    	{
    		boolean equal = false;
    		
    		if(isParallel(other) && theYInt == other.theYInt)
    		{
    			equal = true;
    		}
    		return equal;
    	}
    	public boolean intersects(Line other)
    	{
    		boolean intersect = false;
    		
    		if(!isParallel(other) || equals(other))
    		{
    			intersect = true;
    		}
    		return intersect;
    	}
    	
    }
    Since you guys pretty much solved the problem, let me at least try to explain what I've learned.

    You would use multiple constructors, generally, in the case that you need to accept different types of inputs. In this case, we wanted to accept 4 different representations of a line. However, because we ultimately only needed to know the relations between the slope and y-intercepts, we could just do the math to convert the numbers inputted to either slope or y-intercept. Meaning we needed far fewer instance variables than I had initially.

    Oh, and also Double.POSITIVE_INFINITY:).

    Thanks all.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2012, 01:29 PM
  2. Class is public, should be declared in a file
    By goldhouse in forum New To Java
    Replies: 7
    Last Post: 05-10-2012, 09:17 AM
  3. LinkedList declared as an abstract superclass
    By box2box in forum New To Java
    Replies: 2
    Last Post: 04-21-2010, 10:32 PM
  4. Implicit object in jsp declared where?
    By mahendra.athneria in forum JavaServer Pages (JSP) and JSTL
    Replies: 7
    Last Post: 08-11-2009, 02:23 PM
  5. Initializing variables using constructors
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •