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