JAVA课程设计模拟科学计算器.doc

上传人:仙人指路1688 文档编号:2387055 上传时间:2023-02-17 格式:DOC 页数:24 大小:116KB
返回 下载 相关 举报
JAVA课程设计模拟科学计算器.doc_第1页
第1页 / 共24页
JAVA课程设计模拟科学计算器.doc_第2页
第2页 / 共24页
JAVA课程设计模拟科学计算器.doc_第3页
第3页 / 共24页
JAVA课程设计模拟科学计算器.doc_第4页
第4页 / 共24页
JAVA课程设计模拟科学计算器.doc_第5页
第5页 / 共24页
点击查看更多>>
资源描述

《JAVA课程设计模拟科学计算器.doc》由会员分享,可在线阅读,更多相关《JAVA课程设计模拟科学计算器.doc(24页珍藏版)》请在三一办公上搜索。

1、课程设计报告课程:模拟科学计算器 学号: 姓名: 班级: 教师: 时间:10.12.1011.01.12 计算机科学与技术系课程设计指导书姓 名学 号班 级课程名称JAVA课程性质专业必修课设计时间2010年12 月10日2011年01月12日设计名称模拟科学计算器设计目的巩固所学理论知识,提高程序设计能力的重要实践环节。综合应用Java基础知识和基本方法。设计要求界面模拟Windows中的计算器程序。实现基本数学运算、函数等功能:加、减、乘、除、阶乘、正弦、余弦和指数运算。实现要点:添加相关组件并进行按钮事件处理。要求提交Application和Applet两个版本的程序。设计思路与设计过程

2、设计Application和Applet两个版本的程序,对Application运用继承JFrame设计,类的设计构造方法利用等。而Applet则继承Applet,在浏览器上运行。Application过程:1. 菜单栏设计.2. 文本域,即为计算器的屏幕显示区域3. 初始化操作4. 统一设置按钮的的使用方式5. 计算器的基础操作(+ - )6. 运算符号的事件监听7. 清除按钮的事件监听8. 数字输入的事件监听9. 小数点的事件监听10. main方法Applet过程:1. init()方法:完成初始化操作;2. 事件统一监听;3. 科学计算;4. 建立Calucator.html;5. 浏

3、览器运行;计划与进度12.10-12.13 复习Java程序开发的相关操作,了解“计算器相关知识”;12.14-12.17 熟悉题目并理解,及找寻相关资料;12.18-12.20 根据题目要求进行需求分析设计;12.21-12.25 对Application进行总体程序设计;12.26-12.30 对Applet进行相关各功能的程序设计;12.31-01.05 对程序进行细节完善;01.06-01.10 进行调试运行并完成资料归档。任课教师意 见备 注设计名称:模拟科学计算器日期:2010年01月05日 设计内容:Application和Applet两个版本的程序,实现基本数学运算、函数等功能

4、:加、减、乘、除、阶乘、正弦、余弦和指数运算。对Application运用继承JFrame设计,类的设计构造方法利用等。而Applet则继承Applet,在浏览器上运行。设计目的与要求:界面模拟Windows中的计算器程序。实现基本数学运算、函数等功能:加、减、乘、除、阶乘、正弦、余弦和指数运算。实现要点:添加相关组件并进行按钮事件处理。要求提交Application和Applet两个版本的程序。设计环境或器材、原理与说明:设计环境或器材: 硬件:计算机 软件:MyEclipse 集成开发环境 Java JCreator Pro;设计原理说明:运用Application和Applet两个版本的

5、程序。设计过程(步骤)或程序代码(可以加页):Application程序代码:import java.awt.*;import java.awt.event.*;import java.text.DecimalFormat;import javax.swing.*;public class Calucator extends JFrame private JTextField tf;private JPanel panel1, panel2, panel3, panel4;private JMenuBar myBar;private JMenu menu1, menu2, menu3;priv

6、ate JMenuItem editItem1, editItem2, help1, help2, help3;private JRadioButtonMenuItem seeItem1, seeItem2;/单选框private JCheckBoxMenuItem seeItem3;/复选框private ButtonGroup bgb;private String back;private boolean IfResult = true, flag = false;private String oper = =;private double result = 0;private Num n

7、umActionListener;private DecimalFormat df;public Calucator()super(科学计算器);/设置标题栏df = new DecimalFormat(#.#);/保留四位小数this.setLayout(new BorderLayout(10, 5);panel1 = new JPanel(new GridLayout(1, 3, 10, 10);panel2 = new JPanel(new GridLayout(5, 6, 5, 5);/5行6列panel3 = new JPanel(new GridLayout(5, 1, 5, 5)

8、;panel4 = new JPanel(new BorderLayout(5, 5);/* * 菜单栏 */myBar = new JMenuBar();menu1 = new JMenu(编辑(E);menu2 = new JMenu(查看(V);menu3 = new JMenu(帮助(H);menu1.setFont(new Font(宋体, Font.PLAIN, 12);menu2.setFont(new Font(宋体, Font.PLAIN, 12);menu3.setFont(new Font(宋体, Font.PLAIN, 12);/* * 编辑栏 */editItem1

9、= new JMenuItem(复制(C) Ctrl+C);editItem2 = new JMenuItem(粘贴(P) Ctrl+V);editItem1.setFont(new Font(宋体,Font.PLAIN,12);editItem2.setFont(new Font(宋体,Font.PLAIN,12);/* * 查看栏 */seeItem1 = new JRadioButtonMenuItem(科学型(T);seeItem2 = new JRadioButtonMenuItem(标准型(S);seeItem3 = new JCheckBoxMenuItem(数字分组(I);se

10、eItem1.setFont(new Font(宋体,Font.PLAIN,12);seeItem2.setFont(new Font(宋体,Font.PLAIN,12);seeItem3.setFont(new Font(宋体,Font.PLAIN,12);/* * 帮助栏 */help1 = new JMenuItem(帮助主题(H);help2 = new JMenuItem(关于计算器(A);help1.setFont(new Font(宋体,Font.PLAIN,12);help2.setFont(new Font(宋体,Font.PLAIN,12);bgb = new Button

11、Group();/选项组menu1.add(editItem1);menu1.add(editItem2);menu2.add(seeItem1);menu2.add(seeItem2);menu2.addSeparator();/添加一条分割线menu2.add(seeItem3);menu3.add(help1);menu3.addSeparator();/添加一条分割线menu3.add(help2);myBar.add(menu1);myBar.add(menu2);myBar.add(menu3);this.setJMenuBar(myBar);numActionListener =

12、 new Num();/实现数字监听/* * 文本域,即为计算器的屏幕显示区域 */tf = new JTextField();tf.setEditable(false);/文本区域不可编辑tf.setBackground(Color.white);/文本区域的背景色tf.setHorizontalAlignment(JTextField.RIGHT);/文字右对齐tf.setText(0);tf.setBorder(BorderFactory.createLoweredBevelBorder();init();/对计算器进行初始化/* * 初始化操作 * 添加按钮 */private voi

13、d init()addButton(panel1, Backspace, new Clear(), Color.red);addButton(panel1, CE, new Clear(), Color.red);addButton(panel1, C, new Clear(), Color.red);addButton(panel2, 1/x, new Signs(), Color.magenta);addButton(panel2, log, new Signs(), Color.magenta);addButton(panel2, 7, numActionListener, Color.

14、blue);addButton(panel2, 8, numActionListener, Color.blue);addButton(panel2, 9, numActionListener, Color.blue);addButton(panel2, , new Signs(), Color.red);addButton(panel2, n!, new Signs(), Color.magenta);addButton(panel2, sqrt, new Signs(), Color.magenta);addButton(panel2, 4, numActionListener, Colo

15、r.blue);addButton(panel2, 5, numActionListener, Color.blue);addButton(panel2, 6, numActionListener, Color.blue);addButton(panel2, , new Signs(), Color.red);addButton(panel2, sin, new Signs(), Color.magenta);addButton(panel2, x2, new Signs(), Color.magenta);addButton(panel2, 1, numActionListener, Col

16、or.blue);addButton(panel2, 2, numActionListener, Color.blue);addButton(panel2, 3, numActionListener, Color.blue);addButton(panel2, -, new Signs(), Color.red);addButton(panel2, cos, new Signs(), Color.magenta);addButton(panel2, x3, new Signs(), Color.magenta);addButton(panel2, 0, numActionListener, C

17、olor.blue);addButton(panel2, -/+, new Clear(), Color.blue);addButton(panel2, ., new Dot(), Color.blue);addButton(panel2, +, new Signs(), Color.red);addButton(panel2, tan, new Signs(), Color.magenta);addButton(panel2, %, new Signs(), Color.magenta);addButton(panel2, , numActionListener, Color.orange)

18、;addButton(panel2, e, numActionListener, Color.orange);addButton(panel2, , new Signs(), Color.orange);addButton(panel2, =, new Signs(), Color.red);JButton btns = new JButton(计算器);btns.setBorder(BorderFactory.createLoweredBevelBorder();btns.setEnabled(false);/按钮不可操作btns.setPreferredSize(new Dimension

19、(20, 20);panel3.add(btns);/加入按钮addButton(panel3, MC, null, Color.red);addButton(panel3, MR, null, Color.red);addButton(panel3, MS, null, Color.red);addButton(panel3, M+, null, Color.red);panel4.add(panel1, BorderLayout.NORTH);panel4.add(panel2, BorderLayout.CENTER);this.add(tf, BorderLayout.NORTH);t

20、his.add(panel3, BorderLayout.WEST);this.add(panel4);pack();this.setResizable(false);/窗口不可改变大小this.setLocation(300, 200);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/* * 统一设置按钮的的使用方式 * param panel * param name * param action * param color */private void addButton(JPanel panel, String name, Ac

21、tionListener action, Color color)JButton bt = new JButton(name);panel.add(bt);/在面板上增加按钮bt.setForeground(color);/设置前景(字体)颜色bt.addActionListener(action);/增加监听事件/* * 计算器的基础操作(+ - ) * param x */private void getResult (double x)if(oper = +)result += x;else if(oper = -)result -= x;else if(oper = )result *

22、= x;else if(oper = )result /= x;else if(oper = =)result = x;tf.setText(df.format(result);/* * 运算符号的事件监听 */class Signs implements ActionListenerpublic void actionPerformed(ActionEvent e) /* * 用ActionEvent对象的getActionCommand()方法 * 取得与引发事件对象相关的字符串 */String str = e.getActionCommand();/* sqrt求平方根 */if(st

23、r.equals(sqrt)double i = Double.parseDouble(tf.getText();if(i=0)/* * String.valueOf() 转换为字符串 * df.format() 按要求保留四位小数 * Math.sqrt() 求算数平方根 */tf.setText(String.valueOf(df.format(Math.sqrt(i);elsetf.setText(负数不能开平方根);/* log求常用对数 */else if(str.equals(log)double i = Double.parseDouble(tf.getText();if(i0)

24、tf.setText(String.valueOf(df.format(Math.log(i);elsetf.setText(负数不能求对数);/* %求百分比 */else if(str.equals(%)tf.setText(df.format(Double.parseDouble(tf.getText() / 100);/* 1/x求倒数 */else if(str.equals(1/x)if(Double.parseDouble(tf.getText() = 0)tf.setText(除数不能为零);elsetf.setText(df.format(1 / Double.parseDo

25、uble(tf.getText();/* sin求正弦函数 */else if(str.equals(sin)double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.format(Math.sin(i);/* cos求余弦函数 */else if(str.equals(cos)double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.format(Math.cos(i);/* tan求正切函数 */else if(str.

26、equals(tan)double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.format(Math.tan(i);/* n!求阶乘 */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.

27、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;else if(str = CE)tf.setText(0);IfResult = true;/* * 数字输入的事件监听 */class Num

28、implements ActionListenerpublic void actionPerformed(ActionEvent e) String str = e.getActionCommand();if(IfResult)tf.setText();IfResult = false;if(str=)tf.setText(String.valueOf(Math.PI);else if(str=e)tf.setText(String.valueOf(Math.E);elsetf.setText(tf.getText().trim() + str);if(tf.getText().equals(

29、0)tf.setText(0);IfResult = true;flag = true;/* * 小数点的事件监听 */class Dot implements ActionListenerpublic void actionPerformed(ActionEvent e) IfResult = false;if(tf.getText().trim().indexOf(.) = -1)tf.setText(tf.getText() + .);/* * main方法 */public static void main(String args) new Calucator().setVisible

30、(true);Applet程序代码:import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.applet.*;public class Calucator extends JApplet implements ActionListenerboolean i=true;private JButton num0=new JButton(0);private JButton num1=new JButton(1);private JButton num2=new JBu

31、tton(2);private JButton num3=new JButton(3);private JButton num4=new JButton(4);private JButton num5=new JButton(5);private JButton num6=new JButton(6);private JButton num7=new JButton(7);private JButton num8=new JButton(8);private JButton num9=new JButton(9);private JButton zuok=new JButton();priva

32、te JButton youk=new JButton();private JButton dian=new JButton(.);private JButton NULL=new JButton(N);private JButton plu=new JButton(+);private JButton min=new JButton(-);private JButton mul=new JButton(x);private JButton div=new JButton(/);private JButton equ=new JButton(=);private JButton cle=new

33、 JButton(C);/清除private JTextField space=new JTextField(30);public void init()JPanel text=new JPanel();text.setLayout(new FlowLayout();text.add(space);JPanel buttons=new JPanel();buttons.setLayout(new GridLayout(5,4);buttons.add(num9);buttons.add(num8);buttons.add(num7);buttons.add(plu);buttons.add(n

34、um6);buttons.add(num5);buttons.add(num4);buttons.add(min);buttons.add(num3);buttons.add(num2);buttons.add(num1);buttons.add(mul);buttons.add(num0);buttons.add(cle);buttons.add(equ);buttons.add(div);buttons.add(zuok);buttons.add(youk);buttons.add(dian);buttons.add(NULL);(num9).addActionListener(this)

35、;(num8).addActionListener(this);(num7).addActionListener(this);(num6).addActionListener(this);(num5).addActionListener(this);(num4).addActionListener(this);(num3).addActionListener(this);(num2).addActionListener(this);(num1).addActionListener(this);(num0).addActionListener(this);(plu).addActionListe

36、ner(this);(min).addActionListener(this);(mul).addActionListener(this);(div).addActionListener(this);(equ).addActionListener(this);(cle).addActionListener(this);(zuok).addActionListener(this);(youk).addActionListener(this);(dian).addActionListener(this);setLayout(new BorderLayout();add(North,text);ad

37、d(South,buttons);space.setText(0);public void actionPerformed(ActionEvent e)if(e.getSource()=num9)if(i=true)space.setText(9);i=false;else space.setText(space.getText()+9);if(e.getSource()=num8)if(i=true)space.setText(8);i=false;else space.setText(space.getText()+8);if(e.getSource()=num7)if(i=true)space.setText(7);i=false;else space.setText(space.getText()+7);if(e.getSo

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号