EXPLICIT DATA–TYPE CONVERSION
Note 2.17 Java statements fall under two categories: declaration statements and executable statements. Declaration statements are employed to declare attributes in
a class and to declare variables within an operation. Executable statements include all assignment statements and output statements.
Apago PDF Enhancer
REVIEW
1. A class is a collection of items with identical behavior.
2. Attributes keep necessary information for the class.
3. A comment is ignored by the compiler.
4. Every Java program is a collection of one or more classes.
5. A Java application program must have a main method.
6. Every character in an identifi er is a letter (A to Z, a to z), or a digit (0 to 9), or the underscore character (_), or the dollar sign ($). Th e fi rst character of an identifi er cannot be a digit.
7. A reserved word cannot be used as an identifi er.
8. Java is a case-sensitive language. Th erefore, identifi ers Cat and cat are not the same.
9. Th e concatenation operator + can also be used to join a String and a numeric value or a character.
10. Every reserved word in Java has lowercase alphabets only.
11. Data type is a set of values along with a set of meaningful basic operations on those data values.
12. Java has eight primitive data types: boolean, byte, char, short, int, long, fl oat, and double.
13. Just as an identifi er or a keyword is treated as one token, Java treats special symbols also as one token.
14. Java uses the Unicode character set containing 65536 characters.
15. Th ere are fi ve operations on numeric data types: addition (symbol used is +), subtraction (symbol used is −), multiplication (symbol used is *), division (symbol used is /), and modulus (symbol used is %).
16. Th e operator precedence rule determines the order in which each of the operators in an expression is being evaluated.
17. Java converts the data type of lesser range to the other data type for the purpose of this expression evaluation. Th at is, data values are converted as follows: byte short int long float double or any combination of these in the same direction.
18. Java allows you to allocate memory using a named constant and store a data value.
Th e value stored remains fi xed during the program execution.
19. A named constant is declared and initialized at the same time.
20. In the case of primitive data types, the value is kept at the variable location and in the case of objects, the reference variable do not keep the object, rather reference variable keeps the reference of the object.
21. In an assignment statement, the expression on the right side is evaluated and then the value computed is stored at the variable on the left side.
Apago PDF Enhancer
22. An assignment statement changes the value of the variable on the left -hand side.
23. Using a variable in an expression does not change its contents.
24. If a program receives some input from a human during its execution, then the pro-gram is said to be executing in interactive mode.
25. Java provides for explicit data–type conversion using a cast operator.
26. Java statements fall under two categories: declaration statements and executable statements.
27. Declaration statements are employed to declare attributes in a class and to declare variables within a method.
28. Executable statements include all assignment statements and output statements.
29. A class has attributes and operations.
30. A method is the implementation of an operation.
31. A Java statement ends with a semicolon.
EXERCISES
1. Mark the following statements as true or false:
a. Dog is a class.
b. My dog Mr. Boomer is an instance of the Dog class.
c. An object is an instance of a class.
d. Two objects of a class have identical behavior.
e. In Java, identifi ers Cat and cat are the same.
f. In Java, a class name must start with an uppercase letter.
g. An identifi er can start with a $ character.
h. In Java, there is no diff erence between literals "a" and 'a'.
i. Th e operands of + operator must be numbers.
j. Th e operands of % can be any two numbers.
k. In Java, char data type variable takes 8 bits of memory.
l. If one of the operands of the modulus operator is a double value, the computa-tion produces a double value.
m. Th e number 75. is a literal of type float.
n. Each primitive data type has its own set of operations.
o. Let first and second be two double variables with values 3.5 and 10.7, respectively. Th en aft er the statement first = second executes, value of sec-ond changes to 3.5.
p. Statement m = k + 1 + j; is equivalent to m = ++k + j;
q. Statement k = k + 1 + j; is equivalent to k += j + 1;
Apago PDF Enhancer
r. An expression is evaluated using one operator at a time.
s. Every Java application program must have exactly one method main.
t. A Java application program can have any number of classes.
u. Let x and y be 3 and 5, respectively. Aft er the statements z = x; x = y;
y = z are executed, value of y is 3.
2. Mark the identifi er as valid or invalid. If invalid, explain why it is invalid.
a. James Bond b. wireLess
c. Why?
d. mail.com e. 10DowningSt f. throw
g. Println h. Tiger'sPaw i. %profit j. Int k. _ output
l. Byte
m. AAA
n. L235 o. -23 p. ON-OFF q. Yes/No r. _ $ _ $ _ $ s. out
t. Public
3. Select the best answer.
a. Th e value of 27/2 is
(i) 13 (ii) 13.5 (iii) 13.4999 (iv) none of these b. Th e value of 27/3 is
(i) 9.0 (ii) 9 (iii) 8.9999 (iv) none of these c. Th e value of 27 % 10 is
(i) 2 (ii) 3 (iii) 7 (iv) none of these d. Th e value of 10 % 27 is
(i) 0 (ii) 10 (iii) 2 (iv) none of these
Apago PDF Enhancer
e. Th e value of 27./2 is
(i) 13 (ii) 13.5 (iii) 13.0 (iv) none of these f. Th e value of 27.0 % 10 is
(i) 2.0 (ii) 3.0 (iii) 7.0 (iv) none of these g. Th e value of 27 - 1/2 is
(i) 27 (ii) 13 (iii) 13.0 (iv) none of these h. Th e value of - 27 * 2 + 4.0 * 8/3 is
(i) –43.3333 (ii) -46.0 (iii) -100.0 (iv) none of these 4. Given
int a, b, c;
double x, y;
Determine the value assigned by each of the following assignment statements. If an assignment statement is not valid, state the reason.
a. b = 5 ; a = 7 ; c = 9 ; b. c = 2 + a = 2 + b = 5 ; c. x = y = 10.7 ;
d. (a + b) = c ;
e. a = 2 ; b = 5 * a ; x = 7 * b ; f. x = y = a = b = c = 7;
g. x = 2.5; b = x ; h. c = 7; x = 5.0 / 7;
i. b = 9; y = b % 7; x = 7 % b;
j. x * y = 5.7;
k. x = 7.0; y = x + 1; x = y + 3;
l. b = 25; c = 35; a = b; b = c; c = a;
m. a = 0; b = 10; c = b % a;
n. x = 17 % 3;
o. y = 17.0 % 3;
p. x = 10; y = x % 3;
q. b = 10; c = 20; b = b + c; c = b – c; b = b – c;
5. Which of the following variable declarations are correct? If a variable declaration is not correct, provide the correct variable declaration.
a. short J,I,j = 12;
b. double x = y = z = 5;
c. int a, b = 5; c;
Apago PDF Enhancer
d. boolean ready, okay = 1;
e. char space = " ";
f. String welcome = "Hello!"
6. Which of the following are valid assignment statements? Assume that b, c, d are variables of type double and j, k, t are variables of type int, respectively. Further assume that all these variables are already initialized with nonzero values.
a. j = j + k / t;
b. d + b = c;
c. j = (k < t) ? 10 : 5;
d. b = ++c – j e. c = k % t;
f. t = d % 5;
g. k *= k;
h. d = b = c;
7. Write Java statements that accomplish the following tasks:
a. Declare two variables a and b of type double.
b. Declare and initialize a logical variable fine to true.
c. Modify the value of a double variable x by subtracting 17.5.
d. Initialize a double variable d with three times the sum of two double variables b and c.
e. Increment an int variable k.
f. Initialize a double variable d through an input statement.
g. Initialize a variable k of type int through an input statement.
h. Output statement for variable c of type double;
i. Output statement to print the String “How are you”.
j. Output statement to print the String “\n is the newline character”.
8. Suppose b, c, d, and e are variables of type double. What are their values aft er the execution of the last statement?
b = 17.7;
c = 13;
d = c - b;
e = c + b;
b = d + e + 2 * b – c / 4;
c = b – 3 * e + d;
e = b / c;
d = -e + 27 / 2;
Apago PDF Enhancer
9. Suppose b, c, d, and e are variables of type int. What are their values aft er the execution of the last statement?
b = 17;
c = 13;
d = c – 2 * b / 3;
e = c + b * d % 4;
b = d / e + 2 * b – c / 4;
c = b % 3 / e * d + 1;
e = b / c + e * 5;
d = -e + 7 / 10 ;
10. Suppose b, c are variables of type int and d, e are variables of type double, respectively. What are their values aft er the execution of the last statement?
b = 17;
d = 13.3;
c = 10 – 2 * b / 3;
e = c + d * d / 4;
b = b – c / 3 + 5;
c = b % 3 – 8;
e = b / c - e * 5 - 2;
d = 6 - e + 7 / 2 + 5;
11. Suppose b, c are variables of type int such that b = 7 and c = 9. What is the output produced? If any one of the output statement has an error, explain the exact nature of the error.
a. System.out.println("b = " + b + ", c = " + c");
b. System.out.println("b + c = " + b + c);
c. System.out.println("b - c = " + b - c);
d. System.out.println("b * c = " + b * c);
e. System.out.println("b / c = " + b / c);
f. System.out.println("b % c = " + b % c);
g. System.out.println(b + c + " = b + c");
h. System.out.println((b + c) + " = (b + c)");
12. Repeat Exercise 11. However, assume that b and c are of data type double. Also assume that b and c are 7.0 and 9.0, respectively.
13. Write the output statement necessary for the following:
a. Good Morning America!
b. Good
Apago PDF Enhancer
Morning
America
c. "Good Morning America"
d. \Good Morning America\
e. /Good Morning America\
14. Correct the syntax errors.
a.
public void class SyntaxErrOne {
final ratio = 1.8;
static public main(String args()) {
int b = c = 10;
d = c + 5;
c = C - 10;
b = d / c;
ratio = b / d;
system.out.println("b = + ", b);
System.out.println("c = ", b);
System.out.println("Ratio = " + b / d);
} } b.
public class syntaxErrorTwo{
static final char new = \7777\;
static final int TEN;
public void main(String[] param){
int a, b, c;
TEN = a + 5;
b = a + new;
c = a + 5 * TEN;
(b + c) = b;
System.out.print("a = " + a);
System.out.print("new = " + new);
System.out.print("TEN = " + TEN);
} }
Apago PDF Enhancer
c.
void public class SyntaxErrorThree {
final int OFFSET 32;
void public main(String args()) {
int b; c = 7 ; d;
d = c + 5;
c = d - b;
b = d / c;
c = b % OFFSET++;
c++ = d + b;
system.out.println("B = + ", b);
System.out.println("C = ", c);
System.out.println("D " + d);
} } d.
public class syntaxErrorFour{
static final char new = "A";
static final int SIXTY_SIX = "B";
public void main(String args){
int a, b, c, 10 = a;
b = a + new;
c = a + 5 * SIXTY_SEVEN;
a = ++(b + c);
System.out.print("a : " + a);
System.out.print("b : " + b);
System.out.print("(a+b) : " + (a+b));
} }
15. Write an equivalent compound statement. If no such statement is possible, write “No such statement.” Assume that u, v, w are variables of data type int.
a. u--;
b. u = v – w + u;
c. u = v – w * u d. u = 3*(v + w) + u;
e. u = u % v;
Apago PDF Enhancer
16. Write an equivalent simple statement. If no such statement is possible, write “No such statement.” Assume that u, v, w are variables of data type int.
a. u -= (v + w);
b. u -= w % v - v;
c. v *= u++;
d. u /= --u;
e. u += v % w;
17. For each of the objects listed below, identify the class. Suggest possible attributes (if any) and operations (if any) for the class.
a. Th is text book.
b. Your car.
c. Th e house at 1256 Farnam Circle.
d. Your cellular phone.
e. Th e Zoo in Great Inland.
18. For each of the sentences below, identify the objects and their classes.
a. Let me throw a ball at you.
b. Jill was reading “A passage to South America” by R.G. Wells.
c. Jack took his neighbor Mark’s dog for a walk.
d. Switch on the TV to watch “Th is day is history.”
e. Joy just bought a new bike from “Cycle sport.”