Welcome to Dream.In.Code
Become a Java Expert!

Join 150,374 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,701 people online right now. Registration is fast and FREE... Join Now!




accessor methods & mutator methods

 
Reply to this topicStart new topic

accessor methods & mutator methods

bigdoggy
30 Jan, 2008 - 08:24 AM
Post #1

D.I.C Head
**

Joined: 31 Oct, 2007
Posts: 115


My Contributions
Hi
I am writimg a portion of code which needs to alter the score message here(this portion of code is one part of code for a game) ie I need a meaningfull message to appear in the score message bar of the game and in the title bar of the window. I am struggling with the idea of creatring a variable for storing the score message and then altering the accessor and mutator methods to use this variable, help is much appreciated!
CODE

public class Game
{
  private String message;

  public String getScoreMessage()
  {
    return "A message";
  } // getScoreMessage

  public void setScoreMessage(String message)
  {
  } // setScoreMessage


  public String getAuthor()
  {
    return "Big Doggy";
  } // getAuthor


User is offlineProfile CardPM
+Quote Post

GWatt
RE: Accessor Methods & Mutator Methods
30 Jan, 2008 - 08:55 AM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
You do know that if you just set the instance variable message equal to the argument, everything that uses message after that will use the new value, right?
CODE
public void setScoreMessage(String message)
{
    this.message = message;
}

User is online!Profile CardPM
+Quote Post

ajwsurfer
RE: Accessor Methods & Mutator Methods
30 Jan, 2008 - 09:06 AM
Post #3

D.I.C Regular
Group Icon

Joined: 24 Oct, 2006
Posts: 298



Thanked: 2 times
Dream Kudos: 50
My Contributions
This is a standard Java theme, that forces the programmer to acknowledge what they are doing to data container classes, and it gives the container class the opportunity to add complexity to the data being stored, while keeping the access methods simple. The methods are almost always the same and these two generic functions are used as such:
CODE
  
  public class Game
  {
    private String scoreMessage;
    public String getScoreMessage()
   {
      return this.scoreMessage;
    }

    public void setScoreMessage(String message)
    {
       this.scoreMessage = message;
     }


  public String getAuthor()
  {
     ...
     Game G = new Game();
     G.setScoreMessage("Big Doggy");
     return G.getStoreMessage();
    }

This has a few problems, like on return the instance of the Game class (G) is removed and is not used again, but it shows how the "getter" and "setter" methods are used in a class.
User is offlineProfile CardPM
+Quote Post

bigdoggy
RE: Accessor Methods & Mutator Methods
30 Jan, 2008 - 09:06 AM
Post #4

D.I.C Head
**

Joined: 31 Oct, 2007
Posts: 115


My Contributions
QUOTE(GWatt @ 30 Jan, 2008 - 09:55 AM) *

You do know that if you just set the instance variable message equal to the argument, everything that uses message after that will use the new value, right?
CODE
public void setScoreMessage(String message)
{
    this.message = message;
}


Ok, so I need to rename the instance variable. I am baffled on the use of mutator methods and accessor methods,how do I need to alter my code to get the result mentioned above?

Thanks
User is offlineProfile CardPM
+Quote Post

bigdoggy
RE: Accessor Methods & Mutator Methods
30 Jan, 2008 - 09:23 AM
Post #5

D.I.C Head
**

Joined: 31 Oct, 2007
Posts: 115


My Contributions
QUOTE(ajwsurfer @ 30 Jan, 2008 - 10:06 AM) *

This is a standard Java theme, that forces the programmer to acknowledge what they are doing to data container classes, and it gives the container class the opportunity to add complexity to the data being stored, while keeping the access methods simple. The methods are almost always the same and these two generic functions are used as such:
CODE
  
  public class Game
  {
    private String scoreMessage;
    public String getScoreMessage()
   {
      return this.scoreMessage;
    }

    public void setScoreMessage(String message)
    {
       this.scoreMessage = message;
     }


  public String getAuthor()
  {
     ...
     Game G = new Game();
     G.setScoreMessage("Big Doggy");
     return G.getStoreMessage();
    }

This has a few problems, like on return the instance of the Game class (G) is removed and is not used again, but it shows how the "getter" and "setter" methods are used in a class.

Thanks for your reply, but what is a 'data container class' and what is the notation 'this.' I have not seen it before. When I compile the code error messages appear saying there is no constructor for Game, Why do I need a constructor here?
User is offlineProfile CardPM
+Quote Post

GWatt
RE: Accessor Methods & Mutator Methods
30 Jan, 2008 - 09:47 AM
Post #6

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
Well, every class should have a default constructor that accepts no arguments, so I'm not sure why you get that error. However, I also don't understand why the score message equals "Big Doggy"
I don't think that's a very quantitative score.

Anyway. . .
to define a constructor, you create a method that has the same name as the class.
CODE
public class Game
{
    public Game()    //no return type
    {
    }
}


That's a basic constructor that does nothing.

To create a constructor that actually does something:
CODE
public class Game
{
    private String message;

    public Game(String message)
    {
        this.message = message;
    }
}

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:51PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month