Building Java Programs, 4th edition - Chapter 2 Self-Check Solutions
- Building Java Programs 4th Edition
- Building Java Programs 3rd Edition Exercise Answers Questions
- Building Java Programs 3rd Edition Exercise Answers Key
- Building Java Programs Exercise Solutions
How is Chegg Study better than a printed Building Java Programs 4th Edition student solution manual from the bookstore? Our interactive player makes it easy to find solutions to Building Java Programs 4th Edition problems you're working on - just go to the chapter for your book.
2.10: Oops2-errors
- Java exercises here are indented to provide you the opportunity to practice the Java programming language concepts. You will start from basic Java exercises to more complex exercises. The solution is provided for each exercise. You should try to solve each problem by yourself first before checking the solution.
- Building Java Programs, 3rd Edition Self-check. Contribute to wenchan25/JavaPractice development by creating an account on GitHub.
- Building Java Programs, 4th edition - Chapter 2 Self-Check Solutions Raw. Rewrite the following code from the previous exercise to be shorter, by declaring the.
The following program contains 9 errors. Correct the errors and submit a working version of the program. The corrected version of the program should produce the following output: |
x is 0 |
x is now 15 |
x and y are 15 and 16 |
// FIND ERRORS |
public class Oops2 { |
public static void main(String[] args) { |
int x; |
System.out.println('x is ' x); |
int x = 15.2; // set x to 15.2 |
System.out.println('x is now + x'); |
int y; // set y to 1 more than x |
y = int x + 1; |
System.out.println('x and y are ' + x + and + y); |
} |
} |
// Solution |
public class Oops2 { |
public static void main(String[] args) { |
int x = 0; |
System.out.println('x is ' + x); |
x = 15; // set x to 15 |
System.out.println('x is now ' + x); |
int y = 0; // set y to 1 more than x |
y = x + 1; |
System.out.println('x and y are ' + x + ' and ' + y); |
} |
} |
2.11: digitExpressions
Suppose you have an int variable called number. What Java expression produces the second-to-last digit of the number (the 10s place)? |
What expression produces the third-to-last digit of the number (the 100s place)? |
number / 10 % 10 |
number / 100 % 10 |
2.12: valueOfX
What is the value of variable x after the following code executes? |
int x = 3; |
x = x + 2; |
x = x + x; |
A: |
10 |
2.13: valuesOfABC
What are the values of a, b, and c after the following code statements? |
int a = 5; |
int b = 10; |
int c = b; |
a = a + 1; |
b = b - 1; |
c = c + a; |
A; |
a = 6 |
b = 9 |
c = 16 |
2.14: firstSecond
What are the values of first and second at the end of the following code? |
int first = 8; |
int second = 19; |
first = first + second; |
second = first - second; |
first = first - second; |
A: |
first = 19 |
second = 8 |
2.15: firstSecond2
Rewrite the following code from the previous exercise to be shorter, by declaring the variables together on the same line, |
and by using the special assignment operators (e.g., +=, -=, *=, and /=) as appropriate. |
int first = 8; |
int second = 19; |
first = first + second; |
second = first - second; |
first = first - second; |
A: |
int first = 8, second = 19; |
first += second; |
second = first - second; |
first -= second; |
2.16: valuesOfIJK
What are the values of i, j, and k after the following code statements? |
int i = 2; |
int j = 3; |
int k = 4; |
int x = i + j + k; |
i = x - i - j; |
j = x - j - k; |
k = x - i - k; |
A: |
i = 4 |
j = 2 |
k = 1 |
2.17: maxMin
int max; |
int min = 10; |
max = 17 - 4 / 10; |
max = max + 6; |
min = max - min; |
System.out.println(max * 2); |
System.out.println(max + min); |
System.out.println(max); |
System.out.println(min); |
A: |
46 |
36 |
23 |
13 |
2.19: ComputePay
The following program redundantly repeats the same expressions many times. |
Modify the program to remove all redundant expressions using variables of appropriate types. |
public class ComputePay { |
public static void main(String[] args) { |
// Calculate pay at work based on hours worked each day |
System.out.println('My total hours worked:'); |
System.out.println(4 + 5 + 8 + 4); |
System.out.println('My hourly salary:'); |
System.out.println('$8.75'); |
System.out.println('My total pay:'); |
System.out.println((4 + 5 + 8 + 4) * 8.75); |
System.out.println('My taxes owed:'); // 20% tax |
System.out.println((4 + 5 + 8 + 4) * 8.75 * 0.20); |
} |
} |
public class ComputePay { |
public static void main(String[] args) { |
// Calculate pay at work based on hours worked each day |
int totalHours = 4 + 5 + 8 + 4; |
double hourlyPay = 8.75; |
double totalPay = (totalHours) * hourlyPay; |
double taxesOwed = (totalHours) * hourlyPay * 0.20; |
System.out.println('My total hours worked:'); |
System.out.println(totalHours); |
System.out.println('My hourly salary:'); |
System.out.println('$' + hourlyPay); |
System.out.println('My total pay:'); |
System.out.println(totalPay); |
System.out.println('My taxes owed:'); // 20% tax |
System.out.println(taxesOwed); |
} |
} |
2.1: legalIntLiterals
Which of the following are legal int literals? |
22 |
5. |
2.3 |
10.0 |
1.5 |
'7' |
-6875309 |
-1 |
A: |
22 |
-6875309 |
-1 |
2.20: Receipt
The following program redundantly repeats the same expressions many times. |
Modify the program to remove all redundant expressions using variables of appropriate types. |
// This program computes the total amount owed for a meal, |
// assuming 8% tax and a 15% tip. |
public class Receipt { |
public static void main(String[] args) { |
System.out.println('Subtotal:'); |
System.out.println(38 + 40 + 30); |
System.out.println('Tax:'); |
System.out.println((38 + 40 + 30) * .08); |
System.out.println('Tip:'); |
System.out.println((38 + 40 + 30) * .15); |
System.out.println('Total:'); |
System.out.println(38 + 40 + 30 + |
(38 + 40 + 30) * .08 + |
(38 + 40 + 30) * .15); |
} |
} |
public class Receipt { |
public static void main(String[] args) { |
int subTotal = 38 + 40 + 30; |
double taxOnTotal = subTotal * 0.08; |
double totalWTip = subTotal * 0.15; |
double total = subTotal + taxOnTotal + totalWTip; |
System.out.println('Subtotal:'); |
System.out.println(subTotal); |
System.out.println('Tax:'); |
System.out.println(taxOnTotal); |
System.out.println('Tip:'); |
System.out.println(totalWTip); |
System.out.println('Total:'); |
System.out.println(total); |
} |
} |
2.21: Count2
Complete the following code, replacing the 'FINISH ME' parts with your own code, to produce the following output: |
2 times 1 = 2 |
2 times 2 = 4 |
2 times 3 = 6 |
2 times 4 = 8 |
public class Count2 { |
public static void main(String[] args) { |
for (int i = 1; i <= 4; i++) { |
System.out.println('2 times '+ i + ' = ' + (i * 2)); |
} |
} |
} |
2.23: completeLoop
Complete the following for loop to produce the following output: |
-4 |
14 |
32 |
50 |
68 |
86 |
for (int i = 1; i <= 6; i++) { |
System.out.println((i * 18) - 22); |
} |
2.24: oddStuff
What is the output of the following oddStuff method? |
public static void oddStuff() { |
int number = 4; |
for (int count = 1; count <= number; count++) { |
System.out.println(number); |
number = number / 2; |
} |
} |
output: |
4 |
2 |
2.25: numberTotal
What is the output of the following loop? |
int total = 25; |
for (int number = 1; number <= (total / 2); number++) { |
total = total - number; |
System.out.println(total + ' ' + number); |
} |
24 1 |
22 2 |
19 3 |
15 4 |
10 5 |
2.26: fingerTrap
What is the output of the following loop? |
System.out.println('+----+'); |
for (int i = 1; i <= 3; i++) { |
System.out.println(' /'); |
System.out.println('/ '); |
} |
System.out.println('+----+'); |
+----+ |
/ |
/ |
/ |
/ |
/ |
/ |
+----+ |
2.2: evaluatingExpressions
What is the result of the following expression? |
1 + 2 * 3 + 7 * 2 % 5 |
A: |
11 |
2.3: expressions1
Trace the evaluation of the following expressions, and give their resulting values. |
Make sure to give a value of the appropriate type (such as including a .0 at the end of a double). |
2 + 3 * 4 - 6 = 8 |
14 / 7 * 2 + 30 / 5 + 1 = 11 |
(12 + 3) / 4 * 2 = 6 |
(238 % 10 + 3) % 7 = 4 |
(18 - 7) * (43 % 10) = 33 |
2 + 19 % 5 - (11 * (5 / 2)) = -16 |
813 % 100 / 3 + 2.4 = 6.4 |
26 % 10 % 4 * 3 = 6 |
22 + 4 * 2 = 30 |
23 % 8 % 3 = 1 |
12 - 2 - 3 = 7 |
6/2 + 7/3 = 5 |
6 * 7 % 4 = 2 |
3 * 4 + 2 * 3 = 18 |
177 % 100 % 10 / 2 = 3 |
89 % (5 + 5) % 5 = 4 |
392 / 10 % 10 / 2 = 4 |
8 * 2 - 7 / 4 = 15 |
37 % 20 % 3 * 4 = 8 |
17 % 10 / 4 = 1 |
Building Java Programs 4th Edition
2.4: expressions2
Trace the evaluation of the following expressions, and give their resulting values. |
Make sure to give a value of the appropriate type (such as including a .0 at the end of a double). |
4.0 / 2 * 9 / 2 = 9.0 |
2.5 * 2 + 8 / 5.0 + 10 / 3 = 9.6 |
12 / 7 * 4.4 * 2 / 4 = 2.2 |
4 * 3 / 8 + 2.5 * 2 = 6.0 |
(5 * 7.0 / 2 - 2.5) / 5 * 2 = 6.0 |
41 % 7 * 3 / 5 + 5 / 2 * 2.5 = 8.0 |
10.0 / 2/4 = 1.25 |
8 / 5 + 13 / 2 / 3.0 = 3.0 |
(2.5 + 3.5) / 2 = 3.0 |
9 / 4 * 2.0 - 5/4 = 3.0 |
9 / 2.0 + 7 / 3 - 3.0 / 2 = 5.0 |
813 % 100 / 3 + 2.4 = 6.4 |
27 / 2 / 2.0 * (4.3 + 1.7) - 8 / 3 = 37.0 |
53 / 5 / (0.6 + 1.4) / 2 + 13 / 2 = 8.5 |
2 * 3 / 4 * 2 / 4.0 + 4.5 - 1 = 4.0 |
89 % 10 / 4 * 2.0 / 5 + (1.5 + 1.0 / 2) * 2 = 4.8 |
2.5: expressions3
Trace the evaluation of the following expressions, and give their resulting values. |
Make sure to give a value of the appropriate type (such as including a .0 at the end of a double or quotes around a String). |
2 + 2 + 3 + 4 = 11 |
'2 + 2 ' + 3 + 4 = '2 + 2 34' |
2 + ' 2 + 3 ' + 4 = '2 2 + 3 4' |
3 + 4 + ' 2 + 2' = '7 2 + 2' |
'2 + 2 ' + (3 + 4) = '2 + 2 7' |
'(2 + 2) ' + (3 + 4) = '(2 + 2) 7' |
'hello 34 ' + 2 * 4 = 'hello 34 8' |
2 + '(int) 2.0' + 2 * 2 + 2 = '2(int) 2.042' |
4 + 1 + 9 + '.' + (-3 + 10) + 11 / 3 = '14.73' |
8 + 6 * -2 + 4 + '0' + (2 + 5) = '007' |
1 + 1 + '8 - 2' + (8 - 2) + 1 + 1 = '28 - 2611' |
5 + 2 + '(1 + 1)' + 4 + 2 * 3 = '7(1 + 1)46' |
'1' + 2 + 3 + '4' + 5 * 6 + '7' + (8 + 9) = '123430717' |
2.6: declareRealNumberSyntax
Building Java Programs 3rd Edition Exercise Answers Questions
Which of the following choices is the correct syntax for declaring a real number variable named 'grade' and initializing its value to 4.0? |
int grade : 4.0; |
grade = double 4.0; |
double grade = 4.0; |
4.0 = grade; |
grade = 4; |
A: |
double grade = 4.0; |
2.7: fitnessVariables
Imagine you are writing a personal fitness program that stores the user's age, gender, height (in feet or meters), and weight (to the nearest pound or kilogram). Declare variables with the appropriate names and types to hold this information. Write a complete variable declaration statement with the type, the variable name, and a semicolon. |
age |
gender |
height |
weight |
int age; |
String gender; |
double height; |
int weight; |
Building Java Programs 3rd Edition Exercise Answers Key
2.8: studentVariables
Imagine you are writing a program that stores a student's year (Freshman, Sophomore, Junior, or Senior), the number of courses the student is taking, and his or her GPA on a 4.0 scale. Declare variables with the appropriate names and types to hold this information. Write a complete variable declaration statement with the type, the variable name, and a semicolon. |
year |
courses taken |
GPA |
int year; |
int courseTaken; |
double gpa; |
2.9: lastDigitExpression
Suppose you have an int variable called number. What Java expression produces the last digit of the number (the 1s place)? |
number % 10 |
gistfile1.txt
What is the output of the following loop? |
for (int i = 1; i <= 3; i++) |
System.out.println('How many lines'); |
System.out.println('are printed?'); |
How many lines |
How many lines |
How many lines |
are printed? |
Building Java Programs Exercise Solutions
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment