-2

Please help, I am having trouble with this kind of problem.

my class Exam

    private boolean status;
    private double price;
    String message = "Good luck";

my contructor for the class Exam

public Exam(String period, String level) {
    
}

public void setPrice(double p) {
    this.price = p;
}

public double getPrice() {
    return price;
}

public void setFinished(boolean f) {
    this.status = f;
}

public boolean isFinished() {
    return status;
}


class Midterm extends Exam {
        public Midterm(String period, String level) {
        super(period, level);
        System.out.println("Exam has started");
    }
}

Class Quiz that extends Exam

    public Quiz(String period, String level) {
        super(period, level);
    }

    
class Essay extends Quiz {

    public Essay(String period, String level) {
        super(period, level);
    }
    
}
    
}


public static void main(String[] args) {
    Exam e = new Exam("wew", "wew");
    
    
    System.out.println(e);
}
}

what I am getting here something like this asd.Exam@36baf30c what I want is to display the string I assigned

1337
  • 43
  • 1
  • 7
  • When `e` is an object, `System.out.println(e);` is equivalent to `System.out.println(e.toString());`. Write an appropriate `toString()` method for one or more of your classes. – John Bollinger Nov 09 '20 at 13:43
  • So, @Abra, you are suggesting that I should write an answer for a question that has already been asked and answered? – John Bollinger Nov 09 '20 at 13:56

1 Answers1

0

When you call System.out.println method, it calls toString method of object to print. The default value of toString method is:

getClass().getName() + '@' + Integer.toHexString(hashCode())

So, if you want to print some custom value for object, you would need to override toString in that object as below:

@Override
public String toString() {
    return "Exam: " + period + " : "+ level;
}
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47