Chapter 3 Exercises
3.2
3. Convert the following floating-point numbers to exponential notation: a. 23.5=2.35E1 b. 4.6E-2
4. Convert the following exponential notation to floating-point notation: a. 32.21E4=322,100 b. 0.0556
6. Why is a variable called a variable? Because its value can varry.
7. Return to the programs in Lesson 2 and find an example of each of the different types of variables. "i" is an integer variable and "celsius" is a double. Which of the types listed in this subsection are not included? String ands constants.
8. Declare a floating-point variable called payRate and simultaneously initialize it to $35.67. double payRate=35.67;
9. Declare 3 integer variables (a,b,c) in a single declaratoin and simultaneously initialize b to 4. int b=4, a, c;
10. Give two examples of data that cannot be stored in a variable of type int. 2.03 "hi"
11. There are approximately 2.2 pounds in a kilogram. Name and declare a constant to represent"
13. Find the syntax erros in the folliwng expressions:
a. a-*b+c --- two operators adjacent
b. -(a+b)*c) --- parenthesis don't match
c. () --- there is no operation
14. Assume that x is 4.5 and y is 2. Write the values of the following expressions:
a. x/y = 2.25
b. y/x = .4444444444
c. x%y = .5
15. Assuem taht x and y are 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 --- valid
b. x=y*z --- valid
c. z=x+y --- error
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. Wriet 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 = 0.5);
18. Assume that x refers to the srting "Wizard" and y refers to the string "Java". Write the values of the following expressions:
a. y + x --- JavaWizard
b. y + y.length() + x --- Java4Wizard
c. y + "/n" + x + "/n"
Java
Wizard
(blank line)
19. Declare a variable of type String called myInfo and initialize it to your name, address, and telephone number. Each item of informatin in this string should be followed by a newline character.
String myInfo = "Kevin McCormack/n" + "Address/n" + 098 + "-" + 765 + "-" + 4321
20. What is the difference between a message and a method?
A message is a request sent to an object. A method is what is used to respond to the corresponding message.
3.3
1. Write code segments that perform the followingn tasks:
a. Prompt the user for an hourly wage and read the wage into a double variable wage.
KeyboardReader reader = new KeyboardReader();
double wage;
System.out.println("Enter an hourly wage: ");
wage = reader.readDouble();
b. Propt the user for a Social Security number and read this value into the String variable ssn.
KeyboardReader reader = new KeyboardReader();
String ssn;
System.out.println("Social Security number: ");
ssn = reader.readLine();
2. What is the purpose of the method pause()?
To pause the program until the user hits enter to prevent the terminal window from closing before the output can be read.
True / False
1. The first generation of programming language is assembly language. ~ F
2. Java is an example of a high-level language. ~T
3. Mistakes found early in the coding process are much more expensive to fix than mistakes found later in the process. ~F
4. Byte code is a program that behaves like a computer. ~F
5. An arithmetic expression consists of operands and binary operators combined as in algebra. ~T
6. Programs manipulate objects by sending them methods. ~F
7. An integer is a positive or negative whole number. ~T
8. Strings are objects, not primitive data types. ~T
9. A relational operator is used to compare data items. ~T
10. Most, but not all, information in a computer is represented in binary form. ~F
Fill In The Blank
1. OOP stands for objects oriented programming.
2. The software responsible for translating a program in a high-level language to machine code is called a compiler.
3. JVM stands for Java Virtual Machine.
4. When an object receives a message, the object responds by running a block of code called a method.
5. Numbers with a fractional part are called floating point numbers.
6. When evaluating an expression, Java performs operations of higher precidence first unless overridden by parenthesis.
7. Use the concatination operator to create a new string out of existing strings.
8. The while statements implements a conditional.
9. An off by one error occurs when a loop goes around one too many or one too few times.
10. An infinite loop error occurs when a loop never stops.
Home