Jump to content

Restricted constructor

- - - - -

  • Please log in to reply
6 replies to this topic

#1
ch3etah

ch3etah

    Newbie

  • Members
  • Pip
  • 8 posts
Hey guys.
I'm trying to make a constructor that has a name and a gender for a Person class (basic stuff). What I don't know how to do is to limit the options to the gender to the obvious two ones.
private String name;

private String gender;

public Person(String name, String gender){[INDENT]this.name = name;

this.gender = gender;[/INDENT]}

In Swing I can do it by using a JComboBox by supplying only the two variables to the option list. How can I do it only for text?

Edited by ch3etah, 01 June 2011 - 02:26 AM.


#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Don't use a String type for the gender. Instead, use an enumerated data type. In your Person class, create a public enum type for gender:

public enum Gender {

    MALE, FEMALE

}

When you call your constructor, you would use the words inside the definition of the Gender type without quotes.

That's just one way of doing it. There are others. :)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
ch3etah

ch3etah

    Newbie

  • Members
  • Pip
  • 8 posts
Hey,
I modified the code as follows:

private enum Gender{MALE, FEMALE}

private final Gender gender;


public Person(String name, Gender gender){

[INDENT]this.name = name;

this.gender = gender;

}

[/INDENT]


// now create a person

Person me = new Person("me", MALE);


it doesn't work. MALE cannot be resolved to a variable

#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Since you declared the enum type private, then it is inaccessible outside your class.

It's a good idea to declare enumerated types as public if you need to use those types from outside the class (such as declaring a new instance of the class).
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5
ch3etah

ch3etah

    Newbie

  • Members
  • Pip
  • 8 posts
I think I'm making a constant mistake here, but I made them both "public" an still the same error is there (the code is exactly as above)

What any other ways are there to do this. I'm trying to avoid a method like:

private String getGender(String gender){

    if(gender.toLowerCase().equals("male")){

            gender = "Male";

        }

        if(gender.toLowerCase().equals("female")){

            gender = "Female";

        }

        else{

        //print an error or throw an exception

        }

} 


I'm looking for a more elegant way in doing it.

#6
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
How are you instantiating your Person class? If you're trying to reference your enum type from outside the class, be sure you indicate the class name and enum type:

Person p = new Person("foo", Person.Gender.MALE);


Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#7
ch3etah

ch3etah

    Newbie

  • Members
  • Pip
  • 8 posts
It works like that. I didn't know I have to specify the class when using it.
Many thanks




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users