JAVA程序设计课程设计报告.pdf
JavaJava 程序设计程序设计课程设计报告课程设计报告课题课题: 带函数功能和数制转换功能的计算器带函数功能和数制转换功能的计算器姓名:姓名:张其卿张其卿学号:学号:201017010139同组姓名:同组姓名:谭樟林谭樟林专业班级:专业班级:计科计科 10101指导教师:指导教师:刘国清刘国清设计时间:设计时间:2012.6.8评阅意见:评定成绩:指导老师签名:年月日1,1, 设计目的:设计目的:作为一个计算机科学与技术专业的学生,学习了一个学期的 Java 程序设计之后,有必要进行一次课程设计。目的在于通过课程设计,使学生能够得到较系统的技能训练,从而巩固和加深对 Java 编程的基础理论知识的理解,培养学生综合运用所学理论解决实际问题的能力, 使学生成为具有扎实的计算机理论基础和较强的独立动手能力的复合型、应用型人才。2.2.系统描述:系统描述:计算器是人们日常生活中使用最多的一种运算工具,它可以准确快速的算出各种数据。我们所做的计算器是一种带函数功能和数制转换功能的计算器。在电脑上实现的计算器,虽然它不同于普通的计算器那样便于携带,但是它是所有计算器的雏形。这种计算器能进行简单的加、减、乘、除四则运算,函数计算功能和数制转换功能,使其能实现二进制和十进制之间的转换。并且因为计算器添加了键盘控制功能,使用者可以直接利用键盘操作计算器的程序。这样就可以方便使用者的使用了3,设计与分析1.功能模块的划分:该计算器分为三个模块:一个简单的四则运算模块、一个函数功能模块和一个数制转换功能模块。2.系统流程图: (翻页)空空开始输入所需运算的数异常的判断若无异常,输出结果若有异常,报错结束3.各模块的实现方法:1.四则运算模块:这个模块是加、减、乘、除四项基本运算的功能。如图所示:打开 eclipse 3.7.2,创建一个 Function and To String Calculator 项目,主要类为 Function 类进入设计区,在面板上加入 19 个 JButton 组件和一个 jTextField 组件。修改其属性后, 在面板上如图排好序, 进入代码编写区, 编写 19 个 JButton组件按钮运行所需的代码(代码见后面的代码) 。完成后运行程序,用鼠标单击相应的数字键和运算符号键,然后单击按钮,窗口上方的文本编辑框将会显示计算出来的结果,要将文本编辑框清零,可单击按钮。2.一个函数功能模块这个模块是实现 sin、cos、tan、atan、log、sqrt 等函数的运算功能。 过程为在原来的基础上增加8个JButton组件, 通过Java自带的数学函数实现数学函数运算功能,如图所示:3.一个数制转换功能模块此模块是在原来的基础上增加了数制转换功能, 实现十进制和二进制之间的转换。利用单选钮选择文本域中的数制,并将其转换。数制的转换是利用 Integer 类的方法实现的, 其中将十进制转化为二进制是通过 toBinaryString(n)方法实现的,二进制转化为十进制则是通过 parseInt()方法实现的。如图所示:过程为在原来的基础上添加一 ButtonGroup 组件和二个 JradioButton组件。将组件在面板上排好,修改好属性,进入代码编写区写入组件所需的相应代码。完成后运行:在文本域中任意输入某个数字,然后单击单选钮,文本域中的十进制将变为二进制数;单击单选钮,文本域中的二进制数将还原成十进制。4.最后将此计算器做成可以受键盘控制在模块一中的 number(inti)方法和 sign(int s)方法末端添加代码:this.requestFocus(true);使面板获得焦点。再添加键盘事件响应程序代码。这样用键盘也可以代替的输入,方便了使用者的使用。4.测试数据1模块一的测试:(a) 1+1,期望结果为 2,测试结果为在 jTextField组件框中显示:2。2(b) 10-2,期望结果为 8,测试结果为在 jTextField 组件框中显示:8。(c)10 * 10, 期望结果为 100, 测试结果为在 jTextField组件框显示:100。(d) 121/ 11,期望结果为 11,测试结果为在 jTextField组件框显示:11。3模 块 二 的 测 试 : (a)对 5 求 sin 的 值 , 期 望 结 果 为-0.9589242746631385 测试结果为:-0.9589242746631385(b)对 7 求 cos 的值,期望结果为 0.7539,测试结果为:0.7539022543。(c)对 7 求 tan 的值,期望结果为:0.8714,测试结果为:0.8714479827(d)PI 的值为圆周率的值 3.141592654模块三的测试:输入一个十进制的数 12,期望转换二进制的结果为:1100,测试结果为:1100。再转换为十进制为 12。即测试成功。上述测试中,数字的输入也可以通过键盘实现,因为添加了键盘响应事件(仅限简单数字)。5.5.代码见后面。6.6.总结:通过这次课程设计,我们对 Java 编写的计算器有了一个比较全面的了解。刚开始这个项目的时候,对实现各个按钮的代码感到无从下手,但是,在小组成员探讨和从网上、资料的参考下,我们终于完成了各个按钮的代码,并且排除了一部分常见错误。另外,因为之前学习 Java 程序设计这门课的时候,习惯在 JDK 环境下进行编程,所以项目开始时在 eclipse 3.7.2 的环境下进行代码编写还是感觉有点生疏,但是在求知欲的驱使下,这个并不是大的阻力。7.7.参考文献参考文献:a.辛运帏,饶一梅,张钧java 程序设计M出版社:清华大学出版社,2005b.张伟举一反三java 程序设计实战训练M出版社:人民邮电出版社,2004 年附源代码:package function_and_to_string_calculator;import java.awt.*;import java.awt.event.*;import javax.swing.*;/* * Title:* * * Description:* * * Copyright: Copyright (c) 2005* * * Company:* * author not attributable* version 1.0*/public class Function extends JFrame implements ActionListenerJPanel contentPane;JButton jButton1 = new JButton();JButton jButton2 = new JButton();JButton jButton3 = new JButton();JButton jButton4 = new JButton();JButton jButton5 = new JButton();JButton jButton6 = new JButton();JButton jButton7 = new JButton();JButton jButton8 = new JButton();JButton jButton9 = new JButton();JButton jButton10 = new JButton();JButton jButton11 = new JButton();JButton jButton12 = new JButton();JButton jButton13 = new JButton();JButton jButton14 = new JButton();JButton jButton15 = new JButton();JButton jButton16 = new JButton();JButton jButton17 = new JButton();JButton jButton18 = new JButton();JButton jButton19 = new JButton();double num1, num2;boolean add = false, sub = false, mul = false, div = false;boolean end = false; / 用来设定数据输入是否结束String str;JTextField jTextField1 = new JTextField();JButton jButton20 = new JButton();JButton jButton21 = new JButton();JButton jButton22 = new JButton();JButton jButton23 = new JButton();JButton jButton24 = new JButton();JButton jButton25 = new JButton();JButton jButton26 = new JButton();JButton jButton27 = new JButton();ButtonGroup buttonGroup1 = new ButtonGroup();JRadioButton jRadioButton1 = new JRadioButton();JRadioButton jRadioButton2 = new JRadioButton();/ Construct the framepublic Function() enableEvents(AWTEvent.WINDOW_EVENT_MASK);try jbInit(); catch (Exception e) e.printStackTrace();this.setBounds(200, 200, 500, 500);this.setVisible(true);/ Component initializationprivate void jbInit() throws Exception contentPane = (JPanel) this.getContentPane();jTextField1.setText(0);jTextField1.setBounds(new Rectangle(9, 21, 250, 34);contentPane.setLayout(null);this.setSize(new Dimension(408, 342);this.setTitle(带函数功能和数制转换功能的计算器);/ this.addKeyListener(new Function_this_keyAdapter(this);jButton1.setBounds(new Rectangle(30, 73, 65, 25);jButton1.setForeground(Color.red);jButton1.setText(清空);jButton1.addActionListener(newFunction_jButton1_actionAdapter(this);jButton2.setBounds(new Rectangle(112, 73, 65, 25);jButton2.setForeground(Color.red);jButton2.setText(置零);jButton2.addActionListener(newFunction_jButton2_actionAdapter(this);jButton3.setBounds(new Rectangle(191, 72, 52, 25);jButton3.setForeground(SystemColor.desktop);jButton3.setText(=);jButton3.addActionListener(newFunction_jButton3_actionAdapter(this);jButton4.setBounds(new Rectangle(15, 126, 42, 25);jButton4.setText(9);jButton4.addActionListener(newFunction_jButton4_actionAdapter(this);jButton5.setBounds(new Rectangle(77, 125, 42, 25);jButton5.setText(8);jButton5.addActionListener(newFunction_jButton5_actionAdapter(this);jButton6.setBounds(new Rectangle(137, 123,42, 25);jButton6.setText(7);jButton6.addActionListener(newFunction_jButton6_actionAdapter(this);jButton7.setBounds(new Rectangle(199, 123, 42, 25);jButton7.setText(/);jButton7.addActionListener(newFunction_jButton7_actionAdapter(this);jButton8.setBounds(new Rectangle(15, 170, 42, 25);jButton8.setText(6);jButton8.addActionListener(newFunction_jButton8_actionAdapter(this);jButton9.setBounds(new Rectangle(76, 172, 42, 25);jButton9.setText(5);jButton9.addActionListener(newFunction_jButton9_actionAdapter(this);jButton10.setBounds(new Rectangle(139, 171, 42, 25);jButton10.setText(4);jButton10.addActionListener(newFunction_jButton10_actionAdapter(this);jButton11.setBounds(new Rectangle(200, 171, 42, 25);jButton11.setVerifyInputWhenFocusTarget(true);jButton11.setSelected(false);jButton11.setText(*);jButton11.addActionListener(newFunction_jButton11_actionAdapter(this);jButton12.setBounds(new Rectangle(16, 217,42, 25);jButton12.setText(3);jButton12.addActionListener(newFunction_jButton12_actionAdapter(this);jButton13.setBounds(new Rectangle(79, 217, 42, 25);jButton13.setText(2);jButton13.addActionListener(newFunction_jButton13_actionAdapter(this);jButton14.setBounds(new Rectangle(142, 215, 42, 25);jButton14.setText(1);jButton14.addActionListener(newFunction_jButton14_actionAdapter(this);jButton15.setBounds(new Rectangle(202, 215, 42, 25);jButton15.setText(-);jButton15.addActionListener(newFunction_jButton15_actionAdapter(this);jButton16.setBounds(new Rectangle(15, 263, 42, 25);jButton16.setText(0);jButton16.addActionListener(newFunction_jButton16_actionAdapter(this);jButton17.setBounds(new Rectangle(79, 263, 42, 25);jButton17.setText(-);jButton17.addActionListener(newFunction_jButton17_actionAdapter(this);jButton18.setBounds(new Rectangle(140, 262, 45, 25);jButton18.setText(。);jButton18.addActionListener(newFunction_jButton18_actionAdapter(this);jButton19.setBounds(new Rectangle(205, 265, 42, 25);jButton19.setText(+);jButton19.addActionListener(newFunction_jButton19_actionAdapter(this);jButton20.setBounds(new Rectangle(265, 122, 65, 25);jButton20.setForeground(SystemColor.desktop);jButton20.setText(sin);jButton20.addActionListener(newFunction_jButton20_actionAdapter(this);jButton21.setBounds(new Rectangle(332, 122, 65, 25);jButton21.setForeground(SystemColor.desktop);jButton21.setText(cos);jButton21.addActionListener(newFunction_jButton21_actionAdapter(this);jButton22.setBounds(new Rectangle(266, 170, 65, 25);jButton22.setForeground(SystemColor.desktop);jButton22.setText(tan);jButton22.addActionListener(newFunction_jButton22_actionAdapter(this);jButton23.setBounds(new Rectangle(333, 170, 65, 25);jButton23.setForeground(SystemColor.desktop);jButton23.setText(atan);jButton23.addActionListener(newFunction_jButton23_actionAdapter(this);jButton24.setBounds(new Rectangle(268, 215, 54, 25);jButton24.setForeground(SystemColor.desktop);jButton24.setText(PI);jButton24.addActionListener(newFunction_jButton24_actionAdapter(this);jButton25.setBounds(new Rectangle(337, 215, 65, 25);jButton25.setForeground(SystemColor.desktop);jButton25.setText(Log);jButton25.addActionListener(newFunction_jButton25_actionAdapter(this);jButton26.setBounds(new Rectangle(271, 263, 65, 25);jButton26.setForeground(SystemColor.desktop);jButton26.setText(sqrt);jButton26.addActionListener(newFunction_jButton26_actionAdapter(this);jButton27.setBounds(new Rectangle(342, 262, 54, 25);jButton27.setForeground(SystemColor.desktop);jButton27.setText(nl);jButton27.addActionListener(newFunction_jButton27_actionAdapter(this);contentPane.setRequestFocusEnabled(true);jRadioButton1.setForeground(SystemColor.desktop);jRadioButton1.setText(二进制);jRadioButton1.setBounds(new Rectangle(287, 15, 91, 25);jRadioButton1.addActionListener(this);jRadioButton2.setForeground(SystemColor.desktop);jRadioButton2.setSelected(true);jRadioButton2.setText(十进制);jRadioButton2.setBounds(new Rectangle(288, 47, 91, 25);jRadioButton2.addActionListener(this);contentPane.add(jRadioButton1, null);contentPane.add(jRadioButton2, null);contentPane.add(jTextField1, null);contentPane.add(jButton4, null);contentPane.add(jButton7, null);contentPane.add(jButton6, null);contentPane.add(jButton5, null);contentPane.add(jButton8, null);contentPane.add(jButton9, null);contentPane.add(jButton10, null);contentPane.add(jButton11, null);contentPane.add(jButton12, null);contentPane.add(jButton13, null);contentPane.add(jButton14, null);contentPane.add(jButton15, null);contentPane.add(jButton16, null);contentPane.add(jButton17, null);contentPane.add(jButton18, null);contentPane.add(jButton19, null);contentPane.add(jButton21, null);contentPane.add(jButton20, null);contentPane.add(jButton22, null);contentPane.add(jButton24, null);contentPane.add(jButton25, null);contentPane.add(jButton23, null);contentPane.add(jButton26, null);contentPane.add(jButton27, null);contentPane.add(jButton1, null);contentPane.add(jButton2, null);contentPane.add(jButton3, null);buttonGroup1.add(jRadioButton1);buttonGroup1.add(jRadioButton2);public void actionPerformed(ActionEvent e)/数制转换if(e.getSource()=jRadioButton1)jButton4.setVisible(false);jButton5.setVisible(false);jButton6.setVisible(false);jButton7.setVisible(false);jButton8.setVisible(false);jButton9.setVisible(false);jButton10.setVisible(false);jButton11.setVisible(false);jButton12.setVisible(false);jButton13.setVisible(false);int n = Integer.parseInt(jTextField1.getText();jTextField1.setText(String.valueOf(Integer.toBinaryString(n);end = true;this.requestFocus(true);else if(e.getSource()=jRadioButton2)jButton4.setVisible(true);jButton5.setVisible(true);jButton6.setVisible(true);jButton7.setVisible(true);jButton8.setVisible(true);jButton9.setVisible(true);jButton10.setVisible(true);jButton11.setVisible(true);jButton12.setVisible(true);jButton13.setVisible(true);String n = jTextField1.getText();jTextField1.setText(String.valueOf(Integer.parseInt(n,2);end = true;this.requestFocus(true);/ Overridden so we can exit when window is closedprotected void processWindowEvent(WindowEvent e) super.processWindowEvent(e);if (e.getID() = WindowEvent.WINDOW_CLOSING) System.exit(0);void number(int i) / 添加 number(int i)方法,为数字按钮事件所调用String s = null;s = String.valueOf(i);if (end) / 如果数字输入结束,则将文本框置零,重新输入jTextField1.setText(0);end = false;if (jTextField1.getText().equals(0) / 如果文本框的内容为零,则覆盖文本框的内容jTextField1.setText(s); else / 如果文本框的内容不为零,则在内容后添加数字str = jTextField1.getText() + s;jTextField1.setText(str);this.requestFocus(true);/ 添加 sign(int s)方法,为运算符按钮事件所调用void sign(int s) if (s = 1) add = true;sub = false;mul = false;div = false; else if (s = 2) add = false;sub = true;mul = false;div = false; else if (s = 3) add = false;sub = false;mul = true;div = false; else add = false;sub = false;mul = false;div = true;num1 = Double.parseDouble(jTextField1.getText();end = true;this.requestFocus(true);/ 0-9 数字按钮的调用void jButton4_actionPerformed(ActionEvent e) number(9);void jButton5_actionPerformed(ActionEvent e) number(8);void jButton6_actionPerformed(ActionEvent e) number(7);void jButton8_actionPerformed(ActionEvent e) number(6);void jButton9_actionPerformed(ActionEvent e) number(5);void jButton10_actionPerformed(ActionEvent e) number(4);void jButton12_actionPerformed(ActionEvent e) number(3);void jButton13_actionPerformed(ActionEvent e) number(2);void jButton14_actionPerformed(ActionEvent e) number(1);void jButton16_actionPerformed(ActionEvent e) number(0);/ 运算符按钮的调用void jButton19_actionPerformed(ActionEvent e) sign(1);void jButton15_actionPerformed(ActionEvent e) sign(2);void jButton11_actionPerformed(ActionEvent e) sign(3);void jButton7_actionPerformed(ActionEvent e) sign(4);void jButton17_actionPerformed(ActionEvent e) / “负号”的代码if (end) end = false;jTextField1.setText(-);this.requestFocus(true);void jButton18_actionPerformed(ActionEvent e) / “点”的代码str = jTextField1.getText();if (str.indexOf(.) = 1) str += .;jTextField1.setText(str);this.requestFocus(true);void jButton1_actionPerformed(ActionEvent e) / “清零”的代码jTextField1.setText(0);this.requestFocus(true);void jButton2_actionPerformed(ActionEvent e) / “重置”的代码jTextField1.setText(0);end = false;add = false;sub = false;mul = false;div = false;this.requestFocus(true);void jButton3_actionPerformed(ActionEvent e) / ”等于”的代码num2 = Double.parseDouble(jTextField1.getText();if (add) num1 = num1 + num2; else if (sub) num1 = num1 - num2; else if (mul) num1 = num1 * num2; else if (div) num1 = num1 / num2;jTextField1.setText(String.valueOf(num1);end = true;this.requestFocus(true);void jButton20_actionPerformed(ActionEvent e) / 计算 sin 函数的值num1 = Double.parseDouble(jTextField1.getText();jTextField1.setText(String.valueOf(Math.sin(num1);end = true;this.requestFocus(true);void jButton21_actionPerformed(ActionEvent e) / 计算 cos 函数的值num1 = Double.parseDouble(jTextField1.getText();jTextField1.setText(String.valueOf(Math.cos(num1);end = true;this.requestFocus(true);void jButton22_actionPerformed(ActionEvent e) / 计算 tan 函数的值num1 = Double.parseDouble(jTextField1.getText();jTextField1.setText(String.valueOf(Math.tan(num1);end = true;this.requestFocus(true);void jButton23_actionPerformed(ActionEvent e) / 计算 atan 函数的值num1 = Double.parseDouble(jTextField1.getText();jTextField1.setText(String.valueOf(Math.atan(num1);end = true;this.requestFocus(true);void jButton24_actionPerformed(ActionEvent e) / 得出 PI 的值jTextField1.setText(String.valueOf(Math.PI);end = true;this.requestFocus(true);void jButton25_actionPerformed(ActionEvent e) / 计算对数 log 的值num1 = Double.parseDouble(jTextField1.getText();jTextField1.setText(String.valueOf(Math.log(num1);end = true;this.requestFocus(true);void jButton26_actionPerformed(ActionEvent e) / 计算 sqrt 开方的值num1 = Double.parseDouble(jTextField1.getText();jTextField1.setText