Java Practice Writing Java programs as required:

Source: Internet
Author: User

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 Side

the calculation of 1 to n is required;

(3) write another class:ClassB to Implement Interface Interfacea, Implementing int method (int n) Interface

method 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 oftest class e

The class of the interface.

 Package Pack1;  Public Interface interfacea {    int method (int  n);}
 package   Pack1;  public  class  ClassA implements   Interfacea {@Override  public  int  method (int   N) { ;  for  (int  I=0;i<=n;i++) {A  +=i;     return   A; }}
 package   Pack1;  public  class  ClassB implements   Interfacea {@Override  public  int  method (int   N) { ;  for  (int  I=1;i<=n;i++) {A  *=i;     return   A; }}
 Package Pack1;  Public class E {    publicstaticvoid  main (string[] args) {        interfacea A;        A=new  ClassA ();        System.out.println (A.method (5));        A=new  ClassB ();        System.out.println (A.method (5));}                    }

Java Practice Writing Java programs 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.