2014.10.23

创建一个Point类,定义Triangle类。在Triangle类中定义三个Point实体来表示一个三角形的三个点,在定义一个方法setTri对着三个点进行初始化,然后定义两个方法求三角形的周长perimeter,面积area。在main()中创建一个对象,求给定三点的三角形的周长,面积。

将方法setTri改用构造方法实现初始化。

import java.util.*;
import java.lang.*;
class  Point{
    int x;
    int y;
}
class Triangle{

    Point p1 = new Point();
    Point p2 = new Point();
    Point p3 = new Point();
    double a;
    double b;
    double c;
    public Triangle(){
        Scanner scanf = new Scanner(System.in);
        p1.x=scanf.nextInt();
        p1.y=scanf.nextInt();
        p2.x=scanf.nextInt();
        p2.y=scanf.nextInt();
        p3.x=scanf.nextInt();
        p3.y=scanf.nextInt();
        a=Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
     	b=Math.sqrt((p1.x-p3.x)*(p1.x-p3.x)+(p1.y-p3.y)*(p1.y-p3.y));
     	c=Math.sqrt((p2.x-p3.x)*(p2.x-p3.x)+(p2.y-p3.y)*(p2.y-p3.y));
   }
   double perimeter(){
       double sum;
       sum=a+b+c;
       return sum;
   }

   double area(){
      double p,s;     		
      p=(a+b+c)/2;
      s=Math.sqrt(p*(p-a)*(p-b)*(p-c));
      return s;

   }

}
class Test{

    public static void main(String [] args){
    Triangle q = new Triangle();
    double p=q.perimeter();         
    double s=q.area();
    System.out.println(p+","+s);

   

    }


}

定义一个圆柱体类Cylinder,通过此类可以计算圆柱体的体积和表面积.

</pre><pre name="code" class="java">import java.util.*;
import java.lang.*;
class Cylinder{                    
       double r;     
       double h;
       double s;
       double v; 
       public Cylinder(){     
          Scanner scanf = new Scanner(System.in);     
          r=scanf.nextInt();     
          h=scanf.nextInt();      
       }
       double area(){    
          s=6.28*r*r+6.28*r*h;    
          return s;
       }
       double tiji(){    
       v=3.14*r*r*h;   
       return v;
       }              
}
class Test{
      public static void main(String [] args){
      Cylinder c = new Cylinder();
      double m=c.area();
      double n=c.tiji();
      System.out.println(m+","+n);
      
     }
}
import java.util.*;
import java.lang.*;
class Cylinder{                    
       double r;     
       double h;
       double s;
       double v; 
       void setCy(){     
          Scanner scanf = new Scanner(System.in);     
          r=scanf.nextInt();     
          h=scanf.nextInt();      
       }
       double area(){    
          s=6.28*r*r+6.28*r*h;    
          return s;
       }
       double tiji(){    
       v=3.14*r*r*h;   
       return v;
       }              
}
class Test{
      public static void main(String [] args){
      Cylinder c = new Cylinder();
      c.setCy();
      double m=c.area();
      double n=c.tiji();
      System.out.println(m+","+n);
      
     }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值