Search code examples
javareturn

How can I make this program return a result?


I have been working on this program.

One of the requirements were the following: Write a program that is split in to methods at least one of which returns a result

It don't have any errors but I need to make it return a result in order for it to work properly. More specifically to add a method that returns a result.


Solution

  • All you do it make the method to return something like the age

            public static void CalculateAge() {
         
                int age;
                int heartRate;
                int stretch;
    
                Scanner input = new Scanner(System.in);
    
                System.out.print("What is your age? "); 
    
                age = input.nextInt();
         
                System.out.print("What is your heart rate? "); 
    
                heartRate = input.nextInt();
    
                age = heart(age, heartRate);
                
                System.out.print("How far can you stretch? "); 
    
                stretch = input.nextInt();
         
                age = stretch(age, stretch);
    
                System.out.println("Your body's age is " + age);
    
            }  //END of CalculateAge
            public static int heart(int age, int heartRate) {
                if (heartRate <= 62) {
                    age -= 5;          // block of code to be executed if condition1 is true
                } else if (62 <= heartRate && heartRate <= 64) {
                    age--;            // block of code to be executed if the condition1 is false and condition2 is
                                      // true
                } else if (65 <= heartRate && heartRate <= 70) {
                    age++;           // block of code to be executed if the condition1 and condition2 are false and 
                                     // condition3 is true
                } else {
                    age += 2;       // block of code to be executed if the condition1 and condition2 and condition3
                                    // are false and condition4 is true
                }
                return age;
    
            }
            public static int stretch(int age, int stretch) {
                if (stretch <= 20) {
                    age += 4;      // block of code to be executed if condition1 is true
                } else if (20 <= stretch && stretch <= 32) {
                    age++;         // block of code to be executed if the condition1 is false and condition2 is
                                   // true
                } else if (33 <= stretch && stretch <= 37) {
                    age = age + 0; // block of code to be executed if the condition1 and condition2 are false and
                                   // condition3 is true
                } else {
                    age = age + 3; // block of code to be executed if the condition1 and condition2 and condition3
                                   // are false and condition4 is true
                }
                return age;
            }
    

    So all I did was split the heartRate calculation into a method and also the stretch calculation into a method and out them together.