Skip to content

Instantly share code, notes, and snippets.

@Kishy-nivas
Created February 2, 2018 14:29
Show Gist options
  • Save Kishy-nivas/fbf5c99705fb1db39fd32aed29b6c61b to your computer and use it in GitHub Desktop.
Save Kishy-nivas/fbf5c99705fb1db39fd32aed29b6c61b to your computer and use it in GitHub Desktop.
package hello;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import java.io.*;
public class kkk extends MIDlet implements CommandListener
{
private Form form;
private Display display;
private TextField input1, input2;
private Command add, sub, mul,div,start_timer,stop_timer;
private StringItem item,timer_item;
private long start_time;
private long stop_time;
public void startApp()
{
display = Display.getDisplay(this);
Form form = new Form("Calculator");
form.append("Welcome to scientific calculator ");
item = new StringItem("Result", "");
timer_item = new StringItem("Timer" , "");
input1 = new TextField("First Number:", "", 30, TextField.NUMERIC);
input2 = new TextField("Second Number", "", 30, TextField.NUMERIC);
form.append(input1);
form.append(input2);
add = new Command("Addition", Command.OK, 1);
sub = new Command("Subtraction", Command.OK, 1);
mul = new Command("Multiplication", Command.OK, 1);
div = new Command("Division", Command.OK, 1);
start_timer = new Command("StartTime",Command.OK,1);
stop_timer = new Command("StopTime",Command.OK,1);
form.addCommand(add);
form.addCommand(sub);
form.addCommand(mul);
form.addCommand(div);
form.addCommand(start_timer);
form.addCommand(stop_timer);
form.append(item);
form.append(timer_item);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp() { }
public void destroyApp(boolean uncondn)
{
notifyDestroyed();
}
private void calculate()
{
int one=Integer.parseInt(input1.getString());
int two= Integer.parseInt(input2.getString());
int result=one+two;
item.setText( result + "" );
}
private void startTimer()
{
start_time = System.currentTimeMillis();
timer_item.setText("Timer started" + System.currentTimeMillis() );
}
private void stopTimer()
{
stop_time = System.currentTimeMillis();
timer_item.setText("Timer stopped " + (stop_time- start_time )/1000);
}
private void calculate1()
{
int one = Integer.parseInt(input1.getString());
int two = Integer.parseInt(input2.getString());
int result = one - two;
item.setText(result + "");
}
private void calculate2()
{
int one = Integer.parseInt(input1.getString());
int two = Integer.parseInt(input2.getString());
int result = one * two;
item.setText(result + "");
}
private void calculate3()
{
int one = Integer.parseInt(input1.getString());
int two = Integer.parseInt(input2.getString());
int result = one / two;
item.setText(result + "");
}
public void commandAction(Command c, Displayable d)
{
String label = c.getLabel();
if (label.equals("Addition"))
{
calculate();
}
else if (label.equals("Subtraction"))
{
calculate1();
}
else if (label.equals("Multiplication"))
{
calculate2();
form.append("The Answer is:");
}
else if (label.equals("Division"))
{
calculate3();
form.append("The Answer is:");
}
else if(label.equals("StartTime"))
{
startTimer();
}
else if(label.equals("StopTime"))
{
stopTimer();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment