JAVA面向对象程序设计公选课课程报告.docx
- 文档编号:23199558
- 上传时间:2023-05-15
- 格式:DOCX
- 页数:30
- 大小:69.28KB
JAVA面向对象程序设计公选课课程报告.docx
《JAVA面向对象程序设计公选课课程报告.docx》由会员分享,可在线阅读,更多相关《JAVA面向对象程序设计公选课课程报告.docx(30页珍藏版)》请在冰豆网上搜索。
JAVA面向对象程序设计公选课课程报告
JAVA面向对象程序设计课程报告
学院______________________
专业______________________
年级班别______________________
学号______________________
学生姓名______________________
2016年4月30日
评分要点
评分内容
得分
格式
(10分)
字体、字号正确,间距正确;文本清晰、美观。
源程序
(80分)
源程序无语法、逻辑错误,可读性强,有一定的工作量。
运行结果
(10分)
有运行结果截图,运行界面截图大小合适。
0-59
不及格
60-69
及格
70-79
中等
80-89
良好
90-100
优秀
总分
JAVA面向对象程序设计课程报告评分标准
要求:
1、题目(四号、黑体,居中)
2、完整源代码(中文:
小四,宋体;英文:
小四,TimesNewRoman,单倍行距)。
3、运行界面截图。
4、单面打印,课程报告正文不少于3页,在报告左侧用两颗钉书针装订。
简单计算器的实现
//多功能计算机器
//支持+-*,整数间的整除,小数除
//支持百分比%运算及非负数的平方根sqrt运算
//支持存储器运算,包括:
清空MC,显示MR,存入MS,累计M+
//支持复制粘贴
importjava.awt.*;
importjava.lang.*;
importjavax.swing.*;
importjavax.swing.event.*;
importjava.awt.event.*;
importjava.text.DecimalFormat;
publicclassCalculatorimplementsActionListener{//导入动作监听接口
//设计面板中的单位
JFrameframe;
JTextFieldtextAnswer;
JPanelpanel,panel1,panel2,panel3;
JMenuBarmainMenu;
JTextFieldtextMemory;
JLabellabelMemSpace;//labelMemSpace单纯做摆设,控制面板的形状
JButtonbuttonBk,buttonCe,buttonC;
JButtonbutton[];
JButtonbuttonMC,buttonMR,buttonMS,buttonMAdd;
JButtonbuttonDot,buttonAddAndSub,buttonAdd,buttonSub,buttonMul,
buttonDiv,buttonMod;
JButtonbuttonSqrt,buttonDao,buttonEqual;
JMenueditMenu,viewMenu,helpMenu;
JMenuItemcopyItem,pasteItem,tItem,sItem,numberGroup,topHelp,aboutCal;
DecimalFormatdf;//设置数据输出精度
booleanclickable;//控制当前能否按键
doublememoryd;//使用内存中存储的数字
intmemoryi;
doublevard,answerd;//用来保存double型数据的中间值(vard)和最后结果(answerd)
shortkey=-1,prekey=-1;//key用来保存当前进行何种运算,prekey用来保存前次进行何种运算
booleansign;//判断是否是double型数参与运算,是为true,不是为false
Stringcopy;//做复制用
JTextAreahelp;//帮助
JScrollPanescrollHelp;
Objecttemp;//临时对象
//构造函数
publicCalculator(){
clickable=true;
answerd=0;
frame=newJFrame("计算器");
df=newDecimalFormat("0.##############");//设置数据输出精度(对于double型值)
textAnswer=newJTextField(15);
textAnswer.setText("");
textAnswer.setEditable(false);
textAnswer.setBackground(newColor(255,255,255));
panel=newJPanel();
frame.getContentPane().add(panel);
panel1=newJPanel();
panel2=newJPanel();
panel.setLayout(newBorderLayout());
//设计整个面板
mainMenu=newJMenuBar();
editMenu=newJMenu("编辑(E)");
viewMenu=newJMenu("查看(V)");
helpMenu=newJMenu("帮助(H)");
copyItem=newJMenuItem("复制(C)Ctrl+C");
copyItem.addActionListener(this);
pasteItem=newJMenuItem("粘贴(V)Ctrl+V");
pasteItem.addActionListener(this);
editMenu.add(copyItem);
editMenu.add(pasteItem);
tItem=newJMenuItem("●标准型(T)");
tItem.addActionListener(this);
sItem=newJMenuItem("科学型(S)");
sItem.addActionListener(this);
viewMenu.add(tItem);
viewMenu.add(sItem);
topHelp=newJMenuItem("帮助主题(H)");
topHelp.addActionListener(this);
help=newJTextArea(5,20);
scrollHelp=newJScrollPane(help);
help.setEditable(false);
help.append("执行简单计算\n");
help.append("1.键入计算的第一个数字。
\n");
help.append("2.单击“+”执行加、“-”执行减、“*”执行乘或“/”执行除。
\n");
help.append("3.键入计算的下一个数字。
\n");
help.append("4.输入所有剩余的运算符和数字。
\n");
help.append("5.单击“=”。
\n");
aboutCal=newJMenuItem("关于计算器(A)");
aboutCal.addActionListener(this);
helpMenu.add(topHelp);
helpMenu.add(aboutCal);
mainMenu.add(editMenu);
mainMenu.add(viewMenu);
mainMenu.add(helpMenu);
panel.add(mainMenu,BorderLayout.NORTH);
panel.add(textAnswer,BorderLayout.CENTER);
panel.add(panel1,BorderLayout.SOUTH);
panel1.setLayout(newBorderLayout());
textMemory=newJTextField(3);
textMemory.setEditable(false);
textMemory.setBackground(newColor(217,217,217));
labelMemSpace=newJLabel("");
buttonBk=newJButton("Backspace");
buttonBk.setForeground(newColor(255,0,0));
buttonCe=newJButton("CE");
buttonCe.setForeground(newColor(255,0,0));
buttonC=newJButton("C");
buttonC.setForeground(newColor(255,0,0));
buttonBk.addActionListener(this);
buttonCe.addActionListener(this);
buttonC.addActionListener(this);
panel1.add(panel2,BorderLayout.NORTH);
panel2.setLayout(newFlowLayout(FlowLayout.RIGHT));
panel2.add(textMemory);
panel2.add(labelMemSpace);
panel2.add(buttonBk);
panel2.add(buttonCe);
panel2.add(buttonC);
panel3=newJPanel();
panel1.add(panel3,BorderLayout.CENTER);
button=newJButton[10];
for(inti=0;i button[i]=newJButton(Integer.toString(i)); button[i].setForeground(newColor(0,0,255)); } buttonMC=newJButton("MC"); buttonMC.setForeground(newColor(255,0,0)); buttonMR=newJButton("MR"); buttonMR.setForeground(newColor(255,0,0)); buttonMS=newJButton("MS"); buttonMS.setForeground(newColor(255,0,0)); buttonMAdd=newJButton("M+"); buttonMAdd.setForeground(newColor(255,0,0)); buttonDot=newJButton("."); buttonDot.setForeground(newColor(0,0,255)); buttonAddAndSub=newJButton("+/-"); buttonAddAndSub.setForeground(newColor(0,0,255)); buttonAdd=newJButton("+"); buttonAdd.setForeground(newColor(255,0,0)); buttonSub=newJButton("-"); buttonSub.setForeground(newColor(255,0,0)); buttonMul=newJButton("*"); buttonMul.setForeground(newColor(255,0,0)); buttonDiv=newJButton("/"); buttonDiv.setForeground(newColor(255,0,0)); buttonMod=newJButton("%"); buttonMod.setForeground(newColor(0,0,255)); buttonSqrt=newJButton("sqrt"); buttonSqrt.setForeground(newColor(0,0,255)); buttonDao=newJButton("1/x"); buttonDao.setForeground(newColor(0,0,255)); buttonEqual=newJButton("="); buttonEqual.setForeground(newColor(255,0,0)); //将所有行为与监听绑定 panel3.setLayout(newGridLayout(4,6)); panel3.add(buttonMC); buttonMC.addActionListener(this); panel3.add(button[7]); button[7].addActionListener(this); panel3.add(button[8]); button[8].addActionListener(this); panel3.add(button[9]); button[9].addActionListener(this); panel3.add(buttonDiv); buttonDiv.addActionListener(this); panel3.add(buttonSqrt); buttonSqrt.addActionListener(this); panel3.add(buttonMR); buttonMR.addActionListener(this); panel3.add(button[4]); button[4].addActionListener(this); panel3.add(button[5]); button[5].addActionListener(this); panel3.add(button[6]); button[6].addActionListener(this); panel3.add(buttonMul); buttonMul.addActionListener(this); panel3.add(buttonMod); buttonMod.addActionListener(this); panel3.add(buttonMS); buttonMS.addActionListener(this); panel3.add(button[1]); button[1].addActionListener(this); panel3.add(button[2]); button[2].addActionListener(this); panel3.add(button[3]); button[3].addActionListener(this); panel3.add(buttonSub); buttonSub.addActionListener(this); panel3.add(buttonDao); buttonDao.addActionListener(this); panel3.add(buttonMAdd); buttonMAdd.addActionListener(this); panel3.add(button[0]); button[0].addActionListener(this); panel3.add(buttonAddAndSub); buttonAddAndSub.addActionListener(this); panel3.add(buttonDot); buttonDot.addActionListener(this); panel3.add(buttonAdd); buttonAdd.addActionListener(this); panel3.add(buttonEqual); buttonEqual.addActionListener(this); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } //设置各个按钮行为 publicvoidactionPerformed(ActionEventevent) { temp=event.getSource(); sign=false; try{ //如果按下数据按钮,将按下的按钮代表的数据插入的当前文本框字符串之后 for(inti=0;i<=9;i++){ if(temp==button[i]&&clickable==true) textAnswer.setText(textAnswer.getText()+Integer.toString(i)); } //按下'.'按钮时,判断当前文本框内字符串中含不含'.',如果已含,则不允许再插入'.' if(temp==buttonDot&&clickable==true) { pressPoint(); } //按下+、-、*、/ if((temp==buttonAdd||temp==buttonSub||temp==buttonMul|| temp==buttonDiv)&&clickable==true) { //'+'操作 if(temp==buttonAdd)pressAdd(); //'-'操作 if(temp==buttonSub)pressSub(); //'*'操作 if(temp==buttonMul)pressMul(); //'/'操作 if(temp==buttonDiv)pressDiv(); } //'='操作 if(temp==buttonEqual&&clickable==true) { pressEqaul(); } //'%'操作,对第二个操作数除以100 if(temp==buttonMod&&clickable==true){ pressMod(); } //开根号运算 if(temp==buttonSqrt&&clickable==true){ pressSqrt(); } //倒数运算 if(temp==buttonDao&&clickable==true){ pressDao(); } //按下'+/-'按钮时处理 if(temp==buttonAddAndSub&&clickable==true){ pressAddAndSub(); } //计算器有关内存操作 //'MC'的操作,将内存清0 if(temp==buttonMC&&clickable==true){ pressMC(); } //'MS'的操作,将当前文本框内容保存入内存,显示'M' if(temp==buttonMS&&clickable==true){ pressMS(); } //'MR'的操作,将存储器中的信息输出 if(temp==buttonMR&&clickable==true){ pressMR(); } //'M+'的功能,将当前文本框里的数据和存储器中数据相加后,再存入存储器 if(temp==buttonMAdd&&clickable==true){ pressMAdd(); } //按下'Backspace'键,利用循环将当前字符串中的最后一个字母删除 if(temp==buttonBk&&clickable==true){ pressBK(); } //按下'CE'按钮,将当前文本框内数据清除 if(temp==buttonCe){ pressCE(); } //按下'C'按钮,文本框内数据清除,同时var,answer清0 if(temp==buttonC){ pressC(); } //按下'复制'菜单栏 if(temp==copyItem){ copy=textAnswer.getText(); } //按下'粘贴'菜单栏 if(temp==pasteItem){ textAnswer.setText(copy); } if(temp==sItem){ JOptionPane.showMessageDialog(panel,"当前是标准型计算器,\n科学型计算器有待更新。 "); } //按下'帮助主题'菜单栏 if(temp==topHelp){ JOptionPane.showMessageDialog(panel,scrollHelp); } //按下'关于'菜单栏 if(temp==aboutCal){ JOptionPane.showMessageDialog(panel,"计算器1.00版\n"); } } //输入中如果有操作非法,比如按下两次'+',捕获异常 catch(Exceptione){ textAnswer.setText("操作非法"); clickable=false; } } privatevoidpressPoint() {//按下小数点 booleanisDot=false; if(textAnswer.getText().length()==0) isDot=true; for(inti=0;i if('.'==textAnswer.getText().charAt(i)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- JAVA 面向 对象 程序设计 选课 课程 报告