第4次作业类测试代码+105032014070+胡阳洋
1、类图。
2、代码及界面。
package test.java.distinguish.triangle; public class Distinguish { /** * * @param a 三角形边的数值 * @param b 三角形边的数值 * @param c 三角形边的数值 * @return 返回存在几对相等的边 */ private int SameLengthNum(int a,int b,int c){ int count = 0; if(a==b){ count++;//每存在一对相等的边计数加一 } if(a==c){ count++; } if(b==c){ count++; } return count; } /** * * @param a 三角形边的数值 * @param b 三角形边的数值 * @param c 三角形边的数值 * @return 返回三角形的类型 */ public String Triangle(int a,int b,int c){ String ch = null; if(!(1<=a&&a<=100) || !(1<=b&&b<=100) || !(1<=c&&c<=100)){ return ch="边的值不在范围内"; } if(!(a<(b+c)) || !(b<(a+c)) || !(c<(a+b))){ return ch="不构成三角形"; } switch(SameLengthNum( a, b, c)){//调用函数判断有几对边相等 case 0:{ if((a*a+b*b)==(c*c) || (a*a+c*c)==(b*b) || (c*c+b*b)==(a*a)){ ch="直角三角形"; break; } else{ ch="一般三角形"; break; } }//没有相等边 case 1: ch="等腰三角形";break;//一对边相等 case 3: ch="等边三角形";break;//三对边相等 } return ch; } /** * * @param value String型三角形边长数据数组 * @return false:存在不是整数的值或获取的数据不足 true:验证通过 */ public boolean Validation(String[] value){ if(value.length<3){//没有三个数据 return false; } try { for(int i=0;i<3;i++){ int b = Integer.valueOf(value[i]).intValue(); } } catch (NumberFormatException e){//不能转换成整型 return false; } return true; } /** * * @param a 三角形边的数值 * @param b 三角形边的数值 * @param c 三角形边的数值 * @return 返回三角形的周长 */ public int Perimeter(int a,int b,int c){ return a+b+c; } /** * * @param a 三角形边的数值 * @param b 三角形边的数值 * @param c 三角形边的数值 * @return 返回三角形的面积 */ public float TriangleArea(int a,int b,int c){ float s = (float) (0.5*Perimeter( a, b, c)); return (float) Math.sqrt((s*(s-a)*(s-b)*(s-c))); } }
界面代码:
package test.java.distinguish.view; import java.awt.EventQueue; import java.awt.Font; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import test.java.distinguish.triangle.Distinguish; public class DisTriangle{ private JFrame frame; private final JLabel label = new JLabel("请输入三角形的三条边:"); private final JLabel lblA = new JLabel("a:"); private final JLabel lblB = new JLabel("b:"); private final JLabel lblC = new JLabel("c:"); private final JLabel result_1 = new JLabel("这个三角形是:"); private final JLabel result_2 = new JLabel("面积是: 周长是:"); private final JTextField lengthOfa = new JTextField(); private final JTextField lengthOfb = new JTextField(); private final JTextField lengthOfc = new JTextField(); private final JButton calculate = new JButton("计算"); private final JButton clean = new JButton("清空"); private Distinguish dis; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { new DisTriangle(); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. * @wbp.parser.entryPoint */ public DisTriangle() { frame = new JFrame("三角形测算"); frame.setBounds((Toolkit.getDefaultToolkit().getScreenSize().width/2)-256, (Toolkit.getDefaultToolkit().getScreenSize().height/2)-192, 512, 384); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); frame.setVisible(true); frame.setResizable(false); label.setFont(new Font("宋体", Font.PLAIN, 20)); label.setBounds(55, 10, 350, 30); frame.getContentPane().add(label); result_1.setFont(new Font("宋体", Font.PLAIN, 20)); result_1.setBounds(55, 190, 350, 30); frame.getContentPane().add(result_1); result_2.setFont(new Font("宋体", Font.PLAIN, 20)); result_2.setBounds(55, 255, 350, 30); frame.getContentPane().add(result_2); lblA.setFont(new Font("宋体", Font.PLAIN, 20)); lblA.setBounds(55, 70, 30, 15); lblB.setFont(new Font("宋体", Font.PLAIN, 20)); lblB.setBounds(175, 72, 30, 15); lblC.setFont(new Font("宋体", Font.PLAIN, 20)); lblC.setBounds(295, 70, 30, 15); frame.getContentPane().add(lblA); frame.getContentPane().add(lblB); frame.getContentPane().add(lblC); lengthOfa.setBounds(95, 65, 70, 25); lengthOfb.setBounds(205, 65, 70, 25); lengthOfc.setBounds(315, 65, 70, 25); lengthOfa.setColumns(10); lengthOfb.setColumns(10); lengthOfc.setColumns(10); frame.getContentPane().add(lengthOfa); frame.getContentPane().add(lengthOfb); frame.getContentPane().add(lengthOfc); calculate.setBounds(115, 130, 95, 25); frame.getContentPane().add(calculate); clean.setBounds(290, 130, 95, 25); frame.getContentPane().add(clean); calculate.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0){ if(arg0.getSource()==calculate){ dis = new Distinguish(); String[] value = new String[3]; value[0] = lengthOfa.getText(); value[1] = lengthOfb.getText(); value[2] = lengthOfc.getText(); if(!dis.Validation(value)){//调用验证函数 JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "错误", JOptionPane.ERROR_MESSAGE); allClean(); } else{ int a = Integer.valueOf(value[0]).intValue();//已验证过不再进行异常处理 int b = Integer.valueOf(value[1]).intValue(); int c = Integer.valueOf(value[2]).intValue(); String triangleType = dis.Triangle(a,b,c); result_1.setText("这个三角形是:"+triangleType); if(triangleType.equals("边的值不在范围内")||triangleType.equals("不构成三角形")){ JOptionPane.showMessageDialog(null, "输入有误,请重新输入", "错误", JOptionPane.ERROR_MESSAGE); allClean(); } else{ result_2.setText("面积是:"+dis.TriangleArea(a, b, c)+" 周长是:"+dis.Perimeter(a, b, c)); } } } } }); clean.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent arg0){ if(arg0.getSource()==clean){ allClean(); } } }); } private void allClean(){ lengthOfa.setText(""); lengthOfb.setText(""); lengthOfc.setText(""); result_1.setText("这个三角形是:"); result_2.setText("面积是: 周长是:"); } }
程序运行图: