7

I am working on a homework assignment. I am confused on how it should be done.

The question is:

Create a class called IDCard that contains a person's name, ID number, and the name of a file containing the person's photogrpah. Write accessor and mutator methods for each of these fields. Add the following two overloaded constructors to the class:

public IDCard() public IDCard(String n, int ID, String filename)

Test your program by creating different ojbects using these two constructors and printing out their values on the console using the accessor and mutator methods.

I have re-written this so far:

public class IDCard {
String Name, FileName;
int ID;

public static void main(String[] args) {

}

public IDCard()
{
    this.Name = getName();
    this.FileName = getFileName();
    this.ID = getID();
}

public IDCard(String n, int ID, String filename)
{

}

public String getName()
{
    return "Jack Smith";
}

public String getFileName()
{
    return "Jack.jpg";
}

public int getID()
{

        return 555;
    }
}
Pichu
  • 265
  • 2
  • 4
  • 9
  • 1
    What are you blocked on? – smk Mar 29 '13 at 21:10
  • 2
    An accessor is not supposed to be static. – sp00m Mar 29 '13 at 21:14
  • You have tens of tutorials about encapsulation. Also, respect naming conventions. – Branislav Lazic Mar 29 '13 at 21:14
  • Well, it states I need to create different objects and use the two constructors; then print out the values using the accessor and mutator methods. – Pichu Mar 29 '13 at 21:15
  • So what is the problem? – Branislav Lazic Mar 29 '13 at 21:16
  • The problem is I am confused on how I should apply the accessor and mutator methods with objects to create an "ID" of each person. – Pichu Mar 29 '13 at 21:18
  • 1
    Remove the static keyword from your methods. Also, you may be surprised to find that you have no constructors defined because you put a `static void` in front of each of them, effectively making them poorly named global methods. Use standard Java naming conventions - you will write more maintainable and easy to understand code in the future, by doing so. – Perception Mar 29 '13 at 21:19
  • 2
    All of your static's are wrong and evil the only static required is for the main method. You need to read and understand the difference between static and instance, as you are showing a very clear misunderstanding of static. It's not really a magic keyword that makes everything better the more you use it, it actually causes more problems the more it is used. – crowne Mar 29 '13 at 21:22
  • @Perception Can you show me an example of how I should go about this? – Pichu Mar 29 '13 at 21:24
  • @Sublimity - see the answer below as a starting point. – Perception Mar 29 '13 at 21:32
  • I see no question here. I do see a homework dump. – Raedwald Jan 30 '16 at 01:18

2 Answers2

44

Let's go over the basics: "Accessor" and "Mutator" are just fancy names fot a getter and a setter. A getter, "Accessor", returns a class's variable or its value. A setter, "Mutator", sets a class variable pointer or its value.

So first you need to set up a class with some variables to get/set:

public class IDCard
{
    private String mName;
    private String mFileName;
    private int mID;

}

But oh no! If you instantiate this class the default values for these variables will be meaningless. B.T.W. "instantiate" is a fancy word for doing:

IDCard test = new IDCard();

So - let's set up a default constructor, this is the method being called when you "instantiate" a class.

public IDCard()
{
    mName = "";
    mFileName = "";
    mID = -1;
}

But what if we do know the values we wanna give our variables? So let's make another constructor, one that takes parameters:

public IDCard(String name, int ID, String filename)
{
    mName = name;
    mID = ID;
    mFileName = filename;
}

Wow - this is nice. But stupid. Because we have no way of accessing (=reading) the values of our variables. So let's add a getter, and while we're at it, add a setter as well:

public String getName()
{
    return mName;
}

public void setName( String name )
{
    mName = name;
}

Nice. Now we can access mName. Add the rest of the accessors and mutators and you're now a certified Java newbie. Good luck.

Vaiden
  • 14,116
  • 6
  • 55
  • 86
  • I was able to eventually figure it out. I'm familiar with C#, I've made some pretty complex programs but now I'm learning Java and what was getting me was I kept getting errors after errors. I started using Eclipse and it kept telling me: use this.. no use this.. no use this and I guess it slaughtered my code into what you guys saw. Thank you. – Pichu Mar 29 '13 at 23:30
4

You need to remove the static from your accessor methods - these methods need to be instance methods and access the instance variables

public class IDCard {
    public String name, fileName;
    public int id;

    public IDCard(final String name, final String fileName, final int id) {
        this.name = name;
        this.fileName = fileName
        this.id = id;
    }

    public String getName() {
        return name;
    }
}

You can the create an IDCard and use the accessor like this:

final IDCard card = new IDCard();
card.getName();

Each time you call new a new instance of the IDCard will be created and it will have it's own copies of the 3 variables.

If you use the static keyword then those variables are common across every instance of IDCard.

A couple of things to bear in mind:

  1. don't add useless comments - they add code clutter and nothing else.
  2. conform to naming conventions, use lower case of variable names - name not Name.
Boris the Spider
  • 54,398
  • 6
  • 98
  • 152
  • I have to add the comments as part of the submission requirement. I have revised it and updated the main post with what I have so far. – Pichu Mar 29 '13 at 21:31
  • Boris, is it a good practice to add accessors in the constructor? – Vasile Surdu May 16 '14 at 15:51
  • 1
    @VasileSurdu no. Unless your accessors are `final` then you are calling methods that can be overridden. In this case the constructor of the superclass would be calling methods from the subclass before it has been fully initialised. This is a very insidious bug. – Boris the Spider May 16 '14 at 23:28