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 11 of 11

Thread: HELP!! Cant get point program to work

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default HELP!! Cant get point program to work

    So I am trying to create a program that gives the distances between two points that can be inputted by the user. And here has to be a default constructor to set x and y values to 0. Any help to set me in the right direction would be greatly appricated!
    import java.util.*;
    import java.math.*;        
     
    public class point
    {
    	point P1=new point(0,0);
        point P2=new point(0,0);
    	double x;
    	double y;
     
        double getX()
        {
            return x;
        }
     
        double getY()
        {
           return y;
        }
     
     
        public void distance(double x, double y)
        {
        	double p=Math.sqrt((P1.getX() - P2.getX()) *  (P1.getX() - P2.getX()) + 
        			(P1.getY() - P2.getY()) *  (P1.getY() - P2.getY()) );
        }
     
        public double distance( double p)
        {
     
            distance(x,y);
            return p;
        }
    }

    anddd the main...

    import java.util.*;
    import java.math.*;
    public class Lab07 
    {
     
        public static void main(String[] args)
        {
        	point P1=new point(0,0);
            point P2=new point(0,0);
        	Scanner in = new Scanner(System.in);
            System.out.println("Enter first x coord");
            double x = in.nextDouble();
            System.out.println("Enter first y coord");
            double y = in.nextDouble();      
            System.out.println("The distance between the two points is "+ point.distance(p));
        }
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,485
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: HELP!! Cant get point program to work

    Are you getting errors? If so, post them. Is the program compiling and running but not working as you'd hoped? Then post sample runs and describe how the run does not meet the requirements.

    Some 'points' to consider:

    Follow Java naming conventions and capitalize class names. I recommend that you also don't name your classes the same as existing core Java classes.

    A constructor is a method without a return type, not even void, that has the same name as the class. The "default" constructor refers to the zero-argument constructor.

    A 'point' in a cartesian coordinate system is defined by two parameters, usually referred to as 'x' and 'y'.

    Two points require 4 parameters, (x1, y1) and (x2, y2).

    The distance between 2 points ( x1, y1 ) and ( x2, y2 ) = Sqrt ( ( x2 - x1 )^2 + ( y2 - y1 ) ^ 2 ).

    The method distance() does not belong in a point class. A point has no dimensions.

    Let us know what you need help with.

  3. #3
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: HELP!! Cant get point program to work

    Hello.
    This shall be your point class.

    **code removed**
    Last edited by jps; July 28th, 2013 at 08:22 AM. Reason: spoonfeeding

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,485
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: HELP!! Cant get point program to work

    If the point class must have a 'distance' method, then ifs signature should be:
    // this method returns the distance from point1 to point2 by calling point1.distanceTo( point2 )
    public double distanceTo( Point point2 ) { }

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    Lashickk (July 29th, 2013)

  6. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: HELP!! Cant get point program to work

    @syedbhai
    Please see: The problem with spoonfeeding

  7. #6
    Junior Member
    Join Date
    Jul 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: HELP!! Cant get point program to work

    Ok so I fixed most of the errors , the only problem now is that it cant read "p1 or p2" form my main.

    ---"p1 cannot be resolved, p2 cannot be resolved. in distance(double, double)
    import java.util.*;
    import java.math.*;        
     
    public class point
    {
    	double x;
    	double y;
     
    	public point()
    	{
    		double x=0;
    		double y=0;	
    	}	
        double getX()
        {
            return x;
        }
     
        double getY()
        {
           return y;
        }
     
     
        public double distance(double x, double y)
        {
        	double p=Math.sqrt((P1.getX() - P2.getX()) *  (P1.getX() - P2.getX()) + 
        			(P1.getY() - P2.getY()) *  (P1.getY() - P2.getY()) );
     
        }
     
        public double distance(double p)
        {
     
            distance(p);
            return p;
        }
     
     
     
    }
    and..
    import java.util.*;
    import java.math.*;
    public class  Lab07 
    {
     
        public static void main(String[] args) 
        {
     
        	point P1=new point();
            point P2=new point();
        	Scanner in = new Scanner(System.in);
            System.out.println("Enter first x coord");
            double x = in.nextDouble();
            System.out.println("Enter first y coord");
            double y = in.nextDouble();      
            System.out.println("The distance between the two points is "+ point.distance());
        }
     
    }


    --- Update ---

    Also how can i get this to input two cords without creating two new x and y variables?

  8. #7
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: HELP!! Cant get point program to work

    Hello.
    This shall be your code.
    ...

    Thanks,
    Syed.
    Last edited by copeg; July 29th, 2013 at 03:42 PM. Reason: Removed Code

  9. The Following User Says Thank You to syedbhai For This Useful Post:

    Lashickk (July 29th, 2013)

  10. #8
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: HELP!! Cant get point program to work

    @syedbhai, this is the second time I've had to edit your post in a single thread - consider this a warning, and read post #5. Continue to disregard moderator edits and warnings and we will have no choice but to take further action.

  11. #9
    Junior Member
    Join Date
    Jul 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: HELP!! Cant get point program to work

    Ok guys finally got it down to this last bit, all i need is for my Point class to read the p1 and p2, it comes up with "cant be resolved. Any fixes?

    import java.util.*;
    import java.math.*;        
     
    public class Point 
    {
    	double x0;
    	double y0;
     
    	public  Point()
    	{
    		double x=0;
    		double y=0;	
    	}	
    	public  Point(double x, double y)
    	{
    		x0=x;
    		y0=y;		
    	}
        double getX()
        {
            return x0;
        }
        double getY()
        {
           return y0;
        }
        public static double distance()
        {
        	double p=Math.sqrt((P1.getX() - P2.getX()) *  (P1.getX() - P2.getX()) + 
        			(P1.getY() - P2.getY()) *  (P1.getY() - P2.getY()) );
        	return p;
        }
     
    }
    andddd.....
    import java.util.*;
    import java.math.*;
     
    public class  Lab07 
    {
     
        public static void main(String[] args) 
        {
     
        	Scanner in = new Scanner(System.in);
            System.out.println("Enter first x coord");
            double x = in.nextDouble();
            System.out.println("Enter first y coord");
            double y = in.nextDouble();   
            Point P1 = new Point(x,y);
            System.out.println("Enter second x coord");
            x = in.nextDouble();
            System.out.println("Enter second y coord");
            y = in.nextDouble(); 
            Point P2=new Point(x,y);
            System.out.println("The distance between the two points is " + Point.distance());
        }
     
    }

  12. #10
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: HELP!! Cant get point program to work

    Sir,
    I am sorry. I apoligize. Please forgive me.
    I will do my best to avoid spoonfeeding in the future.

    Thanks,
    Syed.

  13. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,485
    My Mood
    Mellow
    Thanks
    152
    Thanked 610 Times in 597 Posts

    Default Re: HELP!! Cant get point program to work

    . . . all i need is for my Point class to read the p1 and p2, it comes up with "cant be resolved.
    I explained how this should be done in post #4. You can name the method what you like; I don't have to like it. Reread Post #4 and let me know what you don't understand or can't code to make that work.

Similar Threads

  1. Replies: 1
    Last Post: May 13th, 2013, 05:09 PM
  2. What java related software do i use for my Point of sale program?
    By bensonn in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 27th, 2013, 10:04 AM
  3. Java Program that plots point on a graph..need some help.
    By neontiger in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 10th, 2012, 06:55 PM
  4. Replies: 2
    Last Post: November 30th, 2011, 06:35 PM
  5. My program doesnt want to do its math point my errors out please
    By Redlight in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2010, 01:56 PM