java-day03-Object-oriented start

Source: Internet
Author: User
Tags ming

decimal-to-binary class demo1{public static void Main (string[] args) {int num =6;int[] arr = new Int[32];int index =0;while (num!=0       ) {arr[index++] = Num%2;num = NUM/2;}       index--;    for (; index>=0;index--) {System.out.print (Arr[index]);        }}}//decimal to hexadecimal class demo2{public static void Main (string[] args) {toHex2 (800);//00000000 00000000 00000000 0000000 0011 1100}//Decimal Turn hex//Result: void//parameter: one number public static void toHex1 (int num) {int[] arr = new Int[8];int index = Arr.lengt h;//for (int i=1;i<=8;i++) while (num!=0) {Arr[--index] = Num&15;num = num>>>4;} for (int i=index;i<arr.length;i++) {if (arr[i]>9) System.out.print ((char) (arr[i]-10+ ' a ')); else    System.out.print (Arr[i]);}} Decimal to hexadecimal---look up the table method public static void toHex2 (int num) {char[] ch={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' A ', ' 8 ', ' A ', ' B ', ' C ', '   d ', ' e ', ' F '};char[] arr = new Char[8];int index = Arr.length;while (num!=0) {int n = num&15;   Arr[--index] = Ch[n]; num = num>>>4;} for (int i = Index;i<arr.length;i++) {System.out.print (arr[i]);}} decimal-to-binary----check table method public static void ToBinary2 (int num) {char[] ch={' 0 ', ' 1 '};char[] arr = new Char[32];int index = Arr.lengt   H;while (num!=0) {int n = num&1;   Arr[--index] = Ch[n]; num = num>>>1;} for (int i = index;i<arr.length;i++) {System.out.print (arr[i]);}} Decimal to octal---Check table method public static void ToOctal2 (int num) {char[] ch={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 '};char[] arr = new Char   [11];int index = Arr.length;while (num!=0) {int n = num&7;   Arr[--index] = Ch[n]; num = num>>>3;} for (int i = index;i<arr.length;i++) {System.out.print (arr[i]);}} General functions for the conversion of public static void Toany (int num,int base,int offset) {char[] ch={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', '   A ', ' B ', ' C ', ' d ', ' e ', ' F '};char[] arr = new Char[32];int index = Arr.length;while (num!=0) {int n = num&base;   Arr[--index] = Ch[n]; num = Num>>>offset;} for (int i = index;i<arr.length;i++) {System.out.print (arr[i]);}} public static void tobinary (int num){Toany (num,1,1);} public static void tooctal (int num) {toany (num,7,3);} public static void Tohex (int num) {toany (num,15,4);}} Import Java.util.arrays;class Demo3 {public static void main (string[] args) {int[] arr = {3,45,67,89,123,456};int key = 50 ;//int index = half (arr,key); int index = Arrays.binarysearch (Arr,key); System.out.println ("index=" +index); For an ordered array, if you want to insert an element and keep the array in order, ask how to get the element's position. public static int half (int[] arr,int key) {int min = 0,max = Arr.length-1,mid;while (Min<=max) {mid = (Min+max) >>1;i F (key>arr[mid]) min = Mid+1;else if (key<arr[mid]) max = Mid-1;elsereturn mid;} return-min-1;} 1, reverses the given array. {32,65,12,89,41} {41,89,12,65,32}public static void Fanzhuan (int[] arr) {for (int i=0,j=arr.length-1;i<j;i++,j--) { Huan (arr,i,j);}} public static void Huan (int[] Arr,int i,int j) {int temp = Arr[i];arr[i] = arr[j];arr[j] = temp;}}        Two-dimensional array class demo4{public static void Main (string[] args) {int[][] arr = new INT[2][3]; for (int i=0;i<arr.length;i++) {for (int j=0;j<arr[i].length;j++) System.out.print (Arr[i][j]); System.out.println ();} Int[][] a ={{1,2,3,4},{7,8,9},{1,1,1,1,1,1}};for (int i=0;i<a.length;i++) {for (int j=0;j<a[i].length;j++)            System.out.print (A[i][j]); System.out.println ();}        Int[][] B = new int[2][];        B[0] = new Int[5];b[1] = new INT[3]; }}class Car{int num; String color;public void Run () {System.out.println ("Run");}} Variables defined in the class: member variables, functions defined in the class: member method class Demo5{public static void Main (string[] args) {Car car = new Car ();// An object of type car was created using car.class car.num = 4;car.color = "Red"; Car.run ();}}



/* member variable vs. local variable: 1: member variable has default value local variable has no default value 2: The scope of the member variable is the scope of the entire class local variable from where it is defined to the end of the curly brace where it is located 3: Member variable exists in the heap Local variables exist in the Stack 4: The member variable occurs as the object appears, and the local variable disappears as the object is garbage collected as the function is called to the stack, disappears from the stack as its scope ends */class car{int num;//member variable string color ;p ublic void Run () {System.out.println (num+ "," +color);}} Class Demo6{public static void Main (string[] args) {int num = 23; Car car = new car (); System.out.println (Car.num); System.out.println (Car.color); Car.run ();//Anonymous object: When an object needs to be used only once, you can consider the anonymous object new car (). num = 5;new car (). color= "Red";//        After this code is executed, the object becomes garbage//car c = new Car (); Show (new car);//The pass parameter is using an anonymous object that can simplify writing}public static void show (car car) {Car.run ();}} /* encapsulation, inheritance, polymorphic constructor: is used to create the object, if there is no constructor in a class, then the system will automatically add a default constructor in the class, the class name () {} If you write the constructor in the class, Then the default constructor does not have the feature of the constructor: the initialization of the object 1: Must be the same as the class name 2: Cannot have a return value Type 3: cannot be called, can only be used when creating objects */class person{string name;int age; Person () {}person (String ming,int nian) {name = Ming;age = Nian;} public void Speak () {System.out.println ("person Speaks");}} Class Demo7{public static void Main (string[] args) {Person ren = nEW person ("Li Bai", 238);}} 



Encapsulation: Provide only external useful properties and methods class Mymath{private static void Toany (int num,int base,int offset) {char[] ch={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' F '};char[] arr = new Char[32];int index = Arr.length;while (num!=0) {int n = num   &base;   Arr[--index] = Ch[n]; num = Num>>>offset;} for (int i = index;i<arr.length;i++) {System.out.print (arr[i]);}} public static void tobinary (int num) {toany (num,1,1);} public static void tooctal (int num) {toany (num,7,3);} public static void Tohex (int num) {toany (num,15,4);}} Class Demo8 {public static void main (string[] args) {MyMath mm = new MyMath ();}} Class Student{private string name;private int age;public Student (string ming,int nian) {name = Ming;age = Nian;} public void setage (int nian) {if (nian<18 | | nian>38) SYSTEM.OUT.PRINTLN ("Your age is inappropriate"); elseage = Nian;} public int getage () {return age;}}        Class Demo9 {public static void main (string[] args) {Student ren = new Student ("Small Black", 20); Ren.setage (22); System.out.println (Ren.getage ());}} This: is a reference that always points to the object currently being used class student{string Name;int age;public Student () {}public Student (String name,int age)// The member variable is invalid when the local variable and member variable have the same name {this.name = Name;this.age = age;} public void Show () {System.out.println (name+ "," +age);} Compare two learners with peers//results: boolean//parameter: A learner public boolean issameage (Student stu) {return this.age==stu.age;}} Class Demo10 {public static void main (string[] args) {Student stu = new Student ("Stakes");//system.out.println (stu.name+ ", "+stu.age"); Stu.show ();       Student STU2 = new Student ("Stake 2", "$"),//system.out.println (stu2.name+ "," +stu2.age ");   Boolean flag = Stu.issameage (STU2); SYSTEM.OUT.PRINTLN (flag);}} This: can be used for calls between constructors, this function call must be written on the first line of the constructor class student{string Name;int age;public Student () {This ("Lisi", 20);} Public Student (String Name,int age) {//this (), this.name = Name;this.age = Age;}} Class Demo11{public static void Main (string[] args) {Student stu = new Student ("Stakes", 22);}} /*static: modifier, can be decorated member variable, member function 1: Statically decorated member variable, is the class that belongs to all of the object sharing 2: Static is loaded with the load of the class, so it is the existence of precedence over the object 3: Static Another call way, class name. Disadvantages of Static members: The life cycle is too long comparison of static and non-static member variables: 1: storage location static member variables are stored in the static zone of the method area, non-static member variables are stored in the heap 2: life cycle static member variables with the loading of the class in the static zone to open up memory, As the class disappears, the non-static member variable exists as the object is created and disappears as the object is garbage collected 3: The characteristics of the stored data static member variables store data that is shared by all objects, non-static member variables are stored with data that is unique to each object 4: Calling a static member variable can be called either through an object call or through the class name a non-static member variable can only be called through an object */class person{string name;//instance variable static String country = "CN";//class variable//    Non-static can either use static or non-static public void Show ()//instance method {System.out.println (country);} Static can only use static public static void Fun ()//class Method {System.out.println (country);}} Class Demo12 {public-static void main (string[] args) {person-ren = new Person (); ren.name = "Lisi";/*system.out.println (ren . Country); System.out.println (person.country); ren.country= "USA"; */person ren2 = new Person ();//system.out.println ( Ren2.country); Ren2.fun (); Person.fun ();}} /*public static void Main (string[] args) public: Indicates that access is the largest static: Description The function is loaded with void as the class is loaded: Indicates no return value main: A function name, not a keyword, Just being recognized by the JVM. string[] args: An array of string types, parameters */class test{public static void Main (string[] args) {string[] arr ={"Woieuro", "        Eurioew "," Ldkjfgkldf "}; Demo13.main (arr);}} Class Demo13 {public static void main (string[] args) {//system.out.println (args);//[ljava.lang.string;@949f69// System.out.println (args.length); for (int i=0;i<args.length;i++) {System.out.println (args[i]);} Main (10);} public static void main (int a)//overload {System.out.println (a);}} /* When to use static: member variable: The value of this member variable can be shared by all objects of the class to which it belongs: the member function does not use any non-static member of its owning class */class person{string name; Person (String name) {this.name = name;} public static void "fun" (person per)//can be static, no use of non-static member {SYSTEM.OUT.PRINTLN (per.name) in the person class;}} Class Demo14 {public static void main (string[] args) {person P1 = new Person ("Zhangsan"); person P2 = new Person ("Lisi");p 1.fun (p2);}} Class Demo15 {public static void main (string[] args) {int[] arr={23,4,67,78,9};        Arraytool Arraytool = new Arraytool ();//arraytool.select (arr);//arraytool.print (arr); Arraytool.select (arr); Arraytool.print (arr);}} /** This is a tool class that defines the functionality of a commonly used array of operations, such as finding the most value, sorting, finding, and so on @author small black @version v1.0*/public class arraytool{//to privatize the construction method private Arraytool () {}/** This is the method of finding the maximum value in the array @param The array that receives an integer @return returns the maximum value in the array */public static int Getmax (int[] arr) {int max = arr[0];for (int i=0;i<arr.length;i++) {if (AR R[i]>max) max = Arr[i];} return Max;} /** This is the method of finding the smallest value in the array @param receive an integer array @return returns the minimum value in the array */public static int getmin (int[] arr) {int min = arr[0];for (int i=0;i <arr.length;i++) {if (arr[i]<min) min = arr[i];}    return min;} /** This is the method of selecting sorting for an array @param receive an integer type of @return no return value */public static void Select (Int[] arr) {for (int i=0;i<arr.length-1;i++ {for (int j=i+1;j<arr.length;j++) {if (Arr[j]<arr[i]) {Huan (arr,i,j);}}} /** This is the method of exchanging the values in the specified subscript in the array @param receive an integer type @param i receives a subscript @param J receives a subscript */public static void Huan (int[] Arr,int I,int j) {    int temp;temp = Arr[i];arr[i] = arr[j];arr[j] = temp;} /** This is a method of finding a number from an array @param receive an array of integers @param the number of lookups @return returns the subscript of the number being looked up, not found return -1*/public static int halfsearch (int[] Arr,i NT key) {int min = 0,max = Arr.length-1,mid;while (Min<=max) {mid = (Min+max) >>1;if (key>arr[mid]) min = Mid +1;el Se if (key<arr[mid]) max = mid-1;else return mid;}    return-1;} /** This is the method that prints the values in the array @param receive an integer array @return no return value */public static void print (int[] arr) {for (int i=0;i<arr.length;i++) {if ( i!=arr.length-1) System.out.print (arr[i]+ ","); else System.out.print (Arr[i]);}}} /* Static code block: Executes as the class loads, executes only once, takes precedence over the execution of the main method static{}*/class test{static int num =6;//needs to be written in front of the static code block static{ SYSTEM.OUT.PRINTLN (num);} public static void Show () {System.out.println (num);}} Class Demo16 {static{system.out.println ("B");} public static void Main (string[] args) {//test t = new Test ();//test t2 = new Test ();//Each class is loaded only once//system.out.println (" D "); Test.show ();//test Test = Null;//test.class not loaded}static{system.out.println ("C");}} /* Building Blocks: objects are executed as soon as they are created, which takes precedence over the execution of constructors, and can be used to differentiate between initialization and constructors for all objects: constructors simply initialize for an object {}*/class person{string name = "Haha"; String country;static{system.out.println ("a"); Person () {} {System.out.println (this.name);}        Person (String name) {this.name = name; System.out.println (this.name);} public void Show () {System.out.println (this.name);}} Class Demo17 {Static{system.out.println ("B");}        public static void Main (string[] args) {person ren1 = new Person ("hehe");   Local code block: You can limit the scope of a variable {int a = 56;    System.out.println (a);} Initialization of the}}/* object: 1: Because the object is created using class, the bytecode file class name of the corresponding class is loaded first. Class2: If there is a static code block, execute static code block 3: Open Space in memory 4: Default initialization for member variables in heap memory (assign default) 5: Display initialization of member variables in heap memory 6: Execution construct code block 7: Execute constructor 8: Assign memory header address in heap memory to reference in stack */class person{private String name= "Xiaohong";p rivate int Age =23;private static String country= "CN"; {System.out.println (name+ "" +age);} Public person (String Name,int age) {this.name = Name;this.age = age;} public void SetName (String name) {this.name = name;} Public String GetName () {return this.name;}} Class Demo18{public static void Main (string[] args) {person p = new person ("small white");p. SetName ("small Black");}} Exercise: Note: Write the program code according to the Java specification, if you think the program has errors, please indicate, and explain the cause of the program error. 1.class demo{void Show (int a,int b,float c) {}}a.void Show (int a,float c,int b) {}//b,void Show (int a,int b,float c) {}//c.in T Show (int a,float C,int b) {return A;} D.int Show (int a,float c) {return A;} Which answer and show function overloads。 ACD--------------------------------------------------2. Write the results. Class Demo{public static void Main (string[] args) {int x=0,y=1;if (++x==y--&x++==1| | --y==0)/system.out.println ("x=" +x+ ", y=" +y);//elsesystem.out.println ("y=" +y+ ", x=" +x);}} X=2,y=0--------------------------------------------------3. Write out the results. Class Demo{public static void Main (string[] args) {int a=3,b=8;int c= (a>b)? a++:b++;  System.out.println ("a=" +a+ "\tb=" +b+ "\tc=" +c); int d= (a>b)? ++a:++b;  System.out.println ("a=" +a+ "\tb=" +b+ "\td=" +d); int e= (a<b)? a++:b++; System.out.println ("a=" +a+ "\tb=" +b+ "\te=" +e); int f= (a<b)? ++a:++b;  System.out.println ("a=" +a+ "\tb=" +b+ "\tf=" +f); }}a=3,b=9,c=8 a=3,b=10,d=9 a=4,b=10,e=3 a=5,b=10,f=5--------------------------------------------------4. Write the results.     Class Demo{public static void Main (string[] args) {int m=0,n=3;if (m>0) {if (n>2) System.out.println ("A"); ElseSystem.out.println ("B");}}} --------------------------------------------------5. Write the results. public class demo{public static void MaiN (String []args) {int i = 0, j = 5; tp:for (;;) {i++; for (;;) {if (i > j--) break tp;}} System.out.println ("i =" + i + ", j =" + j);}} i= 1,j=-1--------------------------------------------------6. Write the results.       Class Demo {public static void main (string[] args) {String foo= "blue"; boolean[] bar=new boolean[2]; if (bar[0]) {    Foo= "Green"; } System.out.println (foo);}} Blue--------------------------------------------------7. Write the results. public class Test {public static void leftshift (int i, Int. j) {i+=j;} public static void Main (String args[]) {in t i = 4, j = 2; Leftshift (i, j); System.out.println (i); }} i=4--------------------------------------------------8. Write the result. public class demo{public static void Main (string[] args) {int[] a=new int[1]; modify (a); System.out.println (A[0]); }public static void Modify (Int[] a) {a[0]++;}} 1--------------------------------------------------9.class test{public static void Main (string[] args) {String foo= ARGS[1]; String bar=args[2]; String baz=args[3];   }} D:\>java Test Red Green Blue What is the value of Baz? A. Baz has value of "" "B. Baz has value of NULL C. baz has value of" Red "D. Baz has value of" Blue "E. Baz has VA Lue of "Green" F. The code does not compile G. The program throw a exception G--------------------------------------- -----------11. Brief description: function in the program and operation characteristics. Implement code reuse. The function is called into the stack, using the end of the stack--------------------------------------------------12. Print 99 multiplication Table public class foo{public static void main (String[]args)      {for (int i =1;i<=9;i++) {for (int J =1;j<=i;j++) {System.out.print (j+ "*" +i+j*i+ "");    } System.out.println (); }}}--------------------------------------------------13. Print the following graphic (* * * * * * * * * * * * * * * * * public class foo{PU Blic static void Main (String[]args) {for (int i = 1;i<=5;i++) {for (int J =1;j<=j-i;j++) {System.out.          Print ("");           } for (int k =1;k<=i;k++) {System.out.println ("*");    } System.out.println ();    }}}------------------------------------------------------14. Which of the following array definitions is wrong. Add a single-line comment to the wrong answer and write the reason for the error. A,float[]=new Float[3]; No reference name B, float f2[]=new float[];//Not defined length C, float[] f1=new float[3];D, boolean[] b={"true", "false", "true"};//type inconsistent E, Double f4[]={1,3,5};  F, int f5[]=new int[3]{2,3,4}; Cannot define length g, float f4[]={1.2,3.0,5.4};//must be preceded by a value f


java-day03-Object-oriented start

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.