Tuesday, January 8, 2013

Android: Mini Math Game

For practicing purposes I made this program with humble intentions to be a Math game. It creates basic operations like sums, subtractions, multiplications and divisions, in a randomly fashion, along with the right answer and two incorrect answers. Each valid answers adds a point to the user.


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.mytoolbox.utilities.NumberUtilities;

public class MathRallyActivity extends Activity implements OnClickListener {
 
 public enum Operation {
  SUM, DIVISION, MULTIPLICATION, SUBSTRACT  
 }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_math_rally);
        
        Button buttonOption1 = (Button) findViewById(R.id.buttonMath1);
        Button buttonOption2 = (Button) findViewById(R.id.buttonMath2);
        Button buttonOption3 = (Button) findViewById(R.id.buttonMath3);
        ImageButton buttonHome = (ImageButton) findViewById(R.id.imageButtonHome);
        
        buttonOption1.setOnClickListener(this);
        buttonOption2.setOnClickListener(this);
        buttonOption3.setOnClickListener(this);
        buttonHome.setOnClickListener(this);
        
        createOperation();        
    }
    
    private void createOperation() {
     Operation operation = generateRandOperation();
     
     int operand1 = NumberUtilities.generateRandNumber(2, 100);
     int operand2 = NumberUtilities.generateRandNumber(2, 100);
     
     String operationText = String.valueOf(operand1) + " " +
       getOperationString(operation) + " " + String.valueOf(operand2) + "?";
     
     TextView textViewOperation = (TextView) findViewById(R.id.textViewOperation);
     textViewOperation.setText(operationText);
     float rightValue = calculateRightValue(operation, operand1, operand2);
     
     rightBox = NumberUtilities.generateRandNumber(1, 3);
     float randWrongValue1 = NumberUtilities.generateRandNumber(2);
     float randWrongValue2 = NumberUtilities.generateRandNumber(2);
     
     Button buttonOption1 = (Button) findViewById(R.id.buttonMath1);
        Button buttonOption2 = (Button) findViewById(R.id.buttonMath2);
        Button buttonOption3 = (Button) findViewById(R.id.buttonMath3);
     
     switch (rightBox) {
      case 0:
       buttonOption1.setText(String.valueOf(rightValue));
       buttonOption2.setText(String.valueOf(randWrongValue1));
       buttonOption3.setText(String.valueOf(randWrongValue2));
       break;
      case 1:
       buttonOption2.setText(String.valueOf(rightValue));
       buttonOption1.setText(String.valueOf(randWrongValue1));
       buttonOption3.setText(String.valueOf(randWrongValue2));
       break;
      case 2:
       buttonOption3.setText(String.valueOf(rightValue));
       buttonOption1.setText(String.valueOf(randWrongValue1));
       buttonOption2.setText(String.valueOf(randWrongValue2));
       break;
     }     
    }
    
    private float calculateRightValue(Operation oper, int operand1, int operand2) {
     float calculation = 0;
     
     if (oper == Operation.SUM) {
      calculation = operand1 + operand2;
     } else if (oper == Operation.MULTIPLICATION) {
      calculation = operand1 * operand2;
     } else if (oper == Operation.SUBSTRACT) {
      calculation = operand1 - operand2;
     } else if (oper == Operation.DIVISION) {
      calculation = operand1 / operand2;
     }
     
     return calculation;
    }
    
    private String getOperationString(Operation oper) {
     String operationText = "";
     if (oper == Operation.SUM) {
      operationText = "+";
     } else if (oper == Operation.MULTIPLICATION) {
      operationText = "*";
     } else if (oper == Operation.SUBSTRACT) {
      operationText = "-";
     } else if (oper == Operation.DIVISION) {
      operationText = "/";
     }
     return operationText;
    }
    
    private Operation generateRandOperation() {
     int rand = NumberUtilities.generateRandNumber(1, 4);
     Operation operation = null;
     
     switch(rand) {
      case 0:
       operation = Operation.SUM;
       break;
      case 1:
       operation = Operation.DIVISION;
       break;
      case 2:
       operation = Operation.MULTIPLICATION;
       break;
      case 3:
       operation = Operation.SUBSTRACT;
       break;
     }
     return operation;
    }
  
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_math_rally, menu);
        return true;
    }

 @Override
 public void onClick(View view) {
  boolean correct = false;
  boolean play = true;
  
  switch(view.getId()) {
   case R.id.buttonMath1:
    if (rightBox == 0) {
     correct = true;
    }
    break;
   case R.id.buttonMath2:
    if (rightBox == 1) {
     correct = true;
    }
    break;
   case R.id.buttonMath3:
    if (rightBox == 2) {
     correct = true;
    }
    break;
   case R.id.imageButtonHome:
    startActivity(new Intent(MathRallyActivity.this, ChooseActivity.class));
    play = false;
    break;
  }
  if (correct) {
   points++;
   updateScore();
  } if(play) {
   createOperation();
  }
 }
 
 private void updateScore() {
  EditText editText = (EditText) findViewById(R.id.editTextPoints);
  editText.setText(String.valueOf(points));
 }

 private int rightBox;
 private int points;
}

Monday, January 7, 2013

Design Patterns Short Test


A time ago I prepared this test for a course in the internal training program of the company I worked for. I gave a quick training of design patterns using the most common ones as described in the Gang of Four book. You can get it from my drive in Google, or read it here. 


  1. The follow applicability a system should be configured with one of multiple families of products.”, describes the pattern:
    1. Builder
    2. Decorator
    3. Singleton
    4. Abstract Factory

  1. The diagram below is read as
                             
  1. A is an aggregate class
  2. B implements A
  3. B extends from A
  4. A is an abstract class

  1. These two patterns can be used for undo operations.
    1. Decorator & Visitor
    2. Memento & Command
    3. Factory Method & Chain of Responsibility
    4. Command & Observer

  1. Loose coupling is when
    1. A class has just a small number of lines of code
    2. An application is designed with a small set of classes
    3. Reflection is used to instance the objects
    4. A class has little knowledge of another implementing class


  1. Which pattern uses a pool to share objects instead of instancing an excessive amount of them?
    1. Adapter
    2. Flyweight
    3. Proxy
    4. None of above


  1. Which of the follow is a general principle of class designing?
    1. Favor inheritance over composition
    2. Avoid polyphormism
    3. Use only creational patterns
    4. Favor composition over inheritance

  1. Incompatible interfaces can be overcome with the pattern:
    1. Interpreter
    2. Observer
    3. Adapter
    4. Bridge

  1. Which statements are correct about design patterns?
    1. They are proven solutions to common design problems
    2. They benefit code reusability
    3. They provide a common language among programmers
    4. All mentioned

  1. The capacity to add responsibilities on runtime to individual objects, that is, without affecting other objects, can be accomplished with this pattern.
    1. Decorator
    2. Proxy
    3. State
    4. Façade

  1. One applicability of the Observer pattern is :
    1. When a class should have all its attributes declared as public
    2. When an object should be able to notify others about an event
    3. When a class should have all its methods declared as public
    4. All of them

  1. Which pattern is represented in this diagram?
    1. Composite
    2. Prototype
    3. Façade
    4. Singleton

  1. Which pattern is being used in this line of code?
MyPattern s1 = MyPattern.Instance();

  1. Singleton
  2. Prototype
  3. Factory Method
  4. Command

  1. Which type of pattern is described in the next sentence: “Are concerned with algorithms and the assignments of responsibilities between objects”
    1. Creational
    2. Structural
    3. Behavioral
    4. None

  1. This pattern has two types: virtual & security.
    1. Façade
    2. Iterator
    3. Singleton
    4. Proxy

  1. Suppose there is a class that has a request, but you don’t want to hard code the class that will satisfy that request, but more than one class can satisfy it. You will use the follow pattern:
    1. Façade
    2. Chain of responsibility
    3. Proxy
    4. Command