Java Invoking Methods and returning data

I am brand new to Java… I have an assignment that is discussing Methods. For some reason when I invoke my methods and pass the data back to my main everything is "-Infinity" or "0". I have been trying to resolve this for two days straight and I can't seem to find an appropriate solution.

I'm only including the code for the first portion of the assignment because I have a feeling that whatever mistake I am making… I'm making it throughout the entire assignment. So if someone could assist me with this portion it would allow me to hopefully correct my other issues too.

The first method returns: -Infinity, but when I take the code apart and run it without the use of methods… I get 11.8, which is correct.

Any assistance would be greatly appreciated!

/*  
 * Anthony Vincenzo Laginess  
 * CIT 130  
 * Oct. 12th, 2016  
 * HMW 07 - Methods  
 * Time Needed:   
 */

package cit130hmw07_laginess;
    import java.util.Scanner;
public class CIT130HMW07_Laginess {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

//*************************************************         
//****************** Method 1 *********************         
//*************************************************         
//This method calculates body fat. It takes your gender as a parameter         
//and outputs your bodyfat. 

System.out.println("Please enter your gender...");
String gender = input.nextLine();
System.out.println("Please enter your weight: ");
int bodyWeight = input.nextInt();
System.out.println("Please enter your waist measurement: ");
int waistSize = input.nextInt();

  if(gender.equalsIgnoreCase("male")) {                                    
       bodyFatMale(bodyWeight, waistSize);
       double bodyFatPercentage = bodyFatMale(bodyWeight, waistSize);                                                       
       System.out.println("Your body fat is: " + bodyFatPercentage);   }

  else if(gender.equalsIgnoreCase("female"))         {                 
      System.out.println("Please enter your waist size: ");             
      int waist = input.nextInt();             
      System.out.println("Please enter your hip size: ");             
      int hips = input.nextInt();             
      System.out.println("Please enter your forearm size: ");             
      int forearms = input.nextInt();                                             

      bodyFatFemale(bodyWeight, waistSize, waist, hips, forearms);                   
      double answer = bodyFatFemale(bodyWeight, waistSize, waist, hips,     
      forearms);             

      System.out.println("Your body fat is: " + answer);         } 

      else 
         //enter an error message 

}//main

//METHOD 1: Bodyfat calculations     
public static double bodyFatMale(int bodyWeight, int waistSize)     {                     

    int weight = 0;         
    int waist = 0;         
    double A1;         
    double A2;         
    double B;         
    double actualBodyFat;         
    double bodyFatPercentage;                          
    A1 = (weight * 1.082) + 94.42;             
    A2 = waist * 4.15;             
    B = A1 - A2;             
    actualBodyFat = weight - B;             
    bodyFatPercentage = actualBodyFat * 100 / weight;                          

    return bodyFatPercentage;      }

 public static double bodyFatFemale(int bodyWeight, double wristSize, double waistSize, double hipSize, double forearmSize)     {         
    int weight = 0;         
    double wrist = 0;         
    double waist = 0;         
    double hips = 0;         
    double forearms = 0;         
    double A1, A2, A3, A4, A5, B;         
    double actualBodyFat;         
    double bodyFatPercentage;                               

      A1 = (weight * .732) + 8.987;             
      A2 = wrist / 3.14;             
      A3 = waist * 0.157;             
      A4 = hips * 0.249;             
      A5 = forearms * 0.434;             
      B = A1 + A2 - A3 - A4 + A5;             
      actualBodyFat = weight - B;             
      bodyFatPercentage = actualBodyFat * 100 / weight;                           

      return bodyFatPercentage;               }
}//class

Upvotes: 0

Views: 54

Answers (4)

Stewart
Stewart

Reputation: 18313

public static double bodyFatMale(int bodyWeight, int waistSize) {                     

    int weight = 0; // Introducing `0` into all calculations
    .....
    A1 = (weight * 1.082) + 94.42;   // Should be `bodyWeight`?      
    A2 = waist * 4.15;   // Should be `bodyWeight`? 
    B = A1 - A2;             
    actualBodyFat = weight - B;   // Should be `bodyWeight`?
    bodyFatPercentage = actualBodyFat * 100 / weight;   // Should be `bodyWeight`?                   

Upvotes: 0

Crusha K. Rool
Crusha K. Rool

Reputation: 1502

You are not actually using your method parameters in your calculations.

Here would be the fixed version of your first method:

//METHOD 1: Bodyfat calculations     
public static double bodyFatMale(int bodyWeight, int waistSize) {                          
    double A1;         
    double A2;         
    double B;         
    double actualBodyFat;         
    double bodyFatPercentage;                          
    A1 = (bodyWeight* 1.082) + 94.42;             
    A2 = waistSize* 4.15;             
    B = A1 - A2;             
    actualBodyFat = bodyWeight- B;             
    bodyFatPercentage = actualBodyFat * 100 / bodyWeight;                          

    return bodyFatPercentage;
}

Upvotes: 0

Kelsey Brennan
Kelsey Brennan

Reputation: 73

In each of your methods, you are setting all of your variables equal to 0, so the calculations inside the methods are being performed with 0s. Instead, you need to assign the variables to the values that you're passing in as parameters.

So instead of

int weight = 0;

try

int weight = bodyWeight;

Upvotes: 1

Stephen C
Stephen C

Reputation: 719739

Hint: In the bodyFat methods, you have two variables for the body Weight and waist size. And you are using the wrong one; i.e. the one that you have initialized to ero. You should only have one.

Upvotes: 0

Related Questions