Write Java programs as required:
(1) Write an interface: Interfacea, which contains only one method, int (int n);
(2) Write a class: ClassA to implement the interface Interfacea, implement the int method (int n) interface square
The calculation of 1 to n is required;
(3) Write another class: ClassB to Implement Interface Interfacea, implementing the Int method (int n) interface
method, it is required to calculate the factorial of n (n!);
(4) Write Test Class E, test the implementation using the form of an interface callback in the main method of test class E
The class of the interface.
Package Lianxi1;public interface Interfacea {//write an interface: Interfacea, contains only one method, int (int n); int method (int n);}
Package Lianxi1;public class ClassA implements Interfacea {@Overridepublic int method (int n) {//TODO auto-generated methods stub int Sum=0;f or (int i=0;i<=n;i++) {sum+=i;} return sum;} public static void Main (string[] args) {//TODO auto-generated method stub//classa to implement Interface Interfacea, implement int method (int n) interface party//method, Requires calculation of 1 to N and Interfacea a=new ClassA (); A.method (10); System.out.println (A.method (10));}}
Package Lianxi1;public class ClassB implements Interfacea {@Overridepublic int method (int n) {//TODO auto-generated methods stub int A=1;for (int i=1;i<=n;i++) {a*=i;} return A;} public static void Main (string[] args) {//TODO auto-generated method stub Interfacea b=new ClassB (); B.method (10); System.out.println (B.method (10));}}
Package Lianxi1;public class E implements Interfacea {@Overridepublic int method (int n) {//TODO auto-generated methods stub return n;} public static void Main (string[] args) {//write Test Class E, use the form of an interface callback in the main method of test class E to test the class implementing//interface. Interfacea a=new E (); A.method (12); System.out.println (A.method (12));}}
Java Interface--2