0

I would like to make k random points of type TockaXY. TockaXY is defined:

 public static class TockaXY {

      private float x;
      private float y;

      public TockaXY(float x, float y) { 
          this.x = x;
          this.y = y;  
      }

The function is like this:

 public static TockaXY[] randomCentri(int k, int lowerBound, int upperBound) {
       TockaXY[] arrayCentri = new TockaXY[k];
          for (int i = 0; i < k; i++) {
              float x = (float)(Math.random() * (upperBound - lowerBound) + lowerBound)/100;
              float y = (float)(Math.random() * (upperBound - lowerBound) + lowerBound)/100;
              TockaXY point = new TockaXY(x, y);
              arrayCentri[i]=point;
          }
          System.out.println(" Random centri: " + arrayCentri);
          return arrayCentri;

      }

But I get:

Random centri: [LTest$TockaXY;@6ce253f1

What am I doing wrong?

0

2 Answers 2

1

You need to add a toString method in your TockaXY class

@Override
    public String toString() {
        return "TockaXY{" + "x=" + x + ", y=" + y + '}';
}

and use Array.toString while printing

System.out.println(" Random centri: " + Arrays.toString(arrayCentri));

or use a loop to print each array element

for(TockaXY point: arrayCentri){
    System.out.println(point);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Right now, you only see a representation of the pointer. Java internally will call .toString() on String + Object.

Implement a toString() method in your TockaXY class and iterate over the array, transforming every entry of the array to String.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.