Write Java programs (factorial) as required

Source: Internet
Author: User

(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.