Android Open Source - MathApp Activity Sqrt






From Project

Back to project page MathApp.

License

The source code is released under:

MIT License

If you think the Android project MathApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ie.lc.mathApp;
// www.  j  a v a 2s  .c  om
import ie.lc.R;
import android.os.Bundle;
import android.view.Menu;
import android.widget.*;





public class ActivitySqrt extends ActivityGameBase
{
  private double gameRoot;
  private double gameLow;
  private double gameHigh;
  private double gameUserInput;

  
  
  
  
  protected void onSaveInstanceState( Bundle out ) {
    super.onSaveInstanceState( out );
    out.putDouble( "gameRoot",      gameRoot      );
    out.putDouble( "gameLow",       gameLow       );
    out.putDouble( "gameHigh",      gameHigh      );
    out.putDouble( "gameUserInput", gameUserInput );
  }
  
  
  
  
  
  protected void onRestoreInstanceState( Bundle in ) {
    super.onRestoreInstanceState( in );
    gameRoot      = in.getDouble( "gameRoot"      );
    gameLow       = in.getDouble( "gameLow"       );
    gameHigh      = in.getDouble( "gameHigh"      );
    gameUserInput = in.getDouble( "gameUserInput" );
    updateUI();
  }





  protected void onCreate( Bundle savedInstanceState ) {
    super.onCreate( savedInstanceState, R.layout.activity_sqrt );
  }
    
    
    
    
    
    protected void setupGameActions() {
    setupAnswerActions(
      R.id.sqrtFieldAnswer,
      R.id.sqrtButtAnswer
    );
    
    setupSeekBar();
  }
  
  
  
  
  
  private void setupSeekBar() {
    final SeekBar seek = (SeekBar) findViewById( R.id.sqrtSeek );
    
    seek.setMax( 1024 );
    
    seek.setOnSeekBarChangeListener( new SeekBarAdapter() {
      public void onProgressChanged( SeekBar seekBar, int progress, boolean fromUser ) {
        onSeek( progress / (double) seek.getMax() );
      }
    });
  }
  
  
  
  
  
  private void onSeek( double frac ) {
    EditText fieldAnswer = (EditText) findViewById( R.id.sqrtFieldAnswer );
    double   value       = Geo.lerp( gameLow, gameHigh, frac );
    
      gameUserInput = Geo.roundOnThresh( value, 0.2 );
    fieldAnswer.setText( formatDecimal(gameUserInput) );
  }
  
  
  
  
  
  protected void setupNavActions() {
    setupNavActions(
      R.id.sqrtButtWave,       ActivityWave      .class,
      R.id.sqrtButtArithmetic, ActivityArithmetic.class
    );
  }
  
  
  
  
  
  protected void updateUI() {
    double square = gameRoot * gameRoot;
    
    EditText fieldAnswer  = (EditText) findViewById( R.id.sqrtFieldAnswer   );
    TextView textQuestion = (TextView) findViewById( R.id.sqrtTextQuestion  );
    TextView textLow      = (TextView) findViewById( R.id.sqrtTextSeekLeft  );
    TextView textHigh     = (TextView) findViewById( R.id.sqrtTextSeekRight );
    
    textQuestion.setText( "Root of " + (int) square + " = ?" );
    textLow     .setText( "" + (int) gameLow  );
    textHigh    .setText( "" + (int) gameHigh );
    fieldAnswer .setText( "" );
  }
  
  
  
  
  
  protected void gameReset() {
       gameRoot = Util.randomIntRange( 2, 12+1 );
       gameLow  = gameRoot - Util.randomIntRange( 0, 8 );
       gameHigh = gameRoot + Util.randomIntRange( 1, 8 );
       
       updateUI();
  }
  
  
  
  
  
  protected boolean isAnswerCorrect() {
    return gameUserInput == gameRoot;
  }
}




Java Source Code List

ie.lc.mathApp.ActivityArithmetic.java
ie.lc.mathApp.ActivityCommonMenu.java
ie.lc.mathApp.ActivityGameBase.java
ie.lc.mathApp.ActivityScore.java
ie.lc.mathApp.ActivitySqrt.java
ie.lc.mathApp.ActivityWave.java
ie.lc.mathApp.CallbackThread.java
ie.lc.mathApp.Callback.java
ie.lc.mathApp.Geo.java
ie.lc.mathApp.Operator.java
ie.lc.mathApp.ScoreData.java
ie.lc.mathApp.Score.java
ie.lc.mathApp.SeekBarAdapter.java
ie.lc.mathApp.Storage.java
ie.lc.mathApp.TextWatcherAdapter.java
ie.lc.mathApp.Util.java
ie.lc.mathApp.Wave.java