定义一个三角形类,为他添加字段(field)、属性(attributes)和方法。实现三角形周长和面积

如题所述

import java.math.*; import java.util.*; public class Triangle { /** * @param args */ int a,b,c; //三角形的三个边 double p; //求面积时海伦公式中的p double area; //面积 int circle; //周长 public Triangle(int a,int b,int c){ this.a=a; this.b=b; this.c=c; p=(double)1/2*(a+b+c); } public void Area(){ /* * s=根号下:p(p-a)(p-b)(p-c) 其中p=1/2(a+b+c) * 这个公式叫海伦公式 */ this.area=Math.sqrt(p*(p-a)*(p-b)*(p-c)); //三角形的面积 } public void Circle(){ //计算三角形的周长 this.circle=a+b+c; } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("请输入三角形的三个边"); Scanner input = new Scanner(System.in); int a,b,c; a=input.nextInt(); b=input.nextInt(); c=input.nextInt(); Triangle tr = new Triangle(a,b,c); tr.Area(); //计算面积 tr.Circle(); //计算周长 //System.out.println(a ); System.out.println("三角形的面积为:"+tr.area+"周长为:"+tr.circle); } }
温馨提示:答案为网友推荐,仅供参考
相似回答