0

I am trying to make a simple ballistic app that calculates bullet drop and time to target. I have everything in place where I need it, but when I test it and wait for my results it doesn't seems to do the calculations I have in there. Any suggestions for me?

package com.example.koryhershock.testapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_main);

    Button btn = (Button)findViewById(R.id.button1);
    final EditText et1 = (EditText)findViewById(R.id.muzzleVelocity);
    final EditText et2 = (EditText)findViewById(R.id.targetRange);
    final TextView time = (TextView)findViewById(R.id.textView1);
    final TextView bulletdrop = (TextView)findViewById(R.id.textView2);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int x = new Integer(et1.getText().toString());
            int y = new Integer(et2.getText().toString());
            double timetotarget = x / y;
            double grav = 4.9;
            double timesquared = timetotarget * timetotarget;
            double drop = grav * timesquared;
            time.setText("Time to Target: " + timetotarget + " seconds");
            bulletdrop.setText("Bullet Drop: " + drop + " meters");
        }
    });
}
}
Rohit Kharsan
  • 145
  • 1
  • 8
Kory Hershock
  • 107
  • 1
  • 1
  • 5
  • Could you put a log statement in the onClick method to verify that it's getting called? – Andrew Orobator Apr 19 '16 at 03:34
  • It is getting called, I set the values of time and bulletdrop to a different text to start, when I push calculate the text switches to "Time to Target:" etc, but it wont calculate. All it does is have 0 for all values. – Kory Hershock Apr 19 '16 at 03:37
  • 2
    Make `x` and `y` `double`s, instead of `int`s. – Mike M. Apr 19 '16 at 03:39
  • Awesome! Thanks a lot, it worked now. Only other question I would have with doubles is...how do I make it round to 2 decimal places instead of like 10 – Kory Hershock Apr 19 '16 at 03:44
  • http://stackoverflow.com/questions/8065114/how-to-print-a-double-with-two-decimals-in-android – Mike M. Apr 19 '16 at 03:58

1 Answers1

0

Ok, may be problem here: double timetotarget = x / y; because x and y is integer. for example if x 150 and y equals to 100 the result of your's calculatian equals to 1. try to use the next calculation: double timetotarget = (double)x / (double)y;

Konstantin
  • 174
  • 7