1

There is alot more in this project but I dont know how to start Im stuck on how to create a constructor with an array that will take a unknown number of dimensions in a point.

sptBot
  • 51
  • 8
  • why you can't use an array as argument? – Alessio Nov 22 '13 at 20:18
  • I dont know how to structure the constructor to take a unknown dimensional number in a point. The last project we did we had to get the linelenght of 3D coordinate x,y,z. But in this project we have to use n-dimensional which it could be x,y,z,a,b,c.... until user presses Exit. Then I need to get the linelenght from the dimensions from the two points. – sptBot Nov 22 '13 at 20:30

2 Answers2

3

You can have an array as an argument for your constructor

public class MultiDemPoint{
    public MultiDemPoint(double[] coords){

    }
}

You will need to pass an array of doubles:

new MultiDemPoint(new double[]{4, 5.0, 3, 2, 6, 4.6});


You can also expect undefined number of coordinates as separated values

public class MultiDemPoint{
    public MultiDemPoint(double... coords){

    }
}

You can pass parametrs like new MultiDemPoint(4, 5.0, 3, 2, 6, 4.6); in this case.


Practice:

  • Create a new empty project in your IDE
  • Create MultDemPoint.java file with following code:

    public class MultiDemPoint{
        private double[] coords;
        //double... coords will automatically convert all supplied coordinates to the array,
        // we can store it in double[] coords.
        public MultiDemPoint(double... coords){
            this.coords = coords;
        }
    
        public void printCoords(){
            for(int i=0; i<coords.length; i++){
                System.out.println("Coordinate #"+i+": "+coords[i]);
            }
            System.out.println("");
        }
    }
    
  • Use this code for your Main.java

    public class Main {
            public static void main(String[] args){
            MultiDemPoint point1 = new MultiDemPoint(1,2,3,4);
            MultiDemPoint point2 = new MultiDemPoint(3);
            MultiDemPoint point3 = new MultiDemPoint(5.44444444,232323.12323,321321);
            System.out.println("Point1 coordinates:");
            point1.printCoords();
    
            System.out.println("Point2 coordinates:");
            point2.printCoords();
    
            System.out.println("Point3 coordinates:");
            point3.printCoords();
    
        }
    }
    
mdolbin
  • 936
  • 4
  • 8
  • Inside the method block you would treat the varargs argument as an array, so the first `double` would be `coords[0]` exactly as in his first example. It just allows for more convenient invocation of the method... you get to do `MultiDemPoint(1,4,8,9)` instead of `double[] d = {1, 4, 8, 9}; MultiDemPoint(d)`. – dcsohl Nov 22 '13 at 20:27
  • @user3023185 check updates – mdolbin Nov 22 '13 at 20:43
  • Any suggestions of What should I use for the getters and setters in the constructor. I the last project I was using x and y. But in this project is undefined. – sptBot Nov 22 '13 at 20:52
  • @user3023185 for now, there is only private field `coords` of type `double` without getter. But you can add `public double[] getCoords(){ return coords }` and use it to obtain the list of coordinates that you specified on creation. You need to read more about programming to make it clear. https://www.udacity.com/course/cs046 is a great place to start if you're unfamiliar with java and programming in general, Oracle also has a great documentation-like tutorial http://docs.oracle.com/javase/tutorial/reallybigindex.html – mdolbin Nov 22 '13 at 21:03
0

If you don't want to pass an array, you can use so called "varargs".

public class Point
{
 public Point(double... x)
 {
 }
}

Then you can call:

new Point(1, 2, 3);
new Point(1, 2, 3, 4);
Rémi
  • 3,705
  • 1
  • 28
  • 39