Advertisement
mmayoub

school, Math Trainer, 19+26.11.2017

Nov 19th, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.45 KB | None | 0 0
  1. activity_main.xml
  2. -----------------
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="match_parent"
  6.     android:orientation="vertical">
  7.  
  8.     <LinearLayout
  9.         android:layout_width="match_parent"
  10.         android:layout_height="wrap_content"
  11.         android:orientation="horizontal">
  12.  
  13.         <TextView
  14.             android:id="@+id/tvRightCounter"
  15.             android:layout_width="match_parent"
  16.             android:layout_height="wrap_content"
  17.             android:layout_margin="16dp"
  18.             android:layout_weight="1"
  19.             android:background="#00FF00"
  20.             android:gravity="center"
  21.             android:text="0"
  22.             android:textColor="#FFFFFF"
  23.             android:textSize="30sp" />
  24.  
  25.         <TextView
  26.             android:id="@+id/tvWrongCounter"
  27.             android:layout_width="match_parent"
  28.             android:layout_height="wrap_content"
  29.             android:layout_margin="16dp"
  30.             android:layout_weight="1"
  31.             android:background="#FF0000"
  32.             android:gravity="center"
  33.             android:text="0"
  34.             android:textColor="#FFFFFF"
  35.             android:textSize="30sp" />
  36.     </LinearLayout>
  37.  
  38.     <TextView
  39.         android:id="@+id/tvNumber1"
  40.         android:layout_width="match_parent"
  41.         android:layout_height="wrap_content"
  42.         android:layout_margin="16dp"
  43.         android:background="#0009ff"
  44.         android:gravity="center"
  45.         android:textColor="#FFFFFF"
  46.         android:textSize="30sp" />
  47.  
  48.     <TextView
  49.         android:id="@+id/tvOperation"
  50.         android:layout_width="match_parent"
  51.         android:layout_height="wrap_content"
  52.         android:layout_margin="16dp"
  53.         android:background="#f2adad"
  54.         android:gravity="center"
  55.         android:textSize="30sp" />
  56.  
  57.     <TextView
  58.         android:id="@+id/tvNumber2"
  59.         android:layout_width="match_parent"
  60.         android:layout_height="wrap_content"
  61.         android:layout_margin="16dp"
  62.         android:background="#0009ff"
  63.         android:gravity="center"
  64.         android:textColor="#FFFFFF"
  65.         android:textSize="30sp" />
  66.  
  67.     <EditText
  68.         android:id="@+id/etResult"
  69.         android:layout_width="match_parent"
  70.         android:layout_height="wrap_content"
  71.         android:layout_margin="16dp"
  72.         android:background="#0FFFF0"
  73.         android:gravity="center"
  74.         android:hint="Enter your answer"
  75.         android:inputType="numberSigned"
  76.         android:textSize="30sp" />
  77.  
  78.     <Button
  79.         android:id="@+id/btnCheck"
  80.         android:layout_width="match_parent"
  81.         android:layout_height="wrap_content"
  82.         android:text="Check"
  83.         android:textSize="30sp" />
  84. </LinearLayout>
  85.  
  86.  
  87. MainActivity.java
  88. -----------------
  89. package com.example.mohamadpc.mmathapplication;
  90.  
  91. import android.content.Intent;
  92. import android.os.Bundle;
  93. import android.support.v7.app.AppCompatActivity;
  94. import android.view.View;
  95. import android.widget.Button;
  96. import android.widget.EditText;
  97. import android.widget.TextView;
  98. import android.widget.Toast;
  99.  
  100. import java.util.Random;
  101.  
  102. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  103.     TextView tvRightCounter, tvWrongCounter;
  104.     TextView tvNumber1, tvNumber2, tvOperation;
  105.     EditText etResult;
  106.     Button btnCheck;
  107.     Random rnd = new Random();
  108.  
  109.     int rightAnswer;
  110.     int rCount = 0, wCount = 0;
  111.     int more = 10;
  112.  
  113.     @Override
  114.     protected void onCreate(Bundle savedInstanceState) {
  115.         super.onCreate(savedInstanceState);
  116.         setContentView(R.layout.activity_main);
  117.  
  118.         connectToLayout();
  119.         showEx();
  120.     }
  121.  
  122.     private void showEx() {
  123.         int n1 = rnd.nextInt(10);
  124.         int n2 = rnd.nextInt(10);
  125.         int op = rnd.nextInt(5); // +, -, *, /, ^
  126.  
  127.  
  128.         switch (op) {
  129.             case 0:
  130.                 tvOperation.setText("+");
  131.                 rightAnswer = n1 + n2;
  132.                 break;
  133.             case 1:
  134.                 tvOperation.setText("-");
  135.                 if (n1 < n2) {
  136.                     int temp = n2;
  137.                     n2 = n1;
  138.                     n1 = temp;
  139.                 }
  140.                 // n1>=n2 : for sure
  141.                 rightAnswer = n1 - n2;
  142.                 break;
  143.             case 2:
  144.                 tvOperation.setText("*");
  145.                 rightAnswer = n1 * n2;
  146.                 break;
  147.             case 3:
  148.                 tvOperation.setText("/");
  149.                 while (n2 != 0 && n1 % n2 != 0) {
  150.                     n1 = rnd.nextInt(10);
  151.                     n2 = rnd.nextInt(10);
  152.                 }
  153.                 rightAnswer = n1 / n2;
  154.                 break;
  155.             case 4:
  156.                 tvOperation.setText("^");
  157.                 rightAnswer = (int) Math.pow(n1, n2);
  158.                 break;
  159.         }
  160.         tvNumber1.setText(n1 + "");
  161.         tvNumber2.setText(n2 + "");
  162.         etResult.setText("");
  163.  
  164.         more -= 1;
  165.     }
  166.  
  167.     private void connectToLayout() {
  168.         tvRightCounter = (TextView) findViewById(R.id.tvRightCounter);
  169.         tvWrongCounter = (TextView) findViewById(R.id.tvWrongCounter);
  170.  
  171.         tvNumber1 = (TextView) findViewById(R.id.tvNumber1);
  172.         tvOperation = (TextView) findViewById(R.id.tvOperation);
  173.         tvNumber2 = (TextView) findViewById(R.id.tvNumber2);
  174.         etResult = (EditText) findViewById(R.id.etResult);
  175.         btnCheck = (Button) findViewById(R.id.btnCheck);
  176.  
  177.         btnCheck.setOnClickListener(this);
  178.     }
  179.  
  180.     @Override
  181.     public void onClick(View v) {
  182.         String text = etResult.getText().toString();
  183.         int answer = Integer.parseInt(text);
  184.  
  185.         if (answer == rightAnswer) {
  186.             Toast.makeText(this, "Good Answer", Toast.LENGTH_LONG).show();
  187.             rCount += 1;
  188.             tvRightCounter.setText(rCount + "");
  189.             if (more > 0) {
  190.                 showEx();
  191.             } else {
  192.                 // start Grade Activity
  193.                 double grade = ((double) rCount / (rCount + wCount)) * 100;
  194.                 grade = ((int) (grade * 10)) / 10.0;
  195.                 Intent i = new Intent(this, GradeActivity.class);
  196.                 i.putExtra("PlayerGrade", grade);
  197.                 startActivity(i);
  198.                 finish();
  199.             }
  200.  
  201.         } else {
  202.             Toast.makeText(this, "Try again !", Toast.LENGTH_LONG).show();
  203.             wCount += 1;
  204.             tvWrongCounter.setText(wCount + "");
  205.         }
  206.     }
  207. }
  208.  
  209.  
  210.  
  211. activity_grade.xml
  212. ------------------
  213. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  214.     android:layout_width="match_parent"
  215.     android:layout_height="match_parent"
  216.  
  217.  
  218.     android:orientation="vertical">
  219.  
  220.     <TextView
  221.         android:id="@+id/tvGrade"
  222.         android:layout_width="match_parent"
  223.         android:layout_height="match_parent"
  224.         android:layout_weight="1"
  225.         android:background="#e1d158"
  226.         android:gravity="center"
  227.         android:text="Your Grade is 75%"
  228.         android:textColor="#f20df2"
  229.         android:textSize="80sp" />
  230.  
  231.     <Button
  232.         android:id="@+id/btnPlayAgain"
  233.         android:layout_width="match_parent"
  234.         android:layout_height="wrap_content"
  235.         android:text="Play Again"
  236.         android:textSize="30sp" />
  237. </LinearLayout>
  238.  
  239. GradeActivity.java
  240. ------------------
  241. package com.example.mohamadpc.mmathapplication;
  242.  
  243. import android.content.Intent;
  244. import android.os.Bundle;
  245. import android.support.v7.app.AppCompatActivity;
  246. import android.view.View;
  247. import android.widget.Button;
  248. import android.widget.TextView;
  249.  
  250. public class GradeActivity extends AppCompatActivity implements View.OnClickListener {
  251.     TextView tvGrade;
  252.     Button btnPlayAgain;
  253.  
  254.     @Override
  255.     protected void onCreate(Bundle savedInstanceState) {
  256.         super.onCreate(savedInstanceState);
  257.         setContentView(R.layout.activity_grade);
  258.  
  259.         // get grade from previous activity
  260.         Intent i = getIntent();
  261.         double grade = i.getDoubleExtra("PlayerGrade", 0);
  262.  
  263.         // show grade
  264.         tvGrade = (TextView) findViewById(R.id.tvGrade);
  265.         tvGrade.setText("Your Grade Is " + grade + " %");
  266.  
  267.         btnPlayAgain = findViewById(R.id.btnPlayAgain);
  268.         btnPlayAgain.setOnClickListener(this);
  269.     }
  270.  
  271.     @Override
  272.     public void onClick(View v) {
  273.         Intent intent = new Intent(this, MainActivity.class);
  274.         startActivity(intent);
  275.         finish();
  276.     }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement