[Bluej-discuss] syntax of method calling

Bruce Barton bartonb at sunysuffolk.edu
Sun Jul 9 18:21:43 BST 2006


Li,
 
Firstly, ALL references to objects (and arrays) are passed from caller to called method as object references (the objects themselves are not moved around memory).  Since a String is an object instance the argument of the method call is a reference to some String object.  Only primitive typed data are passed, by value, directly between the caller and the called method.  The code you wrote stores the sequence Strings in the SeqReader3 class object instance.  You construct a new object instance in the Reverse class constructor (using an internally defined constant value, which, in the long run, is probably not what you will be wanting).  I think that what you will want, eventually, is for the SeqReader3 class to read in the original sequence from a disk file.  This it should do and store the String read in into the data field seq.  Now, the do_lowerCase() method should remove the blanks and convert to lower case and finally store the results back into seq (actually, new String objects will be created during the transformations, the final new object's reference will be store back into the reference variable seq).  You might wish to write an accessor method, getSeq(), which simply returns the value of the reference variable seq.
 
When you construct a Reverse object instance, call the constructor with the reference to the sequence String by calling the getSeq() method on the object of type SeqReader3.  So if you construct a SeqReader3 object called seqIn with the statement:
 
    SeqReader3 seqIn = new SeqReader3();
 
The construtor here will read in the value from a disk file (or, as in your example, just set the data field to a constant for testing).
 
To reverse the sequence you can use:
 
    Reverse revSeq = new Reverse(seq.getSeq());
 
This will copy the reference returned by getSeq() into a data field in the Reverse object and, as your code shows, convert the sequence to lower case.
 
To get the reversed sequence you can use:
 
    revSeq.do_RevSeq();
 
You chould also add an accessor method to the Reverse class, called getRevSeq() which simply returns the reference to the revSeq data field.
 
I hope this helps you out...
 
Bruce

________________________________

From: bluej-discuss-bounces at bluej.org on behalf of chen li
Sent: Sun 7/9/2006 12:13 PM
To: General discussion for users of BlueJ
Subject: Re: [Bluej-discuss] syntax of method calling



Dear Dr. Barton,

Thank you for the reply.

When you say "Pass the string to be reversed as an
argument to the constructor of class Reverse" do you
mean the string itself(value) or a reference to the
string object? If you mean the first case it might
rise a question for me in the future because I may
have a very big string (KB to MB size)from a file. If
it is the second case I have to create an object from
class 1 first from the popup window( I also think it
is not what I want). To get around these problems 1) I
write a method in class 1 which returns the value of
the string. 2) Then I create an object in the
constructor of class 2, which will read in the
clean-up string once initialized. And here are the
codes. Any comments?

public class SeqReader3 //Class 1
{
   private String seqIn;
   private String seq;                  
 
    public SeqReader3()
    {  
        seqIn="aatt ccGG";
    }      
        
   public String do_lowerCase()
   {
    seq=seqIn.replace(" ","");
    seq=seq.toLowerCase();
    return seq;
    }   
}


public class Reverse //Class 2
{
        private SeqReader3 seq;
        private String revSeq;
       
        public Reverse() //let the constuctor read in
        {                // the clean-up string
          seq=new SeqReader3();
          seq.do_lowerCase();
        }

        public void do_revSeq()
        {
           String str=seq.do_lowerCase();
           revSeq=new
StringBuilder(str).reverse().toString().toUpperCase();
        }
   
    public void print_revSeq()
    {
      if (revSeq==null){
         System.out.println("Please call the do_revSeq
method first:");
      }else{
         System.out.println("This is the reverse
sequence:");
         System.out.println(revSeq);
          }
    }
}



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
mailing list bluej-discuss at bluej.org
To unsubscribe or change your preferences, go to
http://lists.bluej.org/mailman/listinfo/bluej-discuss


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/ms-tnef
Size: 9609 bytes
Desc: not available
Url : http://lists.bluej.org/pipermail/bluej-discuss/attachments/20060709/d595ce85/attachment.bin


More information about the bluej-discuss mailing list