java三角形测试代码 软件测试三角形问题java
用JAVA编写一个类利用对象输出三角形的面积和周长
public class Triangle {
坪山ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联公司的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
int x,y,z; //这是三角形三条边的长度
private void triangle(int x,int y,int z) {
this.x=x;
this.y=y;
this.z=z;
}
private int perimeter(){
return this.x+this.y+this.z;
}
private double area(){
int p = this.perimeter() / 2;
return Math.sqrt(p*(p-this.x)*(p-this.y)*(p-this.z));
}
public static void main(String[] args) throws Exception {
Triangle t = new Triangle(3,4,5);
System.out.println("该三角形的周长为:" + t.perimeter());
System.out.println("该三角形的面积为:" + t.area());
}
}
上面代码保存为Triangle.java即可测试运行~~手写代码,不保证完全正确,但思路绝对是正确的
java输入三角形的三个边判断能否构成三角形。并判断构成三角形的类别(等边,等腰,普通)
import java.util.Scanner;
public class Trigon {
private double a;
private double b;
private double c;
public Trigon(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please input first side:");//输入第1条边
double a = scan.nextDouble();
System.out.println("Please input second side:");//输入第2条边
double b = scan.nextDouble();
System.out.println("Please input third side:");////输入第3条边
double c = scan.nextDouble();
scan.close();
Trigon.isTrigon(a, b, c);//判断输入的能否构成三角形以及类型
}
public static void isTrigon(double a, double b, double c) {
if(a = 0 || b =0 || c = 0){//如果有小于0的边长,显然不行
System.out.println("Can't");
return;
}
if (a + b c a + c b b + c a) {//2边之和一定要大于第三边
if (a == b || a == c || b == c) {//等腰
if (a == b b == c) {//等边
System.out.println("可以组成等边三角形");
} else {
System.out.println("可以组成等腰三角形");
}
return;
}
System.out.println("可以组成普通三角形");//普通
return;
}
System.out.println("不能够组成三角形!");//不能构成三角形
}
}
------------------
Please input first side:
3
Please input second side:
3
Please input third side:
2
可以组成等腰三角形
java判断是否为直角三角形。
import java.util.Arrays;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int caseCount = sc.nextInt();
int[] arr = new int[3];
for(int i = 0 ; i caseCount ; i++){
arr[0] = sc.nextInt();
arr[1] = sc.nextInt();
arr[2] = sc.nextInt();
Arrays.sort(arr);
if(Math.pow((double)arr[0], 2)+Math.pow((double)arr[1], 2) == Math.pow((double)arr[2], 2)){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
}
用Java语言编写,要求申明三角形类,继承图形抽象类,计算三角形的周长和面积!
图形抽象类的代码:
abstract class MyShape {
abstract int calGirth(); //求周长
abstract double calSquare(); //求面积
}
三角形类的实现:
public class Triangle extends MyShape{
int borderA, borderB, borderC;
Triangle(int a, int b, int c){borderA = a; borderB = b; borderC = c;}
Triangle(){borderA = borderB = borderC = 0;}
@Override
int calGirth() {
return borderA + borderB + borderC;
}
@Override
double calSquare() {
double p = calGirth() / 2;
return Math.sqrt(p * (p - borderA) * (p - borderB) * (p - borderC));
}
public static void main(String[] args) {
Triangle test = new Triangle(3, 4, 5);
System.out.println("The girth of the triangle is " + test.calGirth());
System.out.println("The square of the triangle is " + test.calSquare());
}
}
实现两个抽象函数,测试结果正确,输出为:
The girth of the triangle is 12
The square of the triangle is 6.0
java代码关于三角形用junit测试
@Test
public void test() {
SetString typeSet = new HashSetString();
typeSet.add("Equilateral");
typeSet.add("Isosceles");
typeSet.add("Scalene");
Triangle t1 = new Triangle("a", "b", "c");
String type1 = t1.determineTriangleType();
if(typeSet.contains(type1)){
fail("error of type");
}
Triangle t2 = new Triangle("1", "1", "1");
String type2 = t2.determineTriangleType();
if(!typeSet.contains(type2)){
fail("error of type");
}
assertEquals("Isosceles", type2);
}
随便写了两个分支的测试,你自己可以补全,第二个分支已经测试报错了,你可以检查下代码:
if ((s1 == s3) (s2 == s3)) {
type= "Equilateral";
} else if (( s1 == s2) (s2 == s3) (s1 == s3)) {
type = "Isosceles";
} else {
type = "Scalene";
}
第一个条件需要和第二个条件换一下位置,首先判断等边再判断等腰
如何用java代码来做三角形的判断?
public class Test7 {
public static String T(double a,double b,double c){
double tem = Math.max(a, b);
if(temc){
if(tem==a){
a = c;
}else {
b = c;
}
c = tem;
}
if(!(a+bcMath.abs(a-b)c)){
return "无法构成三角形";
}else if(a==b||a==c||b==c){
return "等腰三角形";
}else if(a*a+b*b==c*c){
return "直角三角形";
}else if(a*a+b*bc*c){
return "锐角三角形";
}else return "钝角三角形";
}
public static void main(String[] args) {
System.out.println(Test7.T(11, 5, 12));
}
}
分享名称:java三角形测试代码 软件测试三角形问题java
文章URL:http://hbruida.cn/article/hjcjgi.html