Results 1 to 9 of 9
Like Tree5Likes
  • 1 Post By Fubarable
  • 1 Post By KevinWorkman
  • 1 Post By KevinWorkman
  • 1 Post By Fubarable
  • 1 Post By pbrockway2

Thread: Immutable code to mutable

  1. #1
    Tonyuk2 is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Immutable code to mutable

    Hello,

    I would like to get any suggestion or help to convert a immutable code to mutable.

    The immutable code is a implementation of Poly data type.

    Any links or suggestions are welcome
    Last edited by Tonyuk2; 09-28-2011 at 06:39 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Immutable code to mutable

    Huh? Do you have access to the code? Can you change it? Then just add a setter.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tonyuk2 is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: Immutable code to mutable

    Hi,

    Thanks for the quick reply

    I am given a Immutable implementation of poly data type. I would like to change the same to immutable. I am using Netbeans IDE

    What do you mean by a setter?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Immutable code to mutable

    A setter is a method that sets a property. For instance if the class has an int field called "value", then a setter would be:

    Java Code:
    public void setValue(int value) {
       this.value = value;
    }
    and would allow users of the class to set the value's value.

    And rather than try to make it mutable, why not just create a wrapper class?
    KevinWorkman likes this.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Immutable code to mutable

    Quote Originally Posted by Fubarable View Post
    And rather than try to make it mutable, why not just create a wrapper class?
    I came back to suggest exactly this. Beat me to it!
    Fubarable likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Tonyuk2 is offline Member
    Join Date
    Sep 2011
    Posts
    3
    Rep Power
    0

    Default Re: Immutable code to mutable

    Suppose the immutable code is like this, how would the mutable one look like?

    Java Code:
    // Effects: If q is null throw NullPointerException
        // else return the Poly this * q
        public Poly mul(Poly q) throws NullPointerException {
    
           if ((q.deg == 0 && q.trms[0] == 0) || (deg == 0 && trms[0] == 0)) {
              return new Poly();
           }
    
           Poly r = new Poly(deg + q.deg);
           r.trms[deg + q.deg] = 0;
           for (int i = 0; i <= deg; i++) {
              for (int j = 0; j <= q.deg; j++) {
                 r.trms[i+j] += trms[i]*q.trms[j];
              }
           }
           return r;
        }
    Last edited by Fubarable; 09-28-2011 at 07:00 PM. Reason: quote tags changed to code tags

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default Re: Immutable code to mutable

    String is immutable. What if I want to make it mutable? I add a wrapper:

    Java Code:
    public class StringWrapper{
       private String string;
    
    
       public StringWrapper(String s){
          string = s;
       }
    
       public String getString(){
          return string;
       }
    
       public void setString(String s){
          string = s;
       }
    
    }
    Now wherever I want to use a mutable String, I simply use this wrapper class instead.
    Tonyuk2 likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Immutable code to mutable

    Quote Originally Posted by Tonyuk2 View Post
    Suppose the immutable code is like this, how would the mutable one look like?

    Java Code:
    // Effects: If q is null throw NullPointerException
        // else return the Poly this * q
        public Poly mul(Poly q) throws NullPointerException {
    
           if ((q.deg == 0 && q.trms[0] == 0) || (deg == 0 && trms[0] == 0)) {
              return new Poly();
           }
    
           Poly r = new Poly(deg + q.deg);
           r.trms[deg + q.deg] = 0;
           for (int i = 0; i <= deg; i++) {
              for (int j = 0; j <= q.deg; j++) {
                 r.trms[i+j] += trms[i]*q.trms[j];
              }
           }
           return r;
        }
    Nothing in that code makes the code or class immutable. I suggest that you read a tutorial on Java immutable classes. For e.g., Java Practices -> Immutable objects
    Last edited by Fubarable; 09-28-2011 at 09:04 PM.
    Tonyuk2 likes this.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Immutable code to mutable

    Nothing in that code makes the code or class immutable.
    Yes.

    But I think the thing is that this method creates a new Poly instance to represent the product and returns that. This doesn't, in itself, make the class immutable, but it is what you would do if you wanted an immutable Poly class. Another common way of making a product would be for the method to alter the state of the Poly of which it is a method so that the multiplication is "in place" ie:

    Java Code:
      /** Changes this polynomial so that it represents the product of its initial value with some other given polynomial. */
    public void mul2(Poly q) {
        // your code here
    }
    This has to be done method by method to create this (quite different) behaviour for the other class. It will involve the Poly instance variables like deg and trm[] which you haven't described.
    Fubarable likes this.

Similar Threads

  1. Mutable Objects having equals method
    By Yadu in forum Advanced Java
    Replies: 10
    Last Post: 08-22-2010, 04:43 PM
  2. Strings and Immutable
    By al_Marshy_1981 in forum New To Java
    Replies: 19
    Last Post: 06-18-2010, 07:22 AM
  3. What is Immutable in String
    By elektronika in forum New To Java
    Replies: 4
    Last Post: 12-10-2009, 12:58 PM
  4. [SOLVED] [newbie] Is my method mutable?
    By jon80 in forum New To Java
    Replies: 4
    Last Post: 05-20-2009, 11:29 PM
  5. What is an Immutable Class
    By maheshkanda in forum New To Java
    Replies: 3
    Last Post: 02-06-2009, 08:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •