0

I created an app that calculates the area and circumference of a circle and then sends the results to the user. The only problem is that I have to place the calculations in a separate class from MainActivity.java. I currently have the calculations at the bottom of the MainActivity.java which is not what I want. Here is what I have so far:

package com.areacircumferencecircle;

import android.os.Bundle;
import android.app.Activity;
import android.widget.*;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {

private Button btn;
private EditText edit;
private TextView area;
private TextView crf;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button)findViewById(R.id.button1);
    edit = (EditText)findViewById(R.id.editText1);
    area = (TextView)findViewById(R.id.textArea);
    crf = (TextView)findViewById(R.id.textCircumference);
    btn.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View v) {
    if(btn == v){
        double r = Double.parseDouble(edit.getText().toString());
        String a = Double.toString(3.141592 * (r * r));
        String c = Double.toString(2 * 3.141592 *  r);
        crf.setText("Circumference: " + c);
        area.setText("Area: " + a);
    }

  }

}

Any help is very much appreciated!

Brian
  • 2,494
  • 1
  • 16
  • 21

2 Answers2

2

Create a class just for computing the result:

class Computations{
    public double area(double r){
        return 3.141592 * r * r;
    }
    public double circumference(double r){
        return 2 * 3.141592 *  r;
    }
}

Then, use it in onClick(View v){...}. If you expect this method to be called often, you may instantiate c outside the method and keep it as class variable.

public void onClick(View v) {
    if(btn == v){
        double r = Double.parseDouble(edit.getText().toString());
        Computations c = new Computations();
        crf.setText("Circumference: " + c.circumference(r));
        area.setText("Area: " + c.area(r));
    }
  }

And that's all!

alex
  • 10,900
  • 15
  • 70
  • 100
  • Ok I did exactly that except I get an error under "r" in "c.circumference(r)" stated "r cannot be resolved into a variable". I tried declaring r but only received more errors. – Brian Oct 18 '13 at 23:22
  • `double r = Double.parseDouble(edit.getText().toString());` would still be done in your activity before calling the `Computations` class – Morfic Oct 18 '13 at 23:46
  • Yes, I did that exactly and an error under "Computations c = new Computations();" popped up "No enclosing instance of type Circle is accessible. Must qualify the allocation with an enclosing instance of type Circle (e.g. x.new A() where x is an instance of Circle)." – Brian Oct 18 '13 at 23:59
  • 1
    Did you create the Computations class in a separate stand-alone file, or did you create it in an existing class? You might want to extract it to a Computations.java, otherwise you'd have to instantiate an inner class as described here: http://stackoverflow.com/questions/4070716/instantiating-inner-class. You'd probably want to read the java introduction to classes as well http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html – Morfic Oct 19 '13 at 00:18
  • @BrianPatino Grove is right - put `Computations` in another file in the project and then import it into `MainActivity`. – alex Oct 19 '13 at 00:20
  • Yes, at first I created it in a standalone file but then brought it under another class. Thank you for all the help! – Brian Oct 19 '13 at 00:25
1

Create a class named Circle

Have a private double to store the radius

Have the constructor accept a double

Implement the following methods:

public String area() // or areaStr(), if you also want public double area(), etc

public String circumference()

In MainActivity, create a Circle, retrieve its area and circumference, and so on.

Ray Stojonic
  • 1,260
  • 1
  • 7
  • 11
  • Ok I created a class named Circle and a private double to store the radius but how do I have the constructor accept a double? – Brian Oct 18 '13 at 22:49
  • Form a certain point of view, a constructor is pretty much like a normal method so it can accept parameters: `public Circle(double r){....}` – Morfic Oct 18 '13 at 23:48