• 实验八--————接口


      摘要:接口是类的膜模板

     

      问题:

    1、声明圆锥体类,实现Area和Volume接口,计算表面积和体积,按体积比较大小。

    2、写心得。

    3、给前一位同学评阅。

     

    预期结果:

     

    实施想法:

      1.打开编译器

      2.敲代码

      3.运行

       以下是对实施步骤2详细说明!!!

        忠心建议:直接复制以下代码,运行就完事了

    1 public interface Area {
    2     public abstract double area();//计算表面积  s=PI r l+ PI r^2 
    3 }
    1 public interface volumn {
    2     public abstract double volumne();//计算体积     V=1/3 *r^2*h
    3 }
     1 import java.util.*;
     2 //import java.math.*;
     3 public class Cylinder implements Area,volumn {///圆锥体类
     4     static final double PI=3.1415927;
     5     private double r;//半径
     6     private double h;//
     7     private int flags;//标记圆锥体的号
     8     static int flag=0;
     9     public Cylinder(double a,double b) {
    10         this.r=a;
    11         this.h=b;
    12         this.flags=flag++;
    13         this.getCylinder();
    14     }
    15     public Cylinder() {
    16         this.flags=flag++;
    17         this.setCylinder();
    18         this.getCylinder();
    19     }
    20     public void setCylinder() {
    21         Scanner in=new Scanner(System.in);
    22         System.out.print("输入半径:");
    23         this.r =in.nextDouble();
    24         System.out.print("输入   高:");
    25         this.h =in.nextDouble();
    26     }
    27     public void getCylinder() {
    28         System.out.println(toString());
    29         System.out.println("表面积是 "+this.area()+"="+PI+"*r*(r+sqrt(r^2+h^2)");
    30         System.out.println("体    积是 "+this.volumne()+"="+PI+"*r^2*h");
    31         System.out.println();
    32     }
    33     public String toString() {
    34         return this.flags+"号的圆锥体"+"(半径是"+this.r+"cm、高是"+this.h+"cm)";
    35     }
    36 public double area() {
    37      return PI*(Math.pow(this.r, 2)+this.r*Math.sqrt(Math.pow(this.r, 2)+Math.pow(this.h, 2)));
    38     }
    39 public double volumne() {
    40     return PI*Math.pow(this.r, 2)*this.h/3.0;
    41 }
    42 }
     1 public class main {
     2 
     3     public static void main(String[] args) {
     4         // TODO Auto-generated method stub
     5 
     6         Cylinder c=new Cylinder(5,8);
     7         Cylinder x=new Cylinder();
     8         compareCylinder(c,x);
     9     }
    10 public static void compareCylinder(Cylinder a,Cylinder b)
    11 {
    12     //boolean c=a.volumne()<b.volumne()?false:true;
    13     if(a.volumne()>b.volumne())
    14         System.out.printf("%s 的体积是%.3f 大于  %s 的体积是%.3f"
    15                 ,a.toString(),a.volumne(),b.toString(),b.volumne());
    16         //System.out.println(a.toString()+"的体积是"+a.volumne()+" 大于 "+b.toString()+"的体积是"+a.volumne());
    17     else 
    18         if(a.volumne()<b.volumne())  
    19             System.out.printf("%s 的体积是%.3f 小于  %s 的体积是%.3f"
    20                     ,a.toString(),a.volumne(),b.toString(),b.volumne());
    21         //System.out.println(a.toString()+"的体积是"+a.volumne()+" 小于 "+b.toString()+"的体积是"+a.volumne());
    22     else 
    23         System.out.printf("%s 的体积是%.3f 等于  %s 的体积是%.3f"
    24                 ,a.toString(),a.volumne(),b.toString(),b.volumne());
    25         //System.out.println(a.toString()+"的体积是"+a.volumne()+" 等于 "+b.toString()+"的体积是"+a.volumne());
    26 }
    27 }

        心得:

            一直写一直爽

            接口的虚函数只有在类里面实现了才不会出错,不然会一直提醒你有错误

            记得使用implements

    @勤奋的lu3
  • 相关阅读:
    ios常用方法
    XMPP
    ios ebooks
    uinavigationcontroller swipe back
    navigationController and ToolBar
    EMC VNX5200/5400存储 新增LUN与Hosts映射操作
    H3C交换机telnet服务认证模式配置
    配置H3C交换机ftp服务
    克隆CentOS 6.9 配置静态IP,重启网络服务时报错"eth0 does not seem to be present"
    BFS解决八数码问题和狼人过河问题
  • 原文地址:https://www.cnblogs.com/lul3/p/10884946.html
Copyright © 2020-2023  润新知