This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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);

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
What is the value of variable x after the following code executes?
int x = 3;
x = x + 2;
x = x + x;
A:
10

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

You are watching: Building java programs 4th edition exercise solutions

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;

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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);

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Which of the following are legal int literals?
22
5.
2.3
10.0
1.5
'7'
-6875309
-1
A:
22
-6875309
-1

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

See more: Four Points By Sheraton Kansas City Sports Complex Hotel, Four Points By Sheraton Kansas City

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);

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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 Raw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
What is the output of the following oddStuff method?
public static void oddStuff() {
int number = 4;
for (int count = 1; counting Raw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
What is the output of the following loop?
System.out.println("+----+");
for (int i = 1; ns Raw

Raw

Raw

Raw
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"



Raw

Raw

Raw

Raw

Raw
<br> <br> <br> <h3> <img class=building java programs 4th edition exercise solutions -->