Home > Software engineering >  Trying to do calculations with doubles and Math.sqrt and only getting 0
Trying to do calculations with doubles and Math.sqrt and only getting 0

Time:10-03

I am trying to make an equation that solves for tuning circuit frequency given three variables, but when I print my results, it is always 0.00.

The equations for circut frequency are: frequency=2pi/square root of (L x C) and C=square root of (Cmin x Cmax)

This is what I have.

//initialize variables
double frequency;
double Cmin;
double Cmax;
//Constructor
public TuningCircut(double freq, double Min, double Max){
   frequency = freq;
   Cmin = Min;
   Cmax = Max;
}
//L and C
double C = Math.sqrt(Cmin*Cmax);
double L = Math.pow((2*Math.PI)/frequency, 2)/C;
//methods 
public double getFMin(){
   return 2*Math.PI/Math.sqrt(L*Cmin);
}
public double getFMax(){
   return 2*Math.PI/Math.sqrt(L*Cmax);
}

and then in the tester I have

//Input the circut variables
    System.out.println("Input frequency of circut.");
    double freq = in.nextDouble();
    System.out.println("Input minimum capacitance.");
    double Min = in.nextDouble();
    System.out.println("Input maximum capacitance.");
    double Max = in.nextDouble();
    System.out.println("");
    //Create Circut object
    TuningCircut circut01 = new TuningCircut(freq, Min, Max);
//test methods
    System.out.printf("Min frequency is: %.2f" ,circut01.getFMin());
    System.out.println();
    System.out.printf("Max frequency is: %.2f" ,circut01.getFMax());
    System.out.println();

This is my first coding class so I am probably just overlooking something simple. Any help would be appreciated

CodePudding user response:

C and L are initialized before the constructor body. Change it to something like

public TuningCircut(double freq, double Min, double Max){
   frequency = freq;
   Cmin = Min;
   Cmax = Max;
   C = Math.sqrt(Cmin*Cmax);
   L = Math.pow((2*Math.PI)/frequency, 2)/C;
}
//L and C
double C;
double L;
  •  Tags:  
  • java
  • Related