面向对象编程-练习题-接口篇

1.面积计算相关功能

按如下要求编写Java程序:

定义接口A,里面包含值为3.14的常量PI和抽象方法double area() 定义接口B,里面包含抽象方法void setColor(String c) 定义接口C,该接口继承了接口A和B,里面包含抽象方法void volume() 定义圆柱体类Cylinder实现接口C,该类中包含三个成员变量:底圆半径radius 圆柱体的高height、颜色color 创建主类来测试类Cylinder


/**
 * (1)定义接口A,里面包含值为3.14的常量PI和抽象方法double area()。
 */
interface A {
    public final double PI = 3.14;
    abstract double area();
}

/**
 *(2)定义接口B,里面包含抽象方法void setColor(String c)。
 */
interface B {
    abstract void setColor(String c);
}

/**
 * (3)定义接口C,该接口继承了接口A和B,里面包含抽象方法void volume()。
 */
interface C extends A,B{
    abstract void volume();
}


/**
 * (4)定义圆柱体类Cylinder实现接口C,该类中包含三个成员变量:底圆半径radius、
 *  * 圆柱体的高height、颜色color。
 */
class Cylinder implements C{
    private double radius;
    private double height;
    private String color;

    public Cylinder() {
    }

    public Cylinder(double radius, double height, String color) {
        this.radius = radius;
        this.height = height;
        this.color = color;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public String getColor() {
        return color;
    }

    @Override
    public double area() {
        return 2 * PI * radius * radius + 2 * PI * radius * height;
    }

    @Override
    public void setColor(String c) {
        this.color = c;
    }

    @Override
    public void volume() {
        System.out.println("底面半径为" + radius + ",高为" + height + ",体积为:" +radius*radius*PI*height);
    }
}
/**
 * (5)创建主类来测试类Cylinder
 */
public class CylinderTest {
    public static void main(String[] args) {
        Cylinder cylinder = new Cylinder(3,4,"蓝色");
        cylinder.volume();
        System.out.println("圆柱体的面积为:"+ cylinder.area());
        System.out.println("圆柱体的颜色为:" + cylinder.getColor());
    }
}



2.计算器类

按如下要求编写Java程序, 利用接口做参数,写个计算器,能完成加减乘除运算。

定义一个接口Compute含有一个方法int computer(int n, int m)。 设计四个类分别实现此接口,完成加减乘除运算。 设计一个类UseCompute,类中含有方法:public void useCom(Compute com, int one, int two),此方法能够用传递过来的对象调用computer方法完成运算,并输出运算的结果。 设计一个主类Test,调用UseCompute中的方法useCom来完成加减乘除运算。


/**
 * (1)定义一个接口Compute含有一个方法int computer(int n, int m)。
 */
interface Computer {
    int computer(int n, int m);
}

/**
 *(2)设计四个类分别实现此接口,完成加减乘除运算。
 */
 // 加
class Add implements Computer{
    @Override
    public int computer(int n, int m) {
        return n+m;
    }
}

// 减
class Sub implements Computer {
    @Override
    public int computer(int n, int m) {
        return n-m;
    }
}

// 乘
class Multiply implements Computer{
    @Override
    public int computer(int n, int m) {
        return n*m;
    }
}

// 除
class Div implements Computer{
    @Override
    public int computer(int n, int m) {
        if (m == 0) {
            System.out.println("除数不能为零。");
            return 0;
        }
        return n/m;
    }
}

/**
 * * (3)设计一个类UseCompute,
 * 类中含有方法:public void useCom(Compute com, int one, int two),
 * 此方法能够用传递过来的对象调用computer方法完成运算,并输出运算的结果。
 */
class UseComputer {
    public void useCom(Computer com, int one, int two){
        String operator = null;
        if (com instanceof Add) {
            operator = "+";
        }else if (com instanceof Sub) {
            operator = "-";
        }else if (com instanceof Multiply) {
            operator = "*";
        }else if (com instanceof Div) {
            operator = "/";
        }
        System.out.println(one + " " + operator + " " + two + " = " +com.computer(one,two));
    }
}
/**
 *(4)设计一个主类Test,调用UseCompute中的方法useCom来完成加减乘除运算。
 */
public class UseComputerTest {
    public static void main(String[] args) {
        UseComputer useComputer = new UseComputer();
        useComputer.useCom(new Add(),10,5);
        useComputer.useCom(new Sub(),10,5);
        useComputer.useCom(new Multiply(),10,5);
        useComputer.useCom(new Div(),10,5);
    }
}


3. 学员信息管理系统

按如下要求编写Java程序: 创建Person接口,它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。创建类Student实现Person接口,并对自己的“学生”属性的成员变量sID、speciality设置值和获得它们值所组成的字符串信息。通过Scanner(键盘输入)得到属性,再进行赋值。


/**
 * 按如下要求编写Java程序:
 * 创建Person接口,它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。
 */
public interface Person {
    // 对“人”属性name、sex和birthday赋值
    void setData(String name,String sex,String birthday);
    // 获得这些属性组成的字符串信息
    String getData();
}

/**
 * 创建类Student实现Person接口,
 * 并对自己的“学生”属性的成员变量sID、speciality设置值和获得它们值所组成的字符串信息。
 */
public class Student implements Person{
    private int sID;
    private String speciality;

    private String name;
    private String sex;
    private String birthday;


    @Override
    public void setData(String name, String sex, String birthday) {
        this.name = name;
        this.sex = sex;
        this.birthday = birthday;
    }

    @Override
    // 实现Person接口中的getData()方法,获得“人”的属性组成的字符串信息
    public String getData() {
        return "姓名:" + this.name + ",性别:" + this.sex + ",生日:" + this.birthday;
    }

    // 设置学生属性的成员变量sID和speciality
    public void setInfo(int sID, String speciality) {
        this.sID = sID;
        this.speciality = speciality;
    }

    // 获得学生属性的成员变量sID和speciality所组成的字符串信息
    public String getInfo() {
        return "学号:" + this.sID + ",专业:" + this.speciality;
    }
}

/**
 * 通过Scanner(键盘输入)得到属性,再进行赋值。
 */
public class PersonInfo {
    public static void assignment(){
        Student student = new Student();
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入姓名:");
        String name = sc.next();
        System.out.print("请输入性别:");
        String sex = sc.next();
        System.out.print("请输入生日:");
        String birthday =sc.next();
        System.out.print("请输入ID:");
        int id = sc.nextInt();
        System.out.print("请输入专业:");
        String speciality = sc.next();
        student.setData(name,sex,birthday);
        student.setInfo(id,speciality);
        System.out.println(student.getData());
        System.out.println(student.getInfo());
    }
}
/**
 * 按如下要求编写Java程序:
 * 创建Person接口,它有setData()和getData()方法对“人”属性name、sex和birthday赋值和获得这些属性组成的字符串信息。
 * 创建类Student实现Person接口,并对自己的“学生”属性的成员变量sID、speciality设置值和获得它们值所组成的字符串信息。
 * 通过Scanner(键盘输入)得到属性,再进行赋值。
 */
 public class PersonTest {
    public static void main(String[] args) {
        PersonInfo personInfo = new PersonInfo();
        PersonInfo.assignment();
    }
}




Last Updated 10/25/2025, 5:44:13 AM