图形计算课程设计

  • 格式:docx
  • 大小:715.60 KB
  • 文档页数:23

课程设计报告课程名称:Java语言程序设计课程设计学生姓名:X丨GEEK班级学号:指导教师:完成日期:目录1.需求设计 (3)(1)总体要求: (3)(2)具体要求: (3)(3)完成课题目的: (3)2.概要设计 (4)3.详细设计 (4)4.主要界面 (7)5.完整代码 (8)6.测试 (20)7.用户手册 (21)8.已知问题 (22)9.总结与体会 (22)10.参考文献 (22)(1)总体要求:1.每组完成一个课题,由组长决定各成员的分工及组内排名(将100分按排名分配,每人最高不得超过50分);2.课题必须使用Java语言及其相关技术实现,并且要体现出良好的面向对象程序设计原则(尽可能使用封装、继承及多态,一般不将全部语句写在main()中);3.可以使用NetBeans及Eclipse等IDE开发程序,也可直接使用文本编辑器。

4.程序的界面必须采用图形用户界面(GUI),输入输出应使用合理组件,程序界面的布局应简洁明了;5.程序应具有良好的鲁棒性,即能够处理各种数据,对非法输入有完善的纠错机制;6.程序运行结果一般以文本形式保存进文件。

(2)具体要求:用程序实现求矩形、圆形、三角形的周长与面积,以及三角形的内角,需要定义类Triangle,Rectangle及Circle。

全部数据保存至文件中。

GUI设计要求:三个文本区分别用来输入三角形的三条边,或矩形的两边长,或圆形的半径,点击按钮时计算边长及面积,若是三角形,还要计算三个内角,并在一个文本区上显示结果。

可以用组合框选择图形的形状,也可以根据输入的数据个数判别。

(3)完成课题目的:了解并掌握中小型综合应用程序的一般设计方法,具备初步的独立分析和设计能力;初步掌握软件开发过程的问题分析、系统设计、程序编码、测试等基本方法和技能;提高综合运用所学的理论知识和方法独立分析和解决问题的能力;训练用系统的观点和软件开发一般规范进行软件开发,培养软件工作者所应具备的科学的工作方法和作风。

3.详细设计介绍小组成员 给出建议计算圆形面积及周长 计算矩形面积及周长计算三角形面积及周长4.主要界面5.完整代码/** To change this template, choose Tools | Templates* and open the template in the editor.*/packagegeometrycalculator;import java.io.* ;/**** @author X*/public class RectangleClass {double m;double n;boolean a;public RectangleClass(double m,double n){ //构造方法,用来构造长为m 宽为n的对象this.m = m;this.n = n;}public booleantestRectangle(){ //成员方法判断长和宽是否为0if(this.m>0&&this.n>0)this.a = true;elsethis.a= false;return a;}public double getRectangleArea(){ //成员方法计算面积return m*n;}public double Rectanglecircumference(){ //成员方法就算周长return 2*(m+n);}public void saveRectangledata(){try{FileWriterfw= new FileWriter("Rectangledata.txt",true) ; //以Rectangledata.txt为对象建文件字符输出流fwBufferedWriterbw= new BufferedWriter(fw); //以fw为参数建缓冲字符输出流bwString str = "边长:"+m+","+n+","+"面积:" +(m*n)+","+"周长:"+2*(m+n);// 准备一个字符串bw.write(str) ; //在bw中写入str中的内容bw.newLine(); //在bw 中写入换行符bw.close() ;//关闭所有流}catch(Exception e){}}}/** To change this template, choose Tools | Templates* and open the template in the editor.*/packagegeometrycalculator;import java.io.* ;/**** @author X*/public class CircleClass {double r;boolean a;public CircleClass(double r){ //构造方法,用来构造半径为r的对象this.r = r;}public double getCircleSide(){ //成员方法获取对象的半径returnthis.r;}public booleantestCircle(){ //判断半径是否为正if(this.r<0)this.a = false;elsethis.a= true;return a;}public double getCircleArea(){ //成员方法用来计算圆的面积return 3.14*this.r*this.r;}public double circumference(){ //成员方法用来计算圆的周长return 2*3.14*this.r;}public void saveCircledata(){try{FileWriterfw= new FileWriter("Circledata.txt",true) ; //以Circledata.txt 为参数建文件字符输出流fwBufferedWriterbw= new BufferedWriter(fw) ; //以fw为参数建缓冲字符输出流bwString str = "半径:"+r+","+"周长:"+2*3.14*this.r+"面积:"+3.14*this.r*this.r;// 准备一个字符串bw.write(str) ; //将str的内容写入bwbw.newLine(); //在bw中写入换行符bw.close() ; //关闭所有流}catch(Exception e){}}}/** To change this template, choose Tools | Templates* and open the template in the editor.*/packagegeometrycalculator;/**** @author X*/public class Circle extends javax.swing.JFrame {/*** Creates new form Circle*/public Circle() {initComponents();setTitle("跨平台·跨时代的伟大几何计算器");}/*** This method is called from within the constructor to initialize the form.* WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor.*/@SuppressWarnings("unchecked")// <editor-fold defaultstate="collapsed" desc="Generated Code">private void initComponents() {jComboBox1 = new javax.swing.JComboBox();jLabel6 = new javax.swing.JLabel();jPanel1 = new javax.swing.JPanel();jLabel1 = new javax.swing.JLabel();jLabel3 = new javax.swing.JLabel();jTextField1 = new javax.swing.JTextField();jTextField3 = new javax.swing.JTextField();jLabel4 = new javax.swing.JLabel();jLabel5 = new javax.swing.JLabel();jTextField4 = new javax.swing.JTextField();jButton1 = new javax.swing.JButton();jSeparator2 = new javax.swing.JSeparator();jButton3 = new javax.swing.JButton();jButton4 = new javax.swing.JButton();jMenuBar1 = new javax.swing.JMenuBar();jMenu1 = new javax.swing.JMenu();jMenuItem1 = new javax.swing.JMenuItem();jMenuItem2 = new javax.swing.JMenuItem(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "圆形", "矩形", "三角形" }));jComboBox1.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEventevt) {jComboBox1ActionPerformed(evt);}});jLabel6.setText("选择你想要计算的图形:");jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "圆形", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 0)));jLabel1.setText("半径r :");jLabel3.setText("面积:");jTextField1.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEventevt) {jTextField1ActionPerformed(evt);}});jLabel4.setText("请输入圆形数据:");jLabel5.setText("周长:");jButton1.setText("计算");jButton1.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEventevt) {jButton1ActionPerformed(evt);}});javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout);jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGroup(jPanel1Layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE).addComponent(jLabel1).addGap(42, 42, 42).addComponent(jTextField1,javax.swing.GroupLayout.PREFERRED_SIZE, 100,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(77, 77, 77)).addComponent(jSeparator2,javax.swing.GroupLayout.Alignment.TRAILING).addGroup(jPanel1Layout.createSequentialGroup().addGap(41, 41, 41).addComponent(jButton1).addPreferredGap(ponentPlacement .RELATED, 104, Short.MAX_VALUE).addGroup(jPanel1Layout.createParallelGroup(javax.swing.Group Layout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup().addComponent(jLabel3).addPreferredGap(ponent Placement.UNRELATED).addComponent(jTextField3,javax.swing.GroupLayout.PREFERRED_SIZE, 100,javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup().addComponent(jLabel5).addPreferredGap(ponent Placement.UNRELATED).addComponent(jTextField4,javax.swing.GroupLayout.PREFERRED_SIZE, 100,javax.swing.GroupLayout.PREFERRED_SIZE))).addContainerGap()));jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jLabel4,javax.swing.GroupLayout.PREFERRED_SIZE, 20,javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(ponentPlacement .RELATED).addGroup(jPanel1Layout.createParallelGroup(javax.swing.Group Layout.Alignment.BASELINE).addComponent(jTextField1,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel1)).addGap(18, 18, 18).addComponent(jSeparator2,javax.swing.GroupLayout.PREFERRED_SIZE, 3,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18, 18, 18).addGroup(jPanel1Layout.createParallelGroup(javax.swing.Group Layout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout.createParallelGroup(javax.swi ng.GroupLayout.Alignment.BASELINE).addComponent(jLabel3).addComponent(jTextField3,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addGap(9, 9, 9).addGroup(jPanel1Layout.createParallelGroup(javax.swi ng.GroupLayout.Alignment.BASELINE).addComponent(jTextField4,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel5))).addComponent(jButton1,javax.swing.GroupLayout.PREFERRED_SIZE, 52,javax.swing.GroupLayout.PREFERRED_SIZE)).addContainerGap(44, Short.MAX_VALUE)));jButton3.setText("清空");jButton3.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEventevt) {jButton3ActionPerformed(evt);}});jButton4.setText("退出");jButton4.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEventevt) {jButton4ActionPerformed(evt);}});jMenu1.setText("菜单栏");jMenuItem1.setText("温馨建议");jMenuItem1.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEventevt) {jMenuItem1ActionPerformed(evt);}});jMenu1.add(jMenuItem1);jMenuItem2.setText("制作人员");jMenuItem2.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEventevt) {jMenuItem2ActionPerformed(evt);}});jMenu1.add(jMenuItem2);jMenuBar1.add(jMenu1);setJMenuBar(jMenuBar1);javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(58, 58, 58).addComponent(jLabel6).addPreferredGap(ponentPlacement .RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jComboBox1,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(62, 62, 62)).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addContainerGap()).addGroup(layout.createSequentialGroup().addGap(81, 81, 81).addComponent(jButton3).addPreferredGap(ponentPlacement .RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(jButton4).addGap(68, 68, 68)));layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(13, 13, 13).addGroup(layout.createParallelGroup(javax.swing.GroupLayout. Alignment.BASELINE).addComponent(jComboBox1,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addComponent(jLabel6)).addPreferredGap(ponentPlacement .RELATED).addComponent(jPanel1,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18, 18, 18).addGroup(layout.createParallelGroup(javax.swing.GroupLayout. Alignment.BASELINE).addComponent(jButton3).addComponent(jButton4)).addContainerGap(92, Short.MAX_VALUE)));pack();}// </editor-fold>private void jButton1ActionPerformed(java.awt.event.ActionEventevt) {//计算按钮的事件try{ //异常处理,看创建的对象是否是数字CircleClass c = new CircleClass(Double.parseDouble(jTextField1.getText())); /*创建CircleClass的对象c,参数为jTextField1中的内容,* jTextField1.getText()获取jTextField1中的内容(结果是String型),所以要用Double.parseDouble()转换成double型*/if(c.testCircle()){ //调用CircleClass中的testCircle()方法看半径是否为负jTextField4.setText(String.valueOf(Round.getRound(c.circumference())));/*调用CircleClass中的circumference()方法计算周长,再调用Round类中的静态方法getRound()使结果保留2位小数,再用String.valueOf()转换成字符串,输出在jTextField4中*/jTextField3.setText(String.valueOf(Round.getRound(c.getCircleArea())));/*同上,这句用来计算面积,结果保存在jTextFiled3中*/c.saveCircledata(); /*调用CircleClass中的saveCircledata()来保存数据*/}else{ /*如果圆的半径为负数*/new Error().setVisible(true);//使报错窗口Error出现jTextField1.setText(""); //清空jTextField1jTextField4.setText(""); //清空jTextField4jTextField3.setText(""); //清空jTextField3 }}catch(Exception e){ //如果抓取到用户未输入数据,或输入的不是数字的异常new Error3().setVisible(true); //使报错窗口Error3出现jTextField1.setText(""); //清空jTextField1jTextField3.setText(""); //清空jTextField3jTextField4.setText(""); //清空jTextField4 }// TODO add your handling code here:}private void jButton3ActionPerformed(java.awt.event.ActionEventevt) { //清空按钮的事件jTextField1.setText(""); // 清空jTextField1jTextField3.setText(""); //清空jTextField2jTextField4.setText(""); //清空jTextField3// TODO add your handling code here:}private void jButton4ActionPerformed(java.awt.event.ActionEventevt) {//退出按钮的事件System.exit(0); //退出程序// TODO add your handling code here:}private void jTextField1ActionPerformed(java.awt.event.ActionEventevt) { // TODO add your handling code here:}private void jComboBox1ActionPerformed(java.awt.event.ActionEventevt) { //组合框的事件String a = (String) jComboBox1.getSelectedItem();//用getSelectedItem()方法获取jComboBox1(组合框)中的内容,赋值给a if(a.equals("矩形")){new Rectangle().setVisible(true); //若a的内容为矩形,则显示矩形的GUI窗口}else if(a.equals("三角形")){new Triangle().setVisible(true); //若a 的内容为三角形,则显示三角形的GUI窗口}else{new Circle().setVisible(true); //若a 的内容为圆,则显示圆的GUI窗口}setVisible(false); //关闭当前的GUI窗口// TODO add your handling code here:}private void jMenuItem1ActionPerformed(java.awt.event.ActionEventevt) {new Suggest().setVisible(true); // TODO add your handling code here: }private void jMenuItem2ActionPerformed(java.awt.event.ActionEventevt) {new Producer().setVisible(true); // TODO add your handling code here: }/*** @paramargs the command line arguments*/public static void main(String args[]) {/* Set the Nimbus look and feel *///<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.* For details see/javase/tutorial/uiswing/lookandfeel/plaf.html */try {for (javax.swing.UIManager.LookAndFeelInfo info :javax.swing.UIManager.getInstalledLookAndFeels()) {if ("Nimbus".equals(info.getName())) {javax.swing.UIManager.setLookAndFeel(info.getClassName());break;}}} catch (ClassNotFoundException ex) {java.util.logging.Logger.getLogger(Circle.class.getName()).log(java.util.logging.Level.S EVERE, null, ex);} catch (InstantiationException ex) {java.util.logging.Logger.getLogger(Circle.class.getName()).log(java.util.logging.Level.S EVERE, null, ex);} catch (IllegalAccessException ex) {java.util.logging.Logger.getLogger(Circle.class.getName()).log(java.util.logging.Level.S EVERE, null, ex);} catch (javax.swing.UnsupportedLookAndFeelException ex) {java.util.logging.Logger.getLogger(Circle.class.getName()).log(java.util.logging.Level.S EVERE, null, ex);}//</editor-fold>/* Create and display the form */java.awt.EventQueue.invokeLater(new Runnable() {public void run() {new Circle().setVisible(true);}});}// Variables declaration - do not modifyprivatejavax.swing.JButton jButton1;privatejavax.swing.JButton jButton3;privatejavax.swing.JButton jButton4;privatejavax.swing.JComboBox jComboBox1;privatejavax.swing.JLabel jLabel1;privatejavax.swing.JLabel jLabel3;privatejavax.swing.JLabel jLabel4;privatejavax.swing.JLabel jLabel5;privatejavax.swing.JLabel jLabel6;privatejavax.swing.JMenu jMenu1;privatejavax.swing.JMenuBar jMenuBar1;privatejavax.swing.JMenuItem jMenuItem1;privatejavax.swing.JMenuItem jMenuItem2;privatejavax.swing.JPanel jPanel1;privatejavax.swing.JSeparator jSeparator2;privatejavax.swing.JTextField jTextField1;privatejavax.swing.JTextField jTextField3;privatejavax.swing.JTextField jTextField4;// End of variables declaration}6.测试测试环境:windows764位操作系统内存4.00GB处理器Intel(R)Core(TM)i7-3610QM 2.30GHzNetBeans8.0java version "1.7.0_40"Java(TM) SE Runtime Environment (build 1.7.0_40-b43)Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode) 输入为空时(,,,):输入字符时(三,四,5):输入三个角都为60时:输入负数(-3,4,5)时:当输入(3,4,9)当输入(0,0,0)时当输入(0.00001,0.00001,0.00001)时当输入(3,4,5)时7.用户手册本程序可以用来求矩形,三角形,圆形三类基本图形的面积及周长。