Saran Vadlamudi Saran Vadlamudi - 1 month ago 9
Java Question

Java-How to use multiple classes for a program

I am sorry if this too long but I really need help. I made a heart rate program that displays the name of the person, date of birth, age, heart rate and the rang of the target heart rate. The problem is I am not getting the desired result in the main class

The expected outcome should be:

Enter your first name: Saran

Enter your last name: Vadlamudi

Enter your date of birth in YYYYMMDD format: 1996

08

26

Name : Saran Vadlamudi

Date of birth: 1996-8-26

Age: 20

Heart rate is 200

Target heart rate range in between 100 and 170

Instead I am getting this:

Enter your first name: Saran

Enter your last name: Vadlamudi

Enter your date of birth in YYYYMMDD format: 1996

08

26

Name : Saran Vadlamudi

Date of birth: 1996-8-26

Age: 20

Heart rate is -1796

Target heart rate range in between -898 and -1526

Here are my classes:

Test.java

public class Test {

public static void main(String[] args) {
HeartRates hR = new HeartRates();
HRCalc hrc = new HRCalc();

hR.askUser();

System.out.println("Name : " + hR.getFirstName() + " " + hR.getLastName());
System.out.println("Date of birth: " + hR.getYear() + "-" + hR.getMonth() + "-" + hR.getDay());
System.out.println("Age: " + hR.calculateAge());
System.out.println("Heart rate is " + hrc.calculateMaximumHeartRate());
System.out.println("Target heart rate range in between " + hrc.calculateTargetHeartRate1() + " and " + hrc.calculateTargetHeartRate2());
}

}


HeartRates.java

import java.time.LocalDate;
import java.util.Scanner;

public class HeartRates {
private String fName;//Persons first name
private String lName;//Persons last name
private int year, month, day; //Person's birth date format: YYYY/MM/DD

public HeartRates(String fName,String lName, int year, int month, int day){

this.fName = fName;
this.lName = lName;
this.year = year;
this.month = month;
this.day = day;

}

public HeartRates(){}

public void setFirstName(String fName){
this.fName = fName;
}

public String getFirstName(){
return fName;
}

public void setLastName(String lName){
this.lName = lName;
}

public String getLastName(){
return lName;
}

public void setYear(int year){
this.year = year;
}

public int getYear(){
return year;
}

public void setMonth(int month){
this.month = month;
}

public int getMonth(){
return month;
}

public void setDay(int day){
this.day = day;
}

public int getDay(){
return day;
}

public int calculateAge(){
LocalDate date = LocalDate.now();
return date.getYear() - getYear();
}


public void askUser(){

Scanner input = new Scanner(System.in);

System.out.print("Enter your first name: ");
String fname = input.nextLine();
setFirstName(fname);

System.out.print("Enter your last name: ");
String lname = input.nextLine();
setLastName(lname);

System.out.print("Enter your date of birth in YYYYMMDD format: ");
int year = input.nextInt();
setYear(year);
int month = input.nextInt();
setMonth(month);
int day = input.nextInt();
setDay(day);
}
}


HRCalc.java

public class HRCalc {

public int calculateMaximumHeartRate(){
HeartRates hR = new HeartRates();
return (220 - hR.calculateAge());
}

public int calculateTargetHeartRate1(){
HeartRates hR = new HeartRates();
return (int) (0.5 * (220 - hR.calculateAge()));
}

public int calculateTargetHeartRate2(){
HeartRates hR = new HeartRates();
return (int) (0.85 * (220 - hR.calculateAge()));
}
}

Answer

Like Unamaniac says, you need to pass a HeartRates object to the HRCalc class. Something like

public class HRCalc {
    private HeartRates hR;

    public HRCalc(HeartRates hR){
        this.hR = hR;
    }

    public int calculateMaximumHeartRate(){
         return (220 - hR.calculateAge());
    }

    public int calculateTargetHeartRate1(){
         return (int) (0.5 * (220 - hR.calculateAge()));
    }

    public int calculateTargetHeartRate2(){
         return (int) (0.85 * (220 - hR.calculateAge()));
    }
}

Then, you create eh HRCalc like:

HRCalc hrc = new HRCalc(hr)