我有一个主要类来运行BMI计算器类,它计算BMI信息(体重指数),其中包含姓名,年龄,性别,身高和体重的字段。 还有一个WaistToHip计算器类来计算腰部与臀部的比率 腰部和臀部。 但是,当我想创建一个 BodyFat计算器我需要两个班级的身高和腰围。 我怎么想在我的身体脂肪计算器类中为我的公式调用这些?
public class body_fat_calculation {
private double neck;
private double CBF;
waist_to_hip_ratio waist;
bmiCalculator height;
public body_fat_calculation(double neck) {
super();
this.neck = neck;
}
public double getCBF() {
return CBF;
}
public void setCBF(double cBF) {
CBF = cBF;
}
public double getNeck() {
return neck;
}
public void setNeck(double neck) {
this.neck = neck;
}
public double Round(double Rval, int Rpl){
double p = Math.pow(10, Rpl);
Rval=Rval*p;
double tmp = Math.round(Rval);
return tmp/p;
}
public void calculateWTHR(){
CBF= Round((495/(1.0324 - 0.19077 * Math.log10((waist)-(neck)) + 0.15456 * Math.log10(height)) - 450),2);
}
}
答案 0 :(得分:0)
你为什么不这样做?如果你注意到我在BodyFatCalculator
类中添加了两个参数 - 腰围和身高。
public class Main {
public static void main(string[] args){
// I assume you will want to use a scanner to get user input to set these variables dynamically
// for the sake of the example, I have set them myself.
double height = 1.82; // meters
double weight = 170.0;
double waist = 35.0;
double hip = 40.0;
double neck = 7.1;
String name = "Dave";
String sex = "M";
int age = 20;
// create new BMI Calculator to figure out body mass index
BMICalculator bmiCalc = new BMICalculator(name, age, sex, height, weight);
double bmi = bmiCalc.calculateBmi(); // calculate body mass index
WaistToHipCalculator waistHipCalc = new WaistToHipCalculator(waist, hip);
double whr = waistHipCalc.calculateWhr(); // calculate waist to hip ratio
BodyFatCalculator bfCalc = new BodyFatCalculator(neck, height, waist);
double bf = bfCalc.calculateBf(); // calculate body fat
// print results
}
}
答案 1 :(得分:0)
类是更多的名词。动词应该是一种方法。有一个名为BodyFatCalculation的类,我认为你很难使用很多类。只需将一个Body(名词)类与calculateBodyMassIndex和calculateBodyFat(动词)作为方法。您可以创建一个Dimensions类,其中包含高度和腰围(根据需要使用getter和setter)并在Body类中保留一个实例,但这样做太过分了。