《JAVA面向对象程序设计公选课课程报告.doc》由会员分享,可在线阅读,更多相关《JAVA面向对象程序设计公选课课程报告.doc(27页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、JAVA面向对象程序设计课程报告学 院_专 业_年级班别_学 号_学生姓名_ 2016年 4月 30 日评分要点评分内容得分格式(10分)字体、字号正确,间距正确;文本清晰、美观。源程序(80分)源程序无语法、逻辑错误,可读性强,有一定的工作量。运行结果(10分)有运行结果截图,运行界面截图大小合适。0-59不及格60-69及格70-79中等80-89良好90-100优秀总分JAVA面向对象程序设计课程报告评分标准要求:1、题目(四号、黑体,居中)2、完整源代码(中文:小四,宋体;英文:小四,Times New Roman,单倍行距)。3、运行界面截图。4、单面打印,课程报告正文不少于3页,在
2、报告左侧用两颗钉书针装订。简单计算器的实现/多功能计算机器/支持+ - *,整数间的整除,小数除/支持百分比%运算及非负数的平方根sqrt运算/支持存储器运算,包括:清空MC,显示MR,存入MS,累计M+/支持复制粘贴import java.awt.*;import java.lang.*;import javax.swing.*;import javax.swing.event.*;import java.awt.event.*;import java.text.DecimalFormat;public class Calculator implements ActionListener /
3、导入动作监听接口 /设计面板中的单位 JFrame frame; JTextField textAnswer; JPanel panel, panel1, panel2, panel3; JMenuBar mainMenu; JTextField textMemory; JLabel labelMemSpace; /labelMemSpace单纯做摆设,控制面板的形状 JButton buttonBk, buttonCe, buttonC; JButton button; JButton buttonMC, buttonMR, buttonMS, buttonMAdd; JButton but
4、tonDot, buttonAddAndSub, buttonAdd, buttonSub, buttonMul, buttonDiv, buttonMod; JButton buttonSqrt, buttonDao, buttonEqual; JMenu editMenu, viewMenu, helpMenu; JMenuItem copyItem, pasteItem, tItem, sItem, numberGroup, topHelp, aboutCal; DecimalFormat df; /设置数据输出精度 boolean clickable; /控制当前能否按键 double
5、 memoryd; /使用内存中存储的数字 int memoryi; double vard, answerd; /用来保存double型数据的中间值(vard)和最后结果(answerd) short key = -1, prekey = -1; /key用来保存当前进行何种运算,prekey用来保存前次进行何种运算 boolean sign; /判断是否是double型数参及运算,是为true,不是为false String copy; /做复制用 JTextArea help; /帮助 JScrollPane scrollHelp; Object temp; /临时对象 /构造函数 pu
6、blic Calculator() clickable = true; answerd = 0; frame = new JFrame(计算器); df = new DecimalFormat(0.#); /设置数据输出精度(对于double型值) textAnswer = new JTextField(15); textAnswer.setText(); textAnswer.setEditable(false); textAnswer.setBackground(new Color(255, 255, 255); panel = new JPanel(); frame.getContent
7、Pane().add(panel); panel1 = new JPanel(); panel2 = new JPanel(); panel.setLayout(new BorderLayout(); /设计整个面板 mainMenu = new JMenuBar(); editMenu = new JMenu(编辑(E); viewMenu = new JMenu(查看(V); helpMenu = new JMenu(帮助(H); copyItem = new JMenuItem( 复制(C) Ctrl+C); copyItem.addActionListener(this); paste
8、Item = new JMenuItem( 粘贴(V) Ctrl+V); pasteItem.addActionListener(this); editMenu.add(copyItem); editMenu.add(pasteItem); tItem = new JMenuItem(标准型(T); tItem.addActionListener(this); sItem = new JMenuItem( 科学型(S); sItem.addActionListener(this); viewMenu.add(tItem); viewMenu.add(sItem); topHelp = new
9、JMenuItem( 帮助主题(H); topHelp.addActionListener(this); help = new JTextArea(5, 20); scrollHelp = new JScrollPane(help); help.setEditable(false); help.append(执行简单计算n); help.append(1. 键入计算的第一个数字。n); help.append(2. 单击“+”执行加、“-”执行减、“*”执行乘或“/”执行除。n); help.append(3. 键入计算的下一个数字。n); help.append(4. 输入所有剩余的运算符和
10、数字。n); help.append(5. 单击“=”。n); aboutCal = new JMenuItem( 关于计算器(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.CEN
11、TER); panel.add(panel1, BorderLayout.SOUTH); panel1.setLayout(new BorderLayout(); textMemory = new JTextField(3); textMemory.setEditable(false); textMemory.setBackground(new Color(217, 217, 217); labelMemSpace = new JLabel( ); buttonBk = new JButton(Backspace); buttonBk.setForeground(new Color(255,
12、0, 0); buttonCe = new JButton(CE); buttonCe.setForeground(new Color(255, 0, 0); buttonC = new JButton(C); buttonC.setForeground(new Color(255, 0, 0); buttonBk.addActionListener(this); buttonCe.addActionListener(this); buttonC.addActionListener(this); panel1.add(panel2, BorderLayout.NORTH); panel2.se
13、tLayout(new FlowLayout(FlowLayout.RIGHT); panel2.add(textMemory); panel2.add(labelMemSpace); panel2.add(buttonBk); panel2.add(buttonCe); panel2.add(buttonC); panel3 = new JPanel(); panel1.add(panel3, BorderLayout.CENTER); button = new JButton10; for (int i = 0; i button.length; i+) buttoni = new JBu
14、tton(Integer.toString(i); buttoni.setForeground(new Color(0, 0, 255); buttonMC = new JButton(MC); buttonMC.setForeground(new Color(255, 0, 0); buttonMR = new JButton(MR); buttonMR.setForeground(new Color(255, 0, 0); buttonMS = new JButton(MS); buttonMS.setForeground(new Color(255, 0, 0); buttonMAdd
15、= new JButton(M+); buttonMAdd.setForeground(new Color(255, 0, 0); buttonDot = new JButton(.); buttonDot.setForeground(new Color(0, 0, 255); buttonAddAndSub = new JButton(+/-); buttonAddAndSub.setForeground(new Color(0, 0, 255); buttonAdd = new JButton(+); buttonAdd.setForeground(new Color(255, 0, 0)
16、; buttonSub = new JButton(-); buttonSub.setForeground(new Color(255, 0, 0); buttonMul = new JButton(*); buttonMul.setForeground(new Color(255, 0, 0); buttonDiv = new JButton(/); buttonDiv.setForeground(new Color(255, 0, 0); buttonMod = new JButton(%); buttonMod.setForeground(new Color(0, 0, 255); bu
17、ttonSqrt = new JButton(sqrt); buttonSqrt.setForeground(new Color(0, 0, 255); buttonDao = new JButton(1/x); buttonDao.setForeground(new Color(0, 0, 255); buttonEqual = new JButton(=); buttonEqual.setForeground(new Color(255, 0, 0); /将所有行为及监听绑定 panel3.setLayout(new GridLayout(4, 6); panel3.add(buttonM
18、C); buttonMC.addActionListener(this); panel3.add(button7); button7.addActionListener(this); panel3.add(button8); button8.addActionListener(this); panel3.add(button9); button9.addActionListener(this); panel3.add(buttonDiv); buttonDiv.addActionListener(this); panel3.add(buttonSqrt); buttonSqrt.addActi
19、onListener(this); panel3.add(buttonMR); buttonMR.addActionListener(this); panel3.add(button4); button4.addActionListener(this); panel3.add(button5); button5.addActionListener(this); panel3.add(button6); button6.addActionListener(this); panel3.add(buttonMul); buttonMul.addActionListener(this); panel3
20、.add(buttonMod); buttonMod.addActionListener(this); panel3.add(buttonMS); buttonMS.addActionListener(this); panel3.add(button1); button1.addActionListener(this); panel3.add(button2); button2.addActionListener(this); panel3.add(button3); button3.addActionListener(this); panel3.add(buttonSub); buttonS
21、ub.addActionListener(this); panel3.add(buttonDao); buttonDao.addActionListener(this); panel3.add(buttonMAdd); buttonMAdd.addActionListener(this); panel3.add(button0); button0.addActionListener(this); panel3.add(buttonAddAndSub); buttonAddAndSub.addActionListener(this); panel3.add(buttonDot); buttonD
22、ot.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); /设置各个按钮行为 public void actionPerformed(ActionEvent event) temp
23、 = event.getSource(); sign = false; try /如果按下数据按钮,将按下的按钮代表的数据插入的当前文本框字符串之后 for (int i = 0; i = 9; i+) if (temp = buttoni & clickable = true) textAnswer.setText(textAnswer.getText() + Integer.toString(i); /按下.按钮时,判断当前文本框内字符串中含不含.,如果已含,则不允许再插入. if (temp = buttonDot & clickable = true)pressPoint(); /按下
24、+、-、*、/ 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 & clicka
25、ble = 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(); /计算器有
26、关内存操作 /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) pressMAd
27、d(); /按下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) textAn
28、swer.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 (Exceptio
29、n e) textAnswer.setText(操作非法); clickable = false; private void pressPoint() /按下小数点 boolean isDot = false; if (textAnswer.getText().length() = 0) isDot = true; for (int i = 0; i textAnswer.getText().length(); i+) if (. = textAnswer.getText().charAt(i) isDot = true; break; if (isDot = false) textAnswe
30、r.setText(textAnswer.getText() + .); private void pressAdd() /按下加号 switch (prekey) case 0: answerd += Double.parseDouble(textAnswer.getText(); break; case 1: answerd -= Double.parseDouble(textAnswer.getText(); break; case 2: answerd *= Double.parseDouble(textAnswer.getText(); break; case 3: if (Doub
31、le.parseDouble(textAnswer.getText() = 0) textAnswer.setText(除数不能为零); clickable = false; else answerd /= Double.parseDouble(textAnswer.getText(); break; default: answerd = Double.parseDouble(textAnswer.getText(); textAnswer.setText(); prekey = key = 0; private void pressSub() /按下减号 switch (prekey) ca
32、se 0: answerd += Double.parseDouble(textAnswer.getText(); break; case 1: answerd -= Double.parseDouble(textAnswer.getText(); break; case 2: answerd *= Double.parseDouble(textAnswer.getText(); break; case 3: if (Double.parseDouble(textAnswer.getText() = 0) textAnswer.setText(除数不能为零); clickable = fals
33、e; else answerd /= Double.parseDouble(textAnswer.getText(); break; default: answerd = Double.parseDouble(textAnswer.getText(); textAnswer.setText(); prekey = key = 1; private void pressMul() /按下乘号 switch (prekey) case 0: answerd += Double.parseDouble(textAnswer.getText(); break; case 1: answerd -= D
34、ouble.parseDouble(textAnswer.getText(); break; case 2: answerd *= Double.parseDouble(textAnswer.getText(); break; case 3: if (Double.parseDouble(textAnswer.getText() = 0) textAnswer.setText(除数不能为零); clickable = false; else answerd /= Double.parseDouble(textAnswer.getText(); break; default: answerd =
35、 Double.parseDouble(textAnswer.getText(); textAnswer.setText(); prekey = key = 2; private void pressDiv() /按下除号 switch (prekey) case 0: answerd += Double.parseDouble(textAnswer.getText(); break; case 1: answerd -= Double.parseDouble(textAnswer.getText(); break; case 2: answerd *= Double.parseDouble(
36、textAnswer.getText(); break; case 3: if (Double.parseDouble(textAnswer.getText() = 0) textAnswer.setText(除数不能为零); clickable = false; else answerd /= Double.parseDouble(textAnswer.getText(); break; default: answerd = Double.parseDouble(textAnswer.getText(); textAnswer.setText(); prekey = key = 3; pri
37、vate void pressEqaul() /按下等号 /如果连续按=,则进行连续运算 if (prekey = 5) if (key = 0) answerd += vard; textAnswer.setText(df.format(answerd); if (key = 1) answerd -= vard; textAnswer.setText(df.format(answerd); if (key = 2) answerd *= vard; textAnswer.setText(df.format(answerd); if (key = 3) if (Double.parseDouble(textAnswer.getText() = 0) textAnswer.setText(除数不能为零); clickable = false; else answerd /= vard; textAnswer.setText(df.format(answerd); else vard = D