# Help with Classes/ Instance Methods

• March 9th, 2011, 11:28 AM
Stockholm Syndrome
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 :

Code Java:

```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 :)
• March 9th, 2011, 01:15 PM
KevinWorkman
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)?
• March 9th, 2011, 01:47 PM
Stockholm Syndrome
Re: Help with Classes/ Instance Methods
Quote:

Originally Posted by KevinWorkman
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:

Code Java:

```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?
• March 9th, 2011, 01:49 PM
KevinWorkman
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.
• March 9th, 2011, 02:42 PM
Stockholm Syndrome
Re: Help with Classes/ Instance Methods
Quote:

Originally Posted by KevinWorkman
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
• March 9th, 2011, 03:51 PM
JJeng
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:

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:
Code Java:

``` //@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.
• March 9th, 2011, 04:32 PM
KevinWorkman
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:

Code java:

```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
• March 9th, 2011, 04:54 PM
Stockholm Syndrome
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 :)
• March 9th, 2011, 05:33 PM
KevinWorkman
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.