Java self-test answer-correct me

Source: Internet
Author: User

 

Question 1

1 pair

2 error

3 error

4 pairs

5 pairs

6 Error

7 Error

8 Error

9 Error

10 Error

 

Ii. Short answer

1 Interpretation Type

 

2 void is generally used when the method does not return any value. Null, null. If an object is declared but not instantiated

 

It is null.

 

Class 3 is the concept of Java, an object-oriented programming language, and class is the encapsulation of attributes and methods.

A class library is a collection of classes. It can be a built-in JDK class library or a class Library added externally.

 

Programming.

Generally, during the development of Java projects, the entire project is in the form of a package. The package includes various types, resources used, and class libraries used (

 

It can also be an imported external class library ).

Jar files are compressed files on the Java platform. packages can be packaged into jar files, classes can be packaged into jar files, and class libraries can also be

 

.

 

4 "send messages between objects"

The interaction between objects is through message transmission. Specifically, it can be divided into the following situations:

One method (or function) calls each other;

 

Two objects use the attributes of another object;

 

Three or two objects share a resource for interaction.

 

For example

Public Class {

Public void methodina (){

B = new B ();

B. methodinb ();

B. signal;

// Others

}

Public static void main (string [] ARGs ){

New A (). methodina ();

}

}

Public Class B {

Public static int signal;

Public void methodinb (){

//

}

}

 

For the third point, such as database-centric applications.

 

5 packing and unpacking

 

6. Example

A specific example shows that we need to combine all the numbers from 1 to 100 to form a long string.

Stringbuffer SBF = new stringbuffer ();

For (INT I = 0; I <100; I ++)

{

SBF. append (I );

}

The code above is very efficient, because only one stringbuffer object is created, and the following code is very inefficient because

 

101 objects.

String STR = new string ();

For (INT I = 0; I <100; I ++)

{

STR = STR + I;

}

 

7 Neng

 

8. 1001/10 is an integer division, and the result is 100, which is then converted to 100.0.

 

9 because exception is the parent class of all other exceptions. If it is placed in front, all exceptions will be caught by it.

 

The catch code block will never be executed, and it makes no sense.

 

The first section of 10 is true because the value (content) is compared ).

The second paragraph is false because it compares the memory addresses of two string objects.

 

Three programming questions

 

1. (There seems to be a simpler method to use recursion)

Import java. Io. bufferedreader;

Import java. Io. ioexception;

Import java. Io. inputstreamreader;

Import java. util. Stack;

 

Public class test {

Public static stringbuffer reverse (string Str ){

If (STR = NULL ){

System. Out. println ("illegal input ");

Return NULL;

}

Stack <character> stack = new stack <character> ();

Int I;

For (I = 0; I <Str. Length (); I ++ ){

Stack. Push (Str. charat (I ));

}

Stringbuffer sb = new stringbuffer ();

While (! Stack. isempty ()){

SB. append (stack. Pop ());

}

Return Sb;

}

Public static void main (string [] ARGs) throws ioexception {

System. Out. println ("enter a string :");

Bufferedreader BR = new bufferedreader (New inputstreamreader

 

(System. In ));

System. Out. println ("You entered:" + reverse (Br. Readline ()));

}

 

}

 

Recursive Method

Int Index = 0;

Public void reverse (string in ){

Char CH = in. charat (INDEX );

Index ++;

While (Ch! = '/0 '){

Reverse ();

System. Out. println (CH );

}

}

 

2

 

Public static void caculate (INT [] ARR ){

If (ARR = NULL | arr. Length = 0 ){

System. Out. println ("parameter error, return ");

Return;

}

Double sum = 0;

Double Ave = 0.0;

Int max = arr [0];

Int min = arr [0];

Int I;

For (I = 0; I <arr. length; I ++ ){

Sum + = arr [I];

If (ARR [I]> MAX ){

Max = arr [I];

}

If (ARR [I] <min ){

Min = arr [I];

}

}

Ave = sum/ARR. length;

System. Out. println ("the average value is" + Ave + ", the maximum value is" + MAX + ", and the minimum value is" + min );

}

 

 

3

Public static int [] getrandomizenumbers (INT begin, int end, int count ){

Int [] rs = new int [count];

Int I;

For (I = 0; I <count; I ++ ){

RS [I] = (INT) (math. Random () * (INT) math. Abs (end-begin) +

 

(End> begin )? Begin: End );

}

Return Rs;

}

 

4

Public class Complex {

Int real;

Int virtual;

Public complex (int I, Int J ){

Real = I;

Virtual = J;

}

Public String tostring (){

Return real + "+" + virtual + "I ";

}

}

 

5-depth copy

 

6

Public class invalidscoreexception {

 

Public void handle (string input ){

Int temp = 0;

Try {

Temp = integer. parseint (input );

} Catch (numberformatexception NFE ){

System. Out. println ("incorrect input format ");

Return;

}

 

If (temp <0 | temp> 100 ){

System. Out. println ("the number entered is not in the required range ");

}

 

}

}

 

7. I guess I mainly consider enumeration types (which can be replaced by static variables) and concurrency.

Public class simpleobject {

Private Static string state; // object state

Private Static string createstate = "created ";

Private Static string openstate = "opened ";

Private Static string closestate = "closed ";

Public simpleobject (){

//

State = createstate;

}

Public void opened (){

Synchronized (state ){

State = openstate;

}

}

Public void closed (){

Synchronized (state ){

State = closestate;

}

}

Public String getstate (){

Synchronized (state ){

Return state;

}

}

}

 

8

Book. Java

 

Public class book {

Private string author;

Private Boolean isborrowed;

Private string name;

Private double price;

Public book (string name, string author, double price ){

This. Name = Name;

This. Author = author;

This. Price = price;

}

Public String getauthor (){

Return author;

}

Public void setauthor (string author ){

This. Author = author;

}

Public Boolean isborrowed (){

Return isborrowed;

}

Public void setborrowed (Boolean isborrowed ){

This. isborrowed = isborrowed;

}

Public String getname (){

Return name;

}

Public void setname (string name ){

This. Name = Name;

}

Public double getprice (){

Return price;

}

Public void setprice (double price ){

This. Price = price;

}

Public String tostring (){

String state = NULL;

If (this. isborrowed ){

State = "borrow ";

}

Else {

State = "";

}

Return this. getname () + "," + this. getauthor () + "," +

 

This. getprice () + "," + state;

}

 

}

 

Library. Java

Import java. util. arraylist;

 

 

Public class library {

Private Static arraylist <book> books = new arraylist <book> ();

Public void add (Book ){

Books. Add (book );

}

Public book [] findbook (string bookname ){

Arraylist <book> temp = new arraylist <book> ();

Int I;

For (I = 0; I <books. Size (); I ++ ){

If (books. Get (I). getname (). Equals (bookname )){

Temp. Add (books. Get (I ));

}

}

Book [] books = new book [temp. Size ()];

For (I = 0; I <books. length; I ++ ){

Books [I] = temp. Get (I );

}

Return books;

}

Public book [] getallbooks (){

Book [] books = new book [books. Size ()];

Int I;

For (I = 0; I <books. length; I ++ ){

Books [I] = books. Get (I );

}

Return books;

}

Public static synchronized void Update (book, Boolean isborrow ){

Int I;

For (I = 0; I <books. Size (); I ++ ){

If (books. Get (I). Equals (book )){

Books. Remove (I );

Book. setborrowed (isborrow );

Books. Add (book );

}

}

}

 

}

 

Reader. Java

 

Public class reader {

Private string name;

Public reader (string name ){

Name = Name;

}

 

Public String getname (){

Return name;

}

 

Public void setname (string name ){

Name = Name;

}

Public void borrowbook (Book ){

Library. Update (book, true); // true indicates borrowing

}

Public void returnbook (Book ){

Library. Update (book, false); // false indicates Returning books

}

}

 

Test. Java

 

Public class test {

Public static void main (string [] ARGs ){

Library Lib = new library ();

Book book1 = New Book ("Java programming", "Zhang San", 45 );

Book book2 = New Book ("Java core technology", "Li Si", 50 );

Book book3 = New Book ("Java programming", "Wang Wu", 38 );

Lib. Add (book1 );

Lib. Add (book2 );

Lib. Add (book3 );

Book [] allbooks = Lib. getallbooks ();

Int I;

For (I = 0; I <allbooks. length; I ++ ){

System. Out. println (allbooks [I]);

}

System. Out. println ("___________________________________________");

Reader girlreader = new reader ("onebeautifulgirl ");

Girlreader. borrowbook (book1 );

Book [] newbooks = Lib. getallbooks ();

For (I = 0; I <newbooks. length; I ++ ){

System. Out. println (newbooks [I]);

}

System. Out. println ("___________________________________________");

Girlreader. returnbook (book1 );

Book [] nowbooks = Lib. getallbooks ();

For (I = 0; I <nowbooks. length; I ++ ){

System. Out. println (nowbooks [I]);

}

 

}

 

}

 

 

 

 

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.