javad-ch
دوشنبه 05 خرداد 1393, 19:01 عصر
سلام دوستان، عذر میخوام که سوالم رو اینگونه مطرح میکنم !
بنده هیچ آشنایییی با زبان جاوا نداشته و ندارم و از من برنامه سادهی ماشین حساب رو خواسته شده که کدش رو توضیح بدم !
کد برنامه رو دارم و خواستم ازتون کمک بخوام توی تریس کردنش بهم کمک کنید ، کسانی که بلدن میتونن توضیح بدن که کدهای زیر دقیقن چه کارایی رو انجام میدن ازتون ممنون میشم
public class Calc extends MIDlet implements CommandListener{
TextField inp;
Display display;
Command add,OK,Exit,sub,div,mul,History;
int result;
Image img;
Ticker tk;
public void ArrangeCommand(Command c1,Command c2, Command c3,Command c4,Command c5,Command c6)
{
}
public Calc()
{
try {
tk=new Ticker("Start");
img = Image.createImage("/CALC LOGO.png");
History=new Command("History", Command.OK, 6);
inp = new TextField("Input", "", 10, TextField.NUMERIC);
add = new Command("ADD", Command.OK, 1);
div=new Command("Divide", Command.OK, 3);
mul=new Command("Multiply", Command.OK, 5);
sub=new Command("Subtract", Command.OK, 2);
OK = new Command("OK", Command.STOP, 4);
Exit=new Command("EXIT", Command.EXIT, 7);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void startApp()
{
display =Display.getDisplay(this);
Form f1=new Form("CALCULATOR BY javad");
f1.setTicker(tk);
f1.append(inp);
f1.append(img);
f1.addCommand(History);
f1.addCommand(add);
f1.addCommand(OK);
f1.addCommand(Exit);
f1.addCommand(sub);
f1.addCommand(div);
f1.addCommand(mul);
f1.setCommandListener(this);
display.setCurrent(f1);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
int kpress=0;
void ad()
{
kpress=0;
if(inp.size()!=0)
{
tk.setString(tk.getString()+"+"+inp.getString());
String input=inp.getString();
result+=Integer.parseInt(input);
}
}
void sb()
{
kpress=1;
if(inp.size()!=0)
{
tk.setString(tk.getString()+"-"+inp.getString());
String input=inp.getString();
if(result==0)
result=Integer.parseInt(input);
else
result-=Integer.parseInt(input);
}
}
void ml()
{
kpress=2;
if(inp.size()!=0)
{
tk.setString(tk.getString()+"x"+inp.getString());
String input=inp.getString();
if(result==0)
result=Integer.parseInt(input);
else
result*=Integer.parseInt(input);
}
}
void dv()
{
try
{
kpress=3;
if(inp.size()!=0)
{
tk.setString(tk.getString()+"/"+inp.getString());
String input=inp.getString();
if(result==0)
result=Integer.parseInt(input);
else
result/=Integer.parseInt(input);
}
}
catch(Exception ex)
{
result=0;
}
}
public void commandAction(Command c,Displayable d)
{
if(c==add)
ad();
if(c==sub)
sb();
if(c==mul)
ml();
if(c==div)
dv();
if(c==History)
{
Alert nalt=new Alert("History",tk.getString(),null,AlertType.INFO);
nalt.setTimeout(Alert.FOREVER);
display.setCurrent(nalt);
}
if(c==OK)
{
try
{
if(kpress==0)
ad();
else if(kpress==1)
sb();
else if(kpress==2)
ml();
else if(kpress==3)
dv();
else{}
}
catch(Exception ex)
{}
finally
{
tk.setString(tk.getString()+"="+result);
Alert res=new Alert("Result","RESULT = "+result, img, AlertType.INFO);
res.setTimeout(5000);
res.setImage(img);
display.setCurrent(res);
}
}
if(c==Exit)
destroyApp(true);
inp.setString("");
}
}
بنده هیچ آشنایییی با زبان جاوا نداشته و ندارم و از من برنامه سادهی ماشین حساب رو خواسته شده که کدش رو توضیح بدم !
کد برنامه رو دارم و خواستم ازتون کمک بخوام توی تریس کردنش بهم کمک کنید ، کسانی که بلدن میتونن توضیح بدن که کدهای زیر دقیقن چه کارایی رو انجام میدن ازتون ممنون میشم
public class Calc extends MIDlet implements CommandListener{
TextField inp;
Display display;
Command add,OK,Exit,sub,div,mul,History;
int result;
Image img;
Ticker tk;
public void ArrangeCommand(Command c1,Command c2, Command c3,Command c4,Command c5,Command c6)
{
}
public Calc()
{
try {
tk=new Ticker("Start");
img = Image.createImage("/CALC LOGO.png");
History=new Command("History", Command.OK, 6);
inp = new TextField("Input", "", 10, TextField.NUMERIC);
add = new Command("ADD", Command.OK, 1);
div=new Command("Divide", Command.OK, 3);
mul=new Command("Multiply", Command.OK, 5);
sub=new Command("Subtract", Command.OK, 2);
OK = new Command("OK", Command.STOP, 4);
Exit=new Command("EXIT", Command.EXIT, 7);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void startApp()
{
display =Display.getDisplay(this);
Form f1=new Form("CALCULATOR BY javad");
f1.setTicker(tk);
f1.append(inp);
f1.append(img);
f1.addCommand(History);
f1.addCommand(add);
f1.addCommand(OK);
f1.addCommand(Exit);
f1.addCommand(sub);
f1.addCommand(div);
f1.addCommand(mul);
f1.setCommandListener(this);
display.setCurrent(f1);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional)
{
notifyDestroyed();
}
int kpress=0;
void ad()
{
kpress=0;
if(inp.size()!=0)
{
tk.setString(tk.getString()+"+"+inp.getString());
String input=inp.getString();
result+=Integer.parseInt(input);
}
}
void sb()
{
kpress=1;
if(inp.size()!=0)
{
tk.setString(tk.getString()+"-"+inp.getString());
String input=inp.getString();
if(result==0)
result=Integer.parseInt(input);
else
result-=Integer.parseInt(input);
}
}
void ml()
{
kpress=2;
if(inp.size()!=0)
{
tk.setString(tk.getString()+"x"+inp.getString());
String input=inp.getString();
if(result==0)
result=Integer.parseInt(input);
else
result*=Integer.parseInt(input);
}
}
void dv()
{
try
{
kpress=3;
if(inp.size()!=0)
{
tk.setString(tk.getString()+"/"+inp.getString());
String input=inp.getString();
if(result==0)
result=Integer.parseInt(input);
else
result/=Integer.parseInt(input);
}
}
catch(Exception ex)
{
result=0;
}
}
public void commandAction(Command c,Displayable d)
{
if(c==add)
ad();
if(c==sub)
sb();
if(c==mul)
ml();
if(c==div)
dv();
if(c==History)
{
Alert nalt=new Alert("History",tk.getString(),null,AlertType.INFO);
nalt.setTimeout(Alert.FOREVER);
display.setCurrent(nalt);
}
if(c==OK)
{
try
{
if(kpress==0)
ad();
else if(kpress==1)
sb();
else if(kpress==2)
ml();
else if(kpress==3)
dv();
else{}
}
catch(Exception ex)
{}
finally
{
tk.setString(tk.getString()+"="+result);
Alert res=new Alert("Result","RESULT = "+result, img, AlertType.INFO);
res.setTimeout(5000);
res.setImage(img);
display.setCurrent(res);
}
}
if(c==Exit)
destroyApp(true);
inp.setString("");
}
}