Solving equations

In this third recipe, we will create a new activity responsible for displaying an equation and checking the answer.

How to do it...

We will now see how to solve the equations:

  1. Create a new activity named EquationActivity and make it look like the following:
    namespace TheInternship {
      [Activity (Label = "EquationActivity")]
      public class EquationActivity : Activity {
    
        Dictionary<String, int> equations = new Dictionary<string, int> ();
        KeyValuePair<String, int> current_equation;
    
        protected override void OnCreate (Bundle bundle) {
          base.OnCreate (bundle);
    
          SetContentView (Resource.Layout.Equation);
    
          // Add some basic equations in our Dictionary of equation
          equations.Add ("2+2", 4);
          equations.Add ("4/2", 2);
          equations.Add ("3*3", 9);
          equations.Add ("3*25", 75);
    
          //Select one equation
          changeEquation ();
    
          // Check the response of the equation on click via a delegate
          Button button = FindViewById<Button> (Resource.Id.send_eq);
          button.Click += delegate {
    
            // Convert the response to int32. We could use a try/catch here.
            if(Convert.ToInt32(FindViewById<TextView> (Resource.Id.response).Text) == current_equation.Value) {
              Console.WriteLine ("Answer is OK...");
            }
            else {
              Toast.MakeText (this, "Go to sleep!", ToastLength.Short).Show ();
            }
          };
    
        }
    
        private void changeEquation() {
    
          //Load a random equation from the Dictionary
          Random rand = new Random ();
          current_equation = equations.ElementAt (rand.Next (0, equations.Count));
          FindViewById<TextView> (Resource.Id.eq_text).Text = current_equation.Key;
        }
      }
    }
  2. Create a .axml file under the Resources/Layout folder named Equation.axml, and add the following code in order to construct a graphical interface with a TextView, TextInput, and button elements:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
      <TextView
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/eq_text" />
      <EditText
        android:inputType="number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/response" />
      <Button
        android:text="OK"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/send_eq" />
    </LinearLayout> 
  3. Launch your application. If you enter a wrong answer, a popup saying Go to sleep! will be prompted. The correct answer will print Answer is ok to the console:
    How to do it...

How it works...

In the previous code, we use the OnCreate() method of our newly created activity to create a list of equations and their answers:

Dictionary<String, int> equations = new Dictionary<string, int> ();
equations.Add ("2+2", 4);
equations.Add ("4/2", 2);
equations.Add ("3*3", 9);
equations.Add ("3*25", 75);

The Dictionary argument belongs to the System.Collections.Generic package and represents a list of keys and values, which fit our needs.

Then, we call the changeEquation() method in which we randomly select one of the equations and populate the graphical interface with it:

Random rand = new Random ();
current_equation = equations.ElementAt (rand.Next (0, equations.Count));
FindViewById<TextView> (Resource.Id.eq_text).Text = current_equation.Key; 

Finally, we add a delegate argument on the OK button and click and check whether the answer is correct:

Button button = FindViewById<Button> (Resource.Id.send_eq);
button.Click += delegate {

  // Convert the response to int32. We could use a try/catch here.
  if(Convert.ToInt32(FindViewById<TextView> (Resource.Id.response).Text) == current_equation.Value) {
    Console.WriteLine ("Answer is OK...");
  }
  else {
    Toast.MakeText (this, "Go to sleep!", ToastLength.Short).Show ();
  }
};

See also

Refer to the Sending an SMS recipe to see how to send an SMS using an intent, and finish The Internship application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.105.105