(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 jiekou0923;
public class Ceshi {
public static void Main (string[] args) {
Interfacea a=new ClassA ();
System.out.println ("10 's and =" +a.method (10));
Interfacea b=new ClassB ();
System.out.println ("factorial of 10 is =" +b.method (10));
}
}
Package jiekou0923;
public class ClassA implements Interfacea {
The 1-n and
@Override
public int method (int n)
{
int sum = 0;
for (int i=1;i<=n;i++)
{
Sum+=i;
}
return sum;
}
}
Package jiekou0923;
public class ClassB implements Interfacea {
Factorial
@Override
public int method (int n)
{
int sum = 1;
for (int i=1;i<=n;i++)
{
Sum*=i;
}
return sum;
}
}
Package jiekou0923;
public class Ceshi {
public static void Main (string[] args) {
Interfacea a=new ClassA ();
System.out.println ("10 's and =" +a.method (10));
Interfacea b=new ClassB ();
System.out.println ("factorial of 10 is =" +b.method (10));
}
}
Write Java programs (factorial) as required