13 Replies - 510 Views - Last Post: 01 July 2011 - 09:49 AM Rate Topic: -----

#1 lizg9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 lizg9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
 }


Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1378
  • View blog
  • Posts: 3,038
  • 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).
Was This Post Helpful? 1
  • +
  • -

#5 Elliotd123  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • 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
Was This Post Helpful? 1
  • +
  • -

#6 lizg9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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));
 }

Was This Post Helpful? 0
  • +
  • -

#7 Elliotd123  Icon User is offline

  • New D.I.C Head

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

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

Posted 28 June 2011 - 06:33 PM

Can you show me your main method please?
Was This Post Helpful? 0
  • +
  • -

#8 lizg9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#9 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#10 Elliotd123  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#11 lizg9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#12 Elliotd123  Icon User is offline

  • New D.I.C Head

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

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

Posted 28 June 2011 - 06:52 PM

That looks about right, yes
Was This Post Helpful? 1
  • +
  • -

#13 lizg9  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#14 jackbkjack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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'.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1