Đề bài: Viết chương trình J2ME đơn giản giải hệ phương trình, nhập a, b, c, d, e, f và tính toán xử lý sự kiện
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Main extends MIDlet implements CommandListener {
public Display display;
private Form fmMain1;
private Form fmMain2;
private TextField texta;
private TextField textb;
private TextField textc;
private TextField textd;
private TextField texte;
private TextField textf;
public StringItem kq1;
public StringItem kq2;
private Command cmExit;
private Command cmTinh;
public Command cmBack;
public Main(){
display = Display.getDisplay(this);
fmMain1 = new Form("Giai he phuong trinh");
fmMain2 = new Form("Nghiem cua he phuong trinh");
texta = new TextField("Nhap he so a:","",15,TextField.DECIMAL);
textb = new TextField("Nhap he so b:","",15,TextField.DECIMAL);
textc = new TextField("Nhap he so c:","",15,TextField.DECIMAL);
textd = new TextField("Nhap he so d:","",15,TextField.DECIMAL);
texte = new TextField("Nhap he so e:","",15,TextField.DECIMAL);
textf = new TextField("Nhap he so f:","",15,TextField.DECIMAL);
kq1 = new StringItem("","");
kq2 = new StringItem("","");
cmExit = new Command("Exit",Command.EXIT,1);
cmTinh = new Command("Tinh",Command.OK,1);
cmBack = new Command("Back",Command.BACK,1);
fmMain1.addCommand(cmExit);
fmMain1.addCommand(cmTinh);
fmMain1.append(texta);
fmMain1.append(textb);
fmMain1.append(textc);
fmMain1.append(textd);
fmMain1.append(texte);
fmMain1.append(textf);
fmMain1.setCommandListener(this);
fmMain2.addCommand(cmExit);
fmMain2.addCommand(cmBack);
fmMain2.append(kq1);
fmMain2.append(kq2);
fmMain2.setCommandListener(this);
}
public void giaihpt(int hsa,int hsb,int hsc,int hsd,int hse,int hsf){
int dtx,dty,dt;
float x,y;
dtx=hsc*hse-hsb*hsf;
dty=hsa*hsf-hsc*hsd;
dt=hsa*hse-hsb*hsd;
if(dt!=0) {
x=(float)dtx/dt;
y=(float)dty/dt;
kq1.setText("x="+x);
kq2.setText("y="+y);
}
else {
if(dtx!=0||dty!=0){
kq1.setText("He phuong trinh vo nghiem");
kq2.setText("");
}
else {
kq1.setText("He phuong trinh co vo so nghiem");
kq2.setText("");
}
}
}
public void startApp() {
display.setCurrent(fmMain1);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction(Command c,Displayable s){
if(c == cmTinh){
if(texta.getString().compareTo("")==0||textb.getString().compareTo("")==0||textc.getString().compareTo("")==0||textd.getString().compareTo("")==0||texte.getString().compareTo("")==0||textf.getString().compareTo("")==0){
kq1.setText("Ban chua nhap du cac he so, hay nhap lai");
}
else
{
int hsa,hsb,hsc,hsd,hse,hsf;
hsa =Integer.parseInt(texta.getString());
hsb =Integer.parseInt(textb.getString());
hsc =Integer.parseInt(textc.getString());
hsd =Integer.parseInt(textd.getString());
hse =Integer.parseInt(texte.getString());
hsf =Integer.parseInt(textf.getString());
giaihpt(hsa,hsb,hsc,hsd,hse,hsf);
}
display.setCurrent(fmMain2);
}
if(c == cmExit){
destroyApp(false);
notifyDestroyed();
}
if(c == cmBack){
display.setCurrent(fmMain1);
}
}
}