Second time Java job

Source: Internet
Author: User
Tags first string

I. Key points of study
READ carefully and check the relevant information, master the following content:
Understanding the relationship between object creation and reference
Mastering overloading of construction methods
Mastering the String class
Mastering the design method of class
Master this keyword
Mastering the Static keyword
Understanding reference delivery and mastering basic applications
Mastering the Singleton mode
Understanding Inner Classes
Second, the operation requirements
Publish an essay that mainly includes the following sections:
(a) Learning summary
1. What is a construction method? What is an overload of a constructor method? Can the following program be compiled? Why?
The main function of constructing methods in object-oriented programs is to initialize the properties, in the object Generation format "class name Object name =new class name ()" know, as long as "()" appears, it means that the method is called, this method is actually to call the construction method, the construction method can be regarded as a special method, It is defined in a way similar to a normal method.
The following points should be noted in the construction method:
The name of the 1> construction method must be the same as the class name.
There cannot be any return value type declaration at the declaration of the 2> constructor method.
3> cannot use return to return a value in a constructed method.
The constructor method is called when the keyword new instantiation object is called.
As long as there is a class there must be a construction method, in Java if a class does not explicitly declare a construction method, at compile time will be directly generated a parameterless, do not do the construction method. Example form is as follows:

class Person{        public Person(){}}    }

If a constructor is explicitly declared in a class, the program will not generate a default constructor at compile time, that is, there should be at least one constructor in a class.

public class Test {    public static void main(String args[]) {        Foo obj = new Foo();           }     }class Foo{    int value;    public Foo(int intValue){        value = intValue;    }}

cannot be compiled because there is no parameterless construction method, only the parameter constructor method is defined in the class, at which point the compiler no longer establishes the default parameterless construction method.
2. What is the result of running the following procedure? Analyze the reason and how it should be modified.

public class Test {    public static void main(String[] args) {        MyClass[] arr=new MyClass[3];        arr[1].value=100;    }}class MyClass{    public int value=1;}

Compile an error, declare only the array, not materialized, should each element in the array through the new construction method to instantiate.

3. What is the result of running the following procedure? explain why.

public class Test {    public static void main(String[] args) {        Foo obj1 = new Foo();        Foo obj2 = new Foo();        System.out.println(obj1 == obj2);    }}class Foo{    int value = 100;}

Flase occurs because two objects are opened with new addresses, so the last comparison is the address and two addresses are different.
4. What is object-oriented encapsulation and how is encapsulation implemented in Java? Try to illustrate the example.
1> encapsulation is the first object-oriented feature, so-called encapsulation refers to the external invisible. In order to avoid the occurrence of errors such as accessing properties directly from objects in the class, which is not allowed in object-oriented, it is often necessary to encapsulate the attributes in the class in general development (private).
2> Property Encapsulation: Private Property Type property name;
Method Encapsulation: Private method return value method name (parameter list) {}
All attributes in the 3> class must be encapsulated, and the encapsulated property must be accessed through the setter and getter (that is, set up and obtained through both methods).
4> a property or method declared with private can only be called inside its class, not outside of the class.

5. Read the procedure below to see if you can compile the pass? If not, explain why.

(1)class A{    private int secret = 5;}public class Test{    public static void main(String args[]){        A a = new A();        System.out.println(a.secret++);    }}

The secret is private and cannot be called directly
(2)

public class Test{    int x = 50;    static int y = 200;    public static void method(){        System.out.println(x+y);    }    public static void main(String args[]){        Test.method();    }}

The

method is static decorated and can only access static decorated fields.
6. Using static variables and construction methods of a class, you can track the number of objects created by a class. Declare a book class, the data members are numbered, the title, the book Price, and the number of static data member book record number of books. Book numbering starts with 1000, and each object is generated, the number is automatically incremented (implemented using static variables and construction methods). Here is a partial code for the test class code and the book class, which complements the code as a whole.

class book{int bookId;    String BookName;    Double Price;    Declare static variable public static n;    Defines static code blocks for static variables initialized with the static {n=0;        }//Construction method public book (String bookname,double price) {this.bookname=bookname;        This.price =price;        n++;    This.bookid=1000+n;    } public String Getbookname () {return bookname;    } public void Setbookname (String bookname) {this.bookname = BookName;    } public double GetPrice () {return price;    public void Setprice (double price) {this.price = Price;     }//define method to find the total number of books public int Totalbook () {return n;    }//Override toString method public string ToString () {return "number" +bookld+ "title" +bookname+ "Price" + "prices" + "total books" +n;                        }}}public class test{public static void Main (String args[]) {book[] books = {new book ("C Language Programming", 29.3),        New book ("Database Principle", "New book" ("Java Learning note", 68)}; System.out.pRINTLN ("Total number of books:" + Book.totalbook ());        for (book book:books) {System.out.println (book.tostring ()); }    }   }... 7. What is a singleton design pattern? What characteristics does it have?    Design a solar class sun with a single-instance design pattern. Singleton mode is a kind of common software design pattern. In its core structure consists only of a special class called Singleton. The singleton mode ensures that the class with the pattern can have only one instance of the class that is used in the system. That is, a class has only one instance of an object. Feature: There is only one instance in a class.      ... class sun{private Sun instance = new Sun ();      Private Sun () {} public static Sun GetInstance () {return instance; }}

8. Understand the Java parameter passing mechanism, read the following program, what is the result of running? explain why.

public class Test {    String str = new String("你好  ");    char[] ch = { ‘w‘,‘o‘,‘l‘,‘l‘,‘d‘ };    public static void main(String args[]) {        Test test = new Test();        test.change(test.str, test.ch);        System.out.print(test.str);        System.out.print(test.ch);    }    public void change(String str, char ch[]) {        str = "hello";        ch[0] = ‘W‘;    }}

Running Result: Hello Wolld
-reason: STR is a reference type, call the change method to pass in the address, and then release the address space after changing the value of str in the changes method, without changing the value of str in the Main method. The CH represents the address of the array, and the change method changes the value of CH first character and passes CH back to the main method.
(ii) Experimental summary
This experiment includes experiment two and experiment 32 times content:
1. Complete the scoring system with object-oriented thinking
2.Email Verification
Create a Boolean type method to determine the validity of the email four conditions, four conditions are satisfied when the return of true, there is a condition is not satisfied then return false.

    • Verify in the Main method, enter an email address, call the Judgment method, the return value is true, the output address is valid, otherwise invalid.
      3. Finding substrings
      The characters that are not equal to the substring are deleted, and several substrings can be obtained by dividing the number of remaining digits by the number of substrings.
      4. Statistical documents
      Using the split method to split a string into a single file, the first character of each file is intercepted by the method of the string class, and then the upper method is used to complete the case conversion of the character, plus the first string followed by a re-assignment to the file name. In the loop, press the filename string "." Take apart, save the last array element after the break, define an array of counts after the loop, the size equals the array of file types, and from the first query there is no element with the same, the corresponding count array + +, and the same file type array element is empty and the corresponding count array value is changed to 0, Final output
      5. Design of the class
      The problems encountered in the process of completing the experimental content, the solution and the design thinking and thinking of the procedure are summarized.
      (c) Code hosting (be sure to link to your project)

Second time Java job

Related Article

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.