类的继承和多态性作业

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());
    }
}

 

转载于:https://www.cnblogs.com/hanruyue/p/5896953.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值