What are mutator methods and how do i incorporate them in a java code?

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1848 Views - Last Post: 23 September 2011 - 10:59 AM Rate Topic: -----

#1 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

What are mutator methods and how do i incorporate them in a java code?

Posted 22 September 2011 - 04:19 PM

i do not understand what are different types of mutator and accessor methods and how do i use them in a java code so that it returns certain values to its respected fields and once i have written the code with mutators and accessors how do i create an object and it stores the previous data in it.
Is This A Good Question/Topic? 0
  • +

Replies To: What are mutator methods and how do i incorporate them in a java code?

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: What are mutator methods and how do i incorporate them in a java code?

Posted 22 September 2011 - 04:21 PM

Geez us teh codez.
Was This Post Helpful? -2
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: What are mutator methods and how do i incorporate them in a java code?

Posted 22 September 2011 - 04:22 PM

Show us some of the code you've written with mutators and accessors (setters and getters) and tell us what you don't understand about it.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,105
  • Joined: 19-March 11

Re: What are mutator methods and how do i incorporate them in a java code?

Posted 22 September 2011 - 06:18 PM

Simpleminded accessor methods are simple:

public String getName()
{
  return name;
}


more intricate approaches might do something more involved:

public String getName()
{
  return lastName+","+firstName;
}


Or else you can use deferred calculation to save yourself some work:

public double getSomeComplicatedValueThatWeDontNeedOften()
{
  if (complicatedValueHasBeenCalculated)
     return complicatedValue;
  calculateComplicatedValue();
  complicatedValueHasBeenCalculated = true;
  return complicatedValue;
}



Those are a few ways you can use getter methods. Basically, though, you should only make an accessor if there's a reason why some other class needs to know that value. It's better design, I think, for the class that holds the data to do the work with it, and then return a result. That way you don't have calculations involving some element of class Foo taking place in classes Bar and Baz and Bebop.

Setters - "mutators" - might simply set a value:
public void setX(int x)
{
  this.x = x;
}


But what if x has to be within some bounds?

public void setX(int x)
{
  if (x < xMax && x > xMin)
    this.x = x;
}


So you can do some error checking there.

Or you can set other dependent variables:

public void setX(int x)
{
  if (x < xMax && x > xMin)
    this.x = x;
  if (x > maxXValueSoFar)
    maxXValueSoFar = x;
}


So there are some things you can do with setter methods.

However, it's important to not just let other classes set your values by default. Some people just automatically create get and set methods for every field: this is wrong. You only make these methods if there's some real reason to do so, and if there isn't better way to do what you're trying to do.

Suppose you have a Student. You're not going to just setName() - you might allow an administrator to changeName() but you're going to require a lot of paperwork and authorization if you do, otherwise you're not actually modeling the world. You wouldn't "setGPA" - that's calculated from the grades they've entered. You would enter grades, yes, and you might allow a teacher to change a grade (you might require them to enter a reason, and keep a record of the transaction), but you wouldn't just "setGrade". So instead of changing the Student's data, you're performing actions that make sense to perform. Internally, you might have a private set() method which encapsulates the process of changing that, but you don't give those keys out to anyone else.

On the other hand, clases that are more about manipulating data might want to allow freer use of getters (File.getName() is a perfectly respectable method) or setters (MyFileSpitter, a class that makes Files wholesale, might have setDestinationDirectory(), to tell the object which directory to use as its spittoon)

So when you think about mutators, think about why it is that you want to give some other class access to your data. Sometimes it makes sense, sometimes it makes no sense, and sometimes it makes a sort of sense, but you want to think carefully about how you do it, and only do the right thing.

This post has been edited by jon.kiparsky: 22 September 2011 - 08:38 PM

Was This Post Helpful? 3
  • +
  • -

#5 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Re: What are mutator methods and how do i incorporate them in a java code?

Posted 23 September 2011 - 09:14 AM

this is the code i have written till now
/**
	Lab 3
	I am Divy Tolia and this is Lab Assignment 2 located on page 178 problem number 3.
	The following program will "show the user what inventory is in stock, units on hand, and price".This program is
	designed by
	"Divy Tolia"
*/


public class DTRetailItem
{ //begin class

DecimalFormat formatter = new DecimalFormat("$#,##0.00");

	    public void setJacket(set Jacket)
	     {
	        Jacket =j;
	     }
	     public void setjacketunitsOnHand(int jacketunits)
	     {
	        jacketsunitsOnHand = jUnits;
	     }

	     public void setjacketPrice(double jacketPrice)
	     {
	        jacketPrice = 59.95 ;
	     }

	     public String get designerJeans(get designerJeans)
	     {
	        return description;
	     }

	     public int getUnitsOnHand()
	     {
	        return unitsOnHand;
	     }

	     public double getPrice()
	     {
	        return price;
	     }

	}





This post has been edited by macosxnerd101: 23 September 2011 - 09:18 AM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: What are mutator methods and how do i incorporate them in a java code?

Posted 23 September 2011 - 09:19 AM

You might want to check out my tutorial on class design as well, Moving Away From Parallel Arrays.
Was This Post Helpful? 0
  • +
  • -

#7 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: What are mutator methods and how do i incorporate them in a java code?

Posted 23 September 2011 - 09:21 AM

You need to have variables for the methods.
public void setjacketunitsOnHand(int jacketunits)
         {
            jacketsunitsOnHand = jUnits;
         }


jacketsunitsOnHand isn't a variable that you have created in the class. It will give you a "Cannot find Symbol" error. You need to create each of this variable at class level.
Was This Post Helpful? 0
  • +
  • -

#8 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Re: What are mutator methods and how do i incorporate them in a java code?

Posted 23 September 2011 - 09:25 AM

well what do you mean by creating each variable at class level? i do not understand it if you can show me an example it would be helpful
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: What are mutator methods and how do i incorporate them in a java code?

Posted 23 September 2011 - 09:26 AM

Check out my tutorial that I linked to in my last post. I think it will clarify a lot of these concepts. :)
Was This Post Helpful? 0
  • +
  • -

#10 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: What are mutator methods and how do i incorporate them in a java code?

Posted 23 September 2011 - 09:39 AM

public class DTRetailItem
{
int unitsOnHand; // this is a class level variable because it is not in a method, therefore can be accessed in any method
  public void getUnitsOnhand()
  {
     int unitsOnHand; // This is a method level, only created in the method and is erased after method is done.
  }
}

That is the difference, Class level can be used by any method, method level can be used only inside the method.
Allow me to point out some errors:

1.
public void setJacket(set Jacket)
         {
            Jacket =j;
         }

You do not have a jacket variable, need to make one class level. Also (set jacket) is not a proper parameter. A proper parameter has a type and a name. ie. String jacketName

2.
         public void setjacketunitsOnHand(int jacketunits)
         {
            jacketsunitsOnHand = jUnits;
         }

You have created a good method parameter here howerver a problem. variable jacketunitsOnhand doesn't exist, you need to make one class level. Also, jUnits doesn't exist, you need to use the variable in the parameter there, so instead of jUnit you need to make it = jacketunits

3.
         public void setjacketPrice(double jacketPrice)
         {
            jacketPrice = 59.95 ;
         }

You cannot use a hard coded number like that. You need to make the jacketprice = the parameter. Also, you need to create a jacketPrice variable at class level.

4.
         public String get designerJeans(get designerJeans)
         {
            return description;
         }

When creating a method name, you cannot have a space in it, so it needs to be getDesignerjeans. Also, as I said earlier for parameters need a type and a name, get is not a type it would need to be String. However; you actually don't need any parameters for this method since you are just returning the description.

5.
         public int getUnitsOnHand()
         {
            return unitsOnHand;
         }

You need to create a unitsOnhand variable at class level.

6.
         public double getPrice()
         {
            return price;
         }

You need to create a price variable at class level.

Does this all make sense?
Was This Post Helpful? 1
  • +
  • -

#11 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Re: What are mutator methods and how do i incorporate them in a java code?

Posted 23 September 2011 - 10:01 AM

this is the real problem i am working on
Write a class named RetailItem that holds data about an item in a retail store. The class should have the following fields:
description- The description field is a string object that holds a brief description of the item.

unitsOnHand- The unitsOnHand field is an int variable that holds the number of units currently in inventory.

price- The price field is a double that holds the item's retail price.

Write appropriate mutator methods that store values in these fields and accessor methods that written the values in these fields.Once you have written the class, write a separate program that creates three RetailItem objects and stores the following data in them.
Description UnitsonHand price
item#1 Jacket 12 59.95
item#2 Designer Jeans 40 34.95
item#3 Shirt 20 24.95

so am i on the right path with my code? i am really lost and just need some help
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: What are mutator methods and how do i incorporate them in a java code?

Posted 23 September 2011 - 10:04 AM

Not trying to be rude, but have you even looked at my tutorial? I've pointed it out multiple times. It covers class design very well. I highly recommend checking it out.
Was This Post Helpful? 0
  • +
  • -

#13 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Re: What are mutator methods and how do i incorporate them in a java code?

Posted 23 September 2011 - 10:08 AM

how do i access to your tutorial? and i am sorry that i did not check it out yet.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: What are mutator methods and how do i incorporate them in a java code?

Posted 23 September 2011 - 10:08 AM

View Postmacosxnerd101, on 23 September 2011 - 12:19 PM, said:

You might want to check out my tutorial on class design as well, Moving Away From Parallel Arrays.

Click the link. :)
Was This Post Helpful? 0
  • +
  • -

#15 CodeMasterNinja  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 93
  • Joined: 22-September 11

Re: What are mutator methods and how do i incorporate them in a java code?

Posted 23 September 2011 - 10:11 AM

Okay i checked it out and i understood why you should move away from using arrays but i still do not get why do you have to create a different class for each item and thing
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2