Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Basic Java Program Help (Target-Heart-Rate Calculator)

Options
  • 29-05-2013 12:49am
    #1
    Registered Users Posts: 476 ✭✭


    sorry to intrude on this thread but i dont know where else to ask for help :) could someone please help me with this problem, (Target-Heart-Rate Calculator) While exercising, you can use a heart-rate monitor to see
    that your heart rate stays within a safe range suggested by your trainers and doctors. According to the
    American Heart Association (AHA) (www.americanheart.org/presenter.jhtml?identifier=4736),
    the formula for calculating your maximum heart rate in beats per minute is 220 minus your age in
    years. Your target heart rate is a range that’s 50–85% of your maximum heart rate. [Note: These formulas
    are estimates provided by the AHA. Maximum and target heart rates may vary based on the
    health, fitness and gender of the individual. Always consult a physician or qualified health care professional
    before beginning or modifying an exercise program.] Create a class called HeartRates. The
    class attributes should include the person’s first name, last name and date of birth (consisting of separate
    attributes for the month, day and year of birth). Your class should have a constructor that receives
    this data as parameters. For each attribute provide set and get methods. The class also should
    include a method that calculates and returns the person’s age (in years), a method that calculates and
    returns the person’s maximum heart rate and a method that calculates and returns the person’s target
    heart rate. Write a Java application that prompts for the person’s information, instantiates an object
    of class HeartRates and prints the information from that object—including the person’s first name,
    last name and date of birth—then calculates and prints the person’s age in (years), maximum heart
    rate and target-heart-rate range.
    im sorry for this, i have all the code working but the target heart rate as it cannot return a double for me it is round it thus e.g im 16, maximum HR is 204, it should go, 204/100*50, but when 204/100 = 2 not 2.04.
    Tagged:


«1

Comments

  • Registered Users Posts: 476 ✭✭RoRo979


    sorry to intrude on this thread but i dont know where else to ask for help smile.png could someone please help me with this problem, (Target-Heart-Rate Calculator) While exercising, you can use a heart-rate monitor to see
    that your heart rate stays within a safe range suggested by your trainers and doctors. According to the
    American Heart Association (AHA) (http://www.americanheart.org/present...dentifier=4736),
    the formula for calculating your maximum heart rate in beats per minute is 220 minus your age in
    years. Your target heart rate is a range that’s 50–85% of your maximum heart rate. [Note: These formulas
    are estimates provided by the AHA. Maximum and target heart rates may vary based on the
    health, fitness and gender of the individual. Always consult a physician or qualified health care professional
    before beginning or modifying an exercise program.] Create a class called HeartRates. The
    class attributes should include the person’s first name, last name and date of birth (consisting of separate
    attributes for the month, day and year of birth). Your class should have a constructor that receives
    this data as parameters. For each attribute provide set and get methods. The class also should
    include a method that calculates and returns the person’s age (in years), a method that calculates and
    returns the person’s maximum heart rate and a method that calculates and returns the person’s target
    heart rate. Write a Java application that prompts for the person’s information, instantiates an object
    of class HeartRates and prints the information from that object—including the person’s first name,
    last name and date of birth—then calculates and prints the person’s age in (years), maximum heart
    rate and target-heart-rate range.
    im sorry for this, i have all the code working but the target heart rate as it cannot return a double for me it is round it thus e.g im 16, maximum HR is 204, it should go, 204/100*50, but when 204/100 = 2 not 2.04.


  • Registered Users Posts: 476 ✭✭RoRo979


    import java.util.Scanner;
    public class test {
    Scanner ronan = new Scanner(System.in);
    private int day;
    private int month;
    private int year;
    private String first;
    private String last;
    private int maximum;
    private int number;
    private int higher;
    private int lower;
    private int total;
    private int age;

    public test(String first, String last, int day, int month, int year){
    this.day = day;
    this.month = month;
    this.year= year;
    this.first=first;
    this.last=last;
    }
    public void setDate(int day, int month, int year){
    this.day = day;
    this.month = month;
    this.year= year;
    }
    public String getName(){
    return first;
    }
    public String getName2(){
    return last;
    }
    public int getDate(){
    return day;
    }
    public int getMonth(){
    return month;
    }
    public int getYear(){
    return year;
    }
    public int age(){
    age = 2013-year;
    return age;
    }
    public int Maximum(){
    number = 2013 - year;
    total= 220 - number;
    this.total= total;
    return total;
    }
    public int HRH(){
    total=220-age;
    higher = total/100*50;
    return higher;
    }
    public int HRL(){
    total=220-age;
    lower = total/100*85;
    return lower;
    }

    }

    Thats what i got


  • Registered Users Posts: 3,532 ✭✭✭Unregistered.


    You need to cast the division part (eg 204/100) to an integer.


  • Moderators, Education Moderators, Technology & Internet Moderators Posts: 2,588 Mod ✭✭✭✭KonFusion


    You need to put some line breaks into that wall of text.

    Helping you is a lot easier when people's eyes aren't bleeding :pac:


  • Registered Users Posts: 476 ✭✭RoRo979


    You need to cast the division part (eg 204/100) to an integer.

    i dont think that would be the solution as i need the division anwer to be a double. e.g for the 85% part and im 16, it would divide 204 by 100, giving me 2.04, i need to then multiply this 2.04 by 85 which i would get 173.4. but the when i use the return on it it doesnt allow me, if the division became an int it would become 2 by 85 giving me 170


  • Advertisement
  • Registered Users Posts: 476 ✭✭RoRo979


    import java.util.Scanner;
    public class test {
    private int day;
    private int month;
    private int year;
    private String first;
    private String last;
    private int maximum;
    private int number;
    private int higher;
    private int lower;
    private int total;
    private int age;

    public test(String first, String last, int day, int month, int year){
    this.day = day;
    this.month = month;
    this.year= year;
    this.first=first;
    this.last=last;
    }

    public void setDate(int day, int month, int year){
    this.day = day;
    this.month = month;
    this.year= year; }


    public int age(){
    age = 2013-year;
    return age; }

    public int Maximum(){
    number = 2013 - year;
    total= 220 - number;
    this.total= total;
    return total; }

    public int HRH(){
    total=220-age;
    higher = total/100*50;
    return higher; }


    public int HRL(){
    total=220-age;
    lower = total/100*85;
    return lower; }

    }

    is that easier to read :)
    what i tried was,

    public double HRL(){
    total = 220-age;
    double random = total/100; // 2.04
    double lower = random * 85;
    return lower;
    }

    and the errors follow ;) thanks in advance for any help


  • Registered Users Posts: 3,532 ✭✭✭Unregistered.


    RoRo979 wrote: »
    i dont think that would be the solution as i need the division anwer to be a double. e.g for the 85% part and im 16, it would divide 204 by 100, giving me 2.04, i need to then multiply this 2.04 by 85 which i would get 173.4. but the when i use the return on it it doesnt allow me, if the division became an int it would become 2 by 85 giving me 170

    Cast the result to an integer! int result = (int) ((204/100) * 85 )


  • Registered Users Posts: 189 ✭✭Marlay


    RoRo979 wrote: »
    im sorry for this, i have all the code working but the target heart rate as it cannot return a double for me it is round it thus e.g im 16, maximum HR is 204, it should go, 204/100*50, but when 204/100 = 2 not 2.04.

    Not certain I follow you, but with this:
    double random = total/100; // 2.04
    

    you won't get a double result as you are dividing two integers. You would need to cast one of the operands to a double first:
    double random = (double)total/100; // 2.04
    


  • Registered Users Posts: 1,082 ✭✭✭Feathers


    Translation:
    RoRo979 wrote: »
    Sorry to intrude on this thread but I don't know where else to ask for help :)

    Could someone please help me with the problem below. I have all the code working except the target heart rate as it cannot return a double for me. It is rounding it thus:

    E.g. I'm 16, maximum HR is 204, it should go: 204/100 * 50, but where 204/100 = 2 not 2.04.

    Full instructions of the problem I'm working on:
    Target-Heart-Rate Calculator

    While exercising, you can use a heart-rate monitor to see that your heart rate stays within a safe range suggested by your trainers and doctors. According to the American Heart Association (AHA) , the formula for calculating your maximum heart rate in beats per minute is 220 minus your age in years.

    Your target heart rate is a range that’s 50–85% of your maximum heart rate. [Note: These formulas are estimates provided by the AHA. Maximum and target heart rates may vary based on the health, fitness and gender of the individual. Always consult a physician or qualified health care professional before beginning or modifying an exercise program.]

    Create a class called HeartRates:

    Attributes

    The class attributes should include the person's:
    • first name
    • last name
    • date of birth (consisting of separate attributes for the month, day and year of birth).

    Methods
    • Your class should have a constructor that receives this data as parameters.
    • For each attribute provide set and get methods.

    The class also should include:
    • a method that calculates and returns the person’s age (in years)
    • a method that calculates and returns the person’s maximum heart rate
    • a method that calculates and returns the person’s target heart rate.

    Write a Java application that prompts for the person’s information, instantiates an object of class HeartRates and prints the information from that object— including the person’s first name, last name and date of birth — then calculates and prints the person’s age in (years), maximum heart rate and target-heart-rate range.

    The code I have so far:
    import java.util.Scanner;
    public class test {
        Scanner ronan = new Scanner(System.in);
        private int day;
        private int month;
        private int year;
        private String first;
        private String last;
        private int maximum;
        private int number;
        private int higher;
        private int lower;
        private int total;
        private int age;
        
        public test(String first, String last, int day, int month, int year){
            this.day = day;
            this.month = month;
            this.year= year;
            this.first=first;
            this.last=last;
        }
        public void setDate(int day, int month, int year){
            this.day = day;
            this.month = month;
            this.year= year;    
        }
        public String getName(){
            return first;
        }
        public String getName2(){
            return last;
        }
        public int getDate(){
            return day;
        }
        public int getMonth(){
            return month;
        }
        public int getYear(){
            return year;
        }
        public int age(){
            age = 2013-year;
            return age;    
        }
        public int Maximum(){
            number = 2013 - year;
            total= 220 - number;
            this.total= total;
            return total;
        }
        public int HRH(){
            total=220-age;
            higher = total/100*50;
            return higher;
        }
        public int HRL(){
            total=220-age;
            lower = total/100*85;
            return lower;
        }
    
    }
    

    OP, if you're able to learn Java, BB code formatting shouldn't be too much trouble ;)


  • Registered Users Posts: 476 ✭✭RoRo979


    i tried setting one as double but it didnt work, i also tried casting one as an int but it didnt work, im unsure if im typing it incorrectly but im getting an error each time.


  • Advertisement
  • Registered Users Posts: 773 ✭✭✭Wetai


    If it's anything like C#, and I'm remembering correctly, it's the denominator (maybe both) that needs to be a double. Have you tried that? Can't hurt if you haven't.

    Also, if your function returns an int, no matter what you change (other than the signature (return type)), you'll still get an int.


  • Registered Users Posts: 3,532 ✭✭✭Unregistered.


    public int HRH(){
    total=220-age;
    higher = (int) (total/100) * 50;
    return higher;
    }

    If you want accuracy then just use doubles for your calculations then round them to ints.


  • Registered Users Posts: 476 ✭✭RoRo979


    java.util.IllegalFormatConversionException: d != java.lang.Double
    at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
    at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
    at java.util.Formatter$FormatSpecifier.print(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)
    at java.io.PrintStream.printf(Unknown Source)
    at testmain.main(testmain.java:9)

    ^^^^^^
    That is the error;

    System.out.printf("ideal HR is %d-%d", ro.HRH(),ro.HRL());

    ^^^^
    line 9 in testmain


  • Registered Users Posts: 476 ✭✭RoRo979


    haha im really sorry if im making some basic mistake, i just started learning java and i just want to understand my errors and try learn from them.


  • Registered Users Posts: 773 ✭✭✭Wetai


    Try changing those functions' return type to double (public double ...). %d in the printf is expecting doubles and getting integers.


  • Registered Users Posts: 1,082 ✭✭✭Feathers


    RoRo979 wrote: »
    i tried setting one as double but it didnt work, i also tried casting one as an int but it didnt work, im unsure if im typing it incorrectly but im getting an error each time.

    What code did you try and what's the error you're getting? (Please use "code" tags to wrap both).

    EDIT: sorry, just saw your post. I'd be inclined to remove the use of the formatter for now & get the logic working first.


  • Registered Users Posts: 476 ✭✭RoRo979


    haha so whenever i seem to change anything to a double i get an error haha


  • Registered Users Posts: 476 ✭✭RoRo979


    if someone wouldnt mind could you post some code that will work as i think i may be doing it wrong, thanks


  • Registered Users Posts: 710 ✭✭✭mad turnip


    RoRo you really need to post ALL THE CODE. This includes your public static void main(

    As thats were the errors seem to lie


  • Registered Users Posts: 476 ✭✭RoRo979


    public class testmain {
    
        public static void main(String[] args) {
            Scanner imput= new Scanner(System.in);
            test ro = new test("name1", "name2", 28, 8, 1997);
            System.out.printf("Name:%s %s\nDOB: %d/%d/%d\n age:%d\n", ro.getName(), ro.getName2(),ro.getDate(), ro.getMonth(),ro.getYear(), ro.age());
            System.out.printf("your maximum HR is %d\n", ro.Maximum());
            System.out.printf("ideal HR is %d-%d", ro.HRH(),ro.HRL());
        }
    
    }
    

    apologies should of done that at the start


  • Advertisement
  • Registered Users Posts: 710 ✭✭✭mad turnip


    RoRo979 wrote: »
    public class testmain {
    
        public static void main(String[] args) {
            Scanner imput= new Scanner(System.in);
            test ro = new test("name1", "name2", 28, 8, 1997);
            System.out.printf("Name:%s %s\nDOB: %d/%d/%d\n age:%d\n", ro.getName(), ro.getName2(),ro.getDate(), ro.getMonth(),ro.getYear(), ro.age());
            System.out.printf("your maximum HR is %d\n", ro.Maximum());
            System.out.printf("ideal HR is %d-%d", ro.HRH(),ro.HRL());
        }
    
    }
    

    apologies should of done that at the start

    can you post all your current code as what i'm running has no problems with this output:
    Name:name1 name2
    DOB: 28/8/1997
    age:16
    your maximum HR is 204
    ideal HR is 100-170

    Edit:

    Oh i see now you want to change one to a double?

    Where your using %d this stands for an int
    If you want to change any of these values to a double or a float use %f instead this should resolve your issue.

    If you want some more information on the decimal system of using %f you can check out this link:
    http://docs.oracle.com/javase/tutorial/java/data/numberformat.html


  • Closed Accounts Posts: 235 ✭✭Username99


    public class HeartRates
    {
    private String firstName;
    private String lastName;
    private int day;
    private int month;
    private int year;
    private int currentYear = 2013;


    public HeartRates ( String fName, String lName, int fDay, int fMonth, int fYear, int cYear)
    {
    firstName = fName;
    lastName = lName;
    day = fDay;
    month = fMonth;
    year = fYear;
    currentYear = cYear;
    }//end of constructor

    public void setFirstName( String fName )
    {
    firstName = fName;
    }//end of method setFirstName

    public String getFirstName()
    {
    return firstName;
    }//end of method getFirstName

    public void setLastName( String lName )
    {
    lastName = lName;
    }//end of method getLastName

    public String getLastName()
    {
    return lastName;
    }//end of method getLastName

    public void setDay( int fDay )
    {
    day = fDay;
    }//end of method setDay

    public int getDay()
    {
    return day;
    }//end of method getDay

    public void setMonth( int fMonth )
    {
    month = fMonth;
    }//end of method setMonth

    public int getMonth()
    {
    return month;
    }//end of method getMonth

    public void setYear( int fYear )
    {
    year = fYear;
    }//end of method setYear

    public int getYear()
    {
    return year;
    }//end of method getYear


    public int getAgeInYears()
    {
    return ( currentYear - year );
    }//end of method age in years


    public int getMaxHeartRate()
    {
    return (220 - getAgeInYears() );
    }

    public int targetHeartRateMin()
    {
    return (( getMaxHeartRate() / 100 ) * 50 );
    }

    public int targetHeartRateMax()
    {
    return (( getMaxHeartRate() / 100 ) * 85 );
    }


    }//end of class HeartRates


  • Closed Accounts Posts: 235 ✭✭Username99


    import java.util.Scanner;

    public class HeartRatesTest
    {
    public static void main(String[] args )
    {
    Scanner input = new Scanner ( System.in );

    HeartRates object1HeartRates = new HeartRates ( "John", "BonJovi", 09, 02, 1988, 2013 );

    //start of first name user prompt
    System.out.print( "Please enter your first name: " );
    //end of first name user prompt

    //store the user input in the variable called hoanName
    String hoanName = input.next();

    //call the setFirstName method from the HeartRates and pass hoanName to it
    object1HeartRates.setFirstName( hoanName );

    System.out.printf( "The first name you entered is: %s", object1HeartRates.getFirstName() );

    //start of last name user prompt
    System.out.print( "\nPlease enter your last name: " );
    //end of last name user prompt

    //store the user input in the variable called adoName
    String adoName = input.next();

    //call the setLastName method from the HeartRates and pass adoName to it
    object1HeartRates.setLastName( adoName );

    System.out.printf( "The last name you entered is: %s", object1HeartRates.getLastName() );

    System.out.print( "\nPlease enter the day of your birth: " );
    int dayOfBirth = input.nextInt();
    object1HeartRates.setDay( dayOfBirth );

    System.out.printf( "You entered your day of birth as: %s", object1HeartRates.getDay() );

    System.out.print( "\nPlease enter the month of your birth: " );
    int monthOfBirth = input.nextInt();
    object1HeartRates.setMonth( monthOfBirth );

    System.out.printf( "You entered your month of birth as: %s", object1HeartRates.getMonth() );

    System.out.print( "\nPlease enter the year of your birth: " );
    int yearOfBirth = input.nextInt();
    object1HeartRates.setYear( yearOfBirth );

    System.out.printf( "You entered your year of birth as: %s", object1HeartRates.getYear() );

    int ageYears = object1HeartRates.getAgeInYears();

    System.out.printf( "\nYou are " + ageYears + " years old." );

    System.out.printf( "\nYour max heart rate is " + object1HeartRates.getMaxHeartRate() + "." );





    }//end of main method
    }//end of class HeartRatesTest


  • Registered Users Posts: 476 ✭✭RoRo979


    hi Username99 thanks that was very helpful, just one question, for the target heart rate i am still receiving errors, here is the code i entered
    System.out.printf("\nYour target heart rate is %f - %f\n",targetHeartRateMin(), targetHeartRateMin());
    

    I also tried doing
    System.out.printf("\nYour target is %d- %d\n",targetHeartRateMin(), targetHeartRateMin());
    

    but neither seemed to work, how would you go about gettin this, thank you


  • Registered Users Posts: 3,532 ✭✭✭Unregistered.


    RoRo979 wrote: »
    hi Username99 thanks that was very helpful, just one question, for the target heart rate i am still receiving errors, here is the code i entered
    System.out.printf("\nYour target heart rate is %f - %f\n",[B]targetHeartRateMin()[/B], [B]targetHeartRateMin()[/B]);
    

    I also tried doing
    System.out.printf("\nYour target is %d- %d\n",[B]targetHeartRateMin()[/B], [B]targetHeartRateMin()[/B]);
    

    but neither seemed to work, how would you go about gettin this, thank you

    What seems to be the problem? (Note what I have highlighted in bold)


  • Registered Users Posts: 476 ✭✭RoRo979


    the return value is coming up null- null?


  • Registered Users Posts: 3,532 ✭✭✭Unregistered.


    RoRo979 wrote: »
    the return value is coming up null- null?

    What IDE are you using? It would be worth learning to use a debug tool for this.

    If the result is null, then this is beyond what you have asked help for. There is likely some small changed you have made, perhaps a variable is uninitialized, but without knowing the changes you have made, we can't see that.
    Post your most up-to-date code. Leave out the getters and setters, we don't need to see them.


  • Registered Users Posts: 476 ✭✭RoRo979


    ok so i have the code that Username99 put up. i only added in
    System.out.printf("\nYour target is %f - %f\n",targetHeartRateMin(), targetHeartRateMin());
    
    which i expected to run fine but an error is coming up saying that
    Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    	The method targetHeartRateMin() is undefined for the type testmain
    	The method targetHeartRateMax() is undefined for the type testmain
    

    i am really confused as to why it is not recognising this method but recognises all the rest.

    i am currently using eclipse.


  • Registered Users Posts: 476 ✭✭RoRo979


    right hahah i have discovered that error. i just wrote
    targetHeartRateMin()
    
    instead of
    object1HeartRates.targetHeartRateMin()
    
    but another error now comes up haha. it reads
    xception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer
    	at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
    	at java.util.Formatter$FormatSpecifier.printFloat(Unknown Source)
    	at java.util.Formatter$FormatSpecifier.print(Unknown Source)
    	at java.util.Formatter.format(Unknown Source)
    	at java.io.PrintStream.format(Unknown Source)
    	at java.io.PrintStream.printf(Unknown Source)
    	at testmain.main(testmain.java:59)
    


  • Advertisement
  • Registered Users Posts: 3,532 ✭✭✭Unregistered.


    RoRo979 wrote: »
    right hahah i have discovered that error. i just wrote
    targetHeartRateMin()
    
    instead of
    object1HeartRates.targetHeartRateMin()
    
    but another error now comes up haha. it reads
    xception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer
        at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
        at java.util.Formatter$FormatSpecifier.printFloat(Unknown Source)
        at java.util.Formatter$FormatSpecifier.print(Unknown Source)
        at java.util.Formatter.format(Unknown Source)
        at java.io.PrintStream.format(Unknown Source)
        at java.io.PrintStream.printf(Unknown Source)
        at testmain.main(testmain.java:59)
    


    READ THE ERROR! You changed %d to %f. %f is for double, %d is for ints. You changed it from %d to %f and and your function returns an int. That is what this is telling you.


Advertisement