· R2.15 Explain what each of the following program segments computes.
a. x = 2; y = x + x; b. s = “2”; t = s + s;
· R2.18 Write pseudocode for a program that computes the first and last digit of a number. For example, if the input is 23456, the program should print 2 and 6. Hint: %, Math.log10.
· R2.19 Modify the pseudocode for the program in How To 2.1 so that the program gives change in quarters, dimes, and nickels. You can assume that the price is a multiple of 5 cents. To develop your pseudocode, first work with a couple of specific values.
· R2.20 A cocktail shaker is composed of three cone sections.
· Using realistic values for the radii and heights, compute the total volume, using the formula given in Self Check 25 for a cone section. Then develop an algorithm that works for arbitrary dimensions.
· R2.21 You are cutting off a piece of pie like this, where c is the length of the straight part (called the chord length) and h is the height of the piece.
Macintosh HD:Users:alsaffar97:Desktop:R2_21_image_242x240.png There is an approximate formula for the area: Macintosh HD:Users:alsaffar97:Desktop:R2_21_formula_168x80.png
However, h is not so easy to measure, whereas the diameter d of a pie is usually well-known. Calculate the area where the diameter of the pie is 12 inches and the chord length of the segment is 10 inches. Generalize to an algorithm that yields the area for any diameter and chord length.
· R2.25 For each of the following computations in Java, determine whether the result is exact, an overflow, or a roundoff error.
a. 2.0 – 1.1 b. 1.0E6 * 1.0E6 c. 65536 * 65536 d. 1_000_000L * 1_000_000L
· R2.26 Write a program that prints the values
3 * 1000 * 1000 * 1000 3.0 * 1000 * 1000 * 1000
· Explain the results.
· R2.27 This chapter contains a number of recommendations regarding variables and constants that make programs easier to read and maintain. Briefly summarize these recommendations.
On pages 75-76 of Big Java: Late Objects, complete the following Practice Exercises:
· E2.1 Write a program that displays the dimensions of a letter-size (8.5 × 11 inches) sheet of paper in millimeters. There are 25.4 millimeters per inch. Use constants and comments in your program.
· E2.2 Write a program that computes and displays the perimeter of a letter-size (8.5 × 11 inches) sheet of paper and the length of its diagonal.
· E2.3 Write a program that reads a number and displays the square, cube, and fourth power. Use the Math.pow method only for the fourth power.
· E2.4 Write a program that prompts the user for two integers and then prints
· The sum
· The difference
· The product
· The average
· The distance (absolute value of the difference)
· The maximum (the larger of the two)
· The minimum (the smaller of the two) Hint: The max and min functions are declared in the Math class
· E2.5 Enhance the output of Exercise E2.4 so that the numbers are properly aligned:
Sum: 45 Difference: -5 Product: 500 Average: 22.50 Distance: 5 Maximum: 25 Minimum: 20
· E2.6 Write a program that prompts the user for a measurement in meters and then converts it to miles, feet, and inches.
· E2.7 Write a program that prompts the user for a radius and then prints
· The area and circumference of a circle with that radius
· The volume and surface area of a sphere with that radius
· E2.8 Write a program that asks the user for the lengths of the sides of a rectangle. Then print
· The area and perimeter of the rectangle
· The length of the diagonal (use the Pythagorean theorem)
· E2.9 Improve the program discussed in How To 2.1 to allow input of quarters in addition to bills.
· E2.10 Write a program that helps a person decide whether to buy a hybrid car. Your program’s inputs should be:
· The cost of a new car
· The estimated miles driven per year
· The estimated gas price
· The efficiency in miles per gallon
· The estimated resale value after 5 years Compute the total cost of owning the car for five years. (For simplicity, we will not take the cost of financing into account.) Obtain realistic prices for a new and used hybrid and a comparable car from the Web. Run your program twice, using today’s gas price and 15,000 miles per year. Include pseudocode and the program runs with your assignment.
· E2.11 Write a program that asks the user to input
· The number of gallons of gas in the tank
· The fuel efficiency in miles per gallon
· The price of gas per gallon
· Then print the cost per 100 miles and how far the car can go with the gas in the tank.
· E2.14 Write a program that reads a number between 1,000 and 999,999 from the user and prints it with a comma separating the thousands. Here is a sample dialog; the user input is in color:
Please enter an integer between 1000 and 999999: 23456 23,456
· E2.16 Write a program that reads in an integer and breaks it into a sequence of individual digits. For example, the input 16384 is displayed as
1 6 3 8 4
You may assume that the input has no more than five digits and is not negative.
· E2.17 Write a program that reads two times in military format (0900, 1730) and prints the number of hours and minutes between the two times. Here is a sample run. User input is in color.
· Please enter the first time: 0900 Please enter the second time: 1730 8 hours 30 minutes