首页 > 代码库 > 0923接口——练习题作业
0923接口——练习题作业
1.编写2个接口:InterfaceA和InterfaceB;在接口InterfaceA中有个方法void
printCapitalLetter();在接口InterfaceB中有个方法void printLowercaseLetter();然
后写一个类Print实现接口InterfaceA和InterfaceB,要求printCapitalLetter()方法
实现输出大写英文字母表的功能,printLowercaseLetter()方法实现输出小写英文
字母表的功能。再写一个主类E,在主类E的main方法中创建Print的对象并赋
值给InterfaceA的变量a,对象a调用printCapitalLetter方法;最后再在主类E
的main方法中创建Print的对象并赋值给InterfaceB的变量b,对象b调用
printLowercaseLetter方法。
package jiekouhomework2;public interface InterfaceA { //方法 void printCapitalLetter();}package jiekouhomework2;public interface InterfaceB { //方法 void printLowercaseLetter();}package jiekouhomework2;public class Print implements InterfaceA, InterfaceB { @Override public void printLowercaseLetter() { //输出大写英文字母 for(int i=‘a‘;i<‘a‘+26;i++) { System.out.print((char)i+" "); } } @Override public void printCapitalLetter() { //输出大写英文字母 for(int i=‘A‘;i<‘A‘+26;i++) { System.out.print((char)i+" "); if((char)i==‘Z‘) { System.out.println(); } } }}package jiekouhomework2;public class E { public static void main(String[] args) { Print j=new Print();//实例化Print InterfaceA a=j;//向上转型 a.printCapitalLetter(); InterfaceB b=j; b.printLowercaseLetter(); }}
2.
按要求编写Java程序:
(1)编写一个接口:InterfaceA,只含有一个方法int method(int n);
(2)编写一个类:ClassA来实现接口InterfaceA,实现int method(int n)接口方
法时,要求计算1到n的和;
(3)编写另一个类:ClassB来实现接口InterfaceA,实现int method(int n)接口
方法时,要求计算n的阶乘(n!);
(4)编写测试类E,在测试类E的main方法中使用接口回调的形式来测试实现
接口的类。
package jiekouhomework3;public interface InterfaceA { int method(int n);}package jiekouhomework3;public class ClassA implements InterfaceA { @Override public int method(int n) { int sum=0; for(int i=1;i<=n;i++) { sum+=i; } return sum; }}package jiekouhomework3;public class ClassB implements InterfaceA { @Override public int method(int n) { int sum=1; for(int i=1;i<=n;i++) { sum=sum*i; } return sum; }}package jiekouhomework3;public class E { public static void main(String[] args) { ClassA j=new ClassA(); System.out.println("1到n的和="+j.method(10)); ClassB k=new ClassB(); System.out.println("n的阶乘="+k.method(10)); }}
3.
中国特色社会主义的体制中有这样的现象:地方省政府要坚持党的领导和按
照国务院的指示进行安全生产。请编写一个java应用程序描述上述的体制现象。
要求如下:
(1)该应用程序中有一个“党中央”接口:CentralPartyCommittee,该接口中
有个“坚持党的领导”方法:void partyLeader()
(2)该应用程序中有一个“国务院”抽象类:StateCouncil,该抽象类中有个“安
全生产”的抽象方法:abstract void safetyInProduction()
(3)该应用程序中有一个“省政府”类:Province,该类继承StateCouncil抽象
类并且实现CentralPartyCommittee接口;在实现partyLeader()方法时输出“我们
各省人民一定坚持党的领导!”;在重写safetyInProduction()方法时输出“我们各
省人民一定按照国务院的指示进行安全生产!”。
(4)该应用程序中有一个主类E,在主类E的main方法中创建Province类的对
象,来测试Province类的功能。
package jiekouhomework4;public interface CentralPartyCommittee { void partyLeader(); void safetyInProduction();}package jiekouhomework4;public abstract class StateCouncil { abstract void safetyInProduction();}package jiekouhomework4;public class Province extends StateCouncil implements CentralPartyCommittee { @Override public void partyLeader() { // TODO Auto-generated method stub System.out.println("我们各省人民一定坚持党的领导!"); } @Override public void safetyInProduction() { System.out.println("我们各省人民一定按照国务院的指示进行安全生产!"); }}package jiekouhomework4;public class E { public static void main(String[] args) { // TODO Auto-generated method stub Province p=new Province(); p.partyLeader(); p.safetyInProduction(); }}
4.
package jiekouhomework5;public interface CanSwim { void swim();}package jiekouhomework5;public interface CanFly { void fly();}package jiekouhomework5;public abstract class ActionCharacter { public abstract void fight(String emp); public abstract void sperk(String s);}package jiekouhomework5;public class Hero extends ActionCharacter implements CanSwim, CanFly { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public void fly() { System.out.println(name+"会飞"); } @Override public void swim() { // TODO Auto-generated method stub System.out.println(name+"会游泳,小样你行不"); } @Override public void fight(String emp) { // TODO Auto-generated method stub System.out.println(name+"可以一个大十个"); } @Override public void sperk(String s) { // TODO Auto-generated method stub System.out.println(name+"会吹牛逼"); }}package jiekouhomework5;public class Adventure { public static void main(String[] args) { // TODO Auto-generated method stub Hero hb=new Hero(); hb.setName("蜘蛛侠"); hb.swim(); hb.fight("123"); hb.fly(); CanFly cf=hb; CanSwim cs=hb; cs.swim(); ActionCharacter ac=hb; ac.fight("123"); ac.sperk("123"); }}
5.
利用接口做参数,写个计算器,能完成+-*/运算
(1)定义一个接口Compute含有一个方法int computer(int n,int m);
(2)设计四个类分别实现此接口,完成+-*/运算
(3)设计一个类UseCompute,含有方法:
public void useCom(Compute com, int one, int two)
此方法要求能够:1.用传递过来的对象调用computer方法完成运算
2.输出运算的结果
(4)设计一个测试类,调用UseCompute中的方法useCom来完成+-*/运算
package jiekouhomework6;public interface Compute { double computer(double n,double m);}package jiekouhomework6;public class jia implements Compute { @Override public double computer(double n, double m) { return n+m; }}package jiekouhomework6;public class jian implements Compute { @Override public double computer(double n, double m) { // TODO Auto-generated method stub return n-m; }}package jiekouhomework6;public class cheng implements Compute { @Override public double computer(double n, double m) { // TODO Auto-generated method stub return n*m; }}package jiekouhomework6;public class chu implements Compute { @Override public double computer(double n, double m) { // TODO Auto-generated method stub return n/m; }}package jiekouhomework6;public class UseCompute { public void useCom(Compute com, int one, int two) { System.out.println(com.computer(one, two)); }}package jiekouhomework6;public class test { public static void main(String[] args) { // TODO Auto-generated method stub UseCompute u=new UseCompute(); jia jia=new jia(); u.useCom(jia, 10, 20); u.useCom(new jian(), 10, 20); u.useCom(new cheng(), 10, 20); u.useCom(new chu(), 10, 20); }}
0923接口——练习题作业