第4次作业类测试代码+105032014040+薛龚

类图

第4次作业类测试代码+105032014040+薛龚_第1张图片

代码:

package JudgeTriangle;

import java.text.DecimalFormat;
import java.util.Scanner;

public class JudgeTriangle {
    
    public static boolean Judge(String[] str){
        //判断输入是否为三个字符串
        if(str.length != 3){
            return false;
        }
        //利用正则表达式判断三个字符串是否为纯数字
        for(int i = 0; i < 3; i++){
            if(str[i].matches("[0-9]+")){
                continue;
            }
            else{
                return false;
            }
        }
        return true;
    }
    
    public static String triangle(int a, int b, int c){
        String result;
        if (a >= 1 && a <= 100 && b >= 1 && b <= 100 && c >= 1 && c <= 100){
            if (a<(b + c) && b<(a + c) && c<(a + b)){
                if (a == b&&b == c)  {
                    result = "等边三角形";
                }
                else if (a == b || a == c || b == c){
                    result = "等腰三角形";
                }
                else if (a * a == (b * b + c * c) || 
                        b * b == (a * a + c * c) || c * c ==( a * a + b * b)){
                    result = "直角三角形";
                }
                else{
                    result = "一般三角形";
                }
            }
            else{
                result = "不构成三角形";
            }
        }
        else{
            result = "边的值不在范围内";
        }
        return result;
    }

    
    public String triangleArea(int a,int b,int c){
        String temp = triangle(a, b, c);
        DecimalFormat df = new DecimalFormat("#.00");
        float result=0;        
        float p;    
        String mianjiresult = null;
        if(!temp.equals("边的值不在范围内") &&!temp.equals("不构成三角形"))
        {
            p=(float) ((a+b+c)/2.0);
            result=(float) (Math.sqrt(p*(p-a)*(p-b)*(p-c)));
            mianjiresult=df.format(result);
        }          
        return mianjiresult;
    }    
    
    public  int Perimeter(int a,int b,int c){
        String temp = triangle(a, b, c);
        int result=0;
        if(!temp.equals("边的值不在范围内") &&!temp.equals("不构成三角形"))
        {
            result=a+b+c;
        }
        return result;
    }
    
    
}

 

 

 

package JudgeTriangle;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.WindowEvent; 
import java.util.Arrays;
import java.awt.event.WindowAdapter; 

public class FrameTriangle {
    private JFrame frame = new JFrame("三角形计算程序");
    private JButton b1 = new JButton("OK");
    private JButton b2 = new JButton("Cancel");
    private JLabel lab1 = new JLabel("请输入三角形的三条边:");
    private JLabel length1 = new JLabel("a:");
    private JLabel length2 = new JLabel("b:");
    private JLabel length3 = new JLabel("c:");
    private JLabel define = new JLabel("这个三角形是:");
    private JLabel area = new JLabel("面积是:");
    private JLabel perimeter = new JLabel("周长是:");
    private JTextField a = new JTextField();
    private JTextField b = new JTextField();
    private JTextField c = new JTextField();
    private JTextField judge = new JTextField();
    private JTextField s = new JTextField();
    private JTextField per = new JTextField();
    JudgeTriangle t=new JudgeTriangle();
    public FrameTriangle(){
        b1.addActionListener(new ActionListener(){
        @Override
            public void actionPerformed(ActionEvent e){
                try{
                    int[] num;
                    num = new int[3];
                    num[0]=Integer.parseInt(a.getText());
                    num[1]=Integer.parseInt(b.getText());
                    num[2]=Integer.parseInt(c.getText());
                    if(num[0]>100||num[0]<1||num[1]>100||num[1]<1||num[2]>100||num[2]<1){
                        JOptionPane.showMessageDialog(null,"边的值不在范围内");
                        return;
                    }
                    Arrays.sort(num);
                    judge.setText(JudgeTriangle.triangle(num[0],num[1],num[2]));
                    s.setText(""+t.triangleArea(num[0],num[1],num[2]));
                    per.setText(""+t.Perimeter(num[0],num[1],num[2]));
                }catch(Exception ex){
                    JOptionPane.showMessageDialog(null,"请输入整数");
                 }
           }
       });
       b2.addActionListener(new ActionListener(){
           @Override
           public void actionPerformed(ActionEvent e) {
           a.setText("");
           b.setText("");
           c.setText("");
           judge.setText("");
           s.setText("");
           per.setText("");
       }});
       frame.addWindowListener(new WindowAdapter(){
           public void windowClosing(WindowEvent e){
               System.exit(1);
           }
       });
       
       frame.setLayout(null);
      
       lab1.setBounds(55,10,350,30);
       length1.setBounds(55,60,30,15);
       length2.setBounds(145,60,30,15);
       length3.setBounds(225,60,30,15);
       define.setBounds(55,130,300,30);
       area.setBounds(55,180,50,20);
       perimeter.setBounds(180,180,50,20);
       a.setBounds(70,55,50,25);
       b.setBounds(160,55,50,25);
       c.setBounds(240,55,50,25);
       judge.setBounds(140,130,150,30);
       s.setBounds(100,180,55,25);
       per.setBounds(230,180,55,25);
       b1.setBounds(65,90,100,24);
       b2.setBounds(175,90,100,24);
       frame.setLocationRelativeTo(null);
       frame.add(lab1);
       frame.add(length1);
       frame.add(length2);
       frame.add(length3);
       frame.add(define);
       frame.add(area);
       frame.add(perimeter);
       frame.add(a);
       frame.add(b);
       frame.add(c);
       frame.add(judge);
       frame.add(s);
       frame.add(per);
       frame.add(b1);
       frame.add(b2);
       frame.setSize(360,280);
       frame.setVisible(true);    
    }                 
    
    public static void main(String[] args){
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try{
                    FrameTriangle window = new FrameTriangle();
                    window.frame.setVisible(true);
                    }catch (Exception e){
                        e.printStackTrace();
                     }
            }
        });
    }    
}

 

结果:

第4次作业类测试代码+105032014040+薛龚_第2张图片

 

第4次作业类测试代码+105032014040+薛龚_第3张图片

 

你可能感兴趣的:(第4次作业类测试代码+105032014040+薛龚)