首页 > 代码库 > 第四次作业测试代码+016+兰东华
第四次作业测试代码+016+兰东华
1、类图
2、原代码增添计算面积和周长的功能
import java.util.Scanner;
public class homeWork01 {
public static String triangle(String s1,String s2,String s3){
String str="abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=";
if(str.indexOf(s1)!=-1||str.indexOf(s2)!=-1||str.indexOf(s3)!=-1){
return "输入的是非法字符";
}
//判断输入的值是否是整数
if(isNumeric(s1)&&isNumeric(s2)&&isNumeric(s3)){
}else{
return"输入的数字不是整数";
}
int a = 0,b=0,c=0;
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=Integer.parseInt(s3);
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){
return "等边三角形";
}
if(a==b||a==c||b==c){
return "等腰三角形";
}
if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b){
return"直角三角形";
}
return "一般三角形";
}else{
return"不构成三角形";
}
}else{
return "边的值不在范围内";
}
}
public static boolean isNumeric(String str1){
for (int i = str1.length();--i>=0;){
if (!Character.isDigit(str1.charAt(i))){
return false;
}
}
return true;
}
public static double triangleArea(String s1,String s2,String s3){
int a = 0,b=0,c=0;
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=Integer.parseInt(s3);
float p=(float)(a+b+c)/2;
double area= Math.sqrt(p*(p-a)*(p-b)*(p-c));
return area;
}
public static int primeter(String s1,String s2,String s3){
int a = 0,b=0,c=0;
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=Integer.parseInt(s3);
return a+b+c;
}
public static void main(String []args){
System.out.println("请输入三角形的三条边:");
String s1=null;
String s2=null;
String s3=null;
Scanner scan=new Scanner(System.in);
s1=scan.next();
//如果输入的边长为0,则退出程序
s2=scan.next();
s3=scan.next();
System.out.println(triangle(s1,s2,s3));
System.out.println("三角形的面积为:"+triangleArea(s1,s2,s3));
System.out.println("三角形的边长为:"+primeter(s1,s2,s3));
}
}
3、生成图形界面源代码
package mypack;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
class ActionHandle{
public static String triangle(String s1,String s2,String s3){
String str="abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=";
if(str.indexOf(s1)!=-1||str.indexOf(s2)!=-1||str.indexOf(s3)!=-1){
return "输入的是非法字符";
}
//判断输入的值是否是整数
if(isNumeric(s1)&&isNumeric(s2)&&isNumeric(s3)){
}else{
return"输入的数字不是整数";
}
int a = 0,b=0,c=0;
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=Integer.parseInt(s3);
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){
return "等边三角形";
}
if(a==b||a==c||b==c){
return "等腰三角形";
}
if(a*a==b*b+c*c||b*b==a*a+c*c||c*c==a*a+b*b){
return"直角三角形";
}
return "一般三角形";
}else{
return"不构成三角形";
}
}else{
return "边的值不在范围内";
}
}
public static boolean isNumeric(String str1){
for (int i = str1.length();--i>=0;){
if (!Character.isDigit(str1.charAt(i))){
return false;
}
}
return true;
}
public static double triangleArea(String s1,String s2,String s3){
int a = 0,b=0,c=0;
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=Integer.parseInt(s3);
float p=(float)(a+b+c)/2;
double area= Math.sqrt(p*(p-a)*(p-b)*(p-c));
return area;
}
public static int primeter(String s1,String s2,String s3){
int a = 0,b=0,c=0;
a=Integer.parseInt(s1);
b=Integer.parseInt(s2);
c=Integer.parseInt(s3);
return a+b+c;
}
private JFrame frame=new JFrame("三角形计算程序");
private JButton ok=new JButton("ok");
private JButton reset=new JButton("reset");
private JLabel infoLab=new JLabel("请输入三角形的三条边:");
private JLabel aLab=new JLabel("a:");
private JLabel bLab=new JLabel("b:");
private JLabel cLab=new JLabel("c:");
private JLabel styleLab=new JLabel("这个三角形是:");
private JLabel areaLab=new JLabel("面积是:");
private JLabel preLab=new JLabel("周长是:");
private JTextField aText=new JTextField();
private JTextField bText=new JTextField();
private JTextField cText=new JTextField();
public ActionHandle(){
Font fnt=new Font("Serief",Font.BOLD,12);
infoLab.setFont(fnt);
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource()==ok){
String a=aText.getText();
String b=bText.getText();
String c=cText.getText();
String style=triangle(a,b,c);
String area=String.valueOf(triangleArea(a,b,c));
String pre=String.valueOf(primeter(a,b,c));
styleLab.setText("类型为:"+style);
areaLab.setText("面积为:"+area);
preLab.setText("周长为:"+pre);
}
if(arg0.getSource()==reset){
aText.setText(" ");
bText.setText(" ");
cText.setText(" ");
}
}
});
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent arg0){
System.exit(1);
}
});
frame.setLayout(null);
infoLab.setBounds(5, 5, 60, 20);
aLab.setBounds(5, 25, 60, 20);
aText.setBounds(65, 25, 60, 20);
bLab.setBounds(5,45, 60, 20);
bText.setBounds(65, 45, 60, 20);
cLab.setBounds(5,65, 60, 20);
cText.setBounds(65, 65, 60, 20);
ok.setBounds(5, 85, 60, 20);
reset.setBounds(65,85, 100, 20);
styleLab.setBounds(5, 105, 150, 20);
areaLab.setBounds(5, 125, 100, 20);
preLab.setBounds(5, 145, 100, 20);
frame.add(infoLab);
frame.add(aLab);
frame.add(aText);
frame.add(bLab);
frame.add(bText);
frame.add(cLab);
frame.add(cText);
frame.add(ok);
frame.add(reset);
frame.add(styleLab);
frame.add(areaLab);
frame.add(preLab);
frame.setSize(200,300);
frame.setLocation(500,200);
frame.setVisible(true);
}
}
public class homeWork04 {
public static void main(String []args){
new ActionHandle();
}
}
4、结果截图
第四次作业测试代码+016+兰东华