java模拟科学计算器课程设计.doc_第1页
java模拟科学计算器课程设计.doc_第2页
java模拟科学计算器课程设计.doc_第3页
java模拟科学计算器课程设计.doc_第4页
java模拟科学计算器课程设计.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

软 件 学 院课程设计报告书课程名称 面向对象程序设计设计题目 模拟科学计算器专业班级 嵌入式12-2 学 号 1220030207 姓 名 冯琳 指导教师 2014 年 6 月目 录1设计时间32设计目的33设计任务34设计内容34.1需求分析34.1.1 问题描述34.1.2 基本要求34.2总体设计34.2.1 包的描述34.2.2 类的描述44.2.3 类之间的关系44.3详细设计54.3.1类图54.3.2 页面设计64.4测试与分析64.5程序与代码85总结与展望17参考文献18课程设计成绩评定181 设计时间2014年6月16日-20日2 设计目的面向对象程序设计是一门实践性很强的计算机专业基础课程,课程设计是学习完该课程后进行的一次较全面的综合练习。其目的在于通过实践加深学生对面向对象程序设计的理论、方法和基础知识的理解,掌握使用Java语言进行面向对象设计的基本方法,提高运用面向对象知识分析实际问题、解决实际问题的能力,提高自身的应用能力。3设计任务模拟科学计算器要求:界面模拟Windows中的计算器程序。实现基本数学运算、函数等功能:加、减、乘、除、阶乘、正弦、余弦和指数运算。实现要点:添加相关组件并进行按钮事件处理。4 设计内容 4.1需求分析4.1.1问题描述 日常生活中经常需要用到计算器,比如科学计算,数值计算,会计业务等等,在Applet中实现一个精巧的计算器,可以让用户的操作也更方便和快捷。4.1.2基本要求本项目需要实现一个具有四则运算的计算器,能够实现基本的加、减、乘、除运算,以及其他的辅助功能(阶乘、正弦、余弦、指数运算);界面简洁实用,模拟Windows中的计算器程序。要提供主要的设计思路、类的设计。4.2总体设计4.2.1 包的描述本程序导入了java.awt.BorderLayoutjava.awt.Colorjava.awt.GridLayoutjava.awt.event.ActionEventjava.awt.event.ActionListenerjava.text.DecimalFormatjavax.swing.JButtonjavax.swing.JFramejavax.swing.JPaneljavax.swing.JTextField4.2.2 类的描述本程序共有五个类。其中Calculator是主类,main方法里会建立一个Calculator对象。Signs类为内部类,是一个监听器类,实现Actionlistener,实现运算符号的事件监听。当运算符号为一目运算符(sin,cos,n!)时,实现该类的actionPerformed方法,对sin,cos,n!按钮进行事件处理。Clear类为内部类,是一个监听器类,实现Actionlistener,实现清除按钮的事件监听。当按钮为C(清零)或Backspace时,实现该类的actionPerformed方法,对C,Backspace按钮进行事件处理。Num类为内部类,是一个监听器类,实现Actionlistener,实现数字输入的事件监听。当点击0-9这十个数字按钮时,如果是要输入的运算数则将文本框清空后将数字显示在文本框中。Dot类为内部类,是一个监听器类,实现Actionlistener,实现小数点的事件监听。当点击小数点按钮时,如果文本框中输入值中没有小数点则在文本后加一个小数点。4.2.3类之间的关系类Calculator与Signs类、Clear类、Num类、Dot类是依赖关系,Calculator依赖于Signs类、Clear类、Num类、Dot类。Calculator类中初始化init方法中调用addButton方法,其中创建了Signs类、Clear类、Num类、Dot类的实例,以实现Calculator类的实例的创建。Signs类、Clear类、Num类、Dot类与Calculator类有关联关系。Signs类、Clear类、Num类、Dot类中的actionPerformed方法中调用了Calculator中的私有变量类tf、IfResult、oper、result、df,与Calculator类中这些变量值相关联。4.3详细设计4.3.1类图(1)Calculator类图 (2)Signs类图 (3)Clear类图Calculator类tfpanel1Panel2Panel3IfResultoperresultdfcalculator()init()addButton()getResult()main()Signs类actionPerformed()Clear类actionPerformed()图4.3.1-2Signs类图4.3.1-3Clear类 (4)Num类 (5)Dot类 Num类actionPerformed()Dot类actionPerformed()图4.3.1-4Num类图4.3.1-5Dot类图4.3.1-1Calculator类4.3.2页面设计图4.3.2-1科学计算器页面4.4测试与分析(1)加运算 (2)减运算图4.4-1加运算1+2结果 图4.4-2减运算18-3结果(3)乘运算 (4)除运算 图4.4-4除运算85结果图4.4-3乘运算24结果(5)退格 (2)清零 图4.4-6C1.清零结果图4.4-5Backspace1.6退格结果(7)正弦运算 (8)余弦运算 图4.4-8cos90结果图4.4-7sin90结果(9)指数运算 (10)阶乘运算 图4.4-10 8!结果图4.4-9 23结果4.5 程序代码package fl;import java.awt.BorderLayout;import java.awt.Color;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.DecimalFormat;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;public class Calculator extends JFrame private JTextField tf;/是一个轻量级组件,它允许编辑单行文本tf=filed;private JPanel panel1, panel2, panel3;private boolean IfResult = true;private String oper = =;private double result = 0;private DecimalFormat df;/是 NumberFormat 的一个具体子类,用于格式化十进制数字public Calculator()super(科学计算器); /设置标题栏df = new DecimalFormat(#.#); /保留四位小数this.setLayout(new BorderLayout(10, 5);panel1 = new JPanel(new GridLayout(1, 2, 10, 10);panel2 = new JPanel(new GridLayout(5, 4, 5, 5);/5行4列panel3 = new JPanel(new BorderLayout(5, 5);tf = new JTextField(); /文本域,即为计算器的屏幕显示区域tf.setEditable(false); /文本区域不可编辑tf.setBackground(Color.white); /文本区域的背景色tf.setHorizontalAlignment(JTextField.RIGHT);/文字右对齐tf.setText(0);init(); /对计算器进行初始化private void init() /初始化操作,添加按钮addButton(panel1, Backspace, new Clear(), Color.orange);addButton(panel1, C, new Clear(), Color.orange);addButton(panel2, 7, new Num(), Color.blue);addButton(panel2, 8, new Num(), Color.blue);addButton(panel2, 9, new Num(), Color.blue);addButton(panel2, , new Signs(), Color.red);addButton(panel2, 4, new Num(), Color.blue);addButton(panel2, 5, new Num(), Color.blue);addButton(panel2, 6, new Num(), Color.blue);addButton(panel2, *, new Signs(), Color.red);addButton(panel2, 1, new Num(), Color.blue);addButton(panel2, 2, new Num(), Color.blue);addButton(panel2, 3, new Num(), Color.blue);addButton(panel2, -, new Signs(), Color.red);addButton(panel2, ., new Dot(), Color.blue);addButton(panel2, 0, new Num(), Color.blue);addButton(panel2, !, new Signs(), Color.magenta);addButton(panel2, +, new Signs(), Color.red);addButton(panel2, sin, new Signs(), Color.magenta);addButton(panel2, cos, new Signs(), Color.magenta);addButton(panel2, , new Signs(), Color.magenta);addButton(panel2, =, new Signs(), Color.cyan);panel3.add(panel1, BorderLayout.NORTH);/北panel3.add(panel2, BorderLayout.CENTER);/在中心this.add(tf, BorderLayout.NORTH);this.add(panel3);pack();this.setResizable(false); /窗口不可改变大小this.setLocation(300, 200);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); private void addButton(JPanel panel, String name, ActionListener action, Color color) / 统一设置按钮的的使用方式JButton bt = new JButton(name);panel.add(bt); /在面板上增加按钮bt.setForeground(color); /设置前景(字体)颜色bt.addActionListener(action); /增加监听事件public void getResult (double x) /=操作if(oper = +)System.out.println(+);result += x;else if(oper = -)result -= x; else if(oper = *) result *= x; else if(oper = ) result /= x; else if(oper=) result=Math.pow(result, x); else if(oper = =) result = x; tf.setText(df.format(result);class Signs implements ActionListenerpublic void actionPerformed(ActionEvent e)String str = e.getActionCommand(); if(str.equals(sin) / sin求正弦函数 double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.format(Math.sin(i);else if(str.equals(cos) / cos求余弦函数 double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.format(Math.cos(i); else if(str.equals(!) / n!求阶乘 double i = Double.parseDouble(tf.getText();if(i%2=0)|(i%2=1) /如果是整数进行阶乘操作int j = (int)i; /强制类型转换int result=1;for(int k=1;k 0)if(tf.getText().length() 1)tf.setText(tf.getText().substring(0, tf.getText().length() - 1);/使用退格删除最后一位字符elsetf.setText(0);IfResult = true;elseif(tf.getText().length() 2)tf.setText(tf.getText().substring(0, tf.getText().length() - 1);elsetf.setText(0);IfResult = true;class Num implements ActionListenerpublic void actionPerformed(ActionEvent e)String str = e.getActionCommand();if(0123456789.indexOf(str) != -1)if(IfResult) tf.setText();IfResult = false;tf.setText(tf.getText() + str);else tf.setText(tf.getText().trim() + str);if(tf.getText().equals(0)tf.setText();IfResult = true;class Dot implements ActionListener/小数点public void actionPerformed(ActionEvent e)IfResult = false;/显示条件运算结果if(tf.getText().trim().indexOf(.) = -1)tf.setText(tf.getText() + .); public static void main(String args)new Calculator().setVisible(true); 5总结与展望通过这次课程设计,我真的在实践中学到的不仅是课本知识的巩固和提高,而且

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论