《Java课程设计》计划
班级:物联网111班 任课教师:
一.设计目的
1.通过课程设计把《Java程序设计》这门课所学的内容融会贯通,学会Java设计程序、开发应用软件的基本能力。
2.通过设计能使学生更好掌握JAVA面向对象基本编程技术和方法。
3.培养学生查阅文献资料及编写技术报告的能力。
二.设计题目
简易科学计算器
内容:(1)通过菜单可以切换简易计算器和科学计算器。简易计算器:实现连续的+、-、*、/运算,计算功能符合四则运算法。科学计算器:至少包括开方、平方、幂运算、1/n等操作。(2)图形用户界面美观、布局合理、功能操作简便。
三.设计要求
《Java课程设计》是物联网专业学生在完成《Java程序设计》课程学习后进行的一门专业实践课程,是培养学生综合运用所学知识解决专业相关问题的重要环节,是对学生实际工作能力的具体训练和考察过程。本次课程设计的题目使用Java语言来开发。要求每个学生可以独立完成上述Java基础设计题目,同时可以通过同学间的讨论,共同编写比较复杂的综合课程设计题目。希望学学能够努力实践,提高实际动手能力。要求每个学生必须参加,并完成课程设计任务:
(1)每个学生明确设计目的和要求,根据自己的实际情况选择一个题目,实际独立动手完成这个题目,编写出程序代码,并调试通过。
(2)每个学生必须在上机前做好充分准备,每次上机必须目的明确,事先编写好程序代码,要在规定的时间和地点上机调试。要求利用MyEclipse开发工具调试通过
(3)每个学生程序完成后撰写《Java课程设计报告》,按照《Java课程设计计划》后面附带的报告模版格式实际撰写。
(4)结果形式:程序文件压缩包一个,课程设计报告一份。
四.进度安排
(1)准备阶段(1天):选择设计题目、了解设计目的要求、查阅相关资料。
(2)程序模块设计分析(1天):程序总体设计、详细设计
(3)代码编写调试(2天):程序代码编写、调试、测试。
(4)撰写报告(1天):总结课程设计任务和内容。
五.参考文献
[1] 胡伏湘.Java程序设计实用教程(第2版).北京:清华大学出版社,2009
[2] 叶核亚.Java程序设计实用教程(第3版).北京:电子工业出版社,2010
[3] 耿详义. Java课程设计(第1版). 北京:清华大学出版社,2004
六.成绩评定
成绩按五级制评定。对完成的设计结果(40%)进行测试,结合答辩情况(30%)和课程设计报告(30%),进行成绩评定。
七、课程设计报告要求
1.主要提现内容:
(1)本课题做了哪些工作、实现了什么结果、系统功能,系统运行软件、硬件环境。
(2)系统数据结构设计,主要包括数据信息的组织存储方式
(3)画出系统算法的总体结构图。
(4)系统功能的具体实现。
(5)简要总结及自我评价
2.报告格式:封皮和内容目录格式
Java课 程 设 计 报 告
学校系别:
专业名称: 课程设计科目
学生姓名:
指导教师: 完成时间: 计算机科学系 Java课程设计 2012-11-30
简易科学计算器
一、设计任务与目标(标题均为小三号,宋体)
简易科学计算器:简易计算器实现连续的+、-、*、/运算,计算功能符合四则运算法。科学计算器:至少包括开方、平方、幂运算、1/n等操作。(2)图形用户界面美观、布局合理、功能操作简便。
(正文均为小四号,宋体,行距1.5倍)
(对所选择题目进行分析论证,简要说明此类问题的已有类似工作,我要完成时准备做那些改进,既说明完成此设计的目的意义。列出各项本设计要达到的具体的目标。)
二、.设计思路
(对设计目标进行总体分析,说明要采用的基本思路,说明遇到的问题和解决方法。说明完成本次课程设计的完整过程。要描述程序的设计思想,画出本次课程设计完整的程序框图或流程图。表格的形式列出所有编写的程序清单,划出程序的调用关系图)
三、具体实现
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ert implements ActionListener {
JFrame zct;
JTextField wbk;
JButton an;
JButton gnj;
JButton gnj2;
JButton bM;
boolean isDouble=false;//是否为实数
int opFlag=-1;
static double t1=0,t2=0,t3=0,result=0;
static int opflag1=-1,opflag2=-1,flag=0,resflag=1;
int preOp,currentOp=0;//标准位
double op1=0,op2=0;//操作数
double n3;
StringBuffer str=new StringBuffer(20);
StringBuffer memory=new StringBuffer(20); //Java计算器 构造器
public ert()
{
zct = new JFrame("计算器");
Container contentPane = zct.getContentPane(); //Java计算器 菜单的创建
contentPane.setLayout(new BorderLayout()); JPanel pTop = new JPanel();
wbk = new JTextField("0.",24);
wbk.setHorizontalAlignment(JTextField.RIGHT); wbk.setEditable(false);
pTop.add(wbk);
contentPane.add(pTop,BorderLayout.NORTH); JPanel pBottom = new JPanel();
pBottom.setLayout(new BorderLayout()); JPanel pLeft = new JPanel();
pLeft.setLayout(new GridLayout(5,1,3,3)); //Java计算器 功能键定义
pBottom.add(pLeft,BorderLayout.WEST); JPanel pRight = new JPanel();
pRight.setLayout(new BorderLayout()); JPanel pUp = new JPanel();
pUp.setLayout(new GridLayout(1,1,1,0));
gnj2 = new JButton("CE");
gnj2.addActionListener(this);
gnj2.setForeground(Color.red);
pUp.add(gnj2);
gnj2 = new JButton("C");
gnj2.addActionListener(this);
gnj2.setForeground(Color.red);
pUp.add(gnj2);
//Java计算器 数字键盘区定义
JPanel pDown = new JPanel();
pDown.setLayout(new GridLayout(4,5,3,2)); an = new JButton("7");
an.setForeground(Color.blue);
an.addActionListener(this);
an.setMargin(new Insets(3,3,3,3)); pDown.add(an);
an = new JButton("8");
an.setForeground(Color.blue);
an.addActionListener(this);
an.setMargin(new Insets(3,3,3,3)); pDown.add(an);
an = new JButton("9");
an.setForeground(Color.blue);
an.addActionListener(this);
an.setMargin(new Insets(3,3,3,3)); pDown.add(an);
gnj = new JButton("/");
gnj.setForeground(Color.red);
gnj.addActionListener(this);
gnj.setMargin(new Insets(3,0,3,0)); pDown.add(gnj);
gnj = new JButton("开平方");
gnj.addActionListener(this);
gnj.setForeground(Color.red);
gnj.setMargin(new Insets(3,0,3,0)); pDown.add(gnj);
an = new JButton("4");
an.setForeground(Color.blue);
an.addActionListener(this);
an.setMargin(new Insets(3,3,3,3));
an.setHorizontalTextPosition(JButton.LEFT); pDown.add(an);
an = new JButton("5");
an.setForeground(Color.blue);
an.addActionListener(this);
an.setMargin(new Insets(3,3,3,3)); pDown.add(an);
an = new JButton("6");
an.setForeground(Color.blue);
an.addActionListener(this);
an.setMargin(new Insets(3,3,3,3)); pDown.add(an);
gnj = new JButton("*");
gnj.setForeground(Color.red);
gnj.addActionListener(this);
gnj.setMargin(new Insets(3,3,3,3));
pDown.add(gnj);
gnj = new JButton("幂运算"); gnj.setForeground(Color.red); gnj.addActionListener(this);
gnj.setMargin(new Insets(3,3,3,3)); pDown.add(gnj);
an = new JButton("1");
an.setForeground(Color.blue); an.addActionListener(this);
an.setMargin(new Insets(3,3,3,3)); pDown.add(an);
an = new JButton("2");
an.setForeground(Color.blue); an.addActionListener(this);
an.setMargin(new Insets(3,3,3,3)); pDown.add(an);
an = new JButton("3");
an.setForeground(Color.blue); an.addActionListener(this);
an.setMargin(new Insets(3,3,3,3)); pDown.add(an);
gnj = new JButton("-");
gnj.setForeground(Color.red); gnj.addActionListener(this);
gnj.setMargin(new Insets(3,3,3,3)); pDown.add(gnj);
gnj = new JButton("-/+");
gnj.setForeground(Color.red); gnj.addActionListener(this); pDown.add(gnj);
an = new JButton("1/x");
an.setForeground(Color.red); an.addActionListener(this);
an.setMargin(new Insets(3,3,3,3)); pDown.add(an);
gnj = new JButton("0");
gnj.setForeground(Color.blue); gnj.addActionListener(this);
gnj.setMargin(new Insets(3,3,3,3)); pDown.add(gnj);
gnj = new JButton(".");
gnj.setForeground(Color.red); gnj.addActionListener(this);
gnj.setMargin(new Insets(3,3,3,3)); pDown.add(gnj);
gnj = new JButton("+");
gnj.setForeground(Color.red);
gnj.addActionListener(this);
gnj.setMargin(new Insets(3,3,3,3));
pDown.add(gnj);
gnj = new JButton("=");
gnj.setForeground(Color.red);
gnj.addActionListener(this);
gnj.setMargin(new Insets(3,3,3,3));
pDown.add(gnj);
pRight.add(pUp,BorderLayout.NORTH);
pRight.add(pDown,BorderLayout.SOUTH);
pBottom.add(pRight,BorderLayout.EAST);
contentPane.add(pBottom,BorderLayout.SOUTH);
zct.setSize(new Dimension(270,215));
zct.setResizable(false);
zct.setVisible(true);
zct.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
//Java计算器 计算方法区
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("CE"))
{
//如果是CE则清除文本框
wbk.setText("0.");
}
else if(s.equals("BackSpace"))
{
if(!wbk.getText().trim().equals("0."))
{
//如果文本框中有内容
if(str.length()!=1 && str.length()!=0)
{
wbk.setText(str.delete(str.length()-1,str.length()).toString()); }
else
{
wbk.setText("0.");
str.setLength(0);
}
}
op2 = Double.parseDouble(wbk.getText().trim());
}
else if(s.equals("C"))
{
//如果是C删除当前计算
wbk.setText("0.");
op1 = op2 = 0;
str.replace(0, str.length(), " ");
preOp = currentOp = 0;
}
else if(s.equals("1/x"))
{
String temp = wbk.getText().trim();
double dtemp = Double.parseDouble(temp);
wbk.setText(""+1/dtemp);
}
else if(s.equals("开平方"))
{
String temp = wbk.getText().trim();
double dtemp = Double.parseDouble(temp);
wbk.setText(""+Math.sqrt(dtemp));
}
else if(s.equals("-/+"))
{
String temp = wbk.getText().trim();
double dtemp = Double.parseDouble(temp);
wbk.setText("-"+dtemp);
}
else if(s.equals("幂运算"))
{
str.setLength(0);
if(currentOp==0)
{
preOp=currentOp=5; //op1=op2;op2=0; wbk.setText(""+op1);
}
else
{
currentOp =preOp;
preOp =5;
switch(currentOp){
case 1:op1=op1+op2;wbk.setText(""+op1);break; case 2:op1=op1-op2;wbk.setText(""+op1);break; case 3:op1=op1*op2;wbk.setText(""+op1);break; case 4:op1=op1/op2;wbk.setText(""+op1);break; case 5:wbk.setText(""+Math.pow(op1,op2));break; }
}
}
else if(s.equals("+"))
{
str.setLength(0);
if(currentOp==0)
{
preOp = currentOp = 1;
op2 = 0;
wbk.setText(""+op1);
}
else
{
currentOp = preOp;
preOp = 1;
switch(currentOp){
case 1:
op1 += op2;
wbk.setText(""+op1); break;
case 2:
op1 -= op2;
wbk.setText(""+op1); break;
case 3:
op1 *= op2;
wbk.setText(""+op1); break;
case 4:
op1 /= op2;
wbk.setText(""+op1); break;
}
}
}
else if(s.equals("-")){
str.setLength(0);
if(currentOp==0)
{
preOp=currentOp=2; //op1=op2;op2=0; wbk.setText(""+op1);
}
else
{
currentOp =preOp;
preOp =2;
switch(currentOp){
case 1:op1=op1+op2;wbk.setText(""+op1);break;
case 2:op1=op1-op2;wbk.setText(""+op1);break;
case 3:op1=op1*op2;wbk.setText(""+op1);break;
case 4:op1=op1/op2;wbk.setText(""+op1);break;
}
}
}
else if(s.equals("*"))//*
{
str.setLength(0);
if(currentOp==0)
{
preOp=currentOp=3; //op1=op2;op2=1; wbk.setText(""+op1);//op1=op2;
}
else
{
currentOp =preOp;
preOp =3;
switch(currentOp){
case 1:op1=op1+op2;wbk.setText(""+op1);break;
case 2:op1=op1-op2;wbk.setText(""+op1);break;
case 3:op1=op1*op2;wbk.setText(""+op1);break;
case 4:op1=op1/op2;wbk.setText(""+op1);break;
}
}
}
else if(s.equals("/"))// /
{
str.setLength(0);
if(currentOp==0)
{
preOp=currentOp=4; //op2=1; wbk.setText(""+op1); //op1=op2; }
else
{
currentOp =preOp;
preOp =4;
switch(currentOp){
case 1:op1=op1+op2;wbk.setText(""+op1);break;
case 2:op1=op1-op2;wbk.setText(""+op1);break;
case 3:op1=op1*op2;wbk.setText(""+op1);break;
case 4:op1=op1/op2;wbk.setText(""+op1);break;
}
}
}
else if(s.equals("="))// =
{
if(currentOp==0)
{
str.setLength(0);
wbk.setText(""+op2);
}
else
{
str.setLength(0);
currentOp =preOp;
switch(currentOp){
case 1:op1=op1+op2;wbk.setText(""+op1);break;
case 2:op1=op1-op2;wbk.setText(""+op1);break;
case 3:op1=op1*op2;wbk.setText(""+op1);break;
case 4:op1=op1/op2;wbk.setText(""+op1);break;
case 5:wbk.setText(""+Math.pow(op1,op2));break;
}
currentOp=0;
op2=0;
}
}
else if(s.equals("."))
{
isDouble=true;
if(wbk.getText().trim().indexOf('.')!=-1);
else {
if(wbk.getText().trim().equals("0")) {
str.setLength(0);
wbk.setText((str.append("0"+s)).toString()); }
//else if(tResult.getText().trim().equals("")){}//如果初时显示为空则不做任何操作
else {
wbk.setText((str.append(s)).toString());
}
}
}
else if(s.equals("0")) //如果选择的是"0"这个数字键 {
if(wbk.getText().trim().equals("0.")){}
else{
wbk.setText(str.append(s).toString());
op2=Double.parseDouble(wbk.getText().trim());
}
}
else{
wbk.setText(str.append(s).toString());
op2=Double.parseDouble(wbk.getText().trim());
if(currentOp==0)
op1=op2;
}
} //end actionPerformed
public static void main(String[] args) {
new ert();
}
}
五、程序运行的测试与分析
(要围绕设计任务和目标中的各项指标进行。)
六、总结
(主要说明程序调试中发现的问题和解决办法,包括你学到了什么,哪里遇到了困难,解决的办法,可能但因时间关系没有来得及完成的想法,今后的目标等。)
七、参考资料
[1] 胡伏湘.Java程序设计实用教程(第2版).北京:清华大学出版社,2009
[2] 叶核亚.Java程序设计实用教程(第3版).北京:电子工业出版社,2010
[3] 耿详义. Java课程设计(第1版). 北京:清华大学出版社,2004