JAVA-day03-object-oriented start, java-day03-start

Source: Internet
Author: User
Tags decimal to binary variable scope

JAVA-day03-object-oriented start, java-day03-start

// Convert 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]) ;}}// convert decimal to hexadecimal class Demo2 {public static void main (String [] args) {toHex2 (800 ); // 00000000 00000000 00000000 0000000 0011 1100} // convert decimal to hexadecimal format // result: void // parameter: public static void toHex1 (int num) {int [] arr = new int [8]; int index = arr. length; // 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]) ;}// convert the value to hexadecimal in decimal format --- public static void toHex2 (int num) {char [] ch = {'0 ', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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]) ;}/// convert to binary in decimal format --- public static void toBinary2 (int num) {char [] ch = {'0 ', '1'}; char [] arr = new char [32]; int index = arr. length; 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 --- public static void tooct2 (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 function of hexadecimal conversion 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,);} 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 = {123,456,}; int ke Y = 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 ensure that the array is still ordered, ask how to get the element location. Public static int half (int [] arr, int key) {int min = 0, max = arr. length-1, mid; while (min <= max) {mid = (min + max)> 1; if (key> arr [mid]) min = mid + 1; else if (key <arr [mid]) max = mid-1; elsereturn mid;} return-min-1;} // 1, reverses the given array. {, 41} {, 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 }}; for (int I = 0; I <. 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 = new car (); // use Car. class creates a Car object. num = 4; car. color = "red"; car. run ();}}



/* Comparison of member variables and local variables: 1: member variables have default values. Local variables do not have default values. 2: the scope of the member variable is that the scope of the local variable of the entire class is from the position it is defined to the end of its braces 3: member variables exist in the heap. Local variables exist in the stack. 4. member variables appear with the appearance of objects, the local variable disappears as the object is reclaimed by garbage collection. It is called only when the function in which it is located is loaded into the stack, and disappears from the stack as its scope ends */class Car {int num; // member variable String color; public 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 only needs to be used once, you can consider the anonymous object new Car (). num = 5; new Car (). color = "red"; // After the code is executed, the object becomes garbage // Car c = new Car (); show (new Car ()); // when passing parameters, the anonymous object can be used to simplify writing} public static void show (Car car) {car. run () ;}}/* encapsulation, inheritance, polymorphism constructor: used to create objects. If a class does not contain constructors, the system will automatically add a default constructor to the class. If the class name () {} has written the constructor in the class, the default constructor does not have the constructor feature: it can be used for object initialization. 1: it must be the same as the class name. 2: No return value type. 3: it cannot be called, you can only use */class Person {String name; int age; Person () {} Person (String ming, int nian) {name = ming; age = nian;} when creating an object ;} public void speak () {System. out. println ("talking") ;}} class Demo7 {public static void main (String [] args) {Person ren = new Person ("Li Bai", 238 );}}



// Encapsulation: only useful attributes and methods are provided externally. 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,);} 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; pr Ivate 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 not suitable"); elseage = nian;} public int getAge () {return age ;}} class Demo9 {public static void main (String [] args) {Student ren = new Student ("", 20); ren. setAge (22); System. out. println (ren. getAge () ;}// this: a reference that always points to the currently in use object class Student {String name; int Age; public Student () {} public Student (String name, int age) // when the local variable and the member variable have the same name, the member variable is invalid {this. name = name; this. age = age;} public void show () {System. out. println (name + "," + age);} // compare the two students with their peers. // result: boolean // parameter: public boolean isSameAge (Student stu) {return this. age = stu. age ;}} class Demo10 {public static void main (String [] args) {Student stu = new Student ("Stake", 22); // System. out. println (stu. name + "," + stu. age); st U. show (); Student stu2 = new Student ("stake 2", 22); // System. out. println (stu2.name + "," + stu2.age); boolean flag = stu. isSameAge (stu2); System. out. println (flag) ;}// this: can be used to call between constructors. this function must be written in 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) {Studen T stu = new Student ("", 22) ;}/ * static: modifier, which can be used to modify member variables. member function 1: The member variable that is statically modified, share all objects of the class to which the object belongs. 2. Static is loaded as the class is loaded, so it is the priority of objects. 3. Static is a more called method, class Name. static member disadvantages: Comparison of static member variables and non-static member variables with a long life cycle: 1: storage location static member variables are stored in the static area of the method area. Non-static member variables are stored in the heap. 2. The static member variables in the lifecycle are opened up in the static area as the class is loaded, non-static member variables exist with the creation of the object only when the class to which the object belongs disappears. 3: data storage features static member variables store data shared by all objects. Non-static member variables store data unique to each object. 4: call method static member variables can be called either through an object or through a class name. Non-static member variables can only be called through an object */class Person {String name; // instance variable stat Ic String country = "CN"; // class variable // non-static, either static or non-static public void show () // instance method {System. out. println (country);} // static only 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 (); // Sys Tem. out. println (ren2.country); ren2.fun (); Person. fun () ;}/ * public static void main (String [] args) public: indicates that the maximum access permission is static: this function is loaded with the class. void: indicates that no return value is returned. main: A function name, not a keyword. It is only recognized by JVM. String [] args: an array of the String type. The parameter */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 (arg S); // [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) // reload {System. out. println (a) ;}}/* when to use static: member variable: the value of this member variable can be shared by all objects in the class of the member function: this member function does not use any non-static member */class Person {String name; Person (String name) {this. name = name;} public static void fun (Person per) // It can be static and does not use non-static member {System. out. println (Per. name) ;}} class Demo14 {public static void main (String [] args) {Person p1 = new Person ("zhangsan "); person p2 = new Person ("lisi"); p1.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 functions of commonly used operation arrays, such as finding the most value, sorting, and searching @ autho R blacklist @ version V1.0 */public class ArrayTool {// privatize the constructor private ArrayTool () {}/** this is the method for finding the maximum value in the array @ param receives an integer array @ 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 (arr [I]> max) max = arr [I];} return max ;} /** this is the method for finding the minimum value in the array @ param receives 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) m In = arr [I];} return min ;} /** this is the method for selecting and sorting arrays @ param to receive an integer array @ 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 for exchanging values in the specified subscript in the array @ param receives an integer array @ 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 to find a number from the array. @ Param receives an integer array @ param number to be searched @ return returns the subscript of the number to be searched, -1 */public static int halfSearch (int [] arr, int key) {int min = 0, max = arr. length-1, mid; while (min <= max) {mid = (min + max)> 1; if (key> arr [mid]) min = mid + 1; else if (key <arr [mid]) max = mid-1; else return mid;} return-1 ;} /** this is the method to print the values in the array @ param receives 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: it is executed only once as the class is loaded, the execution of the main method is preferentially static {} */class Test {static int num = 6; // the front side of the static code block must be written in 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 // Sy Stem. out. println ("d"); Test. show (); // Test test = null; // Test. class not loaded} static {System. out. println ("c") ;}}/* constructor code block: an object is executed when it is created, prior to the constructor, the difference between initialization and constructor for all objects: constructor only initializes 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. print Ln (this. name) ;}} class Demo17 {static {System. out. println ("B");} public static void main (String [] args) {Person ren1 = new Person ("Haha"); // partial code block: variable scope can be limited to {int a = 56; System. out. println (a) ;}}/ * object initialization process: 1: Because the class is used to create an object, 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 the memory 4: Initialize the member variables in the heap memory by default (default value) 5: display the member variables in heap memory initialization 6: Execute the constructor code block 7: Execute the constructor 8: assign the first address of the memory in the heap memory to the reference in the stack */class Person {private String name = "xiaohong"; Private 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 ("", 20); p. setName ("") ;}} exercise: Note: Write the program code according to Java specifications. If you think the program has an error, please point out and explain the cause of the program error. 1. class Demo {void show (int a, int B, float c) {}}. void show (int a, float c, int B) {}// B, void show (int a, int B, float c) {}// C.int show (int, float c, int B) {return a;} // D.int show (int a, float c) {return a;} // which answer and show function overload. ACD--------------------------------------------------2. Write the result. 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 the output result. 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 result. 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 result. 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 result. 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 result. Public class Test {public static void leftshift (int I, int j) {I + = j;} public static void main (String args []) {int 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 value of "Green" F. the code does not compile G. the program throw an exception G--------------------------------------------------11. description: Functions in programs and their running features .. Reuse code. When a function is called, the function is pushed to the stack. The end of the function is used to exit the stack -------------------------------- 12. print the 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 figure **************** public class foo {public 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 arrays is incorrectly defined. Add a single line comment to the wrong answer to write out the cause of the error. A, float [] = new float [3]; // no reference name B, float f2 [] = new float []; // no definition 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}; // The length of G cannot be defined, float f4 [] = {1.2, 3.0, 5.4}; // f must be added after the value


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.