Java Learning Diary------Object-oriented ③

Source: Internet
Author: User

Java class design and usage exercises

 Packageday3; Public classTestPerson1 { Public Static voidMain (string[] args) {Person1 A1=NewPerson1 ();        A1.study ();        A1.showage (); A1.addage (2); A1.    Display (); }}classperson1{String name; intAge ; Booleansex;  Public voidStudy () {SYSTEM.OUT.PRINTLN ("Studying"); }     Public voidShowage () {System.out.println ("Age:" +Age ); }     Public intAddage (inti) { age+=i;//Age=age+i;        returnAge ; }     Public voidDisplay () {System.out.println ("Name:" +name+ "Age:" +age+ "Sex:" +sex); }}

 Packageday3;//the Circle class is designed to calculate the area of the circle using an object-oriented approach.  Public classCircle { Public Static voidMain (string[] args) {CIRCL AA=NewCIRCL (); Aa.radius=2.3;    Aa.findarea (); }}classcircl{Doubleradius=1.0;//radius//calculate the area of a circle     Public voidFindarea () {System.out.println (3.14*radius*radius); }}

 Packageday3; Public classTestarry { Public Static voidMain (string[] args) {int[] arr=New int[] {1,2,3,4,5,6,7}; Arryutil au=NewArryutil (); intmax=Au.getmax (arr); System.out.println ("Maximum value is:" +max); intavg=AU.        AVG (arr); System.out.println ("Average is:" +avg);        Au.printarray (arr); Au.sort (arr,"Desc");    Au.printarray (arr); }}//、、、、、、、、、、、、、、、、、、、、、、、、 Packageday3; Public classArryutil {//to find the maximum value of an array     Public intGetmax (int[] arr) {        intMax=arr[0];  for(inti=1;i<arr.length;i++) {            if(max<Arr[i]) {Max=Arr[i]; }        }        returnMax; }            //to find the minimum value of an array     Public intGetmin (int[] arr) {        intMin=arr[0];  for(inti=1;i<arr.length;i++) {            if(min>Arr[i]) {min=Arr[i]; }        }        returnmin; }            //iterating over the elements of an array     Public voidPrintArray (int[] arr) {System.out.print ("[");  for(inti=0;i<arr.length;i++) {System.out.print (Arr[i]+ "\ T"); } System.out.println ("]"); }            //to find the average of an array     Public intAVG (int[] arr) {        intsum=getsum (arr); returnsum/arr.length; }            //find the sum of the arrays     Public intGetsum (int[] arr) {        intSum=0;  for(inti=0;i<arr.length;i++) {sum+=Arr[i]; }        returnsum; }                    //inverse of an array     Public int[] Reverse (int[] arr) {         for(intx=0,y=arr.length-1;x<y;x++,y--) {            inttemp=Arr[x]; ARR[X]=Arr[y]; Arr[y]=temp; }        returnarr; }            //implementing copying of arrays     Public int[] Copy (int[] arr) {        //int[] Arr1=arr; //return arr1; This is actually the case that ARR and arr1 use an array in the same heap space        int[] arr1=New int[Arr.length];  for(inti=0;i<arr1.length;i++) {Arr1[i]=Arr[i]; }        returnarr1; }            //sorting an array     Public voidSortint[] arr,string desc) {        if(desc== "des") {//from small to large//Bubble Sort             for(inti=0;i<arr.length-1;i++) {                 for(intj=0;j<arr.length-1-i;j++) {                    if(arr[j]>arr[j+1]) {                        inttemp=Arr[j]; ARR[J]=arr[j+1]; Arr[j+1]=temp; }                }            }        }Else if(desc== "desc") {            //bubble sort from big to small             for(inti=0;i<arr.length-1;i++) {                 for(intj=0;j<arr.length-1-i;j++) {                    if(arr[j]<arr[j+1]) {                        inttemp=Arr[j]; ARR[J]=arr[j+1]; Arr[j+1]=temp; }                }            }        }    }}

Method overloading for Java classes: In the same class, more than one method of the same name is allowed, as long as their number of arguments or parameter types are different.

Overloading features: Regardless of the return value type, see only the parameter list, and the parameter list must be different. (Number of parameters or parameter type). When called, it differs according to the method parameter list.

overloading Example:

// returns the number of two integers and  int Add (int x,int y) {return x+y;} // returns the number of three integers and  int Add (int x,int y,int z) {return x+y+z;} // returns two decimals and  double Add (double x,double y) {return x+y;}

P

Java Learning Diary------Object-oriented ③

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.