*******************
实践教学
*******************
XXXX大学
计算机与通信学院
秋季学期
java 课程设计
题 目: 迷你计算器 专业班级: 姓 名: 学 号: 指导教师: 成 绩:
目录
序言 .................................................................................................................................................. 2 摘要 .................................................................................................................................................. 2 系统分析与设计 ............................................................................................................................... 3 设计方案构思: ............................................................................................................................... 3 详细设计........................................................................................................................................... 6 数字按键监听类 ............................................................................................................................... 6 运算按键监听类 ............................................................................................................................... 7 记忆操作监听类 ............................................................................................................................... 9 测试方案........................................................................................................................................... 9 测试用例......................................................................................................................................... 10 测试结果......................................................................................................................................... 10 软件使用说明书 ............................................................................................................................. 14 按键说明:..................................................................................................................................... 14 设计总结......................................................................................................................................... 16 参考文献......................................................................................................................................... 16 附录代码:..................................................................................................................................... 17 application版本的迷你计算器 ...................................................................................................... 17 applet版本的迷你计算器 ............................................................................................................. 25
序言
学习完Java语言课程以后,利用这一次的课程设计进行自我检验的一次实践。通过该题目的设计,培养面向对象程序设计的思想,要求达到熟练掌握Java语言的基本知识和技能,基本掌握面向对象程序设计的基本思路和方法,能够利用所学的基本知识和技能,解决简单的面向对象程序设计问题。同时强调好的程 序设计风格,提高解决实际问题的能力。 而我的课题是设计《迷你计算器》,它要求利用所学过的java语言进行设计,并要求可视化界面。通过查阅有关java知识方面的书籍资料来帮助自己完成课程设计,涉及的知识点有以下几点:
利用面向对象的思想首先考虑设计程序的大致框架结构以及相关功能,设计相关的类结构;
由于使用可视化界面,免不了要使用有关界面的设计知识,有AWT、swing来设计人机交互的界面。而事件的相关知识起到人机媒介的作用,要求利用到接口(interface)实现事件的监听;
此次的课程设计包括两个版本:application 和 Applet小应用程序,所以需要了解两个的开发流程,以及两者的区别点。
有关这次是的设计只是简略的迷你计算器的设计,采用清晰,简明的设计层次,同时可以检验对java语言的基本掌握情况。
摘要
计算器是一种很常用的计算工具,它在计算方面、日常生活中给我们提供很大 的方便,在编辑此程序时,除了包括常用的加法运算算法、减法运算算法、乘法运 算算法、除法运算算法等四大模块之外。还特意添加了阶乘、正弦、余弦和指数运算。 其次程序按照要求利用AWT、swing图形界面技术进行windows系统迷你计算器的模拟设计, 用到的接口、对象、类等方法,同时对编程过程中所编写的实验代码也不同程度的进行细化的要求,所以在编程的过程中难免遇到了一些困难,所以在完成本次系统的过程中过程中充满了挑战与机遇。
关键字: AWT、swing、类、接口、事件监听
系统分析与设计
设计方案构思:
本程序采用模块化设计,调试方便,程序的可读性好。在老师的指导下,由主程序启动简易计算器,根据使用者的需要可以在简易计算器中进行运算操作,若不能满足用户功能,可以选择使用科学计算器,具体操作与目前市面上的计算器差不多,用户还可以选择使用说明进行查看具体操作。由于时间及个人水平的问题,科学计算中还有很多功能未实现。
具体思路:用户输入(点击)某一数字或操作符,程序将自行判断是操作数还是操作符。
迷你计算器包括以下部分:
表 按键布局
MC MR MS M+ M- <— CE C ± Sin 7 8 9 + Cos
4 5 6 - x^y
1 2 3 * n! 0 = . /
数字按钮(0~9):10个
内存操作按钮(MC、MR、MS、M+、M-)::5个
、
运算按钮(+、-、*、/、n!、cos、sin、xy、±、=):10个 清零操作按钮(<-、CE、C):3个
事件分为三个类进行操作,如以下部分:
Public class NumberListener implements ActionListener //监听数字按钮
public class OperatorListenerimplements ActionListener //监听运算按钮
publicclassMemoryListenerimplements ActionListener
//监听MC、MR、MS、M+、M-等按钮
Application版本中,以上三个个类作为主类的内部类,即 public class MyCalculator extends JFrame
//主类直接继承swing的框架类JFrame,并采用网格包布局 Applet版本中,以上三个类也作为其内部类,即 public class AplCalculator extends Applet //主类继承Applet类采用网格包布局 方案设计步骤:
1、 创建主类MyCalculator并继承JFrame
2、 创建以上三个类做为MyCalculator类的内部类
Application版本
图 application版本类层次结构
表MyCalculator的成员 数 据 成 员 show1; layout; constraints; StartSymbol; ResultSymbol; result; memory; operator; MemoryListener OperatorListener NumberListener 声明 contentPane; 类型 private Container private JTextField private GridBagLayout private GridBagConstraints private boolean private boolean private double private double private String public class public class public class 描述 容器变量,获取面板 文本框,显示数据 网格包布局变量 网格包辅助变量 判断输入开始结束 判断结果是否可修改 保存结果 保存要记忆的数 运算符号 记忆按键监听类 运算符号监听类 数字按钮监听类
成 员 函 数 MyCalculator( String title) addButton( JPanelp, String label, int row, int column, int with, int height, ActionListenerlistener) main(String[] args) public private void 初始化函数 添加按键函数 public static void 主函数 Applet版本
图 applet版本类层次结构
表ApCalculator的成员 数 据 成 员 show layout; constraints; StartSymbol; ResultSymbol; result; memory; operator; MemoryListener OperatorListener NumberListener
声明 类型 private JTextField private GridBagLayout private GridBagConstraints private boolean private boolean private double private double private String public class public class public class 描述 文本框,显示数据 网格包布局变量 网格包辅助变量 判断数据输入开始 判断结果是否可修改 保存计算结果 记忆数据 运算符号 记忆按键监听 运算符号监听 数字按键监听
成 员 函 数 init() addButton( JPanelp, String label, int row, int column, int with, int height, ActionListenerlistener) public void private void 初始化函数 添加按键函数
详细设计
数字按键监听类
实现用户输入数据时进行监听并在文本框做出相应按键的显示,以下是该类的详细设计。
public class NumberListener implements ActionListener
{//实现动作监听
public void actionPerformed(ActionEvent e)
{//覆盖actionPerformed函数
String input = e.getActionCommand(); if(StartSymbol)
{//判断输入数字是否开始 StartSymbol = false; show1.setText(\"\"); if(input.equals(\"±\"))
show1.setText(show1.getText() + \"-\"); }
if (!input.equals(\"±\")) {
if(input == \"CE\") {
show1.setText(\"\");
//input=null;
StartSymbol = true; }else if(input == \"C\") {
show1.setText(\"0\"); //input=null; operator = \"=\";
StartSymbol = true; }else if(input == \"<—\") {
String str = show1.getText();
if(!ResultSymbol&&str.length() > 0) show1.setText(str.substring(0, str.length() - 1)); else show1.setText(\"\" + result); }else {
show1.setText(show1.getText() + input); ResultSymbol = false; } } } }
运算按键监听类
用来实现当用户使用计算某个已有的运算时,时间监听类监听并做出相应的运算操作,以下是详细的类设计。
public class OperatorListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand(); if(StartSymbol) {
operator = command;
SGCalculator(operator);
//SGCalculator(Double.valueOf(show1.getText())); }else {
DBCalculator(Double.valueOf(show1.getText())); operator = command;
SGCalculator(operator); StartSymbol = true;
}
ResultSymbol = true; } }
public void DBCalculator(double y) {
if(operator == \"+\") {
result += y;
}else if(operator == \"-\") {
result -= y;
}else if(operator == \"*\") {
result *= y;
}else if(operator == \"/\") {
result /= y;
}else if(operator == \"x^y\") {
double x = result;
for(;y>1;y--) result *= x; }else if(operator == \"=\") {
result = y; }
show1.setText(\"\" + result); }
public void SGCalculator(String operator) {
if(operator == \"sin\"){
result = Math.sin(result); }else if(operator == \"cos\"){ result = Math.cos(result); }else if(operator == \"n!\"){
for(double x = result -1;x>1;x--) result *= x; }
show1.setText(\"\" + result); }
记忆操作监听类
实现方便用户记忆计算结果,当用户使用监听的按键时,该类进行监听并做出相应的判断,从而记忆当前显示的数据。
public class MemoryListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String press = e.getActionCommand(); if(press == \"MC\") {
memory = 0;
show1.setText(\"\"+memory); }else if(press == \"MR\") {
show1.setText(\"\"+memory); }else if(press == \"MS\") {
memory = Double.valueOf(show1.getText());
}else if(press == \"M+\") {
memory += Double.valueOf(show1.getText()); }else if(press == \"M-\") {
memory -= Double.valueOf(show1.getText()); }
StartSymbol = true; } }
在函数addButton(JPanel p,String label, int row, int column, int with, int height, ActionListener listener)中将以上三个事件监听类添加到相应的按钮。
测试方案
Manual Test 手动测试:
虽然手动测试最大的缺点就是技术含量低,单调乏味,容易废人,但是手动测试比较适合此次迷你计算器的测试,因为是小程序,手动测试能够细致的检查程序的细节。 1、 先进行MC、MR、MS、M+、M-等的按键的检查。 2、 进行0~9等的按键的检查 3、 进行各个运算按键的检查
4、 5、 进行<-、CE、C按键的测试
与windows7系统自带的计算器进行数据比对。
测试用例
CE键,只是清除当前输入的数据或符号,例如:计算4+2,输入4+3后,发现2输错了。此时按CE键,将3清除,再输入2,即可计算出结果。 按C键,则表述全部清零。 MS:存当前显示的数值
MR:读取存储器中的数值,并显示出来 MC:清除已存的数据
M-:用已存的数值减去当前显示的数值后,再将结果保存 M+:用已存的数值加上当前显示的数值后,再将结果保存
用M+或者M-计算完成之后,计算结果不会直接显示,它需要再点击下MR,这样的话就可以将最后的计算结果显示出来。例如:输入5,再按MS键,再按M+键,再按MR,结果显示6.。
cos运算:先输入45(弧度计算),再按cos键,结果显示:0.5253219888177297 Sin运算:先输入45(弧度计算),再按sin键,结果显示:0.820930352453411824286237967 n!运算:先输入5,再按n!键,结果显示120.0
x^y运算:先输入2,在按x^y键,再输入3,再按“=”键,结果显示:8 基本的加减乘除四则运算由于篇幅省略。
测试结果
所有的测试结果都与windows7自带的计算器进行比对。
图 乘法运算用例
图 cos运算用例
图 sin运算用例
图 n!运算用例
软件可靠性分析
程序实例表明计算值精确到0.0000000000000001上限值使用科学计数值表示。 程序满足日常生活中的基本使用。 如图:
图 计算器精确值
软件使用说明书
按键说明:
MS:存当前显示的数值
MR:读取存储器中的数值,并显示出来 MC:清除已存的数据
M-:用已存的数值减去当前显示的数值后,再将结果保存 M+:用已存的数值加上当前显示的数值后,再将结果保存
用M+或者M-计算完成之后,计算结果不会直接显示,它需要再点击下MR,这样的话就可以将最后的计算结果显示出来。
CE:清除当前显示的数字 C:清除表达式 Sin:进行正弦运算 Cos:进行余弦运算 x^y:进行x的幂运算 <—: 消除当前数据一位 +: 进行加运算 -:进行减法运算 *:进行乘法运算 /:进行除法运算。
图 计算器界面
设计总结
在此次的课程设计实践中,我学到了教科书上没有的东西。这次课程设计,我按照课程设计的要求,分块实现程序的功能。在实践中我遇到许多的问题,通过网上的交流,和同学的帮助,最后都克服,最终我成功的完成了这次课程设计。从中我充分理解了Java面对对象的特性,并体会到Java与C语言的不同之处在实践的同时,我又从中成长了许多,特别是在对程序的编译和调试中掌握了许多的技巧和方法,为今后的其他计算机语言的学习奠定了一定的基础。
在做本项目是时候,会遇到很多小问题,比如说:如何保存输入的数字和下一个数进行加、减、乘、除运算。如何保存结果与如何进行登陆注册页面的添加及用户信息的储存,经过不断地尝试与调整,此次课程设计让我们更了解熟悉了Java中的图形用户界面和它的编程方式。在完成课题的过程中也不断充实了自己,学习到了很多以前没有学习到的知识,收获很大。最大的收获就是对大学学习的总结和培养了解决困难的信心和能力,使我们对所学知识能够融会贯通,又不断丰富了新知识。Java计算器设计使得我们对所学的专业课有了更为深刻的认识,使得知识得到了巩固和提高。
(1)通过编写这个相对复杂的程序,让我认识解决实际问题前应现建立模型,再通过建立的模型使问题简单化,从而解决问题。
(2)这次编写的程序大部分是通过调用各包中的方法来实现的,这也让我充分理解了包和类调用的关系。
(3)由于这次编写的程序相对比较复杂,所以不能一次性编写、调试,也由让我学会把程序分块,逐个编写、调试。
参考文献
[1]曲朝阳,杨杰明等.Java程序设计. 北京: 清华大学出版社,2008.10 [2]耿祥以,张跃平.Java大学生实用教程. 北京: 电子工业出版社,2012.6 [3]明日科技.Java经典编程.北京:清华大学出版社,2012.10 [4]李尊朝,苏军.Java语言程序设计.北京:中国铁道出版社,2004.2 [5]高宏静,从入门到精通.北京工业出版社
附录代码:
application版本的迷你计算器
package design;
import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JTextField;
@SuppressWarnings(\"serial\")
public class MyCalculator extends JFrame{
/**
* @param args */
private Container contentPane; private JTextField show1; private GridBagLayout layout;
private GridBagConstraints constraints; private boolean StartSymbol;
private boolean ResultSymbol; private double result; private double memory; private String operator;
public class NumberListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String input = e.getActionCommand(); if(StartSymbol) { }
if (!input.equals(\"±\")) {
if(input == \"CE\") {
show1.setText(\"\"); //input=null; StartSymbol = true; StartSymbol = false; show1.setText(\"\");
if(input.equals(\"±\"))
show1.setText(show1.getText() + \"-\");
}else if(input == \"C\") {
show1.setText(\"0\"); //input=null; operator = \"=\"; StartSymbol = true;
}else if(input == \"<—\") {
String str = show1.getText(); if(!ResultSymbol&&str.length()
>
0)
show1.setText(str.substring(0, str.length() - 1));
public class OperatorListener implements ActionListener {
public void actionPerformed(ActionEvent e) { }
public void DBCalculator(double y)
String command = e.getActionCommand(); if(StartSymbol) {
operator = command; SGCalculator(operator);
//SGCalculator(Double.valueOf(show1.getText()));
}
}
else show1.setText(\"\" + result);
}else {
show1.setText(show1.getText() + input); ResultSymbol = false;
} }
}else { }
DBCalculator(Double.valueOf(show1.getText())); operator = command; SGCalculator(operator); StartSymbol = true; ResultSymbol = true;
{ }
public void SGCalculator(String operator) {
if(operator == \"sin\"){
result = Math.sin(result); if(operator == \"+\") {
result += y;
}else if(operator == \"-\") {
result -= y;
}else if(operator == \"*\") {
result *= y;
}else if(operator == \"/\") {
result /= y;
}else if(operator == \"x^y\") {
double x = result;
for(;y>1;y--) result *= x;
}else if(operator == \"=\") { }
show1.setText(\"\" + result);
result = y;
}else if(operator == \"cos\"){
result = Math.cos(result);
}else if(operator == \"n!\"){ }
for(double x = result -1;x>1;x--)
result *= x;
}
}
show1.setText(\"\" + result);
public class MemoryListener implements ActionListener { }
/*public void NumberButton() {
public void actionPerformed(ActionEvent e) {
String press = e.getActionCommand(); if(press == \"MC\")
{
memory = 0;
show1.setText(\"\"+memory);
}else if(press == \"MR\") {
show1.setText(\"\"+memory);
}else if(press == \"MS\") {
memory = Double.valueOf(show1.getText());
}else if(press == \"M+\") {
memory += Double.valueOf(show1.getText());
}else if(press == \"M-\") { } }
StartSymbol = true;
memory -= Double.valueOf(show1.getText());
}*/
for(int i=0;i<=9;i++) { }
number[i] = new JButton(String.valueOf(i)); number[i].setForeground(Color.blue);
public MyCalculator(String title) //程序初始化
{
super(title);
contentPane = getContentPane();
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER)); JMenuBar menubar = new JMenuBar(); JMenu editmenu = new JMenu(\"编辑\"); JMenu helpmenu = new JMenu(\"帮助\"); JMenuItem copyitem = new JMenuItem(\"复制\"); JMenuItem pasteitem = new JMenuItem(\"粘贴\"); editmenu.add(copyitem); editmenu.add(pasteitem);
menubar.add(editmenu); menubar.add(helpmenu); setJMenuBar(menubar);
JPanel jp1 = new JPanel();//添加文本框 show1 = new JTextField(20); show1.setEditable(false); show1.setBackground(Color.white); jp1.add(show1); contentPane.add(jp1);
JPanel jp2 = new JPanel();//向jp2面板添加按钮 layout = new GridBagLayout();
constraints = new GridBagConstraints();
jp2.setLayout(layout);
NumberListener number_listener = new NumberListener(); OperatorListener operator_listener = new OperatorListener(); MemoryListener memory_listener = new MemoryListener(); addButton(jp2,\"MC\加入按钮 addButton(jp2,\"MR\addButton(jp2,\"MS\addButton(jp2,\"M+\addButton(jp2,\"M-\addButton(jp2,\"<—\addButton(jp2,\"CE\addButton(jp2,\"C\addButton(jp2,\"±\1,1,number_listener); addButton(jp2,\"sin\addButton(jp2,\"7\addButton(jp2,\"8\addButton(jp2,\"9\addButton(jp2,\"+\addButton(jp2,\"cos\addButton(jp2,\"4\addButton(jp2,\"5\addButton(jp2,\"6\addButton(jp2,\"-\addButton(jp2,\"x^y\addButton(jp2,\"1\addButton(jp2,\"2\addButton(jp2,\"3\addButton(jp2,\"*\addButton(jp2,\"n!\addButton(jp2,\"0\addButton(jp2,\". \addButton(jp2,\"/\addButton(jp2,\"=\
}
}
contentPane.add(jp2);
StartSymbol = true; ResultSymbol = true; result = 0; operator = \"=\";
private void addButton(JPanel p,String label, int row, int column, int with,
int height, ActionListener listener) {
JButton button = new JButton(label); button.setBounds(10,10,10,10); constraints.gridx = row; constraints.gridy = column; constraints.ipady = 10; constraints.gridwidth = with; constraints.gridheight = height;
constraints.fill = GridBagConstraints.HORIZONTAL; button.addActionListener(listener); layout.setConstraints(button, constraints); p.add(button); }
public static void main(String[] args) {
// TODO Auto-generated method stub
MyCalculator myc = new MyCalculator(\"计算器\"); myc.setSize(300,400); myc.setResizable(false); myc.setVisible(true); }
applet版本的迷你计算器
package designApplet;
import java.applet.Applet; import java.awt.FlowLayout; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JTextField;
public class AplCalculator extends Applet{
private GridBagLayout layout;
private GridBagConstraints constraints; JTextField show;
public boolean StartSymbol; private boolean ResultSymbol; private double result; private double memory; private String operator;
public class NumberListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String input = e.getActionCommand(); if(StartSymbol)
{ StartSymbol = false; show.setText(\"\");
if(input.equals(\"±\"))
show.setText(show.getText() + \"-\");
}
if (!input.equals(\"±\")) {
if(input == \"CE\") { show.setText(\"\"); //input=null;
StartSymbol = true; }else if(input == \"C\") { show.setText(\"0\");
//input=null; operator = \"=\";
StartSymbol = true;
}else if(input == \"<—\") { String str = show.getText();
if(!ResultSymbol&&str.length()
show.setText(str.substring(0, str.length() - 1));
else show.setText(\"\" + result);
}else { show.setText(show.getText() + input);
ResultSymbol = false;
} }
}
>
0)
}
public class OperatorListener implements ActionListener {
public void actionPerformed(ActionEvent e) { }
public void DBCalculator(double y) {
if(operator == \"+\") {
result += y;
String command = e.getActionCommand(); if(StartSymbol) {
operator = command; SGCalculator(operator);
//SGCalculator(Double.valueOf(show.getText()));
}else { }
DBCalculator(Double.valueOf(show.getText())); operator = command; SGCalculator(operator); StartSymbol = true; ResultSymbol = true;
}else if(operator == \"-\") {
result -= y;
}else if(operator == \"*\") {
result *= y;
}else if(operator == \"/\")
}
}
{
result /= y;
}else if(operator == \"x^y\") {
double x = result;
for(;y>1;y--) result *= x;
}else if(operator == \"=\") { }
show.setText(\"\" + result);
result = y;
public void SGCalculator(String operator) { }
if(operator == \"sin\"){
result = Math.sin(result);
}else if(operator == \"cos\"){
result = Math.cos(result);
}else if(operator == \"n!\"){ }
show.setText(\"\" + result);
for(double x = result -1;x>1;x--)
result *= x;
public class MemoryListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String press = e.getActionCommand();
}
if(press == \"MC\")
{
memory = 0;
show.setText(\"\"+memory);
}else if(press == \"MR\") {
show.setText(\"\"+memory);
}else if(press == \"MS\") {
memory = Double.valueOf(show.getText());
}else if(press == \"M+\") {
memory += Double.valueOf(show.getText());
}else if(press == \"M-\") { } }
StartSymbol = true;
memory -= Double.valueOf(show.getText());
public void init()
{
layout = new GridBagLayout();
constraints = new GridBagConstraints(); show = new JTextField(20); setLayout(layout);
constraints.gridx = 0;
constraints.gridy =1; constraints.ipady = 10; constraints.gridwidth = 5; constraints.gridheight = 1;
constraints.fill = GridBagConstraints.HORIZONTAL;
//button.addActionListener(listener); layout.setConstraints(show, constraints); add(show);
NumberListener number_listener = new NumberListener(); OperatorListener operator_listener = new OperatorListener(); MemoryListener memory_listener = new MemoryListener();
addButton(\"MC\加入按钮
addButton(\"MR\addButton(\"MS\addButton(\"M+\addButton(\"M-\addButton(\"<—\addButton(\"CE\addButton(\"C\addButton(\"±\ addButton(\"sin\addButton(\"7\addButton(\"8\addButton(\"9\addButton(\"+\addButton(\"cos\addButton(\"4\addButton(\"5\addButton(\"6\addButton(\"-\addButton(\"x^y\addButton(\"1\addButton(\"2\addButton(\"3\addButton(\"*\addButton(\"n!\addButton(\"0\addButton(\".\
addButton(\"/\addButton(\"=\
}
private void addButton(String label, int row, int column, int with,int
height,ActionListener listener){//, ActionListener listener }
}
JButton button = new JButton(label); JPanel pane = new JPanel(); pane.setSize(10, 10);
pane.setLayout(new FlowLayout(FlowLayout.CENTER));
constraints.gridx = row; constraints.gridy = column; constraints.ipady = 10; constraints.gridwidth = with; constraints.gridheight = height;
constraints.fill = GridBagConstraints.HORIZONTAL; button.addActionListener(listener); layout.setConstraints(button, constraints); //pane.add(button); //add(pane); add(button);
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- sceh.cn 版权所有 湘ICP备2023017654号-4
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务