0

I've got these two files:

public class Uebungsblatt {

    public int nummerBlatt;
    public int maxPunkte;
    public int realPunkte;

    public Uebungsblatt(int nummerBlatt, int maxPunkte, int realPunkte) {
        this.nummerBlatt = nummerBlatt;
        this.maxPunkte = maxPunkte;
        this.realPunkte = realPunkte;
    }
}

and

public class Rechner {

    public static void main(String[] args) {
        int random = (int) (Math.random() * 61;

        for(int i = 1; i <= 13; i++){
            Uebungsblatt a = new Uebungsblatt(i, 60, random);
            System.out.println(a);
        }
    }
}

And now I want to print the instance "Uebungsblatt" thirtheen times. But I am not sure how to do it. I thought about a for-loop but this wont work really, I always get something like "Uebungsblatt@42a57993". I saw a tutorial online where they used "string.format" but this wont work either.

styvane
  • 59,869
  • 19
  • 150
  • 156
ethanqt
  • 63
  • 5
  • 2
    You're pointing to the whole object. What part of the Object are you trying to printout? You have no methods to call in the class, unless you are trying to call just the constructor. – whisk Nov 26 '16 at 17:58

2 Answers2

0

this can be done in two ways:

  1. List item by displaying all three values in the main method within loop.

    public class Rechner {

    public static void main(String[] args) {

    int random = (int) (Math.random() * 61;
    
    for(int i = 1; i <= 13; i++)
    {
    
        Uebungsblatt a = new Uebungsblatt(i, 60, random);
    
        System.out.println(a.nummerBlatt);
    
        System.out.println(a.maxPunkte);
    
        System.out.println(a.realPunkte);
    
    }
    

    }

}

  1. Declare a function display in Uebungsblatt class.

    public void display ()

    {

    System.out.println(a.nummerBlatt);
    
    System.out.println(a.maxPunkte);
    
    System.out.println(a.realPunkte);
    

    }

    public static void main(String[] args)

    {

    int random = (int) (Math.random() * 61;
    
    for(int i = 1; i <= 13; i++){
    
    Uebungsblatt a = new Uebungsblatt(i, 60, random);
    
    a.display();
    

    }

    }

Muhammad Ramzan
  • 294
  • 6
  • 14
0

You're trying to Print the whole object, so you're getting this type of result. Here is the two possible solution for yout code:

Print direct by accessing the object properties:

for(int i = 1; i <= 13; i++){
            Uebungsblatt a = new Uebungsblatt(i, 60, random);
            System.out.println(a.maxPunkte);
            System.out.println(a.nummerBlatt);
            System.out.println(a.realPunkte);
        }

Or You can make a Show function to Uebungsblatt Class, like:

public void show() {
        System.out.println(maxPunkte);
        System.out.println(nummerBlatt);
        System.out.println(realPunkte);
    }

then use this in your main:

for(int i = 1; i <= 13; i++){
            Uebungsblatt a = new Uebungsblatt(i, 60, random);
            a.show();
        }