You are on page 1of 11

Exercises

Included below are short-answer and programming exercises. Answers are provided for those exercises whose exercise number is a hyperlink. Because college faculty use these exercises in their exams, we have provided answers to roughly half of the exercises included here. 2.7 Fill in the blanks in each of the following: a. __________ are used to document a program and improve its readability. b. An input dialog capable of receiving input from the user is displayed with method ___________ of class ______________. c. A Java statement that makes a decision is _____________. d. Calculations are normally performed by ________________ statements. e. An input dialog capable of receiving input from the user is displayed with method _____________ of class _______________. 2.8 Write Java statements that accomplish each of the following: a. isplay the message "Enter two numbers" using class JOptionPane.

b. Assign the product of variables b and c to variable a. c. !tate that a program performs a sample payroll calculation "i.e.# use te$t that helps to document a program%. 2.9 !tate which of the following are true and which are false. &f false# e$plain why. a. Java operators are evaluated from left to right. b. 'he following are all valid variable names: _under_bar_# m928134# t5# j7# her_sales$# his_$account_total# a# b$# c# z# z2. c. A valid Java arithmetic e$pression with no parentheses is evaluated from left to right. d. 'he following are all invalid variable names: 3g, 87, 67h2, h22, 2h. 2.10 Fill in the blanks in each of the following: a. What arithmetic operations are on the same level of precedence as multiplication( __________________. b. When parentheses are nested# which set of parentheses is evaluated first in an arithmetic e$pression( ___________________.

showMessageDialog( null.c. b. y = (a * x) * x * x + 7. "x = " + x ). c. b. "x =" ).1. 2.showMessageDialog( null. y = a * x * x * x + 7. f. 2. "The value of x + x is " + ( x + x ) ). a.showInputDialog( "Enter string: ). d. ( x + y ) + " = " + ( y + x ) ). y = a * x * (x * x + 7). JOptionPane. y = a * x * x * (x + 7).11 What displays in the message dialog when each of the following Java statements is performed( Assume x = 2 and y = 3. "variables whose values are destroyed" ).# changed or replaced%( a. JOptionPane. JOptionPane. 2. JOptionPane.13 *iven y = ax3 + 7# which of the following are correct statements for this e+uation( a. x = 7 + 3 * 6 / 2 . c. A location in the computer)s memory that may contain different values at various times throughout the e$ecution of a program is called a _______________.showMessageDialog( null. b. y = a * (x * x * x) + 7.e. .showMessageDialog( null. p = i + j + k + 7.14 !tate 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 = 5" ). d. c.12 Which of the following Java statements contain variables whose values are destroyed "i. JOptionPane. d. JOptionPane. e. 2. stringVal = JOptionPane. y = (a * x) * x * (x + 7).showMessageDialog( null.showMessageDialog(null. a.

Class Math is defined in the java.2 / 2.. 'his constant is more precise than the value 5. /se the */& techni+ues shown in Fig.3. 2. 0.1.78.5555.15 Write an application that displays the numbers . b. /sing four System.78 for π.-.18 Write an application that inputs three integers from the user and displays the sum# average# product# smallest and largest of these numbers in an information message dialog. x = 2 % 2 + 2 * 2 .1.. 2.3. Write the program using the following methods.16 Write an application that asks the user to enter two numbers# obtains the two numbers from the user and prints the sum# product# difference and +uotient of the two numbers. 4ote: 'he average calculation in this e$ercise should result in an integer representation of the average.. /se the constant value 5. &f the numbers are e+ual# print the message 2These numbers are equal.on the same line with each pair of ad. to . x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) ). 2. /se the following formulas "r is the radius%: diameter = 2r# circumference = 2πr# area = πr0.. 0. !o# if the sum of the values is 3# the average will be 0 not 0. 0. /sing one System..acent numbers separated by one space. 0.lang package# so you do not need to import it. 94ote: :ou may also use the predefined constant Math.out statement.20 Write an application that displays in the command window a bo$# an oval# an arrow and a diamond using asterisks "*% as follows: ********* * * * * * * * * * * * * * * ********* *** * * * * * * * *** * * * * * * * * *** ***** * * * * * * * * * * * * * * * * * * * * * * . 2. 2. c.19 Write an application that inputs from the user the radius of a circle and prints the circle6s diameter# circumference and area. a. /se the */& techni+ues shown in Fig.out statements..b. 2.-..2 /se the techni+ues shown in Fig.17 Write an application that asks the user to enter two integers# obtains the numbers from the user and displays the larger number followed by the words 2is larger2 in an information message dialog.PI for the value of π. /se the techni+ues shown in Fig.

27 Write an application that reads an integer and determines and prints whether it is odd or even. "***" ).0>( 2. "****" ).out.println( System.out.out.print( "*****" ).print( "****" ). System.print( "****" ). "?int: /se the modulus operator.out.% 2.println( "***" ). System.2. 2.25 What does the following code print( System.println( "**" ).print( "*" ).% "*" ). System.0> to display the shapes in a JOptionPane.out.26 Write an application that reads five integers and determines and prints the largest and the smallest integers in the group. "**" ). 2.print( "*" ). System.println( System. /se only the programming techni+ues you learned in this chapter.out.print( "***" ). Any multiple of two leaves a remainder of @ero when divided by 0.println( System.22 What does the following code print( System.out. 2.out.println( "*\n**\n***\n****\n*****" ). oes the program display the shapes e$actly as in =$ercise 0. System.out.out.out.out.23 What does the following code print( System.out.21 <odify the program you created in =$ercise 0. System. An even number is a multiple of two.PLAIN_MESSAGE dialog. System.out.out.println( "*****" ).24 What does the following code print( System. 2.println( 2.println( "**" ). "*****" ).println( System.out. "?int: /se the modulus operator. System.28 Write an application that reads in two integers and determines and prints if the first is a multiple of the second. .

When the preceding statement e$ecutes# it displays the character A and the value 65 "from the soA called /nicode character set% as part of the string.PLAIN_MESSAGE dialog.32 Write an application that inputs one number consisting of fiveAdigits from the user# .out. :ou can indicate a character value in a program by simply enclosing that character in single +uotes as with 'A'.08 to display the checkerboard pattern in a JOptionPane. =very character has a corresponding integer representation.08( 2. Write an application that displays the integer e+uivalents of some uppercase letters# lowercase letters# digits and special symbols. Java can also represent uppercase letters# lowercase letters and a considerable variety of special symbols.println( "The character " + 'A' + " has the value " + (int) 'A' ). (int) 'A' 'he following statement would output a character and its integer e+uivalent System.29 Write an application that displays in the command window a checkerboard pattern as follows: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 2. &n this chapter you learned about integers and the data type int.30 <odify the program you wrote in =$ercise 0.2. 2. 'he set of characters a computer uses and the corresponding integer representations for those characters is called that computer6s character set.31 ?ere)s a peek ahead. At a minimum# display the integer e+uivalents of the following: A B C a b c 0 1 2 $ * + / and the blank character. :ou can determine the integer e+uivalent of a character by preceding that character with (int)–this is called a cast "we will say more about casts in Chapter -%. oes the program display the shapes e$actly as in =$ercise 0.

What happens when you e$ecute the program and type a number with more than five digits( What happens when you e$ecute the program and type a number with fewer than five digits( 2.separates the number into its individual digits and prints the digits separated from one another by three spaces each. isplay in a message dialog the concatenated name.34 Write a program that reads a first name and a last name from the user as two separate inputs and concatenates the first name and last name separated by a space. 2.33 /sing only the programming techni+ues you learned in this chapter# write an application that calculates the s+uares and cubes of the numbers from > to . For e$ample# if the user types in the number 42339# the program should print 42339 Hint: 'his e$ercise is possible with the techni+ues you learned in this chapter. Selected Answers .35 Write a program that inputs five numbers and determines and prints the number of negative numbers input# number of positive numbers input and the number of @eros input. :ou will need to use both division and modulus operations to 2pick off2 each digit.> and prints the resulting values in table format as follows: number 0 1 2 3 4 5 6 7 8 9 10 square cube 0 0 1 1 4 8 9 27 16 64 25 125 36 216 49 343 64 512 81 729 100 1000 4ote: 'his program does not re+uire any input from the user. For the purpose of this e$ercise assume that the user enters the correct number of digits. 2.

. 'he e$pression is evaluated according to operator precedence. ANS: False. A Java statement that makes a decision is . &dentifier h22 is a valid variable name.showMessageDialog ( null. e are not able to include answers to every exercise because college faculty use these exercises in their classroom exams. ANS: 'rue. &f false# e$plain why.7 Fill in the blanks in each of the following: a.# assignment# B% evaluate from right to left. JOptionPane. 'he following are all invalid variable names: 3g. ANS: assignment e.Included below are answers to approximately half the of the exercises in the Cyber Classroom.showMessageDialog( null.g. c. "x =" ). ANS: 'he value of $ C $ is c. An input dialog capable of receiving input from the user is displayed with method of class . c. !ome operators "e. 67h2. d. 2. b. d. Calculations are normally performed by statements. ANS: i*. ANS: $ B 0 b.11 What displays in the message dialog when each of the following Java statements is performed( Assume x = 2 and y = 3. JOptionPane. h22. An input dialog capable of receiving input from the user is displayed with method of class . 2. ANS: False. A valid Java arithmetic e$pression with no parentheses is evaluated from left to right. a. ANS: show n!"t#i$lo%& '(!tion)$ne. 2h. Are used to document a program and improve its readability. ANS: Comments b. "x = " + x ). ANS: show+ess$%e#i$lo%& '(!tion)$ne. 87."The value of x + x is " + ( x + x ) ). JOptionPane. Java operators are evaluated from left to right. a.showMessageDialog( null.9 !tate which of the following are true and which are false. ANS: False. 2. 'he following are all valid variable names: _under_bar_# m928134# t5# j7# her_sales$# his_$account_total# a# b$# c# z# z2.

ANS: $ B d.show6essage. ( x + y ) + " = " + ( y + x ) ). /4hese numbers are e5ual.6ESS:<E (# > > S+stem.19 // Exercise 2. -nteger.java Program determines the larger of two numbers ort javax.!" tionPane# ublic class Larger $ ublic static void main% String args&' ( $ String first)umber* // first string entered b+ user second)umber* // second string entered b+ user result# // a string containing the out ut int number1* // first number to com are number2# // second number to com are // read first number from user as a string first)umber .1? Solution // 7ircle.showMessageDialog(null. JOptionPane.ialog% /Enter first integer0/ (# // read second number from user as a string second)umber . arse-nt% second)umber (# if % number1 1 number2 ( result .17 Solution Larger.-)9"86:4-").show-n ut. number1 2 / is larger.ialog% /Enter second integer0/ (# // convert numbers from t+ e String to t+ e int number1 ./# else result .is la+ results !" tionPane. arse-nt% first)umber (# number2 . -nteger.17 // // // im Exercise 2./# // . !" tionPane. number2 2 / is larger.ialog% null* result* /7om arison 8esults/* !" tionPane./# else if % number1 3 number2 ( result .swing. !" tionPane. ANS: 7 B 7 2.java .show-n ut.exit% = (# 2.

ialog% /Enter radius0/ (# // convert number from t+ e String to t+ e int radius . 2.show-n ut...-)9"86:4-").. System.swing.show6essage.out.@ radius (# // .!" tionPane# ublic class "ddEven $ ublic static void main% String args&' ( $ String in ut* // string entered b+ user result# // out ut dis la+ string int number# // number // read from user as a string .24 What does the following code print( System.27 Solution "ddEven.iameter is / 2 % 2 @ radius ( 2 /An:rea is / 2 % 6ath... -nteger.print( "***" ).. arse-nt% in ut (# result .ialog% null* result* /7alculation 8esults/* !" tionPane.out. ANS: . System.!" tionPane# ublic class 7ircle $ ublic static void main% String args&' ( $ String in ut* // string entered b+ user result# // out ut dis la+ string int radius# // radius of circle // read from user as a string in ut .P.is la+ results !" tionPane.swing.out..@ radius @ radius ( 2 /An7ircumference is / 2 % 2 @ 6ath.exit% = (# > > 2.6ESS:<E (# S+stem..// Program calculate the area* circumference* and diameter for a circle im ort javax.java Program determines if a number is odd or even ort javax.27 // // // im Exercise 2. !" tionPane.P.out.print( "*****" ).out..println( "**" ). System.print( "*" ).. /.print( "****" ). System.....

6ESS:<E (# > > S+stem.show6essage.out.31 // // // // Exercise 2.-)9"86:4-").out. arse-nt% in ut (# if % number B 2 . rintln% / @ @ @ @ @ @ @ @/ (# S+stem.in ut . rintln% /4he character / 2 E7E 2 . !" tionPane.ialog% null* result* /7alculation 8esults/* !" tionPane.out. = ( result .is la+ $ ublic static void main% String args&' ( $ S+stem. rintln% /@ @ @ @ @ @ @ @/ (# S+stem.out.exit% = (# 2. rintln% /@ @ @ @ @ @ @ @/ (# S+stem.out.out. rintln% /4he character / 2 EFE 2 / has the value / 2 % int ( EFE (# S+stem.2? Solution // 7hecCer.is la+ results !" tionPane. /)umber is odd. rintln% / @ @ @ @ @ @ @ @/ (# > > 2.ialog% /Enter integer0/ (# // convert number from t+ e String to t+ e int number .out.show-n ut. /)umber is even.out. rintln% / @ @ @ @ @ @ @ @/ (# S+stem.java Program rints a unicode character and its integer e5uivalent ublic class ./# else result . rintln% / @ @ @ @ @ @ @ @/ (# S+stem.D1 Solution .. rintln% /@ @ @ @ @ @ @ @/ (# S+stem./# // . -nteger.29 // Exercise 2. rintln% /@ @ @ @ @ @ @ @/ (# S+stem.out.out.out.java // Program draws a checCerboard ublic class 7hecCer $ ublic static void main% String args&' ( $ S+stem. rintln% /4he character / 2 E:E 2 / has the value / 2 % int ( E:E (# S+stem.is la+.

out.out.out.out. rintln% / has the value / 2 % int ( E7E /4he character / 2 EaE 2 the value / 2 % int ( EaE /4he character / 2 EbE 2 the value / 2 % int ( EbE /4he character / 2 EcE 2 the value / 2 % int ( EcE /4he character / 2 E=E 2 the value / 2 % int ( E=E /4he character / 2 E1E 2 the value / 2 % int ( E1E /4he character / 2 E2E 2 the value / 2 % int ( E2E /4he character / 2 EGE 2 the value / 2 % int ( EGE /4he character / 2 E@E 2 the value / 2 % int ( E@E /4he character / 2 E/E 2 the value / 2 % int ( E/E /4he character / 2 E E 2 the value / 2 % int ( E E /4he character / 2 E*E 2 the value / 2 % int ( E*E (# (# (# (# (# (# (# (# (# (# (# (# . rintln% / has S+stem.out.> > / has S+stem. rintln% / has S+stem.out.out.out. rintln% / has S+stem. rintln% / has S+stem. rintln% / has S+stem. rintln% / has S+stem. rintln% / has S+stem. rintln% / has S+stem. rintln% / has S+stem.out.out. rintln% / has S+stem.out.