Exercise 3: (1) Create a class with a default constructor (that is, no constructor). Prints a message in the constructor. Create an object for this class. P116
public class Test{public Test () {System.out.println ("Hello Word");} public static void Main (string[] args) {new Test ();} /** * Execution Result Hello Word */}
Exercise 4: (1) Add an overloaded constructor to the class in the previous exercise so that it accepts a character parameter. and print out your own information along with the accepted parameters in the constructor.
public class Test{public Test (String msg) {System.out.println ("Hello" +msg);} public static void Main (string[] args) {new Test (' China ');} /** * Execution Result Hello China * *}
Exercise 5: (2) Create a class named Dog that has an overloaded bark () method. This method should be overloaded according to the different base data types. And based on the version number being called. Print out different types of barking (barking), growl (howling), and write main () to invoke all the different version numbers.
public class dog{public void bark () {System.out.println ("barking");} public void Bark (String name) {System.out.println (name + "howing");} public static void Main (string[] args) {dog dog=new dog ();d Og.bark ();d Og.bark ("Lily"); /** * Execution Result barkinglily howing */}
Exercise 6: (1) Change the previous practice to allow two overloaded methods to each accept a different number of parameters of two types, but in the opposite order. Verify that it is working.
public class dog{public void bark (int i) {System.out.println (i+ "barking");} public void Bark (String name) {System.out.println (name + "howing");} public static void Main (string[] args) {dog dog=new dog ();d Og.bark ("Lily");d Og.bark (88);} /** * Execution Results Lily howing88barking */}
Exercise 7: (1) Create a class that does not have a constructor. and create its object in main () to verify that the compiler is actually actively adding the default constructor itself.
public class dog{public void bark (int i) {System.out.println (i+ "barking");} public void Bark (String name) {System.out.println (name + "howing");} public static void Main (string[] args) {dog dog=new dog ();d Og.bark ("Lily");d Og.bark (88);} /** * Execution Results Lily howing88barking */}
Exercise 8: (1) Write a class with two methods, call the second method two times in the first method: the first call does not use Thiskeyword. Second call using thiskeyword--* Here is just to verify that it is working, and you should not use such a method in practice.
public class Test{public static void Main (string[] args) {Apple apple=new apple (); Apple.getapple ();} /** * Execution result not using this call: Created well ... Use this call: Created well ... */}class apple{public void Getapple () {System.out.print ("This call not used:"); Createapple (); System.out.print ("Call with this:"); This.createapple ();} public void Createapple () {System.out.println ("created well ...");}}
Exercise 9: (1) Write a class of two (overloaded) constructors, and in the first constructor, the second constructor is called through this.
Package Mil.oms.main.test;public class Test{public static void Main (string[] args) {new Apple ();} /** * Execution result color: Red */}class apple{public Apple () {This ("red");} Public Apple (String color) {System.out.println ("color:" +color);}}
Exercise 10: (2) Write a class with the Finalize () method and print the message in the method. In main (), create an object for the class and try to explain the behavior of the program.
public class Test{public static void Main (string[] args) {Apple apple=new apple (); apple=null; System.GC ();} /** * Implementation Results <span style= "font-family:arial, Helvetica, Sans-serif;" >----Finalize---------</span> */}class apple{protected Void Finalize () {System.out.println ("---- Finalize---------");}}
Exercise 11: (4) Change the previous exercise program so that your finalize () will always be called.
public class Test{public static void Main (string[] args) {new Apple (); System.GC (); System.runfinalization ();} /** * Execution result color: Red */}class apple{protected void Finalize () {System.out.println ("----Finalize---------");}}
Exercise 12: (4) write a class named tank. The status of this class can be "full" or "empty".
The end condition is that the object must be empty when it is cleaned. * Please write Finalize () to verify that the finalization condition is true. In main () test tank may occur several ways of use
public class Tank{public Boolean state=false;public void Changestate (Boolean state) {this.state=state;} protected void Finalize () throws Throwable{if (state) {System.out.println ("----full, problematic---------"), throw new Throwable (" The state is not empty! ");} ELSE{SYSTEM.OUT.PRINTLN ("----empty, no problem---------"); Super.finalize ();}} public static void Main (string[] args) {new Tank (); System.GC (); System.runfinalization (); new Tank (). Changestate (True); System.GC (); System.runfinalization ();} /** * Execution Results----empty, no problem-------------full, there is a problem---------*/}
Exercise 13: (1) Verifying the statements in the preceding paragraph
public class Explicistatic{public static void Main (string[] args) {System.out.println ("Inside Main ()"); CUPS.CUP1.F1 (1);} Static cups cups1=new cups (); static cups cups2=new cups ();/** * Execution Results Bowl (1) Bowl (2) cups () cups () Inside main () F1 (1) */} Class Cup{cup (int mark) {System.out.println ("Bowl (" +mark+ ")");} void F1 (int mark) {System.out.println ("F1 (" +mark+ ")");}} Class Cups{static Cup Cup1;static Cup Cup2;static{cup1=new Cup (1) Cup2=new Cup (2);} Cups () {System.out.println ("cups ()");}}
Exercise 14: (1) Write a class. has two static string fields. One is initialized at the definition, and one is initialized in a static block. Now, add a static method to print out two field values.
Please prove that they are all initialized before they are used.
public class Explicistatic{public static void Main (string[] args) {cup.print ();} /** * Execution result str1:str1str2:str2 */}class cup{static String str1= "str1"; static string str2;static{str2= "str2";} static void print () {System.out.println ("str1:" +STR1); System.out.println ("str2:" +STR2);}}
Exercise 15: (1) Write a class that contains a string field and initialize it with an instance initialization method.
public class Explicistatic{public static void Main (string[] args) {System.out.println ("Start ()"); new Cup (); System.out.println ("End ()");} /** * Execution result start () field initialization complete Cup () end () */}class cup{string str;{ Str= "Hello"; System.out.println ("Field string field initialization Complete");} Cup () {System.out.println ("Cup ()");}}
Exercise 16 Creates a string object data and assigns a string to each element. Use the For loop to print the array.
public class Explicistatic{public static void Main (string[] args) {string[] strs={"H", "E", "L", "L", "O"};for (String str: STRs) {System.out.print (str);}} /** * Execution Result hello */}
Exercise 17: Create a class that has a constructor that accepts a string parameter.
During the construction phase, the number of the parameters is printed. Creates an array of object references for the class. But not actually to create an object assignment to that array.
When the program is executed. Note whether the initialization message from the call to the constructor prints out
public class Explicistatic{public static void Main (string[] args) {random random=new random (47); Cup[] cups=new Cup[random.nextint (10)];} /** * Execution Result */}class Cup{cup (String str) {System.out.println ("Cup (" +str+ ")");}}
Exercise 18: Assign a value to the reference array by creating an object, thus completing the previous exercise.
public class Explicistatic{public static void Main (string[] args) {random random=new random (47); Cup[] cups=new cup[random.nextint (+)];for (int i=0,j=cups.length;i<j;i++) {cups[i]=new cup ("Cup" +i);}} /** * Execution Result Cup (CUP0) Cup (CUP1) Cup (CUP2) Cup (CUP3) Cup (CUP4) Cup (CUP5) Cup (CUP6) Cup (CUP7) */}class Cup{cup (String str) { System.out.println ("Cup (" +str+ ")");}}
Exercise 19: (1) Write a class that accepts a variable parameter string array to verify that you can pass a comma-delimited list of strings to the method. or a string[].
<pre name= "code" class= "HTML" >public class Explicistatic{public static void Main (string[] args) {String str= "Hello ", strs[]={str}; System.out.print ("Incoming string:"); new Cup (str); System.out.print ("Incoming string array:"); new Cup (STRs); System.out.print ("Incoming Two strings:"); new Cup (STR,STR);} /** * Execution Result: incoming string: Variable ... Parameters passed in an array of strings: variable ...Number of parameters passed in two strings: variable.
。 Parameters */}class Cup{cup (String ... str) {System.out.println ("variable ... ");}}
Exercise 20: (1) Create a main () that uses the variable list instead of the normal main () syntax.
Prints all the elements of the resulting args array and tests it with a number of different command-line parameters.
public class Explicistatic{public static void Main (String ... args) { printstr ("H", "E", "L", "L", "O"); Printstr ( new string[] {"H", "E", "L", "L", "O",}/** the last comma is optional **/ ); } /** * Execution Result: Hellohello */public static void Printstr (String ... strs) {for (string str:strs) {System.out.print (str);} System.out.println ();}}
Exercise 21: Create an enum that includes 6 types of minimum denominations in banknotes. Loop through values () and print each value and its ordinal ()
public class Explicistatic{public static void Main (String args[]) {for (Money M:money.values ()) {System.out.println (m+ "\ T" +m.ordinal ());} } /** * Execution Result: one 0two 1five 2ten 3hun 4tho 5 */}enum Money{one,two,five,ten,hun,tho}
Exercise 22: (2) In the previous example, write a switch statement for the enum, and for each case, output a descriptive narrative of that particular currency.
public class Explicistatic{public static void Main (String args[]) {for (Money M:money.values ()) {System.out.println (m+ "\ T" +m.ordinal ()); Money.describe (m);} } /** * Execution RESULTS: one 01 cents two 12 cents five 25 cents ten 310 bucks Hun 420 yuan tho 550 bucks */}enum money{one,two,five,ten,hun,tho;static void Describ E (money m) {switch (m) {case ONE:SYSTEM.OUT.PRINTLN ("1 cents"); Break;case TWO:SYSTEM.OUT.PRINTLN ("2 cents"); Break;case five: SYSTEM.OUT.PRINTLN ("5 cents"); Break;case ten:System.out.println ("10 dollars"); Break;case hun:System.out.println ("20 dollars"); Break;case tho:System.out.println ("50 dollars"); break;default:break;}}
Java Programming Ideas Fourth Edition * Fifth chapter personal Practice