calculator
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class CALCULATOR extends MIDlet implements CommandListener
{
private Display display;
private Form fmMain;
private Form fmb2;
private TextField tfNum1;
private TextField tfNum2;
private TextField tfx1;
private TextField tfx2;
private TextField tfInput;
private TextBox tbHelp;
private Command cmHelp;
private Command cmExit;
private Command cmBack;
private Command cmCong;
private Command cmTru;
private Command cmNhan;
private Command cmChia;
// private Command cmLuy_thua;
private Command cmTigia;
private Command cmUsd;
private Command cmb2;
private Command cmDapso;
private Form fmTigia;
private TextField tfUsd;
private TextField tfVnd;
private TextField tfb;
private TextField tfa;
private TextField tfc;
private TextField tfKetqua;
private TextBox tbVo_nghiem;
private Form fmNghiem_kep;
private TextField tfx12;
public CALCULATOR()
{
display=Display.getDisplay(this);
tfx12=new TextField("X1=X2","",6,TextField.NUMERIC);
fmNghiem_kep=new Form("PHUONG TRINH CO NGHIEM KEP");
tfa=new TextField("NHAP A","",5,TextField.NUMERIC);
tfb=new TextField("NHAP B","",5,TextField.NUMERIC);
tfc=new TextField("NHAP C","",5,TextField.NUMERIC);
tfNum1 = new TextField(" NUM 1","",17,TextField.NUMERIC);
tfNum2 = new TextField(" NUM 2","",17,TextField.NUMERIC);
tfx1 = new TextField("X1","",5,TextField.NUMERIC);
tfx2 = new TextField("X2","",5,TextField.NUMERIC);
tfInput = new TextField(" INPUT","",17,TextField.NUMERIC);
tfKetqua = new TextField("KET QUA","",17,TextField.NUMERIC);
tfVnd=new TextField("USD","",17,TextField.NUMERIC);
tfUsd=new TextField("VND","",17,TextField.NUMERIC);
tbHelp=new TextBox("HELP","My program is a Calculator.It help you to computing plus,minus,device and..." +
".The program is easy to use." +
"The program is developed by cu0n9bk." +
"The program is bought by Microsoft with price is 1,000$",250,0);
tbVo_nghiem=new TextBox("VO NGHIEM","PHUONG TRINH VO NGHIEM THUC",30,0);
fmTigia= new Form("QUY DOI TY GIA");
fmMain= new Form("CALCULATOR");
fmb2 = new Form("GIAI PHUONG TRINH BAC HAI");
cmExit = new Command("EXIT",Command.EXIT,1);
cmCong = new Command("CONG",Command.SCREEN,1);
cmTru = new Command("TRU",Command.SCREEN,2);
cmNhan = new Command("NHAN",Command.SCREEN,3);
cmChia = new Command("CHIA",Command.SCREEN,4);
// cmLuy_thua = new Command("LUY THUA",Command.SCREEN,5);
cmHelp = new Command("HELP",Command.HELP,7);
cmBack=new Command("BACK",Command.BACK,1);
cmTigia=new Command("TI GIA",Command.SCREEN,5);
cmUsd=new Command("USD2VND",Command.SCREEN,1);
cmb2=new Command("GIAI PTB2",Command.SCREEN,6);
cmDapso=new Command("DAP SO",Command.SCREEN,1);
tbHelp.addCommand(cmBack);
tbHelp.addCommand(cmExit);
tbHelp.setCommandListener(this);
fmMain.addCommand(cmExit);
fmMain.addCommand(cmCong);
fmMain.addCommand(cmTru);
fmMain.addCommand(cmNhan);
fmMain.addCommand(cmChia);
// fmMain.addCommand(cmLuy_thua);
fmMain.addCommand(cmHelp);
fmMain.append(tfNum1);
fmMain.append(tfNum2);
fmMain.append(tfInput);
fmMain.append(tfKetqua);
fmMain.addCommand(cmb2);
fmMain.addCommand(cmTigia);
fmMain.addCommand(cmb2);
fmMain.setCommandListener(this);
fmTigia.addCommand(cmBack);
fmTigia.addCommand(cmUsd);
fmTigia.addCommand(cmExit);
fmTigia.append(tfVnd);
fmTigia.append(tfUsd);
fmTigia.setCommandListener(this);
fmb2.addCommand(cmBack);
fmb2.addCommand(cmExit);
fmb2.addCommand(cmDapso);
fmb2.append(tfa);
fmb2.append(tfb);
fmb2.append(tfc);
fmb2.append(tfx1);
fmb2.append(tfx2);
fmb2.setCommandListener(this);
fmNghiem_kep.addCommand(cmBack);
fmNghiem_kep.addCommand(cmExit);
fmNghiem_kep.append(tfx12);
fmNghiem_kep.setCommandListener(this);
tbVo_nghiem.addCommand(cmExit);
tbVo_nghiem.addCommand(cmBack);
tbVo_nghiem.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(fmMain);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d)
{
if(c==cmExit)
{
destroyApp(false);
notifyDestroyed();
}
if(c==cmHelp)
{
display.setCurrent(tbHelp);
}
if(c==cmBack)
{
display.setCurrent(fmMain);
}
if(c==cmCong)
{
int a,b;
a=Integer.parseInt(tfNum1.getString());
b=Integer.parseInt(tfNum2.getString());
tfKetqua.setString(String.valueOf(a+b));
}
if(c==cmTru)
{
int a,b;
a=Integer.parseInt(tfNum1.getString());
b=Integer.parseInt(tfNum2.getString());
tfKetqua.setString(String.valueOf(a-b));
}
if(c==cmNhan)
{
int a,b;
a=Integer.parseInt(tfNum1.getString());
b=Integer.parseInt(tfNum2.getString());
tfKetqua.setString(String.valueOf(a*b));
}
if(c==cmChia)
{
int a,b;
a=Integer.parseInt(tfNum1.getString());
b=Integer.parseInt(tfNum2.getString());
tfKetqua.setString(String.valueOf(a/b));
}
if(c==cmTigia)
{
display.setCurrent(fmTigia);
}
if(c==cmUsd)
{
int a;
a=Integer.parseInt(tfVnd.getString());
tfUsd.setString(String.valueOf(16*a));
}
if(c==cmb2)
{
display.setCurrent(fmb2);
}
if(c==cmDapso)
{
int a,b,q,delta;
a=Integer.parseInt(tfa.getString());
b=Integer.parseInt(tfb.getString());
q=Integer.parseInt(tfc.getString());
delta=b*b-4*a*q;
if(delta<0)
{
display.setCurrent(tbVo_nghiem);
}
if(delta==0)
{
display.setCurrent(fmNghiem_kep);
int x12;
a=Integer.parseInt(tfa.getString());
b=Integer.parseInt(tfb.getString());
x12=-b/(2*a);
double t=(double) x12;
tfx12.setString(String.valueOf(x12));
}
}
}
}
Bạn đang đọc truyện trên: AzTruyen.Top