2
Introduction to Java
Applications; Input/Output
and Operators
Objectives
In this chapter you’ll:
Write simple Java applications.
Use input and output
statements.
Learn about Java’s primitive
types.
Understand basic memory
concepts.
2Chapter 2 Introduction to Java Applications; Input/Output and Operators
Self-Review Exercises
2.1 Fill in the blanks in each of the following statements:
a) A(n) and a(n) begin and end the body of every method.
c) begins an end-of-line comment.
d) , and are called white space.
e) are reserved for use by Java.
f) Java applications begin execution at method .
g) Methods , and display information in a command window.
2.2 State whether each of the following is true or false. If false, explain why.
a) Comments cause the computer to display the text after the // on the screen when the
program executes.
b) All variables must be given a type when they’re declared.
c) Java considers the variables number and NuMbEr to be identical.
d) The remainder operator (%) can be used only with integer operands.
e) The arithmetic operators *, /, %, + and all have the same level of precedence.
f) The identifier _ (underscore) is valid in Java 9.
2.3 Write statements to accomplish each of the following tasks:
a) Declare variables c, thisIsAVariable, q76354 and number to be of type int and initialize
each to 0.
b) Prompt the user to enter an integer.
c) Input an integer and assign the result to int variable value. Assume Scanner variable
input can be used to read a value from the keyboard.
d) Print “This is a Java program” on one line in the command window. Use method
System.out.println.
e) Print “This is a Java program” on two lines in the command window. The first line
should end with Java. Use method System.out.printf and two %s format specifiers.
Self-Review Exercises 3
f) If the variable number is not equal to 7, display “The variable number is not equal to 7″.
}
2.4 Identify and correct the errors in each of the following statements:
a) if (c < 7); {
System.out.println(“c is less than 7”);
b) if (c => 7) {
System.out.println(“c is equal to or greater than 7″);
}
2.5 Write declarations, statements or comments that accomplish each of the following tasks:
a) State that a program will calculate the product of three integers.
b) Create a Scanner called input that reads values from the standard input.
c) Prompt the user to enter the first integer.
d) Read the first integer from the user and store it in the int variable x.
e) Prompt the user to enter the second integer.
f) Read the second integer from the user and store it in the int variable y.
g) Prompt the user to enter the third integer.
h) Read the third integer from the user and store it in the int variable z.
i) Compute the product of the three integers contained in variables x, y and z, and store
the result in the int variable result.
j) Use System.out.printf to display the message “Product is” followed by the value of
the variable result.
2.6 Using the statements you wrote in Exercise 2.5, write a complete program that calculates
and prints the product of three integers.
4Chapter 2 Introduction to Java Applications; Input/Output and Operators
Exercises 5
a) Java operators are evaluated from left to right.
b) The following are all valid variable names: _under_bar_, m928134, t5, j7, her_sales$,
his_$account_total, a, b$, c, z and z2.
c) A valid Java arithmetic expression with no parentheses is evaluated from left to right.
d) The following are all invalid variable names: 3g, 87, 67h2, h22 and 2h.
2.10 Assuming that x = 2 and y = 3, what does each of the following statements display?
a) System.out.printf(“x = %d%n”, x);
b) System.out.printf(“Value of %d + %d is %d%n”, x, x, (x + x));
c) System.out.printf(“x =”);
d) System.out.printf(“%d = %d%n”, (x + y), (y + x));
2.11 Which of the following Java statements contain variables whose values are modified?
a) int p = i + j + k + 7;
b) System.out.println(“variables whose values are modified”);
c) System.out.println(“a = 5”);
d) int value = input.nextInt();
2.12 Given that y = ax3 + 7, which of the following are correct Java statements for this equation?
a) int y = a * x * x * x + 7;
b) int y = a * x * x * (x + 7);
c) int y = (a * x) * x * (x + 7);
d) int y = (a * x) * x * x + 7;
e) int y = a * (x * x * x) + 7;
f) int y = a * x * (x * x + 7);
2.13 State the order of evaluation of the operators in each of the following Java statements, and
show the value of x after each statement is performed:
a) int x = 7 + 3 * 6 / 21;
b) int x = 2 % 2 + 2 * 22 / 2;
c) int x = (3 * 9 * (3 + (9 * 3 / (3))));
2.19 What does the following code print?
System.out.printf(“*%n**%n***%n****%n*****%n”);
© 2018 Pearson Education, Inc., 330 Hudson Street, NY NY 10013. All rights reserved.
6Chapter 2 Introduction to Java Applications; Input/Output and Operators
2.23 What does the following code print?
System.out.printf(“%s%n%s%n%s%n”, “*”, “***”, “*****”);
ANS: