[Bluej-discuss] syntax of method calling (really, this time)

Stephen Bloch sbloch at adelphi.edu
Thu Jul 13 18:24:49 BST 2006


I wrote:
>  >>    public String do_revComSeq()
>>  >         {
>>  >            String revComSeq= new
>>
>>StringBuilder(do_comSeq()).reverse().toString().toUpperCase();
>>  >             return revComSeq;
>>  >           }
>>
>  > Wouldn't it be simpler to just call do_comSeq on the result of
>  > do_revSeq, or vice versa?

to which Chen Li replied:

>But there is no reverse method in String class. In
>order to reverse a string it must be passed to
>Stringbuilder first then changed back to string using
>toString method afer calling reverse method. This is
>the only way I know how to do it in java.

Yes, that's all true... but you're already doing all of that stuff in 
do_revSeq.  It's such a pain that once I had done it in one method I 
would never want to do it again; I would re-use do_revSeq instead of 
repeating the code in do_revComSeq.

When I find myself (or my students) writing two or more copies of 
almost-identical code, I know something is going wrong, and it's time 
to back up, take a deep breath, and figure out how to avoid the 
duplicate code.

>  > Oh, no, I see, you've still got both of those methods operating on
>  > the instance variable "seq" rather than on a parameter, so you can't
>  > call one on the result of the other.  If you wrote all three methods
>  > to take in a String parameter and return a String result, you could
>  > combine them easily:
>>
>>  public String do_revComSeq(String seq)
>>      {
>>      return do_revSeq(do_comSeq(seq));
>  >     }
>
>If I have methods operating on parameters I need to
>type them from the popup windows and in the future my
>string comes from a file, I don't think it is doable
>for me.

I think you misunderstand something.  If you have methods operating 
on parameters, you CAN invoke them from popup windows... but you can 
ALSO invoke them anywhere in your code that you have a string.  In 
particular,

class SeqReader {
    private String seq;
    public SeqReader (String filename) {
       // initialize this.seq by reading from the file
       }
    public void doSomethingInvolvingReversedSequences () {
       ... do_revSeq(this.seq) ...
       }
    public String do_revSeq(String s) { ... }
    public String do_comSeq(String s) { ... }
    public String do_revComSeq(String s) { ... }
    }

>  > Alternatively, you could have each method return a
>  > SeqReader instead of a String, and write
>  >
>>  public SeqReader do_revComSeq ()
>>      {
>>      return this.do_comSeq().do_revSeq();
>>      }
>>
>>  Either of these approaches would be much more
>  > flexible and elegant than the current version.
>  >
>
>I try both code fragments above on my BlueJ. But they don't work.

No, because you haven't designed do_comSeq and do_revSeq to be used 
that way (they have the wrong parameter types and/or return types). 
Note the phrases "IF you wrote all three methods to take in a String 
parameter and return a String result" and "you could have each method 
return a SeqReader instead of a String."  My point is that if you DID 
design them to be used that way, they'd be much more flexible and 
easier to re-use in other contexts.  OOP is all about designing 
things for future re-use, not just to solve the problem immediately 
in front of you.

-- 
					Stephen Bloch
					sbloch at adelphi.edu


More information about the bluej-discuss mailing list