Archive for November, 2007

Chapter 6, 6.2

1. Describe appropriate test data for the following code segments:
-a. So we need to test the number smaller than zero, equals to zero, or greater than zero.
-b. We need to test the number smaller than zero, equals to zero, between zero and 100, equals to 100, and greater than 100.

2. What hapens when we provide complete code coverage of a program?
-In this condition, we execute every lines in the program at least once.

3. What is an equivalence class? Give an example.
-A equivalence class means the data are equivalent from the perspective of testing the same paths through the program. For example, if one action applys when number> 100, then, 100, 101 and 102 are equivalence class.

4. What are boundary conditions? Give an example.
-The boundary condition s are equivalence classes on or near the boundaries. For instance, if one action applys when number> 100 && number=0 && number<= 200, then 0 and 200 should be tested.

6.Suppose a teacher uses grades from 0 to 100 and wants to discount all grades below 60 in her record. Discuss the equivalence classes, boundary conditions, and extreme conditions used to test a program that processes this information.
-The equivalence classes are 0 to 60 but exclusive 60, and 60 to 100 inclusive. The boundary conditions are around 0, 60, and 100. The extreme conditions are 0, 59, 60, and 100.

Chapter 6, 6.1

1.P Q !((P||Q)&&(P&&Q))
true true False
true false True
false true True
false false False
2. Assume that A is true and B is false, Write the values of thefollowing expression:
- a. true, b. false, c. true, d. false

4. List the logical operators in the order in which each one would be evaluated at run time.
- NOT (!), AND(&&), OR(||)

5. Construct a Boolean expression that tets whether the value of variable x is within the range specified by the variables min (the smallest) and max (the largest).
- if (min<= x && x<=max)
System.out.println (“x is within the min and the max. “)

Chapter 5, 5.5

1. What are the lifetime of an instance variable, a local variable, and a parameter?
–The lifetime of an instance variable is the period that the object runs, so equals to the lifetime of the object. The lifetime of a local variable anc a parameter are the same as the life time of the method, they can only be access while the method is run.

2. What is shadowing? Give an example and describe the bad things that shadowing might cause in a program.
–Shadowing is the use of same name in the local variable and the globle variable. For example:
public int x
public void someMethod (int x){
}
— It will be dangerous because the same name may cause a coding error in the program, since the variables sometimes refer to globle and other time to local.

3. Consider the code segment.
a. list the instance variables, parameters, and local variables in this code.
instance: a, b.
local: c, d.
parameter: x, y
b. describe the scope of each variable or parameter:
The scope of a and b are throughout the object, the class SomeClass.
The scope of x, y, c, d are within the method a Mutator.
c. Describe the lifetime of each variable or parameter.
The lifetime of a, b equals to the lifetime of the object, class SomeClass.
The lifetime of x, y, c, d equal to the lifetime of the method Mutator.

Chapter 5, 5.4

1. Explain the difference between formal parameters and actual parameter.
–The formal parameter is the variables decleared in the server code, listed in the method definition. And the actual parameter is the values in th client code assigned to the formal parameter which are eventually shown.

2. How does Java transmit data by means of parameters?
–Java transmit data by assigning the actual parameters to the formal parameters before running all the other methods under that method.

3. Define a method sum. This method expects two integers as parameters and returns the sum of the numbers ranging from the first integer to the second one.
–public int getSum (int x, int y){
while (sum< y) {
sum = x +1
x++
}

4. What is the purpose of local variables?
–It’s a method that can only be used inside the method.

Chapter 5, 5.2

1. What are mutators and accessors? Give examples.
-Mutators are method that changes an object’s state. One example is the setName method. Accessors are method to access the object’s state to check if the mutator has changed the states of object. One example is the getName method.

2. List two visibility modifiers and describe when they are used.
-(1)private: specify that data or method to only be used in certain cirtaintance, within {}.
-(2)public: the data or imformation after such a modifier can be seen or used by anybody and anywhere.

3.What is a constructor?
– It’s a method that used to initialize the values for the object which with the same name as the class it falls in.

4.Why do we include a toString method with a new user-defined class?
-By using a toString method with a new user-defined class, we converted all the values of the class into strings, therefore, these informations can be easily viewed.

5. How can two variables refer to the same object? Give an example.
-We can refer one to the object , then says the other one equals to the previous one.
– For example: s = new Student ()
s1 = s

6.Explain the difference between a primitive type and a reference type, and give an example of each.
-a primitive type is data type that contains a value, such as int. a reference type contains a pointer to an object, such as class.

7. What is the null value?
-It make the variable no longer refers to anything. It means empty value

8. What is a null pointer exception? Give an example.
-The null pointer exception throws the value that contains a null value, which refers to nothing.
– String krit = null;
System.out.println (krit)

9.How does a default constructor differ from other constructor?
-It’s a primitive constructor java contains behind the scene. It initialize numeric variables to zeros and objects to null. For the other constructor, you can assign all variables values yourself.

10.How does Java handle the initialization of instance variables if no constructors are provided?
-It will give all numeric variables zeros, and all objects null value.

11.What is the purpose of a constructor that expects another object of the same class?
-To be able to change the poperty of one object by using the poperty of another object.