java--第7章 抽象类与接口

举报
北山啦 发表于 2021/04/20 22:45:32 2021/04/20
【摘要】 实验目的:        1.熟悉抽象类和接口的用法。        2.了解Java语言实现多继承的途径。 实验内容:        1.定义一个抽象类Shape。         2.定义一个抽象类Animal。         3.编程用用接口封装小狗的状态。         4.编写实现了两个接口的类并在主类中调用相关的方法。         5.编程...

实验目的:

       1.熟悉抽象类和接口的用法。

       2.了解Java语言实现多继承的途径。

实验内容:

       1.定义一个抽象类Shape。

        2.定义一个抽象类Animal。

        3.编程用用接口封装小狗的状态。

        4.编写实现了两个接口的类并在主类中调用相关的方法。

        5.编程统计并输出研究生的年收入和学费。

        6.计算并输出体操选手和班级的成绩。

实验步骤:

1.定义一个抽象类Shape,

它包含一个抽象方法getArea(),从Shape类派生出Rectangle类和Circle类,这两个类都用getArea()方法计算对象的面积。编写应用程序使用Rectangle类和Circle类输出长5.2、宽13.25的矩形的面积及半径为5.2的圆的面积,要求输出结果保留两位小数。

源代码:

package com.sy7;

public class sy7_1 {

    public static void main(String[] args) {

        Rectangle R= new Rectangle(5.2,13.25);

        System.out.printf("矩形的面积为:%.2f\n",R.getArea());

        Circle C = new Circle(5.2);

        System.out.printf("圆的面积为:%.2f\n",C.getArea());

    }

}

abstract class Shape{

    double x;

    double y;

    double r;

    Shape(){

        x = 0;

        y = 0;

        r = 0;

    }

    Shape(double a){

        r=a;

    }

    Shape(double a,double b){

        x=a;

        y=b;

    }

    public abstract double getArea();

}

class Rectangle extends Shape{

    Rectangle(){

        super();

    }

    Rectangle(double a,double b){

        super(a,b);

    }

    public double getArea(){

        return super.x*super.y;

    }

}

class Circle extends Shape{

    Circle(double r){

        super(r);

    }

    public double getArea(){

        return super.r*super.r*3.1415;

    }

}

 

2.定义一个抽象类Animal

1)其中包括属性name及相关构造方法,抽象方法enjoy()表示动物高兴时动作。 

    2)定义Cat类继承于Animal类,其中包括属性eyesColor,相关构造方法,同时具体化父类中的抽象方法。 

    3)定义Dog类继承于Animal类,其中包括属性furColor,相关构造方法,同时具体化父类中的抽象方法。 

    4)定义Lady类,其中包括属性name,以及Animal 类型的属性pet表示女士所养的宠物,定义构造方法,生成女士对象时初始化姓名和她所养的宠物。定义一个方法:myPetEnjoy表示此女士的宠物在高兴时的动作。提示:对于此类的定义中需要使用到多态性。

    5)定义测试类。

创建一个张女士,养了一只猫,让这只猫笑一笑

创建一个王女士,养了一只狗,让这只狗叫一叫

源代码:

package com.sy7;

public class sy7_2 {

    public static void main(String[] args) {

        Animal a= new Cat("咪咪");

        Lady lady =new Lady("张女士",a);

        System.out.printf("%s的宠物%s叫一叫:",lady.getName(),a.name);

        lady.pet.enjoy();

        Animal b =new Dog("旺财");

        Lady lady2 = new Lady("王女士",b);

        System.out.printf("%s的宠物%s叫一叫:",lady2.getName(),b.name);

        lady2.pet.enjoy();

    }

}

abstract class Animal{

    String name;

    public Animal(){

    }

    public Animal(String name){

        this.name=name;

    }

    public abstract void enjoy();

}

class Cat extends Animal {

    String eyesColor;

    public String getEyesColor() {

        return eyesColor;

    }

    public void setEyesColor(String eyesColor) {

        this.eyesColor = eyesColor;

    }

    public Cat(){

    }

    public Cat(String name){

        this.name=name;

    }

    public void enjoy(){

        System.out.println("喵喵喵");

    }

}

class Dog extends Animal{

    String furColor;

    public String getFurColor() {

        return furColor;

    }

    public void setFurColor(String furColor) {

        this.furColor = furColor;

    }

    public Dog(){}

    public Dog(String name){

        this.name= name;

    }

    public void enjoy(){

        System.out.println("汪汪汪");

    }

}

class Lady {

    String name;

    Animal pet;

    public Lady(){}

    public Lady(String name,Animal pet){

        this.name=name;

        this.pet=pet;

    }

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public Animal getPet() {

        return pet;

    }

    public void setPet(Animal pet) {

        this.pet = pet;

    }

    public void myPetEnjoy(){

        System.out.println("摆尾巴");

    }

}

 

3.小狗在不同的条件下可呈现不同的状态表现,要求用接口封装小狗的状态。

1)编写一个接口DogState,其中有一个void showState()方法,用来呈现小狗的不同状态。

2)编写一个Dog类,通过show()方法回调showState()。

3)编写若干个实现DogState接口的类,负责刻画小狗不同的状态。可参考以下设计:

实现接口中的showState()方法

SoftlyState

System.out.println("听主人的命令");

MeetEnemyState

System.out.println("狂叫,并冲上去狠咬敌人");

MeetFriendState

System.out.println("晃动尾巴,表示欢迎");

MeetAnotherDog

System.out.println("嬉戏");

4)编写主类,测试小狗的不同状态。

源代码:

package com.sy7;

public interface DogState {

    public void showState();

}

class SoftlyState implements DogState{

    public void showState(){

        System.out.println("听主人的命令");

    }

}

class MeetEnemyState implements DogState{

    public void showState(){

        System.out.println("狂叫,并冲上去狠咬敌人");

    }

}

class MeetFriendState implements DogState{

    public void showState(){

        System.out.println("晃动尾巴,表示欢迎");

    }

}

class MeetAnotherDog implements DogState{

    public void showState(){

        System.out.println("嬉戏");

    }

}

class Dog2{

    DogState state;

    public void show(){

        state.showState();

    }

    public void setState(DogState s){

        state = s;

    }

}

public class sy7_3 {

    public static void main(String[] args) {

        Dog2 yDog = new Dog2();

        System.out.println("狗在主人面前:");

        yDog.setState(new SoftlyState());

        yDog.show();

        System.out.println("狗遇到敌人:");

        yDog.setState(new MeetEnemyState());

        yDog.show();

        System.out.println("狗遇到朋友:");

        yDog.setState(new MeetFriendState());

        yDog.show();

        System.out.println("狗遇到同伴:");

        yDog.setState(new MeetAnotherDog());

        yDog.show();

    }

}

 

4.按如下要求编写Java程序:

    (1)编写接口InterfaceA,接口中含有方法void printCapitalLetter()。

    (2)编写接口InterfaceB,接口中含有方法void printLowercaseLetter()。

    (3)编写非抽象类Print,该类实现了接口InterfaceA和InterfaceB。

     要求printCapitalLetter()方法实现输出大写英文字母表的功能,printLowercaseLetter()方法实现输出小写英文字母表的功能。

(4)再写一个主类Test,在main方法中创建Print的对象并赋值给InterfaceA的变量a,由变量a调用printCapitalLetter方法,然后再创建Print的对象并将该对象赋值给InterfaceB的变量b,由变量b调用printLowercaseLetter方法。

 

源代码:

public interface InterfaceA {

    void printCapitalLetter();

}

public interface InterfaceB {

    void printLowercaseLetter();

}

class Print implements InterfaceA,InterfaceB{

    public void printLowercaseLetter(){

        for(int i=97;i<123;i++){

            System.out.print((char)i);

        }

        System.out.println();

    }

    public void printCapitalLetter(){

        for(int i=65;i<91;i++){

            System.out.print((char)i);

        }

        System.out.println();

    }

 

}

public class sy7_4 {

    public static void main(String[] args) {

        Print p=new Print();

        InterfaceA a=p;

        InterfaceB b=p;

        a.printCapitalLetter();

        b.printLowercaseLetter();

    }

}

 

 

 

5.学校中有老师和学生两类人,而在职研究生既是老师又是学生。

    1)设计两个信息管理接口StudentInterface和TeacherInterface,其中StudentInterface接口包括setFee()方法和getFee()方法,分别用于设置和获取学生的学费,TeacherInterface接口包括setPay()和getPay()方法,分别用于设置和获取老师的工资。

    2)定义一个研究生类Graduate,实现StudentInterface接口和TeacherInterface接口,它定义的成员变量有name(姓名),fee(每学期学费) pay(月工资)。

3)创建一个姓名为“zhangsan”的研究生,假设其月工资为5000元,每学期学费为32500元,统计并输出他的年收入和学费,如果收入减去学费不足2000元,则输出“He need a loan!”(需要贷款)的信息,否则输出“His income is enough!”信息。

源代码:

public interface StudentInterface {

    double setFee();//设置学费

    double getFee();//获取学费

}

public interface TeacherInterface {

    double setPay();//设置工资

    double getPay();//获取工资

}

public class Graduate implements StudentInterface,TeacherInterface{

    private String name;

    private double fee;

    private double pay;

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public void setFee(double fee) {

        this.fee = fee;

    }

    public void setPay(double pay) {

        this.pay = pay;

    }

    public void setFeePay(double a,double b){

        fee = a;

        pay = b;

    }

    public double setFee(){

        return 0;

    }

    public double getFee(){//返回每年学费

        return fee*2;

    }

    public double setPay(){

        return 0;

    }

    public double getPay(){//返回年收入

        return pay*12;

    }

}

public class sy7_5 {

    public static void main(String[] args) {

        Graduate G = new Graduate();

        G.setName("zhangSan");

        G.setFeePay(32500,5000);

        System.out.println(G.getName()+"的学费为:"+G.getFee());

        System.out.println(G.getName()+"的年收入为:"+G.getPay());

        if(G.getPay()-G.getFee() >2000){

            System.out.println("He income is enough!");

        }else{

            System.out.println("He need a loan!");

        }

    }

}

 

 

 

 

 

6.体操比赛计算选手成绩的办法是去掉一个最高分和最低分后再计算平均分,而学校考察一个班级的某科目的考试情况时,是计算全班同学的平均成绩。编写能够满足如下条件的程序:

1)定义一个接口,包含计算平均值的抽象方法。

2)定义一个实现上述接口的用于计算体操比赛选手成绩的类。

3)定义一个实现上述接口的用于计算班级平均成绩的类。

4)在主类中通过接口回调分别计算并输出体操选手最后得分和班级考试平均分数。要求输出占6列,前者右对齐,保留三位小数,后者左对齐,保留两位小数。

假设体操选手的打分为:

9.89,9.88,9.99,9.12,9.69,9.76,8.97

班级分数为:

89,56,78,90,100,77,56,45,36,79,98

源代码:

public interface ComputerAverage {

    public double average(double x[]);

}

class Gymnastics implements ComputerAverage{

    public double average(double x[]) {

        int count = x.length;

        double aver = 0, temp = 0;

        for (int i = 0; i < count; i++) {

            for (int j = i; j < count; j++) {

                if (x[j] < x[i]) {

                    temp = x[i];

                    x[i] = x[j];

                    x[j] = temp;

                }

            }

        }

        for (int i = 1; i < count - 1; i++) {

            aver = aver + x[i];

        }

        if (count > 2)

            aver = aver / (count - 2);

        else

            aver = 0;

        return aver;

    }

}

class School implements ComputerAverage{

    public double average(double x[]){

        int count =x.length;

        double aver=0;

        for(int i=0;i<count;i++){

            aver=aver+x[i];

        }

        if (count>0)

            aver= aver/count;

        return aver;

    }

}

public class sy7_6 {

    public static void main(String[] args) {

        double a[]={9.89,9.88,9.99,9.12,9.69,9.76,8.97};

        double b[]={89,56,78,90,100,77,56,45,36,79,98};

        ComputerAverage computer;

        computer=new Gymnastics();

        double result=computer.average(a);

        System.out.printf("体操运动员最后的得分:%6.3f\n",result);

        computer=new School();

        result=computer.average(b);

        System.out.printf("班级考试平均成绩:%-6.2f",result);

    }

}

 

 

 

文章来源: blog.csdn.net,作者:北山啦,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_45176548/article/details/112263098

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。