6

I'm creating an Employee class containing three fields: name, age, and gender. I need to create a gender field but the user can only choose male or female. I guess I have to do this in a boolean form but I don't know how I'm going to do that. Can anyone help me out?

So far I have:

public class Employee {

private String name;
private int age;
private boolean gender;
private boolean male;
private boolean female;

public Employee(String name, int age, boolean gender) 
{
    this.name = name;
    this.age = age;

    boolean f = female;
    boolean m = male;

    if (gender = f)
    {
        System.out.print("female");
    }

    else if (gender = m)
    {
        System.out.print("male");
    }
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public boolean isGender() {
    return gender;
}

public void setGender(boolean gender) 
{
    if (gender = f)
    {
        System.out.print("female");
    }

    else if (gender = m)
    {
        System.out.print("male");
    }
}
}
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259

11 Answers11

23

Better to use an enum for Gender:

public enum Gender {
    MALE, FEMALE
}
Reimeus
  • 158,255
  • 15
  • 216
  • 276
11

Specifying the gender as an enum, has its limitations: Only 65535 attributes (and methods) can be set in a single java class, which in todays diverse world is unaccaptable.

I suggest using BigInteger, which has its maximum value as high as (2 ^ 32) ^ 2147483647.

Gábor DANI
  • 2,063
  • 2
  • 22
  • 40
4

You can use a boolean or else you can create a Gender enum:

In Gender.java:

public enum Gender {
    MALE,
    FEMALE
}

In Employee.java:

public class Employee {

    private String name;
    private int age;
    private Gender gender;

    public Employee(String name, int age, Gender gender) 
    {
        this.name = name;
        this.age = age;
        this.gender = gender;
        printGender();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) 
    {
        this.gender = gender;
    }

    public final void printGender() {
        System.out.println(gender.name().toLower());
    }    
}
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
3

If you are going to use a boolean, it should be called isMale or isFemale, and not isGender. Note that whichever you pick, someone will probably complain that their gender is regarded as false. This is a reason why an enum might be a better option.

Another reason a boolean might be inappropriate is that in some countries it is illegal in some situations to require people to disclose their gender. For this reason you may wish to provide an enum with three options: the third is to store that the gender was not disclosed.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
3

A boolean is not appropriate, and neither is a default value unless it is UNKNOWN. You need at least three values, maybe four. See this answer for a full discussion.

Community
  • 1
  • 1
user207421
  • 305,947
  • 44
  • 307
  • 483
3

Since Java 1.5, the language supports enumeration types (enum), which are perfect for this kind of problems.

Simple Gender enum:

public enum Gender {
    M, F, UNKNOWN
}

Sophisticated Gender enum:

/**
 * Enumeration for reflecting user's gender.
 * @author naXa!
 */
public enum Gender {
    /** Male. */
    M("Male"),
    /** Female. */
    F("Female"),
    /** Gender is not known, or not specified. */
    UNKNOWN("Unknown");

    private final String name;

    private Gender(String name) {
        this.name = name;
    }

    /**
     * @return The string representation of this element in the enumeration.
     */
    public String getName() {
        return this.name;
    }
}

Usage example:

Employee employee = new Employee("John Doe", 23, Gender.M);

Notice also that instances of an enum are constants and should follow the naming conventions for constants. I.e. you should capitalize every letter and separate subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
1

If you're using Java 1.5 or later, you can (and should) use enum types, which provide strong typing, convenient/implementation-independent naming, and extensibility.

Example:

public enum Gender {
    MALE,
    FEMALE,
}

// usage example
myEmployee.setGender(Gender.FEMALE);
Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
1

There are a couple things wrong here. First of all, in your if statements, you're assigning the value of f and m to gender with a single = operator. You need to do if (gender == true) or if (gender == false). Also, I recommend having true represent male or female and false represent the other gender.

jrad
  • 3,172
  • 3
  • 22
  • 24
1

boolean approach is useless for one of many reasons:

The distinction between sex and gender differentiates sex (the anatomy of an individual's reproductive system, and secondary sex characteristics) from gender (social roles based on the sex of the person, usually culturally learned), or personal identification of one's own gender based on an internal awareness (gender identity. In some circumstances, an individual's assigned sex and gender do not align, and the result is sometimes a transgender person.

ceph3us
  • 7,326
  • 3
  • 36
  • 43
0

Two ideas:

1) Make a boolean like isMale or isFemale

2) Make a Gender Enum that only has Gender.MALE and Gender.FEMALE

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Dennis Meng
  • 5,109
  • 14
  • 33
  • 36
0

There is a few things wrong with the code in the question.

First.

A constructor is supposed to set the values of the variables of the Employee class. The male and female boolean variables must be assigned before being used.

public class Employee {

    // other vars
    private boolean male;
    private boolean female;

    public Employee(/*other vars*/, boolean gender) 
    {
        /*THIS IS WRONG. male and female values not set
          so you cant assign anything to f or m. it will be an error.
        */
        boolean f = female;
        boolean m = male;

Second.

= and == have different meanings. = assigns what is on right to variable on the left. == checks for equality.

//will assign the value of f to gender
if (gender = f){

// will check for equality
if(gender == f){

Third.

The setGender(boolean gender) method does not even set the gender.

//as it should be if you choose to use boolean.
public void setGender(boolean gender) 
{
    this.gender = gender;
}

Fourth.

Using multiple Boolean variables to check for the same thing is creating more opportunity's for introducing errors into your code. For example Every time male is set to true female has to be set to false and vice versa.

To answer the question.

You could use one boolean instead of the 3 used in the code in the question. Either isMale of isFemale pick one you don't need both. if isMale is false the emplyee is female.

I would use an Enum. U can Read about Enums here or Ted Hopp's answer

dwalsh84
  • 61
  • 4