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

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

No space is allocated for the defined array. Switch

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

4. What is object-oriented encapsulation and how is encapsulation implemented in Java? Try to illustrate the example.
The property is privately privatized, set and acquired through setter and getter methods, and invoked with the this reference to implement access to the private property by the public method.
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++);    }}


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

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;        The static int id,totalbook;//declares the static{totalbook=0;    id=1000;        }//defines a static code block to initialize a static variable to public book (String bookname,double price) {id++;        This.bookname=bookname;        This.price=price;        Bookid=id;    totalbook++;    }//Construction method 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 static int Totalbook () {return totalbook;    }//Definition Method General Book number public String toString () {return "number:" +bookid+ "title:" +this.bookname+ "Price:" +this.price; }//overriding the ToString method}public class test{public static void Main (String args[]) {book[] books = {new book ("C Language Program set                     29.3), new book ("Database Principle", 30),   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.
The singleton design pattern is a common software design pattern that contains only a special class called a singleton class in its core structure. The singleton mode ensures that there is only one instance of a class in the system.
Features: 1, a class can have only one instance;
2, you must create this instance yourself;
3. This instance must be provided to the entire system on its own.

class Sun{    private Sun() {}    private static Sun s=new Sun();    public static Sun getS() {        return s;    }}

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,;        System.out.print(test.str);        System.out.print(;    }    public void change(String str, char ch[]) {        str = "hello";        ch[0] = ‘W‘;    }}
    结果:你好  Wolld

The arguments passed by the calling method are only used in the called method and do not change the contents of the original parameter.

(ii) Experimental summary

This experiment includes experiment two and experiment 32 times content:
1. Complete the scoring system with object-oriented thinking
Program Design ideas: Follow the teacher's template step by step to complete.
Question 1: There are a lot of problems with writing for the first time.
Reason: Do not understand how to call.
Solution: The teacher helped to modify.
2.Email Verification
Program design ideas: With three layers nested, think the program is more troublesome, but did not think there is any better way.
3. Finding substrings
Program Design ideas: Find the index of the occurrence of the string, the next time from the subscript to continue to find, until the entire string is checked, not found once, Count plus one.
4. Statistical documents
Program Design ideas: First the string "," split, and then each string into a string array, according to the ASCII code value to convert the first letter to uppercase; When the number of files is counted, an empty string is defined, and each file name is pressed in turn. After splitting and other file names comparison, if the equality count plus one, each comparison is placed in an empty string, each time before the next comparison to determine whether to compare, if it is no longer compared.
Issue 1: Before it will appear if there are two identical files, two files will be output two.
Reason: There is no judge whether to count.
Solution: Added an empty string to hold the file name that was counted.
5. Design of the class
Program Design Ideas:
Issue 1: Test class calls
Solution: Multiple changes, still unable to compile

(c) Code hosting (be sure to link to your project)

Code Cloud Commit History


The second job of Java technology

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: 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.