Archive for September, 2007

Chapter 4, 4.2

1. Assume that x has the value 3. and y has the value 4. State the value of the variable z after the followingg statements:
a. z = Math.sqrt (y): z=2
b. z = Math.round (x): x=3
c. z = Math.pow: z=64
d. z = Math. round (Math.sqrt (x)): z=2

2. Write code segments to print the following values in a erminal vindow:
a. A random integer between 1 and 20.
System.out.print (generator.nextInt(20)+1)
b. A random double between 1 and 10.
System.out.print (generator.nextDoulbe(10.0) + 1.0)

Chapter 4, 4.1

1. Thanslate the following statements to equivalent statements that use extended assignment operators:
a. x = x * 2 : x*=2
b. y = y % 2 : y%=2

2. Translate the following statements to equivalent statements that do not use the extended assignment operators:
a. x +=5 : x= x+5
b. x *= x : x= x+x

Chapter 3, 3.2

1. What is the difference between double and int data types?
-Int represents interger, and it requires 4 bytes for storage; Double represents floating pint numbers-numbers with decimals, and it requires 8 bytes for storages.

2. how does he syntax for manipulationg numeric data types and objects differ?
-Numeric data type is a primitive data type and it is combined in expressions involving operators, it doesn’t need to be instantiaed. Objects data type is different, it needs to be instantiated before use.

3. Convert the following floating-point numbers to exponential notations:
23.5=2.35E1
0.046=4.6E-2

4. Convert the following numbers from exponential notation to floating-point natation:
32.21E4=322100
55.6E-3=0.00556

5. Give two examples of string literals:
string a=mile
string b=kilometer

6. Why is a variable called a variable?
-Varialbe is an item whose value can change during the execution of a program.

7. Return to the programs in chapter 2 and find an example of each of the different types of variables. Which of the types listed in this subsection are not included?
-bool a=false
-double q=1.234

8. Declare a floating-point variable called payRate and simultaneously initialize it to $35.67.
-double payRate=35.67

9. Declare three integer variables (a,b,c) in a single declaration and simultaneously initialize b to 4.
– int a, c, b=4

10.Give two examples of data that cannot be stored in a variable of type int.
-int q=1.234, or int w=hello

11.There are approximately 2.2 pounds in a kilogram. Name and declare a constant to represent this value.
– final double KILOGRAM=2.2

12. Assume that the integer variable x is 5 and the integer variable y is 10. Give the values of the following expression:
a. x+y*2 == 25
b. x-y+2 ==-3
c. (x+y)*2 ==30
d. y%x == 0

13. Find the syntax errors in the following expressions:
a. a-*b+c : There are more than one arithmetic operators, in this case the subtraction and multiplication, appear together without a number in between.
b. – (a+b)*c): This equation lack of one left parenthesis.
c. (): There is no actual commend but just a pair of parenthesis in the sentence.

14. Assume that x is 4.5 and y is 2. Write the values of the following expression:
a. x/y == 2.25
b. y/x == 0.444
c. x%y == 0

15. Assume that x and y aree of type double and z is of type int. For each of the following assignment statements, state which are valid and which produce syntax errors :
a. x=z: This is valid because z is a less inclusive data type than x is.
b. x=y*z: This is valid because z’s int data type will be autimatically converted to the more inclusive double data type. Since x is in the double data type, it will be valid for it to hold the result.
c. z=x+y: This is not valid since z is assigned to be in int data type, a less inclusive data type than double data type, so z can’t hole the result of x+y, a double data type.

16. Assume that x is of type double and y is of type int. Also assume that x is 4.5 and y is 2. Write the values of the following expressions:
a. (int) x*y == 8
b. (int) (x*y) == 9

17. Assume that x is of type double and y is of type int. Write a statement that assigns the value contained in x to y after rounding this value to the nearest whole number.
y=(int) x

18. Assume that x refers to the string “Wizard” and y refers to the string “Java”. Write the values of the following expressions:
a. y+x = WizardJava
b. y+y.length () + x = Java4Wizard
c. y + “\n” +x + “\n”= Java
Wizard

19. Declare a variable of type String called myInfo and initialize it to your name, address, and telephone number. Each item of information in this string should be followed by a newline character.
– String myInfo= “David \n” +
“\tworcester \n” +
“\t1234567 \n”

20. What is the difference between a message and a method?
– A message is the order sent to the program. A method is the manipulation done by the computer to the message.

21. Describe the purpose of each item of information that appears in a method’s signiture.
– The name of method tells what the method does; the types tells what kinds of data it deals with; the number of its parameters tells how many data the method can work on at a time.

22. State whether each of the following are valid or invalid user-defined symbols in Java:
a. pricePerSquareInch- valid
b. student2- valid
c. 2GuysFromLexington- invalid because starts with a number
d. PI- valid
e. allDone?- invalid because of the question ma rk.

23. Write names for the following items that follow good programming practice:
a. A variable that represents the diameter of a circle– diameterOfCircle
b. A constant that represent the standard deduction for an income tax return– STANDARD_DEDUCTION_FOR_INCOME_TAX
c. A method that draws a rectangle: drawTrangle

24. Describe the role of the items x, y, and z in the satement import x.y.z;.
– x means the overall name of the package; y means the subsection within the package; and z is the name of a particular class in the subsection to be imported.

25. What happens when the computer executes the statement import x.y.*;?
- It imports all the classes within y subsection of x package.

Chapter 3, 3.1

1.What is the vocabulary of a language? Give an example of an item in the vocabulary of Java.
-Vocabulary is the set of all of the words and symbols in the language. For examples:
arithmetic operators: + – * /
assignment operator: =
numeric liters: 5.73 9
programmer defined variable names: fahrenheit celsius

2. Give an example of a syntax rule in Java.
-The left and right parentheses must occur in matching pairs.
For example: a-b)* c
It’s a invalid statement, since there is only right parenthese.

3. What does the expression (x+y)*z mean?
-It means the sum of (x+y) times z. So to do x+y first, then z times the whole thing, the sum.

4. Describe two differences between progrmming language and natural languages.
-Size: Natural languages have more complex syntax and semantics compared to Programming language.
-Rigidity: Programming Language is more strict on correctness. It requires absolute correct of its syntax, whereas a grammically incorrect English sentence is usually comrehensible.

Chapter 2, 2.4

1. Give a short definition of “program.”
-A program is a sequence of instructions for a computer, it means to be executed by a computer.

2.What is the effect of the message println?
-It send a print message to System.out so to print characters or strings in a terminal window.

3.Describe how to use the System.out object.
-In this program, System.out is the object that execute whatever messages or methods sent to them.

Chapter 2, 2.2

1. What does JVM stand for?
-JVM stands for Java virtual machine.

2. What is byte code? Describe how the JVM uses byte code?
-It’s a pseudomachine language, instead of the classical machine language, into which Java compiler ranslates the higher-level language. A JVM runs the Java byte code on the computer. The JVM behaves like a computer. Such is called an interpreter.

3.What is an applet? Describe how applets are used.
-These applets are small Java programs on Web that already translated into byte code. They can be run in a JVM that is incorprated into the Web Browser.

Chapter 2, 2.1

1. What is a portable program?
-It is a type of yields programs that can be run on different types of compters without change.

2. Describe two features of java that make it a better language than C++?
-Java is easier to use and learn and it has less error prone than C++

3. What is a thread? Describe how threads might be used in a Program.
A thread is a process that can run concurrently with other processes. A program can use thread to run multiple processes at a time, such as Java can run up to 2 processes simultaneously.

What parameters does the main method define?

The main method accepts a single argument: an array of elements of type String. The String is usually named args.

When declaring the main method, which modifier must come first, public or static?

The modifiers public and static can be written in eiher order, but the convention is to use public static.

What is the correct signature of the main method?

The correct signature is “public static void main(String[] args)”.

« Previous entries