473,395 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Help with a Target Heart rate program

KoreyAusTex
These are the instructions, I am not asking anyone to code for me but would like to hear some ideas on how to start, this just trying to figure it out thing isn't working in this case!

Write a program, HeartRates.java, that takes as input a series of name and age pairs, prints out heart rates for exercising, and continues until either the user types "q" or 4 iterations have been executed.

Computing heart rates for exercising (press q to exit)
-----------------------------------------------
Name: John
Enter age: 20
John for your age: 20
Your aerobic zone is: 150
Your fat Burning zone is: 130

Computing heart rates for exercising (press q to exit)
-----------------------------------------------
Name: Jack
Enter age: aaa
Error! Check age again.

Computing heart rates for exercising (press q to exit)
-----------------------------------------------
Name: Joe
Enter age: 21
Joe for your age: 21
Your aerobic zone is: 149
Your fat Burning zone is: 129

Program terminating. Bye!

if you have any ideas would love to hear them, yes I need to learn this on my own but my professor acts like we already should know how to program.
Feb 8 '07 #1
15 6038
horace1
1,510 Expert 1GB
are you working in C or C++ ?

start by writing a program to read data on one person, e.g.
(1) prompt user to enter his/her name;
(2) read the name into a char array if working in C or a string if in C++
(3) display name to check it was read OK
(4) prompt use to enter age
(5) read age into a float
(6) display age to check it is OK
(7) calculate results

when that is working you can add error echecking, e.g. if a numeric value is not entered for age, then add a loop to deal with more than one person
Feb 8 '07 #2
Ganon11
3,652 Expert 2GB
are you working in C or C++ ?
Seeing as this is the Java forum, I'd assume he's using Java, horace...XD

What version of Java are you running? What algorithm will you be using to calculate the output?

What code have you written so far?
Feb 8 '07 #3
horace1
1,510 Expert 1GB
Seeing as this is the Java forum, I'd assume he's using Java, horace...XD
guess you a right there Gannon11 !
I code in both all the time so I often find I am writing C++ in a java program, etc etc!

my idea is now
start by writing a program to read data on one person, e.g.
(1) prompt user to enter his/her name;
(2) read the name into a String
(3) display name to check it was read OK
(4) prompt use to enter age
(5) read age into a float
(6) display age to check it is OK
(7) calculate results

when that is working you can add error echecking, e.g. if a numeric value is not entered for age, then add a loop to deal with more than one person
Feb 8 '07 #4
Expand|Select|Wrap|Line Numbers
  1. public class HeartRates
  2. {
  3.    static final int MAX_HEART_RATE = 220; // cannot change
  4.  
  5.    public static void main(String[] args)
  6.    {
  7.        boolean notQuit = true;
  8.        System.out.println("Computing heart rates for exercising (press 'q' to exit)");
  9.        System.out.println("--------------------------------------------------------");
  10.  
  11.        int i = 0;
  12.  
  13.        Scanner read = new Scanner(System.in);
  14.  
  15.        System.out.print("Enter your name: ");
  16.  
  17.        while(notQuit && i < 4){
  18.  
  19.  
  20.  
  21.            String name = read.next();
  22.            System.out.println();
  23.  
  24.            if( name.charAt(0) == 'q'){
  25.                notQuit = false;
  26.                System.out.print("Program terminating. Bye!");
  27.            }   else{
  28.                System.out.print(name + " enter your age: ");
  29.  
  30.                double age = read.nextDouble();
  31.                    if (age = true){
  32.                        System.out.println("Error! Check age again.");
  33.                    }
  34.  
  35.                System.out.println();
  36.                double newZone = calcAerobicZone(age);
  37.                double newFat = calcFatBurningZone(age);
  38.                System.out.println( name + " your age is " + age);
  39.                System.out.println("Your aerobic zone is: " + newZone);
  40.                System.out.println("Your fat burning zone is: " + newFat);
  41.  
  42.  
  43.                System.out.println("Computing heart rates for exercising (press 'q' to exit)");
  44.                System.out.println("--------------------------------------------------------");
  45.                System.out.print("Name: ");
  46.                i++;
  47.  
  48.            }
  49.  
  50.        }
  51.    }
  52.  
  53.  
  54.    public static double calcAerobicZone(double age)
  55.    {
  56.        double aerobicZone = (MAX_HEART_RATE - age) * .75;
  57.        return aerobicZone;
  58.  
  59.    }
  60.  
  61.    public static double calcFatBurningZone(double age)
  62.    {
  63.        double fatBurningZone = (MAX_HEART_RATE - age) * .65;
  64.        return fatBurningZone;
  65.  
  66.    }
  67. }
  68.  
This is about all I have but I am having problems debugging.
Feb 9 '07 #5
horace1
1,510 Expert 1GB
fixed a couple of errors - try now
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;  // ** for Scanner
  2.  
  3. public class HeartRates
  4. {
  5.    static final int MAX_HEART_RATE = 220; // cannot change
  6.  
  7.    public static void main(String[] args)
  8.    {
  9.        boolean notQuit = true;
  10.        System.out.println("Computing heart rates for exercising (press 'q' to exit)");
  11.        System.out.println("--------------------------------------------------------");
  12.  
  13.        int i = 0;
  14.  
  15.        Scanner read = new Scanner(System.in);
  16.  
  17.        System.out.print("Enter your name: ");
  18.  
  19.        while(notQuit && i < 4){
  20.  
  21.  
  22.  
  23.            String name = read.next();
  24.            System.out.println();
  25.  
  26.            if( name.charAt(0) == 'q'){
  27.                notQuit = false;
  28.                System.out.print("Program terminating. Bye!");
  29.            }   else{
  30.                System.out.print(name + " enter your age: ");
  31.  
  32.                double age = read.nextDouble();
  33.                    if (age > 0){        // ** what test do you want here?
  34.                        System.out.println("Error! Check age again.");
  35.                    }
  36.  
  37.                System.out.println();
  38.                double newZone = calcAerobicZone(age);
  39.                double newFat = calcFatBurningZone(age);
  40.                System.out.println( name + " your age is " + age);
  41.                System.out.println("Your aerobic zone is: " + newZone);
  42.                System.out.println("Your fat burning zone is: " + newFat);
  43.  
  44.  
  45.                System.out.println("Computing heart rates for exercising (press 'q' to exit)");
  46.                System.out.println("--------------------------------------------------------");
  47.                System.out.print("Name: ");
  48.                i++;
  49.  
  50.            }
  51.  
  52.        }
  53.    }
  54.  
  55.  
  56.    public static double calcAerobicZone(double age)
  57.    {
  58.        double aerobicZone = (MAX_HEART_RATE - age) * .75;
  59.        return aerobicZone;
  60.  
  61.    }
  62.  
  63.    public static double calcFatBurningZone(double age)
  64.    {
  65.        double fatBurningZone = (MAX_HEART_RATE - age) * .65;
  66.        return fatBurningZone;
  67.  
  68.    }
  69. }
Feb 9 '07 #6
actually I need the program to show the ERROR message when someone inputs something other than an number.
Feb 9 '07 #7
Purpose: In this assignment, you will learn how to get input from users with the scanner class, print output with slightly more sophistication, use while loops with boolean logic, and use methods to eliminate unnecessary redundancy. When you finish this lab, you will have the basic skills for input and output to your program, and thus a start on being able to more easily debug your programming errors.

You will write a program, HeartRates.java, that takes as input a series of name and age pairs, prints out heart rates for exercising, and continues until either the user types "q" or 4 iterations have been executed. Below is a sample output from an execution of the program:

Computing heart rates for exercising (press q to exit)
-----------------------------------------------
Name: John
Enter age: 20
John for your age: 20
Your aerobic zone is: 150
Your fat Burning zone is: 130

Computing heart rates for exercising (press q to exit)
-----------------------------------------------
Name: Jack
Enter age: aaa
Error! Check age again.

Computing heart rates for exercising (press q to exit)
-----------------------------------------------
Name: Joe
Enter age: 21
Joe for your age: 21
Your aerobic zone is: 149
Your fat Burning zone is: 129

Program terminating. Bye!

In oder to compute the aerobic and fat burning zone you will use the formulas:

aerobic zone = MAX_HEART_RATE - age) * .75

fat burning zone = MAX_HEART_RATE - age) * .65

where MAX_HEART_RATE is 220 and aerobic zone is and fat burning zone are integer numbers.

The calculation of each zone should be enclosed in a method, which will be called at the appropriate place in your program. The method signatures are the following:

int calcAerobicZone(int age)

int calcFatBurningZone(int age)

You should also try to minimize the number of println() method calls in your source code. In order to do that you can use another method.

Note that only "q" should terminate the execution, all other strings are considered valid names. Your program should also contain explicit checks whether the age inserted by the user is indeed a valid number. If not, the program should print an appropriate message informing the user and should not proceed to the calculation of the two zones. If the user does not terminate the iterative process by inserting "q", the program loop should terminate after 4 tries, regardless of whether some of them are successful or not.

Tip 1: You need to put the following statement at the beginning of your class in order to tell the Java compiler where to find the Scanner class.

import java.util.*;

Tip 2: To compare two strings, you should use:

String h = "hello"
String s = "hello"
String t = "later";
h.equals(s) --> resolves to true
h.equals(t) --> resolves to false
Feb 9 '07 #8
horace1
1,510 Expert 1GB
the simplest thing is to use Scanner.hasNextDouble() to check if the next token is a double, if so you read it if not discard it, e.g.
Expand|Select|Wrap|Line Numbers
  1. // read doubles from keyboard and form sum
  2. //  exit on a negative value
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class ScanSum {
  7.     public static void main(String[] args)  {
  8.         Scanner s = new Scanner(System.in);
  9.         double sum = 0;
  10.         while (s.hasNext()) 
  11.           {
  12.            // if next token is a double read it and add to sum
  13.            if (s.hasNextDouble()) 
  14.               {
  15.                double value = s.nextDouble();
  16.                if(value < 0)  break;           // if token < 0 finished
  17.                sum += value;
  18.               } 
  19.            else  s.next();                     // not double, discard token
  20.             }
  21.         System.out.println(sum);
  22.     }
  23. }
  24.  
Feb 9 '07 #9
how would that fit into the program, can you have two classes?
Feb 9 '07 #10
horace1
1,510 Expert 1GB
how would that fit into the program, can you have two classes?
have alook at this simplified version
Expand|Select|Wrap|Line Numbers
  1. // read doubles from keyboard and form sum
  2. //  exit on a negative value
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class ScanSum {
  7.     public static void main(String[] args)  {
  8.         Scanner s = new Scanner(System.in);
  9.         double sum = 0;
  10.         while (s.hasNext()) 
  11.           {
  12.            // if next token is a double read it and add to sum
  13.            if (!s.hasNextDouble()) s.next();    // not double, discard token
  14.            double value = s.nextDouble();
  15.            if(value < 0)  break;           // if token < 0 finished
  16.            sum += value;
  17.           }
  18.         System.out.println(sum);
  19.     }
  20. }
  21.  
just insert a statement such as
Expand|Select|Wrap|Line Numbers
  1.       if (!s.hasNextDouble()) s.next();    // not double, discard token
before you try to read a double - it will skip anything else
Feb 9 '07 #11
ok this is what I have so far but when I try to put 'q' in when prompted to enter age I get the error message intended for entering anything other than a number.

This code is starting to hurt my head anyone have any more ideas????
Expand|Select|Wrap|Line Numbers
  1. import java.util.*
  2.  
  3. public class HeartRates
  4. {
  5.    static final int MAX_HEART_RATE = 220; // cannot change
  6.  
  7.    public static void main(String[] args)
  8.    {
  9.        boolean notQuit = true;
  10.        int i = 0;
  11.        Scanner read = new Scanner(System.in); //
  12.  
  13.        while(notQuit && i < 4){ //looping a max of 4 times and stopping when q is entered
  14.            System.out.println("Computing heart rates for exercising (press 'q' to exit)");
  15.            System.out.println("--------------------------------------------------------");
  16.            System.out.print("Name: ");
  17.            String name = read.next();
  18.  
  19.            if( name.charAt(0) == 'q'){
  20.                notQuit = false; //check to see if they entered q to stop loop
  21.            }   else { //
  22.                System.out.print("Enter age: ");
  23.                try {//attempting to read a double value and catching exception that occurs
  24.                double age = read.nextDouble();
  25.                double newZone = calcAerobicZone(age);
  26.                double newFat = calcFatBurningZone(age);
  27.                System.out.println( name + " for your age: " + age);
  28.                System.out.println("Your aerobic zone is: " + newZone);
  29.                System.out.println("Your fat burning zone is: " + newFat + "\n");
  30.  
  31.                } catch (Exception e){ 
  32.                         System.out.println("Error! Check age again.\n");
  33.                         String garbage = read.next();
  34.                     }
  35.                     finally{
  36.                         i++;
  37.                     }
  38.                 }   
  39.             }
  40.             System.out.print("Program terminating. Bye!");
  41.         }
  42.    public static double calcAerobicZone(double age)
  43.    {
  44.        double aerobicZone = (MAX_HEART_RATE - age) * .75;
  45.        return aerobicZone;
  46.  
  47.    }
  48.  
  49.    public static double calcFatBurningZone(double age)
  50.    {
  51.        double fatBurningZone = (MAX_HEART_RATE - age) * .65;
  52.        return fatBurningZone;
  53.  
  54.    }
  55. }
Feb 10 '07 #12
horace1
1,510 Expert 1GB
you have to read the faulty number as a string and check if the first character is a q, e.g.
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;  // ** added ;
  2.  
  3. public class HeartRates
  4. {
  5.    static final int MAX_HEART_RATE = 220; // cannot change
  6.  
  7.    public static void main(String[] args)
  8.    {
  9.        boolean notQuit = true;
  10.        int i = 0;
  11.        Scanner read = new Scanner(System.in); //
  12.  
  13.        while(notQuit && i < 4){ //looping a max of 4 times and stopping when q is entered
  14.            System.out.println("Computing heart rates for exercising (press 'q' to exit)");
  15.            System.out.println("--------------------------------------------------------");
  16.            System.out.print("Name: ");
  17.            String name = read.next();
  18.  
  19.            if( name.charAt(0) == 'q'){
  20.                notQuit = false; //check to see if they entered q to stop loop
  21.            }   else { //
  22.                System.out.print("Enter age: ");
  23.                //attempting to read a double value and catching exception that occurs
  24.                while (!read.hasNextDouble()) 
  25.                   {
  26.                    String temp = read.next();    // not double, discard token
  27.                    if( temp.charAt(0) == 'q')
  28.                       { System.out.print("Program terminating. Bye!"); System.exit(0); }
  29.                    System.out.print("Error! Enter age: ");
  30.                   }
  31.                double age = read.nextDouble();
  32.                double newZone = calcAerobicZone(age);
  33.                double newFat = calcFatBurningZone(age);
  34.                System.out.println( name + " for your age: " + age);
  35.                System.out.println("Your aerobic zone is: " + newZone);
  36.                System.out.println("Your fat burning zone is: " + newFat + "\n");
  37.                         i++;
  38.                 }   
  39.             }
  40.             System.out.print("Program terminating. Bye!");
  41.         }
  42.    public static double calcAerobicZone(double age)
  43.    {
  44.        double aerobicZone = (MAX_HEART_RATE - age) * .75;
  45.        return aerobicZone;
  46.  
  47.    }
  48.  
  49.    public static double calcFatBurningZone(double age)
  50.    {
  51.        double fatBurningZone = (MAX_HEART_RATE - age) * .65;
  52.        return fatBurningZone;
  53.  
  54.    }
  55. }
Feb 10 '07 #13
When I get my output I am getting numbers like 129.000000001. How could I go about fixing this????
Feb 11 '07 #14
horace1
1,510 Expert 1GB
When I get my output I am getting numbers like 129.000000001. How could I go about fixing this????
you can use the System.out.printf() method (similar to C) to set the number of digits after the decimal point, see
http://java.sun.com/developer/technicalArticles/Programming/sprintf/
Feb 11 '07 #15
actually I just set:

double newZone = (int) calcAerobicZone(age);
double newFat = (int) calcFatBurningZone(age);

this truncated the decimal places which works cause when your target heart rate is 127.75 the .75 is not really necessary.
Feb 11 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: abcd | last post by:
kutthaense Secretary Djetvedehald H. Rumsfeld legai predicted eventual vicmadhlary in Iraq mariyu Afghmadhlaistmadhla, kaani jetvedehly after "a ljetvedehg, hard slog," mariyu vede legai pressed...
2
by: Darryll Petrancuri | last post by:
Greetings! I could really use some suggestions on how to improve on the following, it at all possible: Table 'Customer' --------------------- ID GUID PK ....
20
by: Dj_TRuST | last post by:
This is my homework and i couldn't do it,please help me. I wait your helpsss On the planet Zephod, which orbits the star Betelgeuse, the sharks increase at a rate of 5% of the guppy population...
1
by: abatesf | last post by:
I need to write a tax calculation program in C for a store chain. There 4 stores and 4 different tax rates .0700, .0725, .07500 and .0775 This is what i have so far... any suggestions #include...
36
by: aljamala | last post by:
Hi, I keep getting this warning on a page, but I do not know what the problem is...does anyone have an idea about what could be wrong? line 88 column 7 - Warning: missing </formbefore <td> it...
12
by: Boris Ozegovic | last post by:
Hi, I am working on some system, and the communication will take place through the chatterbot which will be written in AIML (interpreter is written in Python). English is not my mother tongue,...
1
by: normbrc | last post by:
I need to create a program in Java that: 1. Asks the user to enter the name of the input file containing heart rate measurements, and input the file name. .txt format 2. Ask the user to enter...
3
by: milov | last post by:
Project to do simulation testing (me teacher). Page one writes in real time to page two...both displayed at sam time with frames. Code below. Problem...I want to keep score. Each choice needs a...
14
by: imran akhtar | last post by:
Hi. I have created a program which works correctly, and was just wondering if someone could help me in writing another program which works in the same way, but if the code is different. Below is what...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.