Hi, I need help , the assignment I have to do is for a healthprofile class but I am having issues . It doesn ' t show the month , day , or year in mm / dd / yyyy format it only shows 0 / 0 / 0 . It doesn ' t calculate age it still says 2013 and it doesn ' t show the maximum heart rate or targeted heart rate . Can someone please help ?

Here is the code :

mport java . util .*;
public class HealthProfile {
String firstName ;
String lastName ;
char gender ;
int BirthMonth ;
int BirthDay ;
int BirthYear ;
int height ;
int weight ;
public HealthProfile ( String fName , String lName , char Genderr , int birthMonth , int birthDay , int birthYear , int heightt , int weightt ){
firstName = fName ;
lastName = lName ;
gender = Genderr ;
BirthMonth = birthMonth ;
BirthDay = birthDay ;
BirthYear = birthYear ;
height = heightt ;
weight = weightt ;
}
HealthProfile () {
}
public void setFirstName ( String firstName ) {
this . firstName = firstName ;
}
public String getFirstName () {
return firstName ;
}
public void setLastName ( String lastName ) {
this . lastName = lastName ;
}
public String getLastName () {
return lastName ;
}
public void setGender ( char gender ) {
this . gender = gender ;
}
public char getGender () {
return gender ;
}
public void setBirthMonth ( int BirthMonth ) {
this . BirthMonth = BirthMonth ;
}
public int getBirthMonth () {
return BirthMonth ;
}
public void setBirthDay ( int BirthDay ) {
this . BirthDay = BirthDay ;
}
public int getBirthDay () {
return BirthDay ;
}
public void setBirthYear ( int BirthYear ) {
this . BirthYear = BirthYear ;
}
public int getBirthYear () {
return BirthYear ;
}
public void setHeight ( int height ) {
this . height = height ;
}
public double getHeight () {
return height ;
}
public void setWeight ( int weight ) {
this . weight = weight ;
}
public double getWeight () {
return weight ;
}
public int Age (){
Calendar now = Calendar . getInstance ();
int nowYear = now . get ( Calendar . YEAR );
int nowMonth = now . get ( Calendar . MONTH );
int nowDay = now . get ( Calendar . DATE );
int day = now . get ( Calendar . DATE );
int month = now . get ( Calendar . MONTH );
int year = now . get ( Calendar . YEAR );
if ( nowMonth > BirthMonth );
return ( nowYear - BirthYear );
}
public double getBMI (){
return ( weight * 703 )/( height * height );
}
public int MaxHeartRate (){
return 220 - Age ();
}
public double TargetHeartRate (){
return MaxHeartRate () * 0 . 85 + MaxHeartRate () * 0 . 5 ;
}
}

 Here is the test class :

import java . util . Scanner ;
public class HealthProfileTest {
public static void main ( String [] args ) {
Scanner input = new Scanner ( System . in );
String firstName ;
String lastName ;
String DoB ;
String theMonth ;
String theDay ;
String theYear ;
char gender ;
int Month ;
int Day ;
int Year ;
double height = 0 . 0 ;
double weight ;
HealthProfile personalInfo = new HealthProfile ();
System . out . println (" Enter your first name : ");
personalInfo . setFirstName ( input . nextLine ());
System . out . println (" Enter your last name : ");
personalInfo . setLastName ( input . nextLine ());
System . out . println (" Male or female : ");
personalInfo . setGender ( input . nextLine (). charAt ( 0 ));
System . out . println (" Enter your date of birth in mm / dd / yyyy format : ");
DoB = input . nextLine ();
theMonth = DoB . substring ( 0 , 2 );
theDay = DoB . substring ( 3 , 5 );
theYear = DoB . substring ( 6 , 10 );
Month = Integer . parseInt ( theMonth );
Day = Integer . parseInt ( theDay );
Year = Integer . parseInt ( theYear );
System . out . println (" Enter your height in inches : ");
height = input . nextInt ();
personalInfo . setHeight (( int ) height );
System . out . println (" Enter your weight in pounds : ");
weight = input . nextInt ();
personalInfo . setWeight (( int ) weight );
System . out . println (" Name : " + personalInfo . getFirstName () +" "+ personalInfo . getLastName ());
System . out . println (" Gender : " + personalInfo . getGender ());
System . out . println (" DoB : " + personalInfo . getBirthMonth () + "/" + personalInfo . getBirthDay () + "/" + personalInfo . getBirthYear ());
System . out . println (" Height : " + personalInfo . getHeight ());
System . out . println (" Weight : " + personalInfo . getWeight ());
System . out . println (" Age : " + personalInfo . Age ());
System . out . println (" BMI : " + personalInfo . getBMI ());
System . out . printf (" Max heart rate : ", personalInfo . MaxHeartRate ());
System . out . printf (" Target heart rate : ", personalInfo . TargetHeartRate ());
System . out . println (" ");
System . out . println ( " BMI VALUES " );
System . out . println (" Underweight : Under 18 . 5 ");
System . out . println (" Normal : 18 . 5 - 24 . 9 ");
System . out . println (" Overweight : 25 - 29 . 9 ");
System . out . println (" Obese : 30 or over ");
}
}

So can someone please help ?

Dani AI

Generated

Most likely causes (building on points from , and ): the parsed month/day/year never get saved with the correct variables, the Age logic has a stray semicolon and mishandles Calendar.MONTH (it's zero‑based), the printf calls have no format specifiers so nothing prints, BMI uses integer math, and the "target heart rate" is being computed incorrectly (it should be a 50%–85% range of max, not a sum). Also substring(0,2) is fragile for single-digit months/days.

Corrective examples and checks you can apply immediately.

Fix age calculation (watch the +1 on Calendar.MONTH and do not put a stray semicolon after an if):

public int getAge() {
    Calendar now = Calendar.getInstance();
    int y = now.get(Calendar.YEAR);
    int m = now.get(Calendar.MONTH) + 1;          // Calendar.MONTH is 0-based
    int d = now.get(Calendar.DAY_OF_MONTH);
    int age = y - getBirthYear();
    if (m < getBirthMonth() || (m == getBirthMonth() && d < getBirthDay())) {
        age--;
    }
    return age;
}

Compute BMI with double arithmetic and print heart-rate range with proper formatting:

public double getBMI() {
    return (getWeight() * 703.0) / (getHeight() * getHeight());
}

int max = 220 - getAge();
double tMin = max * 0.50;
double tMax = max * 0.85;
System.out.printf("Max heart rate: %d%n", max);
System.out.printf("Target heart rate: %.0f - %.0f bpm%n", tMin, tMax);

Robust DOB parsing + quick debug checklist: use split rather than fixed substrings, then call the setters immediately with the parsed ints; print the parsed values before printing the HealthProfile fields to confirm they were assigned. Example parsing:

String[] p = dob.split("/");
int mm = Integer.parseInt(p[0].trim());
int dd = Integer.parseInt(p[1].trim());
int yy = Integer.parseInt(p[2].trim());
personalInfo.setBirthMonth(mm);    // be careful with variable names / case
personalInfo.setBirthDay(dd);
personalInfo.setBirthYear(yy);

Final quick checks: fix any typos in imports (no "mport"), ensure you call setters after parsing (watch Month vs month), remove the stray semicolon in your Age method, use format specifiers in printf, and run small tests (print parsed variables, then call getters).

Recommended Answers

All 5 Replies

Lines 118-125 you get the date info, but you don't call your set method to save those values. Without date of birth the age/target heart rate etc won't give correct values.

I added this but still no luck:

personalInfo.setBirthMonth(month);
       personalInfo.setBirthDay(day);
       personalInfo.setBirthYear(year);

"still no luck" may be the least helpful problem desription ever posted on DaniWeb! And where exactly did you add that code?
How about an exact description of how the actual output differs from the expected output?

are you sure that the code you posted is exactly the one your working with ? If thats the case , then i see a lot of spelling mistakes in your code , Starting from the 1st line. Maybe taking care of them will bring you better luck.

Your code is painful to read. Why not break things up in your class in segments like

fetchDOB(/* arguments */);
fetchName(/* arguments */);
.
.

each will have their own code inside them , and makes things easy to follow. It will also be easier for you to debug aswell. It would help out both you and us if you do that first.

try again your substring routine looks like you caught a slash in the process.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.