Thing in Java 5th, initialization and cleanup, exercises answer

Source: Internet
Author: User

/**
* Created by Sandy.liu on 2018/7/28.
* Thinking in Java version 4, Chapter 5, Practice 2
* Create A class which includes the values, one is initialized at the point of Defination,
* Another is initialized by constructor. What's the different between the approach.
*/
public class Chap5prac2 {
Chap5prac2 () {
S3 = "Good Bye";
}
String S1;
String s2 = "Hello";
String S3;
public static void Main (string[] args) {
Chap5prac2 t = new Chap5prac2 ();
P.print ("T.S1:" +t.s1);
P.print ("T.S2:" +t.s2);
P.print ("T.S3:" +T.S3);
}

}


/** 
* Created by Sandy.liu to 2018/7/29.
* Thinking in Java version 4, Chapter 5, Practice 3
* Create a C Onstructor which prints message. Create a new object.
*/
public class chap5prac3defaultconstructor{
Chap5prac3defaultconstructor () {
P.print Default constructor ");
}
public static void Main (string[] args) {

Chap5prac3defaultconstructor dc = new Chap5prac3de Faultconstructor ();
}
}

/**
* Created by Sandy.liu on 2018/7/29.
* Thing in Java version 4, Chapter 5, Practice 4
* Add a overload constructor for the class in practice 3 that takes a string argument and prints it
* Along with your message.
*/
public class Chap5prac4 {
CHAP5PRAC4 () {
P.print ("This is a default constructor");
}
CHAP5PRAC4 (String s) {
P.print ("This is a overload constructor with input parameter:" +s);
}
public static void Main (string[] args) {
CHAP5PRAC4 T1 = new Chap5prac4 ();
CHAP5PRAC4 t2 = new Chap5prac4 ("Hello");
}
}
/**
* Created by Sandy.liu on 2018/7/29.
* Thing in Java version 4, Chapter 5, Practice 5
* Create A class named Dog that has overload method bark (). This method is being overloaded by different
* Data type. Print all kind of barking () and howling () message. Write A Main method to use all these
* Overload methods.
*/
public class Chap5prc5dog {
void Bark () {p.print ("No parameter, quiet");}
void Bark (byte b) {p.print ("byte Parameter:miao");}
void bark (short s) {p.print ("short Bark:wang");}
void bark (int i) {p.print ("int Bark:wangwang");}
void Bark (char c) {p.print ("char Bark:chachacha");}
void bark (float f) {p.print ("float Bark:fafafa");}
void bark (long l) {p.print ("long Bark:lalala");}
void Bark (double D) {P.print ("double Bark:doudoudou");}
public static void Main (string[] args) {
byte B = 1;
Short S = 2;
char c = ' C ';
Chap5prc5dog dog = new Chap5prc5dog ();
Dog.bark ();
Dog.bark (b);
Dog.bark (s);
Dog.bark (1);
Dog.bark (c);
Dog.bark (1L);
Dog.bark (1.0f);
Dog.bark (1.0);
}
}

/** 
* Created by Sandy.liu to 2018/7/29.
* Thinking in Java version 4, Chapter 5, Practice 6
* Modify Pre vious exercise, make, overloaded methods taking and parameters with different type but
* reverse order, verify Works correctly.
*/
public class Chap5prac6dog1 {
void bark (int i, String s) {
P.print ("The first dog's name is" + S + ", it is" +i+ "years");
}
Void Bark (String s, int i) {
P.print ("The second dog's name is" + s+ ", it's" +i+ "years old.") );
}
public static void Main (string[] args) {
Chap5prac6dog1 dog = new Chap5prac6dog1 ();
Dog.bark (1, "dog1");
Dog.bark ("Dog2", 2);

}
}

/** 
* Created by Sandy.liu to 2018/7/29.
* Thinking in Java version 4, Chapter 5, Practice 7
* Create a C Lass which hasn ' t constructor, and then create a object of this class in main () to verify
* If the default constructor is added automatically.
*/
public class Chap5prac7dog3 {
void bark () {
P.print ("woof");

public static void Main (string[] args) {
Chap5prac7dog3 dog = new Chap5prac7dog3 ();
Dog.bark ();
}
}

/** 
* Created by Sandy.liu to 2018/7/29.
* Thinking in Java version 4, Chapter 5, Practice 8
* Write a CL The which has a methods. Within the first method, call the second method twice:
* This first time don ' t use this, and the second time to use this.
*/
public class Chap5prac8 {
void method1 () {
method2 ();
THIS.METHOD2 ();
}
void Method2 () {
P.print ("This is Method 2");

public static void Main (string[] args) {
Chap5prac8 test = new Chap5prac8 ();
Test.method1 ();
}
}

/** 
* Created by Sandy.liu to 2018/7/30.
* Thinking in Java version 4, Chapter 5, Practice 9
* Write a CL The overload constructors, and then the second constructor by the
* First constructor.
*/
public class Chap5prac9this {
chap5prac9this (int i) {
P.print ("This is the first constructor, the give N Umber is: "+i);
}
Chap5prac9this (String s) {
This (7);
P.print ("The second constructor, the string is:" +s);
}
public static void Main (string[] args) {
Chap5prac9this ch = new Chap5prac9this ("Sandy");
}
}

/**
* Created by Sandy.liu on 2018/7/30.
* Thinking in Java version 4, Chapter 5, Practice 10
* Write A class with Finalize method which types a message. Create a new object in main{} for this
* class, and explain the behavior of the program.
*/
public class Chap5prac10finalize {
Boolean logout = false;
Chap5prac10finalize (Boolean logout) {
logout = logout;
}
void CheckOut () {
logout = false;
}
protected void Finalize () {
if (logout)
P.print ("error, logout");
}
public static void Main (string[] args) {
Chap5prac10finalize ch = new Chap5prac10finalize (true);
Ch.checkout ();
New Chap5prac10finalize (TRUE);
System.GC ();


}
}






Thing in Java 5th, initialization and cleanup, exercises answer

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.