3 Replies - 34441 Views - Last Post: 22 March 2012 - 12:44 AM

#1 archie_03  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 23-January 10

Calculator code using j2me

Posted 25 January 2010 - 04:44 AM

Here is the j2me code that I have made for mobile Application. Please help

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

public class CALCULATORMIDlet extends MIDlet implements CommandListener, ActionListener{
  private Display display;
  Button n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot,equal;
  private TextField enter numbers, result;
  private Command enter,back;
  

 public LoginExample() {
	form = new Form("Sign in");
	userName = new TextField("enter numbers:", "", 30, TextField.ANY);
	password = new TextField("Result:", "", 30, TextField.result);
	cancel = new Command("enter", Command.CANCEL, 2);
	login = new Command("back", Command.OK, 2);
	 n7 = new Button("7");
	 n7.addActionListener(this);
	 n8 = new Button("8");
	 n8.addActionListener(this);
	 div=new Button("/");
	 div.addActionListener(this);

		 n4=new Button("4");
		 n4.addActionListener(this);
		 n5=new Button("5");
		 n5.addActionListener(this);
		 n6=new Button("6");
		 n6.addActionListener(this);
		 mul=new Button("*");
		 mul.addActionListener(this);

		 n1=new Button("1");
		 n1.addActionListener(this);
		 n2=new Button("2");
		 n2.addActionListener(this);
		 n3=new Button("3");
		 n3.addActionListener(this);
		 minus=new Button("-");
		 minus.addActionListener(this);

		 dot=new Button(".");
		 dot.addActionListener(this);
		 n0=new Button("0");
		 n0.addActionListener(this);
		 equal=new Button("=");
		 equal.addActionListener(this);
		 plus=new Button("+");
		 plus.addActionListener(this);
		 add(panel,BorderLayout.CENTER);
	  }
	   public void actionPerformed(ActionEvent ae)
		 {
	  if(ae.getSource()==n1) assign("1");
	  else if(ae.getSource()==n2) assign("2");
	  else if(ae.getSource()==n3) assign("3");
	  else if(ae.getSource()==n4) assign("4");
	  else if(ae.getSource()==n5) assign("5");
	  else if(ae.getSource()==n6) assign("6");
	  else if(ae.getSource()==n7) assign("7");
	  else if(ae.getSource()==n8) assign("8");
	  else if(ae.getSource()==n9) assign("9");
	  else if(ae.getSource()==n0) assign("0");
	  else if(ae.getSource()==dot)
			{
			 if(((result.getText()).indexOf("."))==-1)
				result.setText(result.getText()+".");
		   }
	  else if(ae.getSource()==minus)
			 {
			 preRes=Double.parseDouble(result.getText());
			 lastCommand="-";
			 result.setText("0");
			 }
	  else if(ae.getSource()==div)
			 {
			 preRes=Double.parseDouble(result.getText());
			 lastCommand="/";
			 result.setText("0");
			 }
	  else if(ae.getSource()==equal)
			 {
			 secVal=Double.parseDouble(result.getText());
			 if(lastCommand.equals("/"))
				  res=preRes/secVal;
			 else if(lastCommand.equals("*"))
				  res=preRes*secVal;
			 else if(lastCommand.equals("-"))
				  res=preRes-secVal;
			 else if(lastCommand.equals("+"))
				  res=preRes+secVal;
			 result.setText(" "+res);
			 lastCommand="=";
			 }
	  else if(ae.getSource()==mul)
			 {
			  preRes=Double.parseDouble(result.getText());
			  lastCommand="*";
			  result.setText("0");
			  }
	  else if(ae.getSource()==plus)
			  {
			  preRes=Double.parseDouble(result.getText());
			  lastCommand="+";
			  result.setText("0");
			  }

	   }
 
	  private static void assign(String no)
		{
		 if((result.getText()).equals("0"))
			result.setText(no);
		  else if(lastCommand=="=")
		   {
			result.setText(no);
			lastCommand=null;
		   }
		  else
			result.setText(result.getText()+no);
		 }
 }



Is This A Good Question/Topic? 1
  • +

Replies To: Calculator code using j2me

#2 archie_03  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 23-January 10

Re: Calculator code using j2me

Posted 28 January 2010 - 04:24 AM

View Postarchie_03, on 25 Jan, 2010 - 03:44 AM, said:

Here is the j2me code that I have made for mobile Application. Please help

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

public class CALCULATORMIDlet extends MIDlet implements CommandListener, ActionListener{
  private Display display;
  Button n1,n2,n3,n4,n5,n6,n7,n8,n9,n0,plus,minus,mul,div,dot,equal;
  private TextField enter numbers, result;
  private Command enter,back;
  

 public LoginExample() {
	form = new Form("Sign in");
	userName = new TextField("enter numbers:", "", 30, TextField.ANY);
	password = new TextField("Result:", "", 30, TextField.result);
	cancel = new Command("enter", Command.CANCEL, 2);
	login = new Command("back", Command.OK, 2);
	 n7 = new Button("7");
	 n7.addActionListener(this);
	 n8 = new Button("8");
	 n8.addActionListener(this);
	 div=new Button("/");
	 div.addActionListener(this);

		 n4=new Button("4");
		 n4.addActionListener(this);
		 n5=new Button("5");
		 n5.addActionListener(this);
		 n6=new Button("6");
		 n6.addActionListener(this);
		 mul=new Button("*");
		 mul.addActionListener(this);

		 n1=new Button("1");
		 n1.addActionListener(this);
		 n2=new Button("2");
		 n2.addActionListener(this);
		 n3=new Button("3");
		 n3.addActionListener(this);
		 minus=new Button("-");
		 minus.addActionListener(this);

		 dot=new Button(".");
		 dot.addActionListener(this);
		 n0=new Button("0");
		 n0.addActionListener(this);
		 equal=new Button("=");
		 equal.addActionListener(this);
		 plus=new Button("+");
		 plus.addActionListener(this);
		 add(panel,BorderLayout.CENTER);
	  }
	   public void actionPerformed(ActionEvent ae)
		 {
	  if(ae.getSource()==n1) assign("1");
	  else if(ae.getSource()==n2) assign("2");
	  else if(ae.getSource()==n3) assign("3");
	  else if(ae.getSource()==n4) assign("4");
	  else if(ae.getSource()==n5) assign("5");
	  else if(ae.getSource()==n6) assign("6");
	  else if(ae.getSource()==n7) assign("7");
	  else if(ae.getSource()==n8) assign("8");
	  else if(ae.getSource()==n9) assign("9");
	  else if(ae.getSource()==n0) assign("0");
	  else if(ae.getSource()==dot)
			{
			 if(((result.getText()).indexOf("."))==-1)
				result.setText(result.getText()+".");
		   }
	  else if(ae.getSource()==minus)
			 {
			 preRes=Double.parseDouble(result.getText());
			 lastCommand="-";
			 result.setText("0");
			 }
	  else if(ae.getSource()==div)
			 {
			 preRes=Double.parseDouble(result.getText());
			 lastCommand="/";
			 result.setText("0");
			 }
	  else if(ae.getSource()==equal)
			 {
			 secVal=Double.parseDouble(result.getText());
			 if(lastCommand.equals("/"))
				  res=preRes/secVal;
			 else if(lastCommand.equals("*"))
				  res=preRes*secVal;
			 else if(lastCommand.equals("-"))
				  res=preRes-secVal;
			 else if(lastCommand.equals("+"))
				  res=preRes+secVal;
			 result.setText(" "+res);
			 lastCommand="=";
			 }
	  else if(ae.getSource()==mul)
			 {
			  preRes=Double.parseDouble(result.getText());
			  lastCommand="*";
			  result.setText("0");
			  }
	  else if(ae.getSource()==plus)
			  {
			  preRes=Double.parseDouble(result.getText());
			  lastCommand="+";
			  result.setText("0");
			  }

	   }
 
	  private static void assign(String no)
		{
		 if((result.getText()).equals("0"))
			result.setText(no);
		  else if(lastCommand=="=")
		   {
			result.setText(no);
			lastCommand=null;
		   }
		  else
			result.setText(result.getText()+no);
		 }
 }


Please see if this code is correct and suggest the solution.
Was This Post Helpful? 1
  • +
  • -

#3 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Calculator code using j2me

Posted 28 January 2010 - 12:07 PM

Does the code compile?
What problems are you encountering with the source code?
Does a particular feature not work?
Was This Post Helpful? 1
  • +
  • -

#4 kvk761  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 22-March 12

Re: Calculator code using j2me

Posted 22 March 2012 - 12:44 AM

how to run this program ?
please send my mail_id kvk761@gmail.com
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1