Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- activity_main.xml
- -----------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <TextView
- android:id="@+id/tvRightCounter"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="16dp"
- android:layout_weight="1"
- android:background="#00FF00"
- android:gravity="center"
- android:text="0"
- android:textColor="#FFFFFF"
- android:textSize="30sp" />
- <TextView
- android:id="@+id/tvWrongCounter"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="16dp"
- android:layout_weight="1"
- android:background="#FF0000"
- android:gravity="center"
- android:text="0"
- android:textColor="#FFFFFF"
- android:textSize="30sp" />
- </LinearLayout>
- <TextView
- android:id="@+id/tvNumber1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="16dp"
- android:background="#0009ff"
- android:gravity="center"
- android:textColor="#FFFFFF"
- android:textSize="30sp" />
- <TextView
- android:id="@+id/tvOperation"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="16dp"
- android:background="#f2adad"
- android:gravity="center"
- android:textSize="30sp" />
- <TextView
- android:id="@+id/tvNumber2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="16dp"
- android:background="#0009ff"
- android:gravity="center"
- android:textColor="#FFFFFF"
- android:textSize="30sp" />
- <EditText
- android:id="@+id/etResult"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="16dp"
- android:background="#0FFFF0"
- android:gravity="center"
- android:hint="Enter your answer"
- android:inputType="numberSigned"
- android:textSize="30sp" />
- <Button
- android:id="@+id/btnCheck"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Check"
- android:textSize="30sp" />
- </LinearLayout>
- MainActivity.java
- -----------------
- package com.example.mohamadpc.mmathapplication;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.Random;
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- TextView tvRightCounter, tvWrongCounter;
- TextView tvNumber1, tvNumber2, tvOperation;
- EditText etResult;
- Button btnCheck;
- Random rnd = new Random();
- int rightAnswer;
- int rCount = 0, wCount = 0;
- int more = 10;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- connectToLayout();
- showEx();
- }
- private void showEx() {
- int n1 = rnd.nextInt(10);
- int n2 = rnd.nextInt(10);
- int op = rnd.nextInt(5); // +, -, *, /, ^
- switch (op) {
- case 0:
- tvOperation.setText("+");
- rightAnswer = n1 + n2;
- break;
- case 1:
- tvOperation.setText("-");
- if (n1 < n2) {
- int temp = n2;
- n2 = n1;
- n1 = temp;
- }
- // n1>=n2 : for sure
- rightAnswer = n1 - n2;
- break;
- case 2:
- tvOperation.setText("*");
- rightAnswer = n1 * n2;
- break;
- case 3:
- tvOperation.setText("/");
- while (n2 != 0 && n1 % n2 != 0) {
- n1 = rnd.nextInt(10);
- n2 = rnd.nextInt(10);
- }
- rightAnswer = n1 / n2;
- break;
- case 4:
- tvOperation.setText("^");
- rightAnswer = (int) Math.pow(n1, n2);
- break;
- }
- tvNumber1.setText(n1 + "");
- tvNumber2.setText(n2 + "");
- etResult.setText("");
- more -= 1;
- }
- private void connectToLayout() {
- tvRightCounter = (TextView) findViewById(R.id.tvRightCounter);
- tvWrongCounter = (TextView) findViewById(R.id.tvWrongCounter);
- tvNumber1 = (TextView) findViewById(R.id.tvNumber1);
- tvOperation = (TextView) findViewById(R.id.tvOperation);
- tvNumber2 = (TextView) findViewById(R.id.tvNumber2);
- etResult = (EditText) findViewById(R.id.etResult);
- btnCheck = (Button) findViewById(R.id.btnCheck);
- btnCheck.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- String text = etResult.getText().toString();
- int answer = Integer.parseInt(text);
- if (answer == rightAnswer) {
- Toast.makeText(this, "Good Answer", Toast.LENGTH_LONG).show();
- rCount += 1;
- tvRightCounter.setText(rCount + "");
- if (more > 0) {
- showEx();
- } else {
- // start Grade Activity
- double grade = ((double) rCount / (rCount + wCount)) * 100;
- grade = ((int) (grade * 10)) / 10.0;
- Intent i = new Intent(this, GradeActivity.class);
- i.putExtra("PlayerGrade", grade);
- startActivity(i);
- finish();
- }
- } else {
- Toast.makeText(this, "Try again !", Toast.LENGTH_LONG).show();
- wCount += 1;
- tvWrongCounter.setText(wCount + "");
- }
- }
- }
- activity_grade.xml
- ------------------
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView
- android:id="@+id/tvGrade"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:background="#e1d158"
- android:gravity="center"
- android:text="Your Grade is 75%"
- android:textColor="#f20df2"
- android:textSize="80sp" />
- <Button
- android:id="@+id/btnPlayAgain"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Play Again"
- android:textSize="30sp" />
- </LinearLayout>
- GradeActivity.java
- ------------------
- package com.example.mohamadpc.mmathapplication;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class GradeActivity extends AppCompatActivity implements View.OnClickListener {
- TextView tvGrade;
- Button btnPlayAgain;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_grade);
- // get grade from previous activity
- Intent i = getIntent();
- double grade = i.getDoubleExtra("PlayerGrade", 0);
- // show grade
- tvGrade = (TextView) findViewById(R.id.tvGrade);
- tvGrade.setText("Your Grade Is " + grade + " %");
- btnPlayAgain = findViewById(R.id.btnPlayAgain);
- btnPlayAgain.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(this, MainActivity.class);
- startActivity(intent);
- finish();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement