# Creating a class for points in three dimensions.

• January 7th, 2010, 05:56 PM
joachim89
Creating a class for points in three dimensions.
I have written a class for, as the title says, points in the three-dimensional space.

Code :

```import java.awt.Point;   public class Point3d extends Point{   public Point3d(int x, int y, int z){   }   public static double distance (double X1, double Y1, double Z1, double X2, double Y2, double Z2){   double distance;   distance = Math.sqrt(Math.pow((X1-X2), 2) + Math.pow((Y1-Y2), 2) + Math.pow((Z1-Z2), 2));     return distance; }     }```

I've tested the distance()-method just by calling it (without creating an instance of Point3d, that is) and it worked as intended.

The instructions to this assignment states that Point3d is to, apart from handle coordinates in 3d, be able to store x-,y- and z-coordinates and calculate the distance between two points by overriding the distance()-method in the superclass.

My tutor told me that I still have some things to take care of, leaving me with the advice "Take a look at the superclass".
Well, I have tried but failed to find what's "wrong" with my class or what it's lacking. (Quotation marks on wrong since it worked when I tried it).

Point2d: Java 2 Platform SE v1.3.1: Class Point2D
Point: Java 2 Platform SE v1.3.1: Class Point

Thanks in advance for any help.
• January 8th, 2010, 12:02 PM
vkumar23
Re: Creating a class for points in three dimensions.

a) your method should not be static as the superclass doesnot have this method as static so in essence you have
not yet overridden the method.

The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method it overrides. An overriding method can also return a subtype of the type returned by the overridden method

to override your method signature should be exactly similar to distance method in point2D

b) Your constructor doesnot do anything. It needs to stored all the 6 variables and the 6 variables needs to
be accessed in your distance method
• January 8th, 2010, 12:12 PM
copeg
Re: Creating a class for points in three dimensions.
In this particular case, many of the Point2d methods cannot be overridden by Point3d because you are dealing with 3 variables (xyz) as opposed to 2 variables (xy) - you can however overload the methods.
• January 8th, 2010, 12:35 PM
joachim89
Re: Creating a class for points in three dimensions.
Quote:

Originally Posted by vkumar23

b) Your constructor doesnot do anything. It needs to stored all the 6 variables and the 6 variables needs to
be accessed in your distance method

Ok, but how do I make it store the six variables and make them accesible in distance() ?

• January 8th, 2010, 06:13 PM
vkumar23
Re: Creating a class for points in three dimensions.
something like this

import java.awt.Point;

public class Point3d extends Point{

private double Xco1;
private double Xco2;
private double Yco1;
private double Yco1;
private double Zco1;
private double Zco1;

public Point3d(double X1, double Y1, double Z1, double X2, double Y2, double Z2){

Xco1=X1;
Xco2=X2;
Yco1=Y1;
Yco2=Y2;
Zco1=Z1;
Zco2=Z2;

}

public double distance (){

double distance = Math.sqrt(Math.pow((Xco1-Xco2), 2) + Math.pow((Yco1-Yco2), 2) + Math.pow((Zco1-Zco2), 2));

return distance;
}

}

and

you can then instantiate an object of Point3d and call the method distance

thanks
vinod