The second job of Java technology

Source: Internet
Author: User

(a) Learning summary

1. What is a construction method? What is an overload of a constructor method?
1) There is no return value, the name is the same as the class name, and when the new object is created, the constructor is called, and to construct the function, the object must be declared and initialized. Each class has a constructor, and if there is no write constructor, the compiler automatically adds the parameterless constructor method

2) The overload of the constructor is that the function name is the same as the class name, with different parameter types and different parameters.
1. The name of the construction method must be the same as the class name
2. The declaration of a constructor method cannot have any declaration of a return value type
3. Cannot use return in construction method
4. When you declare a constructor, the system no longer creates a default parameterless construction method
Can the following program be compiled? Why?

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;}

Result: Compile error,
Cause: Only declared array of objects, without implementing object instantiation, should be instantiated for each object element in the array, through the new constructor method,
Modified: Arr[1]=new MyClass (); arr[1].value=100;

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;}

Result: false,
Reason: Because the comparison is the reference address of two objects, instead of the content, two objects are different new spaces opened through new, and cannot be compared by = =.

4. What is object-oriented encapsulation and how is encapsulation implemented in Java? Try to illustrate the example.
Encapsulation: encapsulation means that some information is closed, not all users can see, the protection of some properties and methods of the current class is not seen externally,
Encapsulation implementation: The encapsulation of Java is implemented by declaring properties and methods with the private modifier;
Example:

public class Birthday {    private int year;    public int month;    public int day;}

The year of this Birthday class is encapsulated when you define an object Birthday b=new Birthday (); For object B, you can use B.month and b.day; but you cannot use b.year;
The corresponding Get/set method is called through the object.

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

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

No, because the secret field is private, you cannot access the operation directly.

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 Mothod method is static decorated and can only access static decorated fields, X is not
Workaround: Add static in front of X.

6. Use static variables and construction methods of a class to track the number of objects created by a class. Declare a book class, data members are numbered,

Title, book price, and the total number of static data member books recorded. The book number starts with 1000, each producing an object,
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; Declares static variables public static n;//defines static code blocks to static variables initialized 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 book the total number of books public int Totalbook () {return n;} Override the ToString method public string ToString () {return "number" +bookld+ "title" +bookname+ "Price" + "prices" + "book totals" +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: The core structure contains only one special class that is called a singleton.
Features: Through a single case mode can be guaranteed in the system, the application of the pattern Class A class has only one instance. That is, a class has only one instance of an object

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‘;}}

Result: Hello Wolld
Cause: The char type passes the address, so the result changes, and the string is passed in a new address to store the content to be passed, the content of the new address changes, the original address will not change;

(ii) Experimental summary
1. Complete the scoring system with object-oriented thinking

Program Design ideas: Enter the number of contestants, the number of judges, and then enter the player information, so that all the judges to score the contestants, after the score will be sorted, remove the highest score and the lowest score, the average score, will be the final average spread to the player's information, as the final result of the contestant;
Problem:
Workaround:

2.Email Verification

This question is no problem.

3. Finding substrings

This question is no problem.

4. Statistical documents

Issue 1: For some special situations, consider not comprehensive, such as when a file name contains multiple '. ' ;
Workaround: In this case we need to find the last '. ' And then go to this '. ' The following content, with a loop to handle the constant search '. ' and then go '. ' After the content, know that the content does not contain '. ' ;

5. Design of the class

Question 1: Establishing linkages between two classes;
Workaround: Two classes to establish each other's object, and then make the call;

(c) Code hosting

The second job of Java technology

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.