当前位置:编程学习 > JAVA >>

有一道数学题目求解

设计一个表示三角形的类sjx(成员包含三条边),在主程序中创建对象t,按如下格式输出三角形的周长和面积
T(3,4,5),Area=6
T(3,4,5),Perimeter --------------------编程问答--------------------
public class Triangle {

private double a;
private double b;
private double c;

public Triangle(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;

}

/**
 * 计算三角形的周长
 * 
 * @return
 */
public double getLength() {
return (a + b + c);
}

/**
 * 计算三角形的面积
 * 
 * @return
 */
public double getSpace() {
double temp = getLength() / 2;
return java.lang.Math.sqrt(temp * (temp - a) * (temp - b) * (temp - c));
}

public static void main(String[] args) {
Triangle t = new Triangle(3, 4, 5);
System.out.println("周长: " + t.getLength());
System.out.println("面积: " + t.getSpace());

}

}
--------------------编程问答-------------------- import java.lang.Math.*;
public class Sanjiao{
private double a;
private double b;
private double c;
public Sanjiao(double a,double b,double c){
this.a = a;
this.b = b;
this.c = c;
}
public double Sanjiaolength(){
return a+b+c;
}
public double Sanjiaospace(){
double temp = Sanjiaolength()/2;
return Math.sqrt(temp * (temp - a) * (temp - b) * (temp - c));
}
public static void main(String[] args){
Sanjiao sanjiao = new Sanjiao(3,4,5);
System.out.println("三角形的周长是:"+sanjiao.Sanjiaolength());
System.out.println("三角形的面积是:"+sanjiao.Sanjiaospace());
}
}
--------------------编程问答-------------------- 除 --------------------编程问答-------------------- 可以利用海伦公式
补充:Java ,  J2ME
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,