zoukankan      html  css  js  c++  java
  • 类的继承和多态性作业

    JAVA上机题

    实验三 类的继承和多态性

    1.(1)编写一个接口ShapePara,要求: 接口中的方法: int getArea():获得图形的面积。int getCircumference():获得图形的周长

    (2)编写一个圆类Circle,要求:圆类Circle实现接口ShapePara。

    该类包含有成员变量:

    radius:public 修饰的double类型radius,表示圆的半径。

    x:private修饰的double型变量x,表示圆心的横坐标。

    y:protected修饰的double型变量y,表示圆心的纵坐标。

    包含的方法有:

    Circle(double radius) 有参构造方法。以形参表中的参数初始化半径,圆心为坐标原点。 double getRadius():获取半径为方法的返回值。void setCenter(double x, double y):利用形参表中的参数设置类Circle的圆心坐标。void setRadius(double radius):利用形参表中的参数设置类Circle的radius域。

    在主方法中产生半径为5的圆。
     interface ShapePara {
        double getArea(double r);
        double  getCircumference(double r);
    }//注: Circle是在接口中建立的calss,即先建立接口,在建立接口的类
     class Circle implements ShapePara{
       private double x;
        protected double y;
        public double r;
        Circle(double r){
            this.r=r;
            }
        void setRadius(double r){
            this.r=r;
        }
        double getRadius(){
            return r;
        }
        
        double getArea(){
            return (3.14*r*r);
        }
        double getCircumference(){
            return 3.14*2*r;
        }
        void setCenter(double x,double y){
            this.x=x;
            this.y=y;
        }
        double getCenterx(){
            return x;
        }
        double getCentery(){
            return y;
        }
    }
    public class A {
        public static void main(String[] args) {
            Circle ci=new Circle(5);
            ci.setRadius(5);
            ci.setCenter(0, 0);
            System.out.println(ci.getArea());
            System.out.println(ci.getCircumference());
            System.out.println(ci.getCenterx());
            System.out.println(ci.getCentery());
        }
    }
    答案:
    78.5 31.400000000000002 0.0 0.0

    2.定义图形类Shape,该类中有获得面积的方法getArea();定义长方形类Rect,该类是Shape的子类,类中有矩形长和宽的变量double a,double b,设置长和宽的方法setWidth()、setHeight(),使用getArea()求矩形面积;利用getArea方法实现题1中圆面积的求解。

    class Shape {
        double getArea(double r){
            return 0;
        }
    }
    public class Rect extends Shape {
          double a,b,area;
          Rect(double width,double heigh){
            a=width;
            b=height;;
          } 
            void setWidth(double width) {
                a=width;
              } 
            void setHeight(double height) {
                b=height;
             } 
            double getWidth(){
               return a;
            }
            double getHeight(){
               return b;
            }
            double getArea(){
               area=a*b;
               return area;
            }
        }
    public class A {
        public static void main(String args[]) {
                   Rect rect=new Rect();
                   double w=12.76,h=25.28;
                   rect.setWidth(w);
                   rect.setHeight(h); 
                   System.out.println("矩形对象的宽:"+rect.getWidth()+" 高:"+rect.getHeight());
                   System.out.println("矩形的面积:"+rect.getArea());
              }
    }
    答案:
    圆的的面积:78.5
    矩形对象的宽:12.76 高:25.28
    矩形的面积:322.57280000000003

    3. 编写Java应用程序,定义Animal类,此类中有动物的属性:名称 name,腿的数量legs,统计动物的数量 count;方法:设置动物腿数量的方法 void setLegs(),获得腿数量的方法 getLegs(),设置动物名称的方法 setKind(),获得动物名称的方法 getKind(),获得动物数量的方法 getCount()。定义Fish类,是Animal类的子类,统计鱼的数量 count,获得鱼数量的方法 getCount()。定义Tiger类,是Animal类的子类,统计老虎的数量 count,获得老虎数量的方法 getCount()。定义SouthEastTiger类,是Tiger类的子类,统计老虎的数量 count,获得老虎数量的方法 getCount()。

    public class Animal {
        String name;
        int legs;
        static int count;
        Animal(){count++;}
        void setLegs(int legs){
            this.legs=legs;
        }
        int getLegs(){
            return legs;
        }
        void setKind(String name){
            this.name=name;
        }
        String getKind(){
            return name;
        }
        int getCount(){
            return count;
        }
    }
    public class Fish extends Animal{
    static int countFish;
        Fish(){countFish++;}
    int getCount(){
            return countFish;
        }
    }
    public class Tiger extends Animal{
    static int countTiger;
        Tiger(){countTiger++;}
    int getCount(){
            return countTiger;
        }
    }
    public class SouthEastTiger extends Tiger{}
    public class A {
        public static void main(String args[]){
            Fish f=new Fish();
            System.out.println(f.getCount());
            Tiger t=new Tiger();
            System.out.println(t.getCount());
            SouthEastTiger st=new SouthEastTiger();
            System.out.println(st.getCount());
        }
    }
  • 相关阅读:
    关于 Dev中的GridControl 中 GridView 的 PopulateColumns() 方法
    操作系统 页面置换算法LRU和FIFO
    C#中有哪些类型的数组
    博弈论:取石子问题
    java中 sleep 与 wait 的区别
    java 中 ArrayList LinkedList Vector 三者的异同点
    C# Mysql You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ????
    我在使用vs进行C#编程中常用的几个快捷键
    javascript 数据类型基础
    html5 <script>
  • 原文地址:https://www.cnblogs.com/hanruyue/p/5896953.html
Copyright © 2011-2022 走看看