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

Thread: Creating a class for points in three dimensions.

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Junior Member
    Join Date
    Jan 2010
    Posts
    11
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: Creating a class for points in three dimensions.

    to start with

    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.

    read the following

    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. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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. #4
    Junior Member
    Join Date
    Jan 2010
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Creating a class for points in three dimensions.

    Quote Originally Posted by vkumar23 View Post

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

    Copeg: Yes, I meant Overloading, not overriding. But is my code overloading the distance method at this point?

  5. #5
    Junior Member
    Join Date
    Jan 2010
    Posts
    11
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default 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

Similar Threads

  1. Creating new instance
    By vluong in forum Object Oriented Programming
    Replies: 2
    Last Post: November 28th, 2009, 11:35 PM
  2. how to load a class and type cast to that class?
    By chinni in forum Object Oriented Programming
    Replies: 2
    Last Post: November 9th, 2009, 10:18 AM
  3. Creating a class file
    By ipatch in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 8th, 2009, 07:19 PM
  4. Need help creating this program
    By ixjaybeexi in forum File I/O & Other I/O Streams
    Replies: 25
    Last Post: October 19th, 2009, 07:08 AM
  5. creating a gui
    By rsala004 in forum AWT / Java Swing
    Replies: 2
    Last Post: July 21st, 2009, 02:17 AM