JavaSE编程题目~4

Java接口相关的练习~

1. 说出下面程序的执行结果:
interface InterfaceA {
    String S = "good ";
    void f();
}
abstract class ClassA {
    abstract void g();
}
class ClassB extends ClassA implements InterfaceA {
    void g() {
        System.out.print(S);
    }
    public void f() {
        System.out.print(" "+ S);
    }
}
public class Test {
    public static void main(String[] args) {
        ClassA a = new ClassB();
        InterfaceA b = new ClassB();
        a.g();
        b.f();
    }
}
//编译结果:输出good good
2. 利用接口做参数,写个计算器,能完成加减乘除运算。

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

interface ICompute{
    public int Computer(int m,int n);
}
class Add implements ICompute{
    public int Computer(int m,int n){
        return m+n;
    }
}
class Sub implements ICompute{
    public int Computer(int m,int n){
        return m-n;
    }
}
class Mul implements ICompute{
    public int Computer(int m,int n){
        return m*n;
    }
}
class Divs implements ICompute{
    public int Computer(int m, int n) {
        if(n!=0) {
            return n/m;
        }else {
            System.out.println("分母不能为零");
            return 0;
        }
    }
}
class UseCompute{
    public void usecom(ICompute com,int m,int n) {
        System.out.println(com.Computer(m,n));
    }
}
public class Test{
    public static void main(String[] args) {
        UseCompute useCompute = new UseCompute();
        useCompute.usecom(new Add(), 10, 20);
        useCompute.usecom(new Sub(), 10, 20);
        useCompute.usecom(new Mul(), 10, 20);
        useCompute.usecom(new Divs(), 10, 20);
        useCompute.usecom(new Divs(), 10, 0);
    }
}
3. 按如下要求编写Java程序:

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

interface IA{
    double PI = 3.14;
    public abstract double area();
}
interface IB{
    abstract void setColor(String c);
}
interface IC extends IA,IB{
    void volume();
}
abstract class Temp implements IC{
    public double area(double d){};
    public void setColor(String c){};
    public void volume(){};
}
class Cylinder extends Temp{
    private double PI = 3.14;
    private double radius;
    private double height;
    private String color;
    public double area(double radius){
        this.radius = radius;
        System.out.println("area is"+(radius*radius*PI));
    }
    public void setColor(String color){
        this.color = color;
        System.out.println("color is"+color);
    }
    public void volumn(double height){
        this.height = height;
        System.out.println("volumn is"+(radius*radius*PI*height));
    }
}
public class Test{
    public static void main(String[] args) {
        Cylinder cy = new Cylinder();
        cy.area(3.0);
        cy.setColor("绚丽紫");
        cy.volumn(4.0);
    }
}
4. 编程求完数

一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3.编程 找出1000以内的所有完数。

public class Test{
    public static void main(String[] args) {
        for(int i = 1; i<1000; i++){
            // 找因子:让这个数对“从1到它本身的½值”取余,结果为0。
            int sum = 0;
            for (int j=1; j<=(i/2); j++){
                if (i%j == 0){
                    sum = sum + j;
                }
            }
            if(sum == i){
                System.out.println("完数为"+i);
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Hannah_Hsq/article/details/84494359
今日推荐