# Problem assigning the character 'f' or 'F' for female

Page 1 of 1

## 13 Replies - 839 Views - Last Post: 01 July 2011 - 09:49 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=237420&amp;s=2e5743140e4d0ae6a027cd79aea74148&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 lizg9

Reputation: 0
• Posts: 11
• Joined: 27-June 11

# Problem assigning the character 'f' or 'F' for female

Posted 28 June 2011 - 06:07 PM

ok so now Im doing the ObesityStats class and this is part of my instructions...

***An index is 27.8 or greater for men or 27.3 or greater for women is considered obese. Write the weightResults method that prints the message "obese" or "not obese". The character gender will contain either an 'f' or 'F' for female or a 'm' or 'M' for male. You may call the function getMassIndex and assume it works as intended. ***

i know my getMassIndex() is doing the calculations right, but I am very confused when I am trying to assign the character 'f' or 'F' for female, does it go insite setmyGender() ? or getMassIndex()? and if yall dont mind double checking my weightResults() just to make sure im on the right track!

``` public void weightResults() {
if ((bodyMass > MALELIMIT) || (bodyMass > FEMALELIMIT)){
System.out.println("Obese");
}else{
System.out.println("Not Obese");
}
}
public double getMassIndex() {
bodyMass = ((double)myWeight / (double)(Math.pow(myHeight, 2)));
return bodyMass;
}

//accessor
public char getMyGender() {
return myGender;
}

// modifier
public void setMyGender(char myGender) {
boolean Male = true;
boolean Female = true;

if (Male) {
myGender = 'm';
myGender = 'M';
} else if (Female) {
myGender = 'f';
myGender = 'F';
}
// this.myGender = myGender;
}

```

Is This A Good Question/Topic? 0

## Replies To: Problem assigning the character 'f' or 'F' for female

### #2 Elliotd123

Reputation: 12
• Posts: 37
• Joined: 24-January 11

## Re: Problem assigning the character 'f' or 'F' for female

Posted 28 June 2011 - 06:12 PM

Where is your declaration for myGender? It looks like your set accessor is always going to make myGender 'M', since you set male to be true right away in the setMyGender accessor.

Also if you are trying to change the variable myGender, it's a terrible idea to declare myGender as a parameter for your set accessor. You end up masking the original myGender variable and cause confusion.

This post has been edited by Elliotd123: 28 June 2011 - 06:14 PM

### #3 lizg9

Reputation: 0
• Posts: 11
• Joined: 27-June 11

## Re: Problem assigning the character 'f' or 'F' for female

Posted 28 June 2011 - 06:14 PM

```// class constants
public static final double MALELIMIT = 27.8;
public static final double FEMALELIMIT = 27.3;

// fields
private char myGender;
private double myWeight; // in kilograms
private double myHeight; // in meters

private double bodyMass;

// constructor
public ObesityStats(char myGender, double myWeight, double myHeight) {
super();
this.myGender = myGender;
this.myWeight = myWeight;
this.myHeight = myHeight;
}

```

### #4 CasiOo

• D.I.C Lover

Reputation: 1575
• Posts: 3,542
• Joined: 05-April 11

## Re: Problem assigning the character 'f' or 'F' for female

Posted 28 June 2011 - 06:16 PM

This can't be right.
```19	 public void setMyGender(char myGender) {
20	  boolean Male = true;
21	  boolean Female = true;
22
23	  if (Male) {
24	   myGender = 'm';
25	   myGender = 'M';
26	  } else if (Female) {
27	   myGender = 'f';
28	   myGender = 'F';
29	  }
30	  // this.myGender = myGender;
31	 }
```

First you are making two local variables which you use for nothing. Then you first set myGender to 'm', but change it to 'M' right after???? Also it should be this.myGender you should be changing.

First you have to make sure the character is either m or f, and then you will have to make it uppercase (You can find this static method in the java API under Character).

### #5 Elliotd123

Reputation: 12
• Posts: 37
• Joined: 24-January 11

## Re: Problem assigning the character 'f' or 'F' for female

Posted 28 June 2011 - 06:23 PM

there's a couple ways to tackle it, and Java has a built-in very simple way to use accessors, so you could look into Java style get and set accessors....

But then again, do you NEED an accessor? It looks like you set the gender when you call the constructor (It is passed as an arguement).

Also your weightResults is going to have to check if it's male or female, otherwise it won't work right

### #6 lizg9

Reputation: 0
• Posts: 11
• Joined: 27-June 11

## Re: Problem assigning the character 'f' or 'F' for female

Posted 28 June 2011 - 06:30 PM

so something similar to this?

``` public void setMyGender(char myGender) {
if(/*female*/)
this.myGender = 'f';
myGender.isUpperCase(myGender.toUpperCase(f));
else
this.myGender = 'm';
myGender.isUpperCase(myGender.toUpperCase(m));
}
```

### #7 Elliotd123

Reputation: 12
• Posts: 37
• Joined: 24-January 11

## Re: Problem assigning the character 'f' or 'F' for female

Posted 28 June 2011 - 06:33 PM

### #8 lizg9

Reputation: 0
• Posts: 11
• Joined: 27-June 11

## Re: Problem assigning the character 'f' or 'F' for female

Posted 28 June 2011 - 06:35 PM

```public static void main(String[] args) {
// A male that weighs 81.8 kilos and is 1.7 meters tall
ObesityStats male = new ObesityStats('m', 81.8, 1.7);
System.out.println(male.toString());
System.out.println("Body Mass: " +male.getMassIndex());

// A Female that weighs 59 kilos and is 1.57 meters tall.
ObesityStats fem = new ObesityStats('f', 59, 1.57);

}
```

This post has been edited by lizg9: 28 June 2011 - 06:37 PM

### #9 immeraufdemhund

• D.I.C Regular

Reputation: 79
• Posts: 495
• Joined: 29-March 10

## Re: Problem assigning the character 'f' or 'F' for female

Posted 28 June 2011 - 06:39 PM

I wouldn't even test if it's upper case. Just make it uppercase always. It's smaller quicker and more to the point.

```public void setMyGener(char Gender){
myGender = Character.toUpperCase(Gender);
}

```

Also you should make your code to have names that mean something. I can't tell if you are testing for male or female or setting the value. I would assume you are trying to set the gender because of the name of the function, but in the body you are testing the gender. If you want it to throw a error because a wrong type of gender was entered that's fine...but your nameing convention in this case needs a little fixing.

### #10 Elliotd123

Reputation: 12
• Posts: 37
• Joined: 24-January 11

## Re: Problem assigning the character 'f' or 'F' for female

Posted 28 June 2011 - 06:40 PM

It looks like you're not going to need ANY of the public void setGender method, unless you need it for some requirement, but it won't even get called.

Your problem is almost certainly in the weightResults method. You need to do something more like

```if (myGender == 'm' || myGender == 'M')
{
if (bodyMass > MALELIMIT)
system.out.println("obese");
else
system.out.println("not obese");
}

```

And then a similar setup for females.

### #11 lizg9

Reputation: 0
• Posts: 11
• Joined: 27-June 11

## Re: Problem assigning the character 'f' or 'F' for female

Posted 28 June 2011 - 06:50 PM

so then the weightResults() method would need to look like this ?

```public void weightResults() {
if (myGender == 'm' || myGender == 'M') {
if (bodyMass > MALELIMIT)
System.out.println("Obese");
else
System.out.println("Not Obese");
}
if (myGender == 'f' || myGender == 'F') {
if (bodyMass > FEMALELIMIT)
System.out.println("Obese");
else
System.out.println("Not Obese");
}
}
```

This post has been edited by lizg9: 28 June 2011 - 06:51 PM

### #12 Elliotd123

Reputation: 12
• Posts: 37
• Joined: 24-January 11

## Re: Problem assigning the character 'f' or 'F' for female

Posted 28 June 2011 - 06:52 PM

### #13 lizg9

Reputation: 0
• Posts: 11
• Joined: 27-June 11

## Re: Problem assigning the character 'f' or 'F' for female

Posted 28 June 2011 - 06:54 PM

cool thank you!

### #14 jackbkjack

Reputation: 0
• Posts: 5
• Joined: 01-July 11

## Re: Problem assigning the character 'f' or 'F' for female

Posted 01 July 2011 - 09:49 AM

Put a simple condition before storing data that if selection is Male then set 'm' or 'M' and if Female then set 'f' or 'F'.