Lab 4 BMI Application
Select PoliteMall when prompt to Login so as to watch the video
Discussion
Click on the picture below to see a zoom-in view
Discussion on the codings
//Step1: Binding java object to xml object//Java //XML GUI components etWeight = (EditText) findViewById(R.id.etWeight);
etHeight = (EditText) findViewById(R.id.etHeight);btnCalculate = (Button) findViewById(R.id.btnCalculate); tvResult = (TextView) findViewById(R.id.tvResult);
//Step2: Add a listener to the button
btnCalculate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
//Step3 : get the height and weight entered by the user using getText String height = etHeight.getText().toString(); String weight = etWeight.getText().toString();
//Step 4 : convert the height and weight from String to double double h = Double.parseDouble(height); double w = Double.parseDouble(weight);
//Step 5 : Calculate the bmidouble bmi= w/h/h;
//Step 6 : show the bmi result to the TextView using setTexttvResult.setText("BMI is "+bmi); }
});To express in 2 Decimal places
double number = 22.12345678; DecimalFormat twoDForm = new DecimalFormat("#.##");
double amount = Double.valueOf(twoDForm.format(number));https://onlinegdb.com/hcvwLSwgX
import java.text.DecimalFormat; public class Main { public static void main(String[] args) { double number = 22.12845678; System.out.println("The number is "+number); //The number is 22.12845678 DecimalFormat twoDForm = new DecimalFormat("#.##"); number = Double.valueOf(twoDForm.format(number)); System.out.println("The number is "+number);//The number is 22.13
} }
if we used System.out.println("The number is "+number);
the result will appear in the logcat as shown below:


Comments
Post a Comment