Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Having troubles with printing a String  RSS feed

 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I've got a class (named Implement), with one sub-class (Bicycle).

I have several different types of variable which I would like to print, however I am having difficulty printing a String + Int + Boolean.
Is it possible to print all of these in a single statement?


 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please, don't write long lines.

You need to override toSrting() method in your Bicycle class.
If you do, you will be able to write:
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello once again Pawel!

I actually have overridden toString in the Bicycle class, although I may be off in my method.

Please examine if you wish:

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what is the problem? I was able to print a Bicycle.
The result was:
*Vehicle Statistics* Seats: 1, Name: Haro BMX, Contains Moving Parts: true, Materials: Steel, Rubber Plastic

Although, I had to correct your code because it didn't compile. The order of constructor parameters was different in the code in your first post compared to the code in your second post.
Also, you can't split a String literal between lines.
Also, I missed Implement class so I needed to create one.

Please, always provide a SSCCE when asking questions.
It will be easier for others to help you.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cory Marcus wrote:I actually have overridden toString in the Bicycle class, although I may be off in my method.

No. That sounds fine.

The main problem I see is that the only thing that's defined in Bicycle is 'seatNumber'. Are the other fields defined in Implement? (not a great name for your superclass, BTW)

My suggestion would be that you show us the code for that class too; but off the top of my head, I'd say that at the very least you're missing a super(...) call in your constructor.

Winston
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haha. Well, there you have it.

Such a simple issue..

Thank you very much Pawel!
 
Cory Marcus
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

So it actually seems like I have to make an object array of the sub-class (Bicycle).

I then add two objects to this.. and loop the array and print what each object is constructed from.

This sounds thoroughly confusing to me.

I'll also post the rest of my code, to make more sense. Apologies, I should've just done so originally.

MAIN:


PARENT CLASS:


SUB CLASS

 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't call subclasses child and superclasses parent; computer inheritance is different from biological inheritance.
You still have lines too long; I have broken up the worst offenders.
You may have been warned against the + operator on Strings because of performance problems. That warning is correct, but you can use the + operator as many times as you like on Strings and get fast performance as long as all the + operators are in the same expression. I think that you have the String divided into parts which are compile‑time constants, so the javac tool will do the String joining for you rather than leaving it to runtime. The best way to join multiple Strings all in the same expression is what beginners all use: the + operator.

Back to inheritance. I am not sure that Implement is the best name for the superclass, but that is a minor question. Remember that a subclass IS‑A superclass and a subclass instance IS‑A superclass instance. Those things are by no means easy to achieve. You have correctly put three of the four fields in the superclass and correctly provided public getXXX methods. Those methods are implicltly inherited by subclasses. But you should give all the fields (in both classes) private access. You have correctly set up the fields in the constructors. But you should give each class only one constructor. But you should not be able to access private members. You have forgotten the super() syntax. If you read the Java® Language Specification, you get the full instance creation process, which is probably difficult to read, but it progresses something like this:-

Start Bicycle constructor, but do nothing.
Go to superclass (=Implement) and call constructor, but do nothing.
Go to Object class constructor. Remembering that Object has no superclasses, execute all code in that constructor.
Back to Implement constructor. Execute all its code.
Back to Bicycle constructor. Execute all its code.

Notice you have two problems:
  • 1: Repeated code. Look at lines 13-15 and 35-37.
  • 2: A constructor which allows you to create objects with no values in the fields (lines 9-10).
  • There is a way to get rid of those problems. Start by deleting the no‑arguments constructor. Now you have one constructor only in each class, which I think is the ideal. And both constructors initialise all the fields, which is even better. But the compiler won't be happy because you are accessing private fields and it cannot work out how to call the superclass' constructor. The super() syntax will solve both those problems. You specifically call the superclass' constructor with super(name, hasMovingParts, constructedFrom);
    Remember the super(...); call must be the first thing written in the constructor. Now the superclass constructor sets up three of the fields, the subclass constructor sets up the fourth, and you can delete lines 35-37. And it will all work nicely.
     
    Campbell Ritchie
    Marshal
    Posts: 55698
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A few minutes ago, I wrote:. . . Remembering that Object has no superclasses, execute all code in that constructor. . . .
    I am not certain, but I think the Object class has no code in its constructor.
     
    Campbell Ritchie
    Marshal
    Posts: 55698
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please think about the other field names. It is customary for a getXXX method returning a boolean to start with “is” rather than “get”, but that would mean your method is called isHasMovingParts hasMovingParts might be a better name.

    I am sure other people will be able to produce much better suggestions than mine.
     
    Campbell Ritchie
    Marshal
    Posts: 55698
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You would have to replace the field names with getXXX method names in the overridden toString method, but there may be a simpler way to do it:-That is a very crude attempt which you will need to refine for pretty‑printing.
    You can get the words Implement and Bicycle in the toString method (or any other non‑static method) like this:-
    String.format("...%s...", ..., getClass().getSimpleName(), ...)
    Because of polymorphism you do not have to change that in subclasses. Try it.
     
    Winston Gutkowski
    Bartender
    Posts: 10573
    65
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:You would have to replace the field names with getXXX method names in the overridden toString method, but there may be a simpler way to do it:-That is a very crude attempt which you will need to refine for pretty‑printing.

    Obviously, great minds think alike because I was about to post something just like that.

    @Cory: The only minor issue I'd take with Campbell's post is that toString() is NOT meant for "pretty printing". It is
    "a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read."
    (from the Object.toString() docs)
    and I seem to remember reading somewhere (probably EJ) that it should be written with debugging in mind.

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 55698
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What I meant by pretty printing is
    Bicycle 1 seat.
    not
    Bicycle1seats.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!