• 实验八 实验九


    实验八:

    源代码:

    package 计算圆锥体面积;

    class abc extends yuanzhuiti implements Area,Volume {

    final double PI=3.14;
    public double volume(double r,double h) {
    double v;
    v=r*h/3;
    return v;
    }
    public double area(double r,double l) {
    double a;
    a=PI*r*l+PI*r*r;
    return a;
    }
    }
    public class yuanzhuiti{
    public static void main(String[] args) {

    abc a=new abc();
    abc b=new abc();
    System.out.println("圆锥体A的面积为:"+a.area(2, 4));
    System.out.println("圆锥体A的体积为:"+a.volume(3, 6));
    System.out.println("圆锥体B的面积为:"+b.area(3, 6));
    System.out.println("圆锥体B的体积为:"+b.volume(4, 8));
    System.out.println("体积较大的是:"+Math.max(a.area(3, 6), b.volume(4, 8)));
    }
    }

    package 计算圆锥体面积;


    public interface Area{
    public abstract double area(double r,double l);
    }

    package 计算圆锥体面积;

    public interface Volume {
    public abstract double volume(double r,double h);
    }

    结果:

    实验九:

    源代码:

    package 抛出异常;

    public class 实验 {
    public static void main(String[] args) {
    point p=new point(1,3);
    point p1=new point(1,2);
    point p2=new point(1,1);
    rectangle r=new rectangle(p,5,6);
    triangle t=new triangle(p,p1,p2);
    }

    }
    class point {
    public int x,y;
    public point() {}
    public point(int x,int y)throws IllegalArgumentException
    {
    this.x=x;
    this.y=y; 

    if(x<0||y<0)
    throw new IllegalArgumentException("无效参数");
    }
    }
    class rectangle extends point{
    public int width,length;
    //public point point1(3,6);
    public rectangle(point point1,int length,int width)throws IllegalArgumentException
    {

    this.length=length;
    this.width=width;
    if(length<0||width<0)
    throw new IllegalArgumentException("参数无效");
    }

    class triangle extends point{
    public triangle(point point1,point point2,point point3)throws IllegalArgumentException
    {
    if(((point1.x-point2.y)-(point2.x-point1.y))+((point2.x-point3.y)-(point3.x-point2.y))+((point3.x-point1.y)-(point3.y-point1.x))==0)
    throw new IllegalArgumentException("无效的参数");
    }
    }

    实验结果:

  • 相关阅读:
    PHP Document 注释标记及规范 && PHP命名规范
    JavaScript 最佳实践
    PHP正则表达式详解(三)
    $_SERVER["SCRIPT_NAME"]、$_SERVER["PHP_SELF"]、$_SERVER["QUERY_STRING"]、$_SERVER["REQUEST_URI"]
    PHP判断远程文件是否存在
    HDOJ1251-统计难题(trie树入门)
    Spark的日志配置
    实现Android 动态载入APK(Fragment or Activity实现)
    OC与JS互相调用
    mac os使用lsusb命令和连接未知的Android设备
  • 原文地址:https://www.cnblogs.com/myb1128/p/10930098.html
Copyright © 2020-2023  润新知