com.github.wkapga.bmicalc.BmicalcActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.github.wkapga.bmicalc.BmicalcActivity.java

Source

/*
    
Copyright (c) 2011 by Gabriel Panzenboeck 
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    
    
*/

package com.github.wkapga.bmicalc;

// http://www.a-g-a.de/Leitlinies2.pdf

import java.text.DecimalFormat;
import java.text.NumberFormat;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import com.github.wkapga.bmicalc.R;
import org.apache.commons.math.MathException;
// import org.apache.commons.math.distribution.NormalDistribution;
import org.apache.commons.math.distribution.NormalDistributionImpl;

public class BmicalcActivity extends Activity implements OnClickListener {

    protected Button CalcLabel;
    protected TextView ErgebnisLabel;
    protected EditText HeightLabel;
    protected EditText WeightLabel;
    protected EditText AgeLabel;
    protected RadioButton MaleLabel;

    protected double[] avalues;
    protected double[] lvalues;
    protected double[] svalues;
    protected double[] pvalues;
    protected double sds;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        CalcLabel = (Button) findViewById(R.id.calc_bmi);
        ErgebnisLabel = (TextView) findViewById(R.id.et_result);
        HeightLabel = (EditText) findViewById(R.id.et_height);
        WeightLabel = (EditText) findViewById(R.id.et_weight);
        AgeLabel = (EditText) findViewById(R.id.et_age);
        MaleLabel = (RadioButton) findViewById(R.id.radio_male);

        CalcLabel.setOnClickListener(this);

        /*        String s2 =  Double.toString(avalues[8])+Double.toString(pvalues[8])+Double.toString(lvalues[8])+Double.toString(svalues[8]);
                ErgebnisLabel.setText(s2);
        */

        HeightLabel.setText("105");
        WeightLabel.setText("16.5");
        AgeLabel.setText("4.5");
        MaleLabel.setChecked(true);
    }

    public void onClick(View v) {
        if (v == CalcLabel) {
            String s1 = HeightLabel.getText().toString();
            Double h = Double.parseDouble(s1);

            String s2 = WeightLabel.getText().toString();
            Double w = Double.parseDouble(s2);

            Double age = Double.parseDouble(AgeLabel.getText().toString());

            Boolean sex = MaleLabel.isChecked(); // male = 1

            h = h / 100;

            Double bmi = w / (h * h);

            // l,s,m
            // Double sds = ((bmi/m)^l-1) / ( l *s)
            // perzentil = kum.stdnormvert(sds)

            //  double perc = cumulativeProbability(double -2,02);

            NumberFormat formatter = new DecimalFormat(".00");
            String s3 = formatter.format(bmi);

            String s4, s5;

            NumberFormat formatter2 = new DecimalFormat(".0");

            try {
                s4 = formatter2.format(getperc(age, bmi, sex));
            } catch (MathException e) {
                // TODO Auto-generated catch block
                s4 = " na";
                e.printStackTrace();
            }

            if (sex == true) {
                s5 = getString(R.string.boys);
            } else {
                s5 = getString(R.string.girls);
            }

            ErgebnisLabel.setText(getString(R.string.result1) + " " + s3 + " " + getString(R.string.result2) + "\n"
                    + s4 + " " + getString(R.string.result3) + " " + s5 + " " + getString(R.string.result4));

        }
    }

    public double getperc(double age, double bmi, boolean sex) throws MathException {

        String[] perc;
        Resources res = getResources();
        Double s;
        Double p;

        if (sex == true) {
            perc = res.getStringArray(R.array.boys);
        } else {
            perc = res.getStringArray(R.array.girls);
        }

        int n = perc.length;

        Double[] avalues = new Double[n];
        Double[] lvalues = new Double[n];
        Double[] svalues = new Double[n];
        Double[] pvalues = new Double[n];

        for (int i = 0; i < n; i++) {

            String x1 = (perc[i]).toString();
            String x[] = x1.split(" ");
            avalues[i] = Double.parseDouble(x[0].toString());
            lvalues[i] = Double.parseDouble(x[1].toString());
            svalues[i] = Double.parseDouble(x[2].toString());
            pvalues[i] = Double.parseDouble(x[6].toString());

        }

        age = Math.min(age, (double) 18.0);

        int i = (int) Math.round(age * 2.) - 1; // casting

        s = (Math.pow(bmi / pvalues[i], lvalues[i]) - 1) / (lvalues[i] * svalues[i]);

        // Double sds = ((bmi/m)^l-1) / ( l *s)
        // perzentil = kum.stdnormvert(sds)

        //  double perc = cumulativeProbability(double -2,02);
        p = new NormalDistributionImpl(0, 1).cumulativeProbability(s, 20);
        p = p * 100;
        return p;
    }
}