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 Test1
 { 
    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 Computer
{
    int computer(int n, int m);
}

class Add implements Computer
{
    public int computer(int n, int m)
    {
        System.out.print(n+"+"+m+"=");
        return n+m;
    }
}

class Sub implements Computer
{
    public int computer (int n, int m)
    {
        System.out.print(n+"-"+m+"=");
        return n-m;
    }
}

class Mul implements Computer
{
    public int computer (int n, int m)
    {
        System.out.print(n+"*"+m+"=");
        return n*m;
    }
}

class Div implements Computer
{
    public int computer (int n, int m)
    {
        System.out.print(n+"/"+m+"=");
        return n/m;
    }
}
class UseComputer 
{
    public void useCom(Computer com, int one, int two)
    {
        System.out.println (com.computer(one, two));
    }
}
public class Test
{
    public static void main (String[] args)
    {
        UseComputer uc = new UseComputer();
        uc.useCom(new Add(), 10, 20);
        uc.useCom(new Sub(), 15, 10);
        uc.useCom(new Mul(), 10, 20);
        uc.useCom(new Div(), 15, 5);
    }
}

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
{
	final double pl = 3.14;
	double area();
}
interface B
{
	void setColor(String c);
}
interface C extends A, B
{
	void volume();
}
class Cylinder implements C
{
	private double radius;
	private double height;
	private String color;
	public Cylinder (double radius, double height)
	{
		this.height = height;
		this.radius = radius;
	}
	public double area ()
	{
		return pl*radius*radius;
	}
	public void setColor (String color)
	{
		this.color = color;
	}
	public void volume ()
	{
		System.out.println ("圆柱体积为:"+area()*height+"颜色为:"+this.color);
	}
}
public class Test
{
	public static void main (String[] args)
	{
		Cylinder cyl = new Cylinder(10.0, 20.0);
		cyl.setColor("红色");
		cyl.volume();
	}
}

运行结果为:

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

public class Test
{
	public static void main (String[] args)
	{
		for (int i=1; i<1000; ++i)
		{
			wanShu(i);
		}
	}
	public static void wanShu(int num)
	{
		int sum = 0;
		for (int i=num-1; i>0; --i)
		{
			if (num%i == 0)
			{
				sum+= i;
			}
		}
		if (sum == num)
		{
			System.out.print (num+" ");
		}
	}
}

运行结果:

猜你喜欢

转载自blog.csdn.net/A__B__C__/article/details/83590627