--snipimport androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initButton();
}
private void initButton()
{
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText rad = (EditText) findViewById(R.id.editTextRadius);
double r = Double.parseDouble(rad.getText().toString());
if (r <= 0)
r = r * -1;
Circle circ= new Circle(r);
TextView textViewA = (TextView) findViewById(R.id.textViewArea);
TextView textViewC = (TextView) findViewById(R.id.textViewCircumference);
textViewA.setText(String.format("Area: %.2f", circ.getArea()));
textViewC.setText(String.format("Circumference: %.2f", circ.getCircumference()));
}
});
}
}
package ... //this is the first line in your Circle.java file
public class Circle { double radius; public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } public Circle(double radius){ this.radius = radius; } public double getArea(){ return Math.PI * radius * radius; } public double getCircumference(){ return 2 * Math.PI * radius; } }
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.