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

Thread: Help with Classes/ Instance Methods

  1. #1
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Help with Classes/ Instance Methods

    Hello everyone,

    I'm stuck on a programming exercise and was wondering if anybody could help. I'll start off with the question:

    Add to class Point in Question 1 an instance method midPoint which returns a new Point object representing the mid-point of two points where the second point is accessed via a parameter. Write a test program which reads two points and prints their midpoint in a form typified in the following i/o:

    Enter coordinates: 2.1 3.4
    Enter coordinates: 5.5 9.2
    The mid-point of (2.1,3.4) and (5.5,9.2) is (3.8,6.3)

    To be specific, i'm not quite sure if my midPoint() method is right, and also i'm unsure as to how to call it and display it, the way it is requested in the question. All the other methods in class Point have been tested and are working correctly. Oh and incase you were wondering, the midpoint formula is:

    ((x1+x2)/2 , (y1+y2)/2)

    Here is my attempted code so far :

    class Point 
    {
    	double x, y; 			// cooordinates
     
    	void getPoint() 
    	{ 		// read coordinates 
    		System.out.print("Enter coordinates: ");
    		x = Double.parseDouble(Console.readToken());   
    		y = Double.parseDouble(Console.readToken()); 
    	}
     
    	double distance() 
    	{ 		// distance from the origin
    		return(Math.sqrt(x*x+ y*y)); 
    	}
     
    	Point midPoint(Point mid)
    	{
    		Point pt = new Point();
    		pt.x = (x+y)/2;
    		pt.y = ((mid.x + mid.y)/2);
    		return pt;
    	}
     
    }
     
     
    class PointTest 
    {
    	public static void main(String[] args) 
    	{
    		Point p = new Point();
    		p.getPoint();
    		Point r = new Point();
    		r.getPoint();
    		System.out.print(p.midPoint(r));
    	}
    }

    Many thanks in advance to any help given
    Last edited by Stockholm Syndrome; March 9th, 2011 at 12:34 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with Classes/ Instance Methods

    I might be misunderstanding your requirements, but I don't see how your midPoint funciton corresponds to the midpoint formula. It looks like you're adding the x and y coordinates of one point, dividing that by two, and making that the x of the midpoint, then adding the x and y coordinates of another point, dividing by two, and making that the y value of the midpoint. That doesn't make any sense to me. Hint: What happens when you take the midpoint of (-100, 100) and (100, -100)?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Classes/ Instance Methods

    Quote Originally Posted by KevinWorkman View Post
    I might be misunderstanding your requirements, but I don't see how your midPoint funciton corresponds to the midpoint formula. It looks like you're adding the x and y coordinates of one point, dividing that by two, and making that the x of the midpoint, then adding the x and y coordinates of another point, dividing by two, and making that the y value of the midpoint. That doesn't make any sense to me. Hint: What happens when you take the midpoint of (-100, 100) and (100, -100)?
    Apologies yes, you're right I forgot to update my code the code should be:

    Point pt = new Point();
    		pt.x = (x+mid.x)/2;
    		pt.y = ((y+mid.y)/2);
    		return pt;

    I'm still not sure how to display this though. When i use it in a print statement the output in my IDE is:

    Enter coordinates: 3.1 4.5
    Enter coordinates: 2.2 5.6
    Point@42e816

    The question is a bit vague as it doesnt explain whether i'm allowed to add another method maybe to print the midpoint in the required format. Do you think it needs another method?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with Classes/ Instance Methods

    The problem is that Java doesn't "know" how to simply print a Point, so it prints out the stuff you posted (from Object's toString() method). You probably want to print out the actual x and y values of the point in a way that makes sense to you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Classes/ Instance Methods

    Quote Originally Posted by KevinWorkman View Post
    The problem is that Java doesn't "know" how to simply print a Point, so it prints out the stuff you posted (from Object's toString() method). You probably want to print out the actual x and y values of the point in a way that makes sense to you.
    I understand what you mean now. But this is my first time using instance methods which return objects and i'm a bit puzzled. For example, is there a way of individually printing the variables from the Point midPoint method ? Because if i try something like System.out.print(pt.x + ", " + pt.y); it will tell me that "pt" cannot be resolved to a variable.

    Edit: I've solved it by adding another method to class Point which prints the variables as im unaware of doing it any other way. Thanks for your help
    Last edited by Stockholm Syndrome; March 9th, 2011 at 04:46 PM.

  6. #6
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Help with Classes/ Instance Methods

    Ok, taking a look at the method, it will return a Point object. When you try to do a System.out.print(p.midPoint(r)) in the main method, the print() method wants a String. So Java, by design, extends everything off Object, and Object has a toString() method, Java calls Point.toString() on your Point to convert your Point into a String. Looking in Object's java api docs it's toString method returns

    getClass().getName() + '@' + Integer.toHexString(hashCode());

    Thus the reason you get Point@42e816.

    What you want to do is override the toString() method to something useful like
    return("Point: x = " + x + " y = " + y + "\n")

    then, when you try to print the point object, it will look into your version of toString and follow your instructions on how to build and return the string, instead of the instructions in Object.toString()

    Hope this helps.

    Edit:
    After rereading the thread...

    pt.x and pt.y should have returned the x and y values, if you declared pt correctly in the main. The scope of pt is limited to the midPoint method in the Point class, and not available in the main unless you store what midPoint returns.

    Point pt = p.midPoint(r); // in main method

    Also please reconsider learning to do it via overriding the toString() Method. If you are curious as to what it would look like:
    		//@override Object.toString() in Point Class
    		public String toString(){
    			return("Point: x = " + x + " y = " + y);
    		}
    Apologies to Mods in advance for spoon feeding... last post seems to imply that OP wasn't going to bother trying this way.
    Last edited by JJeng; March 9th, 2011 at 05:45 PM.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with Classes/ Instance Methods

    I think the issue you're struggling with is what to do with the value returned from the midpoint function. You could do something like this:

    Point p1 = new Point();
    Point p2 = new Point();
    Point midPoint = p1.midPoint(p2);
    System.out.println(midPoint.x + ", " + midPoint.y);

    By the way, there is already a standard Java class called Point, so your use of the name Point keeps throwing me off :p
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Stockholm Syndrome (March 9th, 2011)

  9. #8
    Member
    Join Date
    Oct 2010
    Location
    UK
    Posts
    42
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Help with Classes/ Instance Methods

    Thanks mate, thats helped me solved it without the use of an extra method. I'm now understanding the concept of returning objects a lot better. Cheers for your help

  10. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with Classes/ Instance Methods

    Cool. What JJeng was saying is valid though, and it's not really an extra method. You'd simply be overriding an existing method, throwing in what you already have as the return statement, then printing it out how you were originally trying to print it out.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Odd Instance Field Issue
    By destructobob in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 01:46 AM
  2. Replies: 2
    Last Post: January 22nd, 2011, 11:20 PM
  3. Compare instance of a class to another
    By srs in forum Java Theory & Questions
    Replies: 5
    Last Post: December 2nd, 2010, 10:39 AM
  4. [SOLVED] Help with classes & instance methods
    By ShakeyJakey in forum Object Oriented Programming
    Replies: 16
    Last Post: July 30th, 2010, 01:20 PM
  5. Creating new instance
    By vluong in forum Object Oriented Programming
    Replies: 2
    Last Post: November 28th, 2009, 11:35 PM

Tags for this Thread