Java Second Job

Source: Internet
Author: User

(a) Study summary 1. What is a construction method? What is an overload of a constructor method? Can the following program be compiled? Why?

(1) The main function of constructing methods in object-oriented programs is to initialize the properties in the class. The following points should be noted in the construction method ① the name of the constructor must be the same as the class name
② cannot have any description of the return value type at the declaration of the constructor method
③ cannot use return to return a value in a constructor method
(2) The overloading of the constructor method is the same as the method name, but the type of the parameter differs from the number of arguments. Method invocations of different functions can be done by passing the number of parameters and the different types.
(3) The following program cannot be compiled
Reason: Only constructors with parameters are defined, no parameterless construction methods are defined.

public class Test {    public static void main(String args[]) {        Foo obj = new Foo();           }     }class Foo{    int value;    public Foo(int intValue){        value = intValue;    }}
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;}

Value is a global variable and should be static after public
Modify the following:

public class Test {    public static void main(String[] args) {        MyClass[] arr=new MyClass[3];        arr[1].value=100;    }}class MyClass{    public static int value=1;}
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;}

The result of the run is: false because the comparison is compared to the address.

4. What is object-oriented encapsulation and how is encapsulation implemented in Java? Try to illustrate the example.

Encapsulation is a major feature of face-to-face objects, so-called encapsulation refers to the external invisible.
The format of the encapsulation is as follows:
Property Encapsulation: Private Property Type property name;
Method Encapsulation: The name of the private method's return value method (parameter list) {}

class Person{    String name;    int age;    public void tell() {        System.out.println("姓名: "+name+"年龄:"+age);    }}public class bky {    public static void main(String args[]) {     Person per=new Person();    per.name="张三";    per.age=-30;    per.tell(); }    }
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++);    }}

Cannot be compiled, secret in A is private class 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();    }}

Cannot pass because static references to non-static field x should be added in front of int 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, 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;   static int num;  Declares static variables, {num=1000; Defines static code blocks to initialize static variables} public book () {//constructor method} public book (String bookname,double price) {thi        S.bookname=bookname;        This.price=price;        This.bookid=num;    num++;    } 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;    } public int Getbookid () {///define method to find the total number of books return bookId;    } public void Setbookid (int bookId) {this.bookid = BookId;        } public static int Totalbook () {int n;        n=num-1000;    return n; } public String ToString () {//Override toString Method return "number:" + BookId + ", Title:" + BookName + ", Price:" + PR    Ice }}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.

(1) Singleton design mode: guarantees that a class has only one instance and provides a global access point to access it

(2) Features: ①, Single-Class construction method privatization (private modification), there can only be one instance;
②, the Singleton class must create its own instantiation object (private static type);
③, singleton classes must provide this instance to other classes (define a static method)

(3) Advantages: ①, avoid the repeated creation of instantiated objects, not only reduce the time cost of each object creation, but also save memory space;
(②, avoid situations in which some values are inconsistent due to the creation of multiple instances

(4)

public class sun {                private sun(){}         private static sun asd=new sun();         public static sun getAsd(){                                return asd;            }    }
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
The method cannot modify the contents of any parameter variables that are passed to it by addressing it and capitalizing the letter with the address 0. The change call is a copy of STR and cannot alter the contents of STR.

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

(1). Create a contestant class, define the construction method, getter and setter method, tosting method, real comparable interface, complete object comparison method.
(2). Create a scoring class that defines the construction method, getter and setter methods, and calculates the average avarage (double score[])
(3). Create a test class, enter the number of contestants and judges, build a contestant class, and a scoring class. Using the Loop input member information, the judges score, using the class method to calculate the average. )

2.Email Verification
    用 if 进行判断
3. Find sub-strings (c) Code hosting

Https://gitee.com/hebau_java_cs16/Java_CS02WWQ/commits/master

Java second job

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.