Hi I wanted to know how I could change the output of the display method so the output, which currently looks like this:
Leonardo da Vinci
40 seconds ago - 2 people like this.
No comments.
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
can be made to look like this
Leonardo da Vinci
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
40 seconds ago - 2 people like this.
No comments.
These are the two methods, which are responsible for part of the output in the superclass
public String toString()
{
String text = username + "\n" + timeString(timestamp);
if(likes > 0) {
text+= " - " + likes + " people like this.\n";
}
else {
text+= "\n";
}
if(comments.isEmpty()) {
return text + " No comments.\n";
}
else {
return text + " " + comments.size() +
" comment(s). Click here to view.\n";
}
}
public void display()
{
System.out.println(toString());
}
and these are the two methods in the subclass, which complete the output by calling the superclass above.
public String toString()
{
return super.toString() + message + "\n";
}
/**
* Display the details of this post.
*
* (Currently: Print to the text terminal. This is simulating display
* in a web browser for now.)
*/
public void display()
{
System.out.println(toString());
}
This is the result you expect:
Leonardo da Vinci
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
40 seconds ago - 2 people like this.
No comments.
The value of the message
is:
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
And the value of the super.toString()
:
40 seconds ago - 2 people like this.
No comments.
Your subclass toString() returns this:
public String toString()
{
return super.toString() + message + "\n";
}
And your result is: super.toString():
Leonardo da Vinci
40 seconds ago - 2 people like this.
No comments.
and message
:
Had a great idea this morning.
But now I forgot what it was. Something to do with flying...
Do you see why it is being printed in that order? You can simply change the order to:
return message + super.toString() + "\n";
This will change the order in which it is displayed.
And have the value of the username
from the super.toString()
printed in the variable message