JAVA课程设计-模拟科学计算器

上传人:e****s 文档编号:71747458 上传时间:2022-04-07 格式:DOC 页数:24 大小:102KB
收藏 版权申诉 举报 下载
JAVA课程设计-模拟科学计算器_第1页
第1页 / 共24页
JAVA课程设计-模拟科学计算器_第2页
第2页 / 共24页
JAVA课程设计-模拟科学计算器_第3页
第3页 / 共24页
资源描述:

《JAVA课程设计-模拟科学计算器》由会员分享,可在线阅读,更多相关《JAVA课程设计-模拟科学计算器(24页珍藏版)》请在装配图网上搜索。

1、课程设计报告课程:模拟科学计算器 学号: 姓名: 班级: 教师: 时间:10.12.1011.01.12 计算机科学与技术系课程设计指导书姓 名学 号班 级课程名称JAVA课程性质专业必修课设计时间2021年12 月10日2021年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 进行调试运行并完成资料归档。任课教师意 见备 注设计名称:模拟科学计算器日期:2021年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;private

6、 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 nu

7、mActionListener;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);, 12);menu2.setFont(new Font(宋体, Font.PLAIN, 12);menu3.setFont(new Font(宋体, Font.PLAIN, 12);/* * 编辑栏 */editItem1 = new JMenuItem(复制(C) Ctrl+C);editItem

9、2 = 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);seeItem1.setFont(new Font(宋体,Font.PLAIN,

10、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 ButtonGroup();/选项组menu1.add(editItem1);menu1

11、.add(editItem2);menu2.add(seeItem1);menu2.add(seeItem2);menu2.addSeparator();/添加一条分割线menu2.add(seeItem3);dd(help1);menu3.addSeparator();/添加一条分割线menu3.add(help2);myBar.add(menu1);myBar.add(menu2);myBar.add(menu3);this.setJMenuBar(myBar);numActionListener = new Num();/实现数字监听/* * 文本域,即为计算器的屏幕显示区域 */tf

12、= new JTextField();tf.setEditable(false);/文本区域不可编辑tf.setBackground(Color.white);/文本区域的背景色tf.setHorizontalAlignment(JTextField.RIGHT);/文字右对齐tf.setText(0);tf.setBorder(BorderFactory.createLoweredBevelBorder();init();/对计算器进行初始化/* * 初始化操作 * 添加按钮 */private void init()addButton(panel1, Backspace, new Clea

13、r(), 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.blue);addButton(panel2, 8, numActionListener,

14、 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, Color.blue);addButton(panel2, 5, numActionListene

15、r, 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, Color.blue);addButton(panel2, 2, numActionListen

16、er, 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, Color.blue);addButton(panel2, -/+, new Clear()

17、, 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);addButton(panel2, e, numActionListener, Colo

18、r.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(20, 20);panel3.add(btns);/参加按钮addButton(pane

19、l3, 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);this.add(panel3, BorderLayout.WEST);this.add(p

20、anel4);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, ActionListener action, Color color)JButton bt =

21、 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 *= x;else if(oper = )result /= x;else if(oper = =)r

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

23、Text();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)tf.setText(String.valueOf(df.format(Math.log(i);el

24、setf.setText(负数不能求对数);/* %求百分比 */else if(str.equals(%)(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.parseDouble(tf.getText();/* sin求正弦函数 */else if(str.equals(sin)ouble

25、(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.equals(tan)double i = Double.parseDouble(tf.getText();tf.setText(String.valueOf(df.f

26、ormat(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.getText().substring(0, tf.getText().length() - 1);/使用退格删除最后一位字符elsetf.setText(0);IfR

27、esult = 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 implements ActionListenerpublic void actionPerformed(ActionEvent e) String str = e.g

28、etActionCommand();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(0)tf.setText(0);IfResult = true;flag = true;/* * 小数点的事件监听 */class Dot implements Act

29、ionListenerpublic 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(true);Applet程序代码:import java.util.*;import java.awt.*;import java.awt.event.*;impor

30、t 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 JButton(2);private JButton num3=new JButton(3);private JButton num4=new JButton(4);priv

31、ate 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();private JButton youk=new JButton();private JButton dian=new JButton(.);private JButton NU

32、LL=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 JButton(C);/去除private JTextField space=new JTextField(30);public void init()JPanel

33、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(num6);buttons.add(num5);buttons.add(num4);buttons.add(min);buttons.add(num3);buttons.

34、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);(num8).addActionListener(this);(num7).addActionListener(this);(num6).addActionListe

35、ner(this);(num5).addActionListener(this);(num4).addActionListener(this);(num3).addActionListener(this);(num2).addActionListener(this);(num1).addActionListener(this);(num0).addActionListener(this);(plu).addActionListener(this);(min).addActionListener(this);(mul).addActionListener(this);(div).addActio

36、nListener(this);(equ).addActionListener(this);(cle).addActionListener(this);(zuok).addActionListener(this);(youk).addActionListener(this);(dian).addActionListener(this);setLayout(new BorderLayout();add(North,text);add(South,buttons);space.setText(0);public void actionPerformed(ActionEvent e)if(e.get

37、Source()=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.getSource()=num6)i

38、f(i=true)space.setText(6);i=false;else space.setText(space.getText()+6);if(e.getSource()=num5)if(i=true)space.setText(5);i=false;else space.setText(space.getText()+5);if(e.getSource()=num4)if(i=true)space.setText(4);i=false;else space.setText(space.getText()+4);if(e.getSource()=num3)if(i=true)space.

39、setText(3);i=false;else space.setText(space.getText()+3);if(e.getSource()=num2)if(i=true)space.setText(2);i=false;else space.setText(space.getText()+2);if(e.getSource()=num1)if(i=true)space.setText(1);i=false;else space.setText(space.getText()+1);if(e.getSource()=num0)if(i=true)space.setText(0);i=fa

40、lse;else space.setText(space.getText()+0);if(e.getSource()=zuok)if(i=true)space.setText();i=false;else space.setText(space.getText()+(); if(e.getSource()=youk)if(i=false)space.setText(space.getText()+);if(e.getSource()=dian)if(i=false)space.setText(space.getText()+.);if(e.getSource()=plu)space.setTe

41、xt(space.getText()+);i=false;if(e.getSource()=min)space.setText(space.getText()+-);i=false;if(e.getSource()=mul)space.setText(space.getText()+*);i=false;if(e.getSource()=div)space.setText(space.getText()+/);i=false;if(e.getSource()=equ)space.setText(String.valueOf(Calculator(space.getText();i=true;i

42、f(e.getSource()=cle)space.setText(0);i=true;public double Calculator(String f)/科学计算int i=0,j=0,k;char c;StringBuffer s=new StringBuffer();s.append(f);s.append(=);String formula=s.toString();char anArray;anArray=new char50;Stack mystack=new Stack();while(formula.charAt(i)!=)c=formula.charAt(i);switch

43、(c)case (: mystack.push(new Character(c);i+;break;case ):while(mystack.peek().charValue()!=()anArrayj+=mystack.pop().charValue();mystack.pop();i+;break;case +:case -:while(!mystack.empty()&mystack.peek().charValue()!=()anArrayj+=mystack.pop().charValue();mystack.push(new Character(c);i+;break;case *

44、:case /:while(!mystack.empty()&(mystack.peek().charValue()=*|mystack.peek().charValue()=/)anArrayj+=mystack.pop().charValue();mystack.push(new Character(c);i+;break;case : i+;break;default: while(c=0&c=9)|c=.)anArrayj+=c;i+;c=formula.charAt(i);anArrayj+=#;break;while(!(mystack.empty()anArrayj+=mysta

45、ck.pop().charValue();i=0;int count;double a,b,d;Stack mystack1 =new Stack();while(i=0&c=0&c=9)count+;d=d+(c-0)/Math.pow(10,count);i+;c=anArrayi;if(c=#)mystack1.push(new Double(d);i+;break;return(mystack1.peek().doubleValue();设计结果与分析(可以加页):本实验是对稳固与复习,运用了AWT和Swing组件,利用容器和事件接听器对按钮的接听,并将程序运行于浏览器以便更方便被用户

46、运用。Java代码调试完成后,能完成根本运算。功能举例:13+20=33-7*14=-98:先输入7,再单击-/+将其变成负数,然后依次单击号、14与7!=5040sin30=0.5:先输入30,然后按下按钮转换成角度值,再按下sin即可显示结果sqrt(4)=2:利用鼠标点击4后再点击sqrt按键即可显示结果2*Application界面:设计体会与建议:通过用java编写计数器课程设计,找到了亲身体验设计Java程序的乐趣。我不仅复习了以前学习过的知识,更深刻的懂得了学好学会了并不是代表就能够在真正的实践中运用的流畅,这次实践给了我们一个既动手又动脑,独立实践的时机但其中又包含着共同合作,这次又将理论和实践相结合,提高我们的分析、解决实际问题的能力,并且让我认识到了要从要从实际、实践出发提高自己的学术能力; 经过这么多天的学习,我深刻认识到自己的缺乏和缺点,自己处于怎么样的水平,但是我也了解到自己的长处和优点,不管怎么样,通过这次的实践练习,我懂得了怎么和他人合作,要取长补短,相互合作。 虽然系统的根本功能都已实现,但还是存在系统不稳定等多个问题尚待解决。针对这次课程设计结果来看,离我理想的管理系统相差甚远,对结果也深感惭愧。 感触最深的是:学程序类的东西,光是看书时不行的,学的也吃力枯燥,在实践中学习,不断的练习揣摩,这才是好方法。设计成绩:教师签名:年月日

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

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!