Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Developer
PCaceres
Posts: 20
Registered: 02-07-2012
My Carrier: Movistar

A BASIC CALCULATOR

package Calc;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

public class Calculator extends UiApplication {

	public Calculator() {
		pushScreen(new Operaciones());
	}

	public static void main(String[] args) {
		Calculator app = new Calculator();
		app.enterEventDispatcher();
	}
}

final class Operaciones extends MainScreen implements FieldChangeListener{
	private ObjectChoiceField choiceField;
    private int select;
    private int a, b, r;
    
    BasicEditField N1;
    BasicEditField N2;
    BasicEditField R;
    ButtonField btnCalc;
	
        
    public Operaciones()
    {
         super();
         a = b = r = 0;
         
         LabelField title = new LabelField("Calculador Básico by PGCG.89",
                        					LabelField.ELLIPSIS | 
                        					LabelField.USE_ALL_WIDTH);
         setTitle(title); 
         
         add(new RichTextField("Ingrese dos números"));

         String choices[] = {"Sumar", "Restar", "Multiplicar", "Dividir"};
         choiceField = new ObjectChoiceField("Tipo Operación", choices);
         add(choiceField);     
                       
         N1 = new BasicEditField("N1: ", null, 20, Field.EDITABLE);
         N2 = new BasicEditField("N2: ", null, 20, Field.EDITABLE);
         R = new BasicEditField("", null, 50, Field.READONLY);
         btnCalc = new ButtonField("=");
              
         btnCalc.setChangeListener(this);
         
         add(new SeparatorField());
         add(N1);
         add(N2);
         add(new SeparatorField());
         add(btnCalc);         
         add(R);
    }
    
    public boolean onClose()
    {
         Dialog.alert("Goodbye!");
         System.exit(0);
         return true;
    }
    
    public void fieldChanged(Field field, int context) 
    {	
    	if (field == btnCalc)
    	{
    		select = choiceField.getSelectedIndex();
               
    		a = Integer.parseInt(N1.getText()); 
    		b = Integer.parseInt(N2.getText());
        
    		if (select == 0) 
    		{
    			r = a + b;    			
    			R.setText("La suma es: " + Integer.toString(r));             
    		} 
    		else if (select == 1) 
    		{
    			r = a - b;       	 		
       	 		R.setText("La resta es: " + Integer.toString(r));       	 		
    		} 
    		else if (select == 2) 
    		{
    			r = a * b;    			
    			R.setText("La multiplicación es: " + Integer.toString(r));    			
    		}
    		else if (select == 3) 
    		{    			
    			if (b != 0){ 
    				r = a / b;
    				R.setText("La división es: " + Integer.toString(r));    				
       			 }  		 
    			else
    				R.setText("División entre cero");            	 
    		}    
    	}
	}   

    //Create a menu item for BlackBerry device users to click to close the 
    //BlackBerry device application.
    private MenuItem _closeItem = new MenuItem("Close", 200000, 10) 
    {
         public void run()
         {
              onClose();
         }
    };

    //To add menu items to the menu of the BlackBerry device application, 
    //override the makeMenu method.
    protected void makeMenu( Menu menu, int instance )
    {         
         menu.add(_closeItem);
    }	 
}

 

Please use plain text.
Developer
peter_strange
Posts: 14,614
Registered: 07-14-2008

Re: A BASIC CALCULATOR

Nice sample - thanks.

Please use plain text.