• Post Reply Bookmark Topic Watch Topic
  • New Topic

toString override not implemented correctly and giving unexpected output  RSS feed

 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please consider the following class:



As you can see, in my main() method, I am instantiating a Flight object by passing 175 into the constructor. This is using my first constructor which takes an int value.
However, my overridden toString() method is skipping the return statement immediately after the if condition and returning the else condition, instead.  Since I never passed a "flight class"
the output is:

"My Flight is Flight Class," followed by an aberrant ascii symbol.
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
passing 175 into the constructor

What is the constructor doing with that value?

an aberrant ascii symbol.

That is probably the char variable's default value of 0.

If you don't assign any values to the class variables, their values will be the defaults.
 
Mark Richardson
Ranch Hand
Posts: 108
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! Silly mistake!  Fixed by doing a this.flightClass = flightClass; in the constructor. Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!