Java Packages and access rights--3

Source: Internet
Author: User

To create a new Class A in package A, there is an int add (int m) method in Class A to seek 1+2+...+m

The and. In package B, create a new Class B, in Class B there is an int cheng (int n) method, used to seek n!

The result. Create a new main Class C in Package C, call the method in a, B to output 1+2+...+30 and,

and 5! Results of the calculation.

Package A;public Class A {//creates a new Class A in packet a, and in Class A has an int add (int m) method, which is used to seek the 1+2+...+m//. Create a new class B in package B, in Class B there is an int cheng (int n) method, which is used to find the result of n!//. Create a new main Class C in Package C, call the method in a, B to output 1+2+...+30,//and 5! Results of the calculation.         int m;            public int Getm () {return m;} public void Setm (int m) {this.m = m;} public int  Add (int m)            {int sum=0;            for (int i=0;i<m;i++)            {            sum+=i;            }            return sum;            }}

Package B;public Class B {//Creates a new class B in Pack B, and in Class B there is an int cheng (int n) method that is used to find the result of n!//.          int n;public int getn () {return n;} public void Setn (int n) {THIS.N = n;} public int Cheng (int n) {int ji=1;for (int i=1;i<=n;i++) {ji*=i;} return ji;}}

Package C;import A.a;import B.b;public class C {public static void main (string[] args) {//TODO auto-generated method stub//Create a new main class C in packet C, call a , b the method output 1+2+...+30 and,//and 5! Results of the calculation. A a=new a (); A.setm (30); a.add; System.out.println (A.add (30)); b b=new B (); B.cheng (5); System.out.println (B.cheng (5));}}

Java Packages and access rights--3

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.