Java之 实现计算器 找出完数

版权声明:转载请联系 :[email protected] https://blog.csdn.net/weixin_40928253/article/details/81003828

1.说出下面程序的执行结果:

 

interface InterfaceA { String S = "good "; void f(); //空实现 } abstract class ClassA { abstract void g(); //抽象类 } class ClassB extends ClassA implementsInterfaceA { 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 Compute{
    int compute(int n,int m);
}

class Add implements Compute {
    public int compute (int n,int m){
        return n+m;
    }
}

class Sub implements Compute{
    public int compute(int n,int m){
        return n-m;
    }
}

class Mul implements Compute{
    public int compute(int n, int m){
        return n*m;
    }
}

class Div implements Compute{
    public int compute(int n,int m){
        return n/m;
    }
}

class UseCompute{
    public void useCom (Compute com,int one,int two){
        System.out.println(com.compute(one,two));
    }
}

public class Test{
	public static void main(String[] args) {
		UseCompute uc = new UseCompute();
		Add  s1 = new Add();
		Sub  s2 = new Sub();
		Mul  s3 = new Mul();
		Div  s4 = new Div();
		System.out.print("和:");uc.useCom(s1,4,2);
		System.out.print("差:");uc.useCom(s2,4,2);
		System.out.print("积:");uc.useCom(s3,4,2);
		System.out.print("商:");uc.useCom(s4,4,2);
    }
}

运行结果如下:

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

(1)定义接口A,里面包含值为3.14的常量PI和抽象方法doublearea()。

(2)定义接口B,里面包含抽象方法void setColor(String c)。

(3)定义接口C,该接口继承了接口A和B,里面包含抽象方法voidvolume()。

(4)定义圆柱体类Cylinder实现接口C,该类中包含三个成员变量:底圆半径radius、

圆柱体的高height、颜色color。

(5)创建主类来测试类Cylinder。

/*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 A {
    double PI = 3.14;
    double area();
}
interface B {
    void  setColor(String color);
}
interface C extends A,B {
    void volume();
}
class Cylinder implements C {
    private double radius;
    private double height;
    @SuppressWarnings("unused")
	private String color;

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

    public void setColor(String color){
        System.out.println("圆柱体颜色为:" + color);
    }
    public void volume() {
        System.out.println("圆柱体体积为:"+area()*height);
    }
}
public class Test {
    public static void main(String[] args){
       Cylinder cylinder = new Cylinder(3.9,3.8,"red");
       System.out.println(cylinder.area());
       cylinder.setColor("red");
       cylinder.volume();
    }
}

4.(附加题-算法)

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

输出如下:

public class WanShu {

    public static void main(String[] args) {
        
        for (int i = 1; i <= 1000; i++)
        {
            int sum=0;
            for (int j = 1; j < i; j++)
            {
            if(i%j==0)
            {
                sum+=j;
            }            
            }
            if(i==sum)
            {
                System.out.println(i);
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40928253/article/details/81003828