Gabriel Colón
Gabriel Colón

Reputation: 21

My parent's class method is not receiving my instance variable value

I'm doing a fat percentage calculator and I'm extending the men's calculator since it has basically the same functionality. What happens is I'm not adding the fat calculating method from the women's class (parent) to try and save some code, but when I create an instance of the men's class and use the fat method, it does not receive the value that I got from a method in the men's class. If you can explain why I will really appreciate it.


public class DC_Calculator_Women implements DC_Fat_Calculator{
   private int age;
   private int total_Pliegues;
   private double DC;
   protected double fatPercentage;
   public DC_Calculator_Women(int age, int total_Pliegues) {
       this.age = age;
       this.total_Pliegues = total_Pliegues;

   }
   public double calculateDC() {
       // •    Mujeres: Densidad Corporal (DC) = 1.0994921 – (0.0009929 × Suma Pliegues) + (0.0000023 × Suma Pliegues al Cuadrado) – (0.0001392 × Edad)

       DC = 1.0994921 - (0.0009929 * total_Pliegues) + (0.0000023 * (total_Pliegues * total_Pliegues)) - (0.0001392 * age);
       return DC;

   }
   public double calculateFat() {
       // •    (%G) = [(4.95/Densidad Corporal) – 4.5] × 100
       fatPercentage = ((4.95 / DC) - 4.5) * 100;
       System.out.println(DC);
       return fatPercentage;
   }


}

public class DC_Calculator_Men extends DC_Calculator_Women{
   private int age;
   private int total_Pliegues;
   private double DC;

   public DC_Calculator_Men(int age, int total_Pliegues) {
       super(age, total_Pliegues);
       this.age = age;
       this.total_Pliegues = total_Pliegues;
   }

   @Override
   public double calculateDC() {
       // •    Hombres: Densidad Corporal (DC) = 1.1093800 – (0.0008267 × Suma Pliegues) + (0.0000016 × Suma Pliegues al Cuadrado) – (0.0002574 × Edad)
       DC =  1.1093800 - (0.0008267 * total_Pliegues) + (0.0000016 * (total_Pliegues * total_Pliegues) - (0.0002574 * age));
       return DC;
   }

}


public class Main {
   public static void main(String[] args) {
       double DC;
       double fat;

       // Women
       DC_Calculator_Women Wtest1 = new DC_Calculator_Women(25, 51);
       DC = Wtest1.calculateDC();
       fat = Wtest1.calculateFat();

       System.out.printf("Women (DC): %5.2f\n", DC);
       System.out.printf("Women (fat): %5.2f\n", fat);

       // Men
       DC_Calculator_Men Mtest1 = new DC_Calculator_Men(35, 40);
       DC = Mtest1.calculateDC(); // **** DC in this method counts as 0 ***
       fat = Mtest1.calculateFat();

       System.out.printf("Men (DC): %5.2f\n", DC);
       System.out.printf("Men (fat): %5.2f\n", fat);

   }
}


Upvotes: 0

Views: 62

Answers (2)

Aditya Rewari
Aditya Rewari

Reputation: 2707

BUG : The value of DC being being calculated at child(men) class, was not accessible at parent(women) class.

So, every time, the child class, was using the Parent's class calculateFat(), the value of DC variable being referred was of Parent class(as uninitialized, this was 0.0)

Solution : I have created a setter method setWomenDC(double DC), to set value of DC at parent(women) class . This will be used by child(men) class, while calculating the value of DC for itself. Using this, it will set the same value for DC in parent(women) class.

Add this method to DC_Calculator_Women class

//add this method to receive DC value from child(men) class
public void setWomenDC(double DC){
    this.DC = DC;
}

Edit calculateDC() method at DC_Calculator_Men class

@Override
public double calculateDC() {
    // •    Hombres: Densidad Corporal (DC) = 1.1093800 – (0.0008267 × Suma Pliegues) + (0.0000016 × Suma Pliegues al Cuadrado) – (0.0002574 × Edad)
    DC =  1.1093800 - (0.0008267 * total_Pliegues) + (0.0000016 * (total_Pliegues * total_Pliegues) - (0.0002574 * age));
    setWomenDC(DC); //CHANGED HERE: pass the value DC to parent class
    return DC;
}   

Solution No.2

make the access modifier of DC at DC_Calculator_Women class as protected

And, remove the DC variable declaration from DC_Calculator_Men class

Now, run the program

Here, we are making the child class use variable of Parent class.

Upvotes: 1

Tapan Pandya
Tapan Pandya

Reputation: 114

Mtest1.calculateFat() calls calculatFat method in DC_Calculator_Women class. However, DC variable in DC_Calculator_Women class is initialized to 0 by default while calling constructor to DC_Calculator_Men class (DC_Calculator_Men Mtest1 = new DC_Calculator_Men(35, 40);). This causes fatPercentage = ((4.95 / DC) - 4.5) * 100; to return infinity (4.95 / DC = infinity).

You should put proper checks in your code to prevent divide by 0 and consider passing DC as a method parameter or call calculateDC in calculateFat method. In fact there are many issues in code like bad inheritance design. variable names etc. But not going into all this here.

Upvotes: 0

Related Questions