Section 5.13 Exercises
Note:For programming exercises, first draw a UML class diagram describing all classes and their inheritance relationships and/or associations.
Explain the difference between the following pairs of terms:
Representation and action
Binary operator and unary operation
Class constant and class variable
Helper method and class method
Operator overloading and method overloading
Method call and method composition
Type conversion and type promotion
For each of the following data types, list how many bits are used in its representation and how many values can be represented:
Fill in the blanks.
Methods and variables that are associated with a class rather than with its instances must be declared __________.
When an operation involves values of two different types, one value must be __________ before the expression can be evaluated.
Constants should be declared __________.
Variables that take
falseas their possible values are known as __________.
Arrange the following data types into a promotion hierarchy:
o2is false, and
o3is false, evaluate each of the following expressions:
o1 || o2 && o3
o1 ^ o2
!o1 && !o2
Arrange the following operators in precedence order:
+ - () * / % < ==
Arrange the following operators into a precedence hierarchy:
*,++, %, ==
Parenthesize and evaluate each of the following expressions. (If an expression is invalid, mark it as such):
11 / 3 % 2 == 1
11 / 2 % 2 > 0
15 % 3 >= 21 %
12.0 / 4.0 >= 12 / 3
15 / 3 == true
What value would
mhave after each of the statements that follow is executed? Assume that
m, k, jare reinitialized before each statement.
int m = 5, k = 0, j = 1;
m = ++k + j;
m += ++k * j;
m %= ++k + ++j;
m = m - k - j;
m = ++m;
What value would
bhave after each of the statements that follow is executed? Assume that
m, k, jare reinitialized before each statement. It may help to parenthesize the right-hand side of the statements before evaluating them.
boolean b; int m = 5, k = 0, j = 1;
b = m > k + j;
b = m * m != m * j;
b = m <= 5 && m % 2 == 1;
b = m < k || k < j;
b = --m == 2 * ++j;
For each of the following expressions, if it is valid, determine the value of the variable on the left-hand side (if not, change it to a valid expression):
char c = 'a' ; int m = 95;
c = c + 5;
c = 'A' + 'B';
m = c + 5;
c = (char) m + 1;
m = 'a' - 32;
Translate each of the following expressions into Java:
pitimes the radius squared.
Area is assigned
pitimes the radius squared.
Volume is assigned
pitimes radius cubed divide by
nare equal, then
mis incremented by one; otherwise
mis greater than
ntimes 5, then square
n; otherwise square
What would be output by the following code segment?
int m = 0, n = 0, j = 0, k = 0; m = 2 * n++; System.out.println("m= " + m + " n= " + n); j += ( --k * 2 ); System.out.println("j= " + j + " k= " + k);
Write a method to calculate the sales tax for a sale item. The method should take two
doubleparameters, one for the sales price and the other for the tax rate. It should return a
double. For example,
calcTax(20.0, 0.05)should return 1.0.
Challenge: Suppose you're writing a program that tells what day of the week someone's birthday falls on this year. Write a method that takes an
intparameter, representing what day of the year it is, and returns a
Stringlike ``Monday.'' For example, for 2004, a leap year, the first day of the year was on Thursday. The thirty-second day of the year (February 1, 2004) was a Sunday, so
getDayOfWeek(1)should return “Thursday” and
getDayOfWeek(32)should return “Sunday.” (
Hint: If you divide the day of the year by 7, the remainder will always be a number between 0 and 6, which can be made to correspond to days of the week.)
Challenge: As part of the birthday program, you'll want a method that takes the month and the day as parameters and returns what day of the year it is. For example,
getDay(1,1)should return 1;
getDay(2,1)should return 32; and
getDay(12,31)should return 365. (
Hint: If the month is 3, and the day is 5, you have to add the number of days in January plus the number of days in February to 5 to get the result: 31 + 28 + 5 = 64.)
Write a Java method that converts a
charto lowercase. For example,
toLowerCase('A')should return `a'. Make sure you guard against method calls like
Challenge: Write a Java method that shifts a
nplaces in the alphabet, wrapping around to the start of the alphabet, if necessary. For example,
shift('a',2)should return `c';
shift('y',2)should return `a'. This method can be used to create a Caesar cipher, in which every letter in a message is shifted by
nplaces---hfu ju? (Refer to Chapter~1 exercises for a refresher on Caesar cipher.)
Write a method that converts its
booleanparameter to a
String. For example,
boolToString(true)should return ``true.''
Write a Java application that first prompts the user for three numbers, which represent the sides of a rectangular cube, and then computes and outputs the volume and the surface area of the cube.
Write a Java application that prompts the user for three numbers and then outputs the three numbers in increasing order.
Write a Java application that inputs two integers and then determines whether the first is divisible by the second. (
Hint: Use the modulus operator.)
Write a Java application that prints the following table:
N SQUARE CUBE 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125
Design and write a Java GUI that converts kilometers to miles and vice versa. Use a
JTextFieldfor I/O and
JButtons for the various conversion actions.
Design and write a GUI that allows a user to calculate the maturity value of a CD. The user should enter the principal, interest rate, and years, and the applet should then display the maturity value. Make use of the
BankCDclass covered in this chapter. Use separate
JTextFields for the user's inputs and a separate
JTextFieldfor the result.
Design and write a GUI that lets the user input a birth date (month and day) and reports what day of the week it falls on. Use the
getDay()methods that you developed in previous exercises.
Design and write a GUI that allows the users to input their exam grades for a course and computes their average and probable letter grade. The applet should contain a single
JTextFieldfor inputting a grade and a single
JTextFieldfor displaying the average and letter grade. The program should keep track internally of how many grades the student has entered. Each time a new grade is entered, it should display the current average and probable letter grade.
One of the reviewers of this text has suggested an alternative design for the
Temperatureclass (Figure 5.7.6). According to this design, the class would contain an instance variable, say,
temperature, and access methods that operate on it. The access methods would be:
setFahrenheit(double) getFahrenheit():double setCelsius(double) getCelsius():double
One way to implement this design is to store the temperature in the Kelvin scale and then convert from and to Kelvin in the access methods. The formula for converting Kelvin to Celsius is
K = C + 273.15
Draw a UML class diagram representing this design of the
Temperatureclass. Which design is more object oriented, this one or the one used in Figure 5.7.6?
Write an implementation of the
Temperatureclass using the design described in the previous exercise.