Results 1 to 9 of 9
5Likes Thread: Immutable code to mutable
- 09-28-2011, 06:27 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 3
- Rep Power
- 0
- 09-28-2011, 06:32 PM #2
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!
- 09-28-2011, 06:41 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 3
- Rep Power
- 0
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?
-
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:
and would allow users of the class to set the value's value.Java Code:public void setValue(int value) { this.value = value; }
And rather than try to make it mutable, why not just create a wrapper class?
- 09-28-2011, 06:47 PM #5
Re: Immutable code to mutable
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 09-28-2011, 06:55 PM #6
Member
- Join Date
- Sep 2011
- Posts
- 3
- Rep Power
- 0
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
- 09-28-2011, 07:01 PM #7
Re: Immutable code to mutable
String is immutable. What if I want to make it mutable? I add a wrapper:
Now wherever I want to use a mutable String, I simply use this wrapper class instead.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; } }How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
-
Re: Immutable code to mutable
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.
- 09-29-2011, 02:59 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Immutable code to mutable
Yes.Nothing in that code makes the code or class immutable.
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:
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.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 }
Similar Threads
-
Mutable Objects having equals method
By Yadu in forum Advanced JavaReplies: 10Last Post: 08-22-2010, 04:43 PM -
Strings and Immutable
By al_Marshy_1981 in forum New To JavaReplies: 19Last Post: 06-18-2010, 07:22 AM -
What is Immutable in String
By elektronika in forum New To JavaReplies: 4Last Post: 12-10-2009, 12:58 PM -
[SOLVED] [newbie] Is my method mutable?
By jon80 in forum New To JavaReplies: 4Last Post: 05-20-2009, 11:29 PM -
What is an Immutable Class
By maheshkanda in forum New To JavaReplies: 3Last Post: 02-06-2009, 08:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks