Discovering Classes and Objects

Một phần của tài liệu Apress learn java for android development 3rd (Trang 1025 - 1031)

1. A class is a container for housing an application and is also a template for manufacturing objects.

2. You declare a class by minimally specifying reserved word class followed by a name that identifies the class (so that it can be referred to from elsewhere in the source code), followed by a body. The body starts with an open brace character ({) and ends with a close brace (}). Sandwiched between these delimiters are various kinds of member declarations.

3. The answer is false: you can declare only one public class in a source file.

4. An object is an instance of a class.

5. You obtain an object by using the new operator to allocate memory to store a class instance and a constructor to initialize this instance.

6. A constructor is a block of code for constructing an object by initializing it in some manner.

7. The answer is true: Java creates a default noargument constructor when a class declares no constructors.

8. A parameter list is a round bracket-delimited and comma-separated list of zero or more parameter declarations. A parameter is a constructor or method variable that receives an expression value passed to the constructor or method when it’s called.

9. An argument list is a round bracket-delimited and comma-separated list of zero or more expressions. An argument is one of these expressions whose value is passed to the corresponding parameter when a constructor or method is called.

10. The answer is false: you invoke another constructor by specifying this followed by an argument list.

11. Arity is the number of arguments passed to a constructor or method or the number of operator operands.

12. A local variable is a variable that’s declared in a constructor or method and isn’t a member of the constructor or method parameter list.

13. Lifetime is a property of a variable that determines how long the variable exists. For example, parameters come into existence when a constructor or method is called and are destroyed when the constructor or method finishes.

Similarly, an instance field comes into existence when an object is created and is destroyed when the object is garbage collected.

14. Scope is a property of a variable that determines how accessible the variable is to code. For example, a parameter can be accessed only by the code within the constructor or method in which the parameter is declared.

15. Encapsulation refers to the merging of state and behaviors into a single source code entity. Instead of separating state and behaviors, which is done in structured programs, state and behaviors are combined into classes and objects, which are the focus of object-based programs. For example, where a structured program makes you think in terms of separate balance state and deposit/withdraw behaviors, an object-based program makes you think in terms of bank accounts, which unite balance state with deposit/withdraw behaviors through encapsulation.

16. A field is a variable declared within a class body.

17. The difference between an instance field and a class field is that an instance field describes some attribute of the real-world entity that an object is modeling and is unique to each object, and a class field identifies some data item that’s shared by all objects.

18. A blank final is a read-only instance field. It differs from a true constant in that there are multiple copies of blank finals (one per object) and only one true constant (one per class).

19. You prevent a field from being shadowed by changing the name of a same- named local variable or parameter, or by qualifying the local variable’s name or parameter’s name with this or the class name followed by the member access operator.

20. A method is a named block of code declared within a class body.

21. The difference between an instance method and a class method is that an instance method describes some behavior of the real-world entity that an object is modeling and can access a specific object’s state, whereas a class method identifies some behavior that’s common to all objects and cannot access a specific object’s state.

22. Recursion is the act of a method invoking itself.

23. You overload a method by introducing a method with the same name as an existing method but with a different parameter list into the same class.

24. A class initializer is a static-prefixed block that’s introduced into a class body. An instance initializer is a block that’s introduced into a class body as opposed to being introduced as the body of a method or a constructor.

25. A garbage collector is code that runs in the background and occasionally checks for unreferenced objects.

26. An object graph is a hierarchy of all of the objects currently stored in the heap.

27. The answer is false: String[] letters = new String[2] { "A", "B" }; is incorrect syntax. Remove the 2 from between the square brackets to make it correct.

28. A ragged array is a two-dimensional array in which each row can have a different number of columns.

29. Listing A-5 presents the Image application that was called for in Chapter 3.

Listing A-5. Testing the Image Class public class Image

{

Image() {

System.out.println("Image() called");

}

Image(String filename) {

this(filename, null);

System.out.println("Image(String filename) called");

}

Image(String filename, String imageType) {

System.out.println("Image(String filename, String imageType) called");

if (filename != null) {

System.out.println("reading " + filename);

if (imageType != null)

System.out.println("interpreting " + filename + " as storing a " + imageType + " image");

}

// Perform other initialization here.

}

public static void main(String[] args) {

Image image = new Image();

System.out.println();

image = new Image("image.png");

System.out.println();

image = new Image("image.png", "PNG");

} }

30. Listing A-6 presents the Conversions application that was called for in Chapter 3.

Listing A-6. Converting Between Degrees Fahrenheit and Degrees Celsius public class Conversions

{

static double c2f(double degrees) {

return degrees * 9.0 / 5.0 + 32;

}

static double f2c(double degrees) {

return (degrees - 32) * 5.0 / 9.0;

}

public static void main(String[] args) {

System.out.println("Fahrenheit equivalent of 100 degrees Celsius is " + Conversions.c2f(100));

System.out.println("Celsius equivalent of 98.6 degrees Fahrenheit is " + Conversions.f2c(98.6));

System.out.println("Celsius equivalent of 32 degrees Fahrenheit is " + f2c(32));

} }

31. Listing A-7 presents the Utilities application that was called for in Chapter 3.

Listing A-7. Calculating Factorials and Summing a Variable Number of Double Precision Floating-Point Values public class Utilities

{

static int factorial1(int n) {

int product = 1;

for (int i = 2; i <= n; i++) product *= i;

return product;

}

static int factorial2(int n) {

if (n == 0 || n == 1) return 1; // base problem

else

return n * factorial2(n - 1);

}

static double sum(double... values) {

int total = 0;

for (int i = 0; i < values.length; i++) total += values[i];

return total;

}

public static void main(String[] args) {

System.out.println(factorial1(4));

System.out.println(factorial2(4));

System.out.println(factorial2(0));

System.out.println(factorial2(1));

System.out.println(sum(10.0, 20.0));

System.out.println(sum(30.0, 40.0, 50.0));

} }

32. Listing A-8 presents the GCD application that was called for in Chapter 3.

Listing A-8. Recursively Calculating the Greatest Common Divisor public class GCD

{

public static int gcd(int a, int b) {

// The greatest common divisor is the largest positive integer that // divides evenly into two positive integers a and b. For example, // GCD(12, 18) is 6.

if (b == 0) // Base problem return a;

else

return gcd(b, a % b);

}

public static void main(String[] args) {

System.out.println(gcd(12, 18));

} }

33. Listing A-9 presents the Book application that was called for in Chapter 3.

Listing A-9. Building a Library of Books public class Book

{

private String name;

private String author;

private String isbn;

public Book(String name, String author, String isbn) {

this.name = name;

this.author = author;

this.isbn = isbn;

}

public String getName() {

return name;

}

public String getAuthor() {

return author;

}

public String getISBN() {

return isbn;

}

public static void main(String[] args) {

Book[] books = new Book[]

{

new Book("Jane Eyre", "Charlotte Brontở", "0895772000"),

new Book("A Kick in the Seat of the Pants", "Roger von Oech",

"0060155280"),

new Book("The Prince and the Pilgrim", "Mary Stewart",

"0340649925") };

for (int i = 0; i < books.length; i++)

System.out.println(books[i].getName() + " - " + books[i].getAuthor() + " - " + books[i].getISBN());

} }

Một phần của tài liệu Apress learn java for android development 3rd (Trang 1025 - 1031)

Tải bản đầy đủ (PDF)

(1.190 trang)