Hello everybody,

This is my first time on this forum. I started this year with a study, where i learn Java. I'm from the Netherlands, so some names in the classes you may not know.
I have a problem with printing out a few arrays of P in the class Veelhoek.
I think i have made some mistakes in the print constructor.
All help is appreciated! :)

Kind Regards

MeandJava

class Veelhoek {

    Punt[]P;

    public Veelhoek() {

        P = new Punt[3];
        P[0] = new Punt (0,0);
        P[1] = new Punt (0,0);
        P[2] = new Punt (0,0);

    }

    public Veelhoek (int d) {
        P = new Punt [d];
        for(int i = 0; i<d; i++){
            P[i] = new Punt (0,0);
        }
    }
        void print() {

        for(int i = 0; i<P.length; i++){
        System.out.println(P[i]);
        }

    }
}

public static void main(String[] args) {
    
    Veelhoek V;
        V = new Veelhoek();
        V.print();
        System.out.println();
    }

}

Recommended Answers

All 11 Replies

Member Avatar for coil

Your code seems correct. What errors are you getting (if any), or what output are you getting, and what should the output look like?

This is the output i get:

run:

practicum4.Punt@addbf1
practicum4.Punt@42e816
practicum4.Punt@9304b1

BUILD SUCCESSFUL (total time: 3 seconds)

greetz

MeandJava

XD the problem actually is not in the snippet of code you posted here. You are printing the contents of the Array, namely, the three Punt objects. But it doesn't know how to print a "Punt object" so it is printing the memory address of the objects. If you go into the Punt class, create a method with the header public String toString() and have it return the String of what you want to be printed to the screen when that "Punt object" is printed. Then it should print correctly.

Example:

public class Punt
{
    public String toString()
    {
        return "Punt!";
    }
}
public class PuntPrinter
{
    public static void main(String[] args)
    {
        Punt p = new Punt();
        System.out.println(p);
    }
}

When you run the main method, it is told to print a "Punt object". How does it know what to print? It automatically calls the toString() method in the Punt class, and sees it should print "Punt!" onto the screen, which it does. If there is no toString() method then it prints the memory address, that weird output you were getting.

The reason for why toString() works like this relates to inheritance and polymorphism, but I don't want to confuse you too much with that if you haven't covered it.

Good luck!!!

Hi Kvass,

I don't really get it. I thinks it is because i haven't show you the total code. That's because otherwise it is hard to understand and my Praticum papers are in dutch. But now i have already a class Punt and i have two mains at this moment. All help is appreciated :) I will show you the total code:

/*
 * We maken een punt aan A.
 * Daarnaast maken we nog een punt aan B.
 * Het punt van B verschuift door middel van een methode.
 */
package practicum4;
//@author Patrick

//We maken een nieuwe class genaamd Punt
class Punt {
    double x;
    double y;

    public String toString() {
        return "Punt!";
    }


public Punt() {
    x = 0;
    y = 0;
}

public Punt (double x, double y) {
    this.x = x;
    this.y = y;
}



void print(){
    System.out.println("Het coördinaat van x is" + " " + x + "\n" +"Het coördinaat van y is" + " " + y);
    }

void verschuif(double x, double y) {
    this.x = x;
    this.y = y;
    
                               }
}

/*We maken een nieuwe class genaamd veelhoek. De Veelhoek heeft een array van
punten.*/
class Veelhoek {

    Punt[]P;

    public Veelhoek() {

        P = new Punt[3];
        P[0] = new Punt (0,0);
        P[1] = new Punt (0,0);
        P[2] = new Punt (0,0);

    }

    public Veelhoek (int d) {
        P = new Punt [d];
        for(int i = 0; i<d; i++){
            P[i] = new Punt (0,0);
        }
    }
    
    public class PuntPrinter {

        public static void main(String[] args) {
        {
            Punt p = new Punt();
            System.out.println(p);
        }
    }

    
}


public class Main {
  
   public static void main(String[] args) {
    Punt a;
        a = new Punt(0,0);
        a.print();
        System.out.println();

    Punt b;
        b = new Punt (3,2);
        b.print();
        System.out.println();
        b.verschuif(5,3);
        b.print();
        System.out.println();

    Veelhoek V;
        V = new Veelhoek();
        V.print();
        System.out.println();


    }

            }
}

Re-read kvass's post. It contains exactly the info you need, and gives you exactly the instructions you need.

Hi JamesCherill,

I have re-read Kvass's post, but i don't understand where i could put it into my code. Could someone tell where i should put it? The code i showed you all is that good, what do i have too change? I started a few weeks ago with java so i don't know that much. I understand the answer of Kvass, but i don't know where to put it. All help is appreciated. :P

Greetz

MeandJava

You can put the
public String toString() { ...}
method anywhere in the Punt class (but not inside another method). It's just an ordinary method.
Then, anytime you want to print, println(...) will automatically use that method to get the String when you want to print a Punt.

Hello everybody,

When i use it on the way i show you in this code i get a override annotation.
Could you tell me what i'm doing wrong, i have read all your advices, but he doesn't print the values. All help is appreciated :)
This is the code:

/*
 * We maken een punt aan A.
 * Daarnaast maken we nog een punt aan B.
 * Het punt van B verschuift door middel van een methode.
 */
package practicum4;
//@author Patrick

//We maken een nieuwe class genaamd Punt
class Punt {
    double x;
    double y;



public Punt() {
    x = 0;
    y = 0;
}

public Punt (double x, double y) {
    this.x = x;
    this.y = y;
}



void print(){
    System.out.println("Het coördinaat van x is" + " " + x + "\n" +"Het coördinaat van y is" + " " + y);
    }

void verschuif(double x, double y) {
    this.x = x;
    this.y = y;
    
                               }
}

/*We maken een nieuwe class genaamd veelhoek. De Veelhoek heeft een array van
punten.*/
class Veelhoek {

    Punt[]P;

    public String toString() {
        return "Punt!";
    }

    public Veelhoek() {

        P = new Punt[3];
        P[0] = new Punt (0,0);
        P[1] = new Punt (0,0);
        P[2] = new Punt (0,0);

    }

    public Veelhoek (int d) {
        P = new Punt [d];
        for(int i = 0; i<d; i++){
            P[i] = new Punt (0,0);
        }
    }

     
    
public class PuntPrinter {

   public void main(String[] args) {
        {
    Punt P = new Punt();
    System.out.println(P);
        }
    }

    
}
}


public class Main {
  
   public static void main(String[] args) {
    Punt a;
        a = new Punt(0,0);
        a.print();
        System.out.println();

    Punt b;
        b = new Punt (3,2);
        b.print();
        System.out.println();
        b.verschuif(5,3);
        b.print();
        System.out.println();

    Veelhoek V;
        V = new Veelhoek();
        
        System.out.println();


    }

            }

You put the method in Veelhoek; it should be in Punt.
The override annotation is right. it shows you have got the right method name etc.
It's because the class Object has a toString() method, it's the one that gives you stuff like "practicum4.Punt@addbf1" (classname@memory address). Because every class inherits from Object, every class inherits toString(). When you override it in your class the @override annotation confirms that you have correctly and intentionally overridden an inherited method.
so: put the method in Punt. return something like
"Punt: x is " + x + ", y is " + y
and check that you do get the @override annotation.

I have changed the code like you said, but it doesn't print. Thanks for all the help I already got! The target is too print the values of the class veelhoek, are we doing the right steps for that goal. Thanks for the help in advance :)

/*
 * We maken een punt aan A.
 * Daarnaast maken we nog een punt aan B.
 * Het punt van B verschuift door middel van een methode.
 */
package practicum4;
//@author Patrick

//We maken een nieuwe class genaamd Punt
class Punt {
    double x;
    double y;

public String toString() {
        return "Punt: x is" + x + "y is" +y;
    }


public Punt() {
    x = 0;
    y = 0;
}

public Punt (double x, double y) {
    this.x = x;
    this.y = y;
}



void print(){
    System.out.println("Het coördinaat van x is" + " " + x + "\n" +"Het coördinaat van y is" + " " + y);
    }

void verschuif(double x, double y) {
    this.x = x;
    this.y = y;
    
                               }
}

/*We maken een nieuwe class genaamd veelhoek. De Veelhoek heeft een array van
punten.*/
class Veelhoek {

    Punt[]P;

    public Veelhoek() {

        P = new Punt[3];
        P[0] = new Punt (0,0);
        P[1] = new Punt (0,0);
        P[2] = new Punt (0,0);

    }

    public Veelhoek (int d) {
        P = new Punt [d];
        for(int i = 0; i<d; i++){
            P[i] = new Punt (0,0);
        }
    }

     
    
public class PuntPrinter {

   public void main(String[] args) {
        {
    Punt P = new Punt();
    System.out.println(P);
        }
    }

    
}
}


public class Main {
  
   public static void main(String[] args) {
    Punt a;
        a = new Punt(0,0);
        a.print();
        System.out.println();

    Punt b;
        b = new Punt (3,2);
        b.print();
        System.out.println();
        b.verschuif(5,3);
        b.print();
        System.out.println();

    Veelhoek V;
        V = new Veelhoek();
        
        System.out.println();


    }

            }

but it doesn't print

That doesn't help me. Exactly what does or doesn't happen.
To make the V... class printable, do exactly the same thing - give it a toString method. maybe return something like:

String s = "";
for(int i = 0; i<d; i++){
 s+=  p[i].toString() + "\n";
}
return s;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.