Chris
Chris

Reputation: 21

How do I compute area, perimeter, & in radius with Views

So, I'm creating an application whose main purpose is to compute the area, perimeter, and inradius of a triangle. The user is supposed to enter three values that are of type double. & whichever button they click should spit out the answer in the result view. The user can click any button (it doesn't matter which). If they click the "area" button first & then decide to click the "perimeter" button, the result should change. Here is a picture to give you an idea of what it looks like: (http://tinypic.com/r/v3ncyh/8). I'm not sure if I am programming this correctly because my values look completely incorrect & I do not think my Views are connected correctly to my code. The first section of this code is my Triangle class that contains all my getters/setters & my methods for computing the area, perimeter, and inradius. The second section is my Activity which is the main (& only) Activity for my application.

public class Triangle {

// instance variables
private double sideA;
private double sideB;
private double sideC;

// Getters and setters
public double getSideA() {
    return sideA;
}

public void setSideA(double sideA) {
    this.sideA = sideA;
}

public double getSide2B() {
    return sideB;
}

public void setSideB(double sideB) {
    this.sideB = sideB;
}

public double getSideC() {
    return sideC;
}

public void setSideC(double sideC) {
    this.sideC = sideC;
}

// Constructor
public Triangle(double sideA, double sideB, double sideC) {
    this.sideA = sideA;
    this.sideB = sideB;
    this.sideC = sideC;
}

// Three methods...
public double getPerimeter() {

    return sideA + sideB + sideC;
}

public double getArea() {

    double s = 1/2 * (sideA + sideB + sideC);
    double area = Math.sqrt(s*(s-sideA)*(s-sideB)*(s-sideC));

    return area;
}

public double getInRadius() {

    double r = ((-sideA + sideB + sideC)*(sideA - sideB + sideC)*(sideA + sideB - sideC))/(4*(sideA + sideB + sideC));
    double answer = Math.sqrt(r);

    return r;
}

}

public class TriangleComputationActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_triangle_computation);

    // Declare our View variables and assign them the Views from the layout file
    final TextView lengthOne = (TextView) findViewById(R.id.lengthOne);
    final TextView lengthTwo = (TextView) findViewById(R.id.lengthTwo);
    final TextView lengthThree = (TextView) findViewById(R.id.lengthThree);
    final TextView result = (TextView) findViewById(R.id.result);

    Button areaButton = (Button) findViewById(R.id.area);
    Button perimeterButton = (Button) findViewById(R.id.perimeter);
    Button inradiusButton = (Button) findViewById(R.id.inradius);

    final Triangle triangle = new Triangle((double)lengthOne.getInputType(), (double)lengthTwo.getInputType(),(double)lengthThree.getInputType());

    // Methods in Action...
    areaButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            result.setText(triangle.getArea());

        }
    });

    perimeterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            result.setText(triangle.getPerimeter());
        }
    });

    inradiusButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            result.setText(triangle.getInRadius());
        }
    });

}

Upvotes: 2

Views: 105

Answers (1)

Karakuri
Karakuri

Reputation: 38595

TextView.getInputType() does not return a value that is useful to you. It has nothing to do with the actual text of the TextView.

You should be using TextView.getText() instead. You will have to convert this to a double using Double.parseInt() or Double.valueOf(). If it were me, I would set android:inputType="numberDecimal" on those TextViews.

Upvotes: 1

Related Questions