实验报告 编写一个简易实用的计算器。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
开课实验室:现代信息交流中心40*开课时间:2012年**月**日实验报告:年月日
一、实验目的:熟悉Java图形用户界面的开发原理与技术,包括各种组件和容器技术。
二、实验内容:
编写一个简易实用的计算器。
三、源代码清单:
package exp3;
import Result extends Panel {
TextField tf;
Result() {
tf = new TextField("0", 25);
add(tf);
}
}
class Key extends Panel {
Button backSpace, clearEnter, cancel,lBracket,rBracket;
Button one, two, three, four, five, six, seven, eight, nine, zero;
Button add, subtract, multiplay, divide;
Button sqrt, reciprocal, mod;
Button positiveAndNegative, point, equal;
Key() {
setLayout(new GridLayout(5, 5));
backSpace = new Button("←");
(new Color(255, 0, 0));
add(backSpace);
clearEnter = new Button("CE");
(new Color(255, 0, 0));
add(clearEnter);
cancel = new Button("C");
(new Color(255, 0, 0));
add(cancel);
lBracket =new Button("(");
add(lBracket);
rBracket =new Button(")");
add(rBracket);
seven = new Button("7");
add(seven);
eight = new Button("8");
add(eight);
nine = new Button("9");
add(nine);
divide = new Button("/");
add(divide);
sqrt = new Button("sqrt");
add(sqrt);
four = new Button("4");
add(four);
five = new Button("5");
add(five);
six = new Button("6");
add(six);
multiplay = new Button("*");
add(multiplay);
mod = new Button("%");
add(mod);
one = new Button("1");
add(one);
two = new Button("2");
add(two);
three = new Button("3");
add(three);
subtract = new Button("-");
add(subtract);
reciprocal = new Button("1/x");
add(reciprocal);
zero = new Button("0");
add(zero);
positiveAndNegative = new Button("+/-");
add(positiveAndNegative);
point = new Button(".");
add(point);
add = new Button("+");
add(add);
equal = new Button("=");
add(equal);
}
}
public class Calculator extends Applet implements ActionListener { Result result = new Result();
Key key = new Key();
boolean isInit = true;
String operation = "";
float theFirstNum = 0;
float theSecondNum = 0;
float f = 0; == -1) {
+ ".");
isInit = false;
}
}
if () == {
theFirstNum = operation = "add";
isInit = true;
}
if () == {
theFirstNum = operation = "subtract";
isInit = true;
}
if () == {
theFirstNum = operation = "multiplay";
isInit = true;
}
if () == {