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

# Thread: Creating a class for points in three dimensions.

1. ## Creating a class for points in three dimensions.

I have written a class for, as the title says, points in the three-dimensional space.

```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.

2. ## 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

3. ## 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.

4. ## Re: Creating a class for points in three dimensions.

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() ?

5. ## 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