Help with a Target Heart rate program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KoreyAusTex
    New Member
    • Feb 2007
    • 36

    Help with a Target Heart rate program

    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.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    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

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Originally posted by horace1
      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?

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by Ganon11
        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

        Comment

        • KoreyAusTex
          New Member
          • Feb 2007
          • 36

          #5
          Code:
          public class HeartRates
          {
             static final int MAX_HEART_RATE = 220; // cannot change
          
             public static void main(String[] args)
             {
                 boolean notQuit = true;
                 System.out.println("Computing heart rates for exercising (press 'q' to exit)");
                 System.out.println("--------------------------------------------------------");
          
                 int i = 0;
                 
                 Scanner read = new Scanner(System.in);
          
                 System.out.print("Enter your name: ");
          
                 while(notQuit && i < 4){
          
          
          
                     String name = read.next();
                     System.out.println();
          
                     if( name.charAt(0) == 'q'){
                         notQuit = false;
                         System.out.print("Program terminating. Bye!");
                     }   else{
                         System.out.print(name + " enter your age: ");
          
                         double age = read.nextDouble();
                             if (age = true){
                                 System.out.println("Error! Check age again.");
                             }
          
                         System.out.println();
                         double newZone = calcAerobicZone(age);
                         double newFat = calcFatBurningZone(age);
                         System.out.println( name + " your age is " + age);
                         System.out.println("Your aerobic zone is: " + newZone);
                         System.out.println("Your fat burning zone is: " + newFat);
          
          
                         System.out.println("Computing heart rates for exercising (press 'q' to exit)");
                         System.out.println("--------------------------------------------------------");
                         System.out.print("Name: ");
                         i++;
          
                     }
          
                 }
             }
          
          
             public static double calcAerobicZone(double age)
             {
                 double aerobicZone = (MAX_HEART_RATE - age) * .75;
                 return aerobicZone;
          
             }
          
             public static double calcFatBurningZone(double age)
             {
                 double fatBurningZone = (MAX_HEART_RATE - age) * .65;
                 return fatBurningZone;
          
             }
          }
          This is about all I have but I am having problems debugging.
          Last edited by horace1; Feb 9 '07, 06:10 AM. Reason: added code tags

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            fixed a couple of errors - try now
            Code:
            import java.util.Scanner;  // ** for Scanner
            
            public class HeartRates
            {
               static final int MAX_HEART_RATE = 220; // cannot change
            
               public static void main(String[] args)
               {
                   boolean notQuit = true;
                   System.out.println("Computing heart rates for exercising (press 'q' to exit)");
                   System.out.println("--------------------------------------------------------");
            
                   int i = 0;
                   
                   Scanner read = new Scanner(System.in);
            
                   System.out.print("Enter your name: ");
            
                   while(notQuit && i < 4){
            
            
            
                       String name = read.next();
                       System.out.println();
            
                       if( name.charAt(0) == 'q'){
                           notQuit = false;
                           System.out.print("Program terminating. Bye!");
                       }   else{
                           System.out.print(name + " enter your age: ");
            
                           double age = read.nextDouble();
                               if (age > 0){        // ** what test do you want here?
                                   System.out.println("Error! Check age again.");
                               }
            
                           System.out.println();
                           double newZone = calcAerobicZone(age);
                           double newFat = calcFatBurningZone(age);
                           System.out.println( name + " your age is " + age);
                           System.out.println("Your aerobic zone is: " + newZone);
                           System.out.println("Your fat burning zone is: " + newFat);
            
            
                           System.out.println("Computing heart rates for exercising (press 'q' to exit)");
                           System.out.println("--------------------------------------------------------");
                           System.out.print("Name: ");
                           i++;
            
                       }
            
                   }
               }
            
            
               public static double calcAerobicZone(double age)
               {
                   double aerobicZone = (MAX_HEART_RATE - age) * .75;
                   return aerobicZone;
            
               }
            
               public static double calcFatBurningZone(double age)
               {
                   double fatBurningZone = (MAX_HEART_RATE - age) * .65;
                   return fatBurningZone;
            
               }
            }

            Comment

            • KoreyAusTex
              New Member
              • Feb 2007
              • 36

              #7
              actually I need the program to show the ERROR message when someone inputs something other than an number.

              Comment

              • KoreyAusTex
                New Member
                • Feb 2007
                • 36

                #8
                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 calcFatBurningZ one(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

                Comment

                • horace1
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 1510

                  #9
                  the simplest thing is to use Scanner.hasNext Double() to check if the next token is a double, if so you read it if not discard it, e.g.
                  Code:
                  // read doubles from keyboard and form sum
                  //  exit on a negative value
                  
                  import java.util.Scanner;
                  
                  public class ScanSum {
                      public static void main(String[] args)  {
                          Scanner s = new Scanner(System.in);
                          double sum = 0;
                          while (s.hasNext()) 
                            {
                             // if next token is a double read it and add to sum
                             if (s.hasNextDouble()) 
                                {
                                 double value = s.nextDouble();
                                 if(value < 0)  break;           // if token < 0 finished
                                 sum += value;
                                } 
                             else  s.next();                     // not double, discard token
                              }
                          System.out.println(sum);
                      }
                  }

                  Comment

                  • KoreyAusTex
                    New Member
                    • Feb 2007
                    • 36

                    #10
                    how would that fit into the program, can you have two classes?

                    Comment

                    • horace1
                      Recognized Expert Top Contributor
                      • Nov 2006
                      • 1510

                      #11
                      Originally posted by KoreyAusTex
                      how would that fit into the program, can you have two classes?
                      have alook at this simplified version
                      Code:
                      // read doubles from keyboard and form sum
                      //  exit on a negative value
                      
                      import java.util.Scanner;
                      
                      public class ScanSum {
                          public static void main(String[] args)  {
                              Scanner s = new Scanner(System.in);
                              double sum = 0;
                              while (s.hasNext()) 
                                {
                                 // if next token is a double read it and add to sum
                                 if (!s.hasNextDouble()) s.next();    // not double, discard token
                                 double value = s.nextDouble();
                                 if(value < 0)  break;           // if token < 0 finished
                                 sum += value;
                                }
                              System.out.println(sum);
                          }
                      }
                      just insert a statement such as
                      Code:
                            if (!s.hasNextDouble()) s.next();    // not double, discard token
                      before you try to read a double - it will skip anything else

                      Comment

                      • KoreyAusTex
                        New Member
                        • Feb 2007
                        • 36

                        #12
                        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????
                        Code:
                        import java.util.*
                        
                        public class HeartRates
                        {
                           static final int MAX_HEART_RATE = 220; // cannot change
                        
                           public static void main(String[] args)
                           {
                               boolean notQuit = true;
                               int i = 0;
                               Scanner read = new Scanner(System.in); //
                        
                               while(notQuit && i < 4){ //looping a max of 4 times and stopping when q is entered
                                   System.out.println("Computing heart rates for exercising (press 'q' to exit)");
                                   System.out.println("--------------------------------------------------------");
                                   System.out.print("Name: ");
                                   String name = read.next();
                        
                                   if( name.charAt(0) == 'q'){
                                       notQuit = false; //check to see if they entered q to stop loop
                                   }   else { //
                                       System.out.print("Enter age: ");
                                       try {//attempting to read a double value and catching exception that occurs
                                       double age = read.nextDouble();
                                       double newZone = calcAerobicZone(age);
                                       double newFat = calcFatBurningZone(age);
                                       System.out.println( name + " for your age: " + age);
                                       System.out.println("Your aerobic zone is: " + newZone);
                                       System.out.println("Your fat burning zone is: " + newFat + "\n");
                                     
                                       } catch (Exception e){ 
                                                System.out.println("Error! Check age again.\n");
                                                String garbage = read.next();
                                            }
                                            finally{
                                                i++;
                                            }
                                        }   
                                    }
                                    System.out.print("Program terminating. Bye!");
                                }
                           public static double calcAerobicZone(double age)
                           {
                               double aerobicZone = (MAX_HEART_RATE - age) * .75;
                               return aerobicZone;
                        
                           }
                        
                           public static double calcFatBurningZone(double age)
                           {
                               double fatBurningZone = (MAX_HEART_RATE - age) * .65;
                               return fatBurningZone;
                        
                           }
                        }
                        Last edited by horace1; Feb 10 '07, 07:28 AM. Reason: added code tags

                        Comment

                        • horace1
                          Recognized Expert Top Contributor
                          • Nov 2006
                          • 1510

                          #13
                          you have to read the faulty number as a string and check if the first character is a q, e.g.
                          Code:
                          import java.util.*;  // ** added ;
                          
                          public class HeartRates
                          {
                             static final int MAX_HEART_RATE = 220; // cannot change
                          
                             public static void main(String[] args)
                             {
                                 boolean notQuit = true;
                                 int i = 0;
                                 Scanner read = new Scanner(System.in); //
                          
                                 while(notQuit && i < 4){ //looping a max of 4 times and stopping when q is entered
                                     System.out.println("Computing heart rates for exercising (press 'q' to exit)");
                                     System.out.println("--------------------------------------------------------");
                                     System.out.print("Name: ");
                                     String name = read.next();
                          
                                     if( name.charAt(0) == 'q'){
                                         notQuit = false; //check to see if they entered q to stop loop
                                     }   else { //
                                         System.out.print("Enter age: ");
                                         //attempting to read a double value and catching exception that occurs
                                         while (!read.hasNextDouble()) 
                                            {
                                             String temp = read.next();    // not double, discard token
                                             if( temp.charAt(0) == 'q')
                                                { System.out.print("Program terminating. Bye!"); System.exit(0); }
                                             System.out.print("Error! Enter age: ");
                                            }
                                         double age = read.nextDouble();
                                         double newZone = calcAerobicZone(age);
                                         double newFat = calcFatBurningZone(age);
                                         System.out.println( name + " for your age: " + age);
                                         System.out.println("Your aerobic zone is: " + newZone);
                                         System.out.println("Your fat burning zone is: " + newFat + "\n");
                                                  i++;
                                          }   
                                      }
                                      System.out.print("Program terminating. Bye!");
                                  }
                             public static double calcAerobicZone(double age)
                             {
                                 double aerobicZone = (MAX_HEART_RATE - age) * .75;
                                 return aerobicZone;
                          
                             }
                          
                             public static double calcFatBurningZone(double age)
                             {
                                 double fatBurningZone = (MAX_HEART_RATE - age) * .65;
                                 return fatBurningZone;
                          
                             }
                          }

                          Comment

                          • KoreyAusTex
                            New Member
                            • Feb 2007
                            • 36

                            #14
                            When I get my output I am getting numbers like 129.000000001. How could I go about fixing this????

                            Comment

                            • horace1
                              Recognized Expert Top Contributor
                              • Nov 2006
                              • 1510

                              #15
                              Originally posted by KoreyAusTex
                              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.prin tf() method (similar to C) to set the number of digits after the decimal point, see
                              http://java.sun.com/developer/technicalArticl es/Programming/sprintf/

                              Comment

                              Working...