Vous êtes sur la page 1sur 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( ___________________.

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 _______________. 2.11 What displays in the message dialog when each of the following Java statements is performed( Assume x = 2 and y = 3. a. JOptionPane.showMessageDialog( null, "x = " + x ); b. JOptionPane.showMessageDialog( null, "The value of x + x is " + ( x + x ) ); c. JOptionPane.showMessageDialog( null, "x =" ); d. JOptionPane.showMessageDialog(null, ( x + y ) + " = " + ( y + x ) ); 2.12 Which of the following Java statements contain variables whose values are destroyed "i.e.# changed or replaced%( a. p = i + j + k + 7; b. JOptionPane.showMessageDialog( null, "variables whose values are destroyed" ); c. JOptionPane.showMessageDialog( null, "a = 5" ); d. stringVal = JOptionPane.showInputDialog( "Enter string: ); 2.13 *iven y = ax3 + 7# which of the following are correct statements for this e+uation( a. y = a * x * x * x + 7; b. y = a * x * x * (x + 7); c. y = (a * x) * x * (x + 7); d. y = (a * x) * x * x + 7; e. y = a * (x * x * x) + 7; f. y = a * x * (x * x + 7); 2.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. x = 7 + 3 * 6 / 2 - 1;

b. x = 2 % 2 + 2 * 2 - 2 / 2; c. x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) ); 2.15 Write an application that displays the numbers , to - on the same line with each pair of ad.acent numbers separated by one space. Write the program using the following methods. a. /sing one System.out statement. b. /sing four System.out statements. 2.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. /se the techni+ues shown in Fig. 0.1. 2.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. &f the numbers are e+ual# print the message 2These numbers are equal.2 /se the techni+ues shown in Fig. 0.,3. 2.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. /se the */& techni+ues shown in Fig. 0.,3. 4ote: 'he average calculation in this e$ercise should result in an integer representation of the average. !o# if the sum of the values is 3# the average will be 0 not 0.5555... 2.19 Write an application that inputs from the user the radius of a circle and prints the circle6s diameter# circumference and area. /se the constant value 5.,-,78 for . /se the */& techni+ues shown in Fig. 0.1. 94ote: :ou may also use the predefined constant Math.PI for the value of . 'his constant is more precise than the value 5.,-,78. Class Math is defined in the java.lang package# so you do not need to import it.; /se the following formulas "r is the radius%: diameter = 2r# circumference = 2r# area = r0. 2.20 Write an application that displays in the command window a bo$# an oval# an arrow and a diamond using asterisks "*% as follows: ********* * * * * * * * * * * * * * * ********* *** * * * * * * * *** * * * * * * * * *** ***** * * * * * * * * * * * * * * * * * * * * * *

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

2.29 Write an application that displays in the command window a checkerboard pattern as follows: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

2.30 <odify the program you wrote in =$ercise 0.08 to display the checkerboard pattern in a JOptionPane.PLAIN_MESSAGE dialog. oes the program display the shapes e$actly as in =$ercise 0.08( 2.31 ?ere)s a peek ahead. &n this chapter you learned about integers and the data type int. Java can also represent uppercase letters# lowercase letters and a considerable variety of special symbols. =very character has a corresponding integer representation. 'he set of characters a computer uses and the corresponding integer representations for those characters is called that computer6s character set. :ou can indicate a character value in a program by simply enclosing that character in single +uotes as with 'A'. :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 -%. (int) 'A' 'he following statement would output a character and its integer e+uivalent System.out.println( "The character " + 'A' + " has the value " + (int) 'A' ); 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. Write an application that displays the integer e+uivalents of some uppercase letters# lowercase letters# digits and special symbols. At a minimum# display the integer e+uivalents of the following: A B C a b c 0 1 2 $ * + / and the blank character. 2.32 Write an application that inputs one number consisting of fiveAdigits from the user#

separates the number into its individual digits and prints the digits separated from one another by three spaces each. 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. :ou will need to use both division and modulus operations to 2pick off2 each digit. For the purpose of this e$ercise assume that the user enters the correct number of digits. 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.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 ,> 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. 2.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. isplay in a message dialog the concatenated name. 2.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.

Selected Answers

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

ANS: $ B d. JOptionPane.showMessageDialog(null, ( x + y ) + " = " + ( y + x ) ); ANS: 7 B 7 2.17


// // // im Exercise 2.17 Solution Larger.java Program determines the larger of two numbers ort javax.swing.!" 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 , !" tionPane.show-n ut.ialog% /Enter first integer0/ (# // read second number from user as a string second)umber , !" tionPane.show-n ut.ialog% /Enter second integer0/ (# // convert numbers from t+ e String to t+ e int number1 , -nteger. arse-nt% first)umber (# number2 , -nteger. arse-nt% second)umber (# if % number1 1 number2 ( result , number1 2 / is larger./# else if % number1 3 number2 ( result , number2 2 / is larger./# else result , /4hese numbers are e5ual./# // .is la+ results !" tionPane.show6essage.ialog% null* result* /7om arison 8esults/* !" tionPane.-)9"86:4-");6ESS:<E (# > > S+stem.exit% = (#

2.19
// Exercise 2.1? Solution // 7ircle.java

// Program calculate the area* circumference* and diameter for a circle im ort javax.swing.!" 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 , !" tionPane.show-n ut.ialog% /Enter radius0/ (# // convert number from t+ e String to t+ e int radius , -nteger. arse-nt% in ut (# result , /.iameter is / 2 % 2 @ radius ( 2 /An:rea is / 2 % 6ath.P- @ radius @ radius ( 2 /An7ircumference is / 2 % 2 @ 6ath.P- @ radius (# // .is la+ results !" tionPane.show6essage.ialog% null* result* /7alculation 8esults/* !" tionPane.-)9"86:4-");6ESS:<E (# S+stem.exit% = (# > >

2.24 What does the following code print( System.out.print( "*" ); System.out.print( "***" ); System.out.print( "*****" ); System.out.print( "****" ); System.out.println( "**" ); ANS: ,,,,,,,,,,,,,,, 2.27
// // // im Exercise 2.27 Solution "ddEven.java Program determines if a number is odd or even ort javax.swing.!" 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

in ut , !" tionPane.show-n ut.ialog% /Enter integer0/ (# // convert number from t+ e String to t+ e int number , -nteger. arse-nt% in ut (# if % number B 2 ,, = ( result , /)umber is even./# else result , /)umber is odd./# // .is la+ results !" tionPane.show6essage.ialog% null* result* /7alculation 8esults/* !" tionPane.-)9"86:4-");6ESS:<E (# > > S+stem.exit% = (#

2.29
// Exercise 2.2? Solution // 7hecCer.java // Program draws a checCerboard ublic class 7hecCer $ ublic static void main% String args&' ( $ S+stem.out. rintln% /@ @ @ @ @ @ @ @/ (# S+stem.out. rintln% / @ @ @ @ @ @ @ @/ (# S+stem.out. rintln% /@ @ @ @ @ @ @ @/ (# S+stem.out. rintln% / @ @ @ @ @ @ @ @/ (# S+stem.out. rintln% /@ @ @ @ @ @ @ @/ (# S+stem.out. rintln% / @ @ @ @ @ @ @ @/ (# S+stem.out. rintln% /@ @ @ @ @ @ @ @/ (# S+stem.out. rintln% / @ @ @ @ @ @ @ @/ (# >

>

2.31
// // // // Exercise 2.D1 Solution .is la+.java Program rints a unicode character and its integer e5uivalent

ublic class .is la+ $ ublic static void main% String args&' ( $ S+stem.out. rintln% /4he character / 2 E:E 2 / has the value / 2 % int ( E:E (# S+stem.out. rintln% /4he character / 2 EFE 2 / has the value / 2 % int ( EFE (# S+stem.out. rintln% /4he character / 2 E7E 2

> >

/ has S+stem.out. rintln% / has S+stem.out. rintln% / has S+stem.out. rintln% / has S+stem.out. rintln% / has S+stem.out. rintln% / has S+stem.out. rintln% / has S+stem.out. rintln% / has S+stem.out. rintln% / has S+stem.out. rintln% / has S+stem.out. rintln% / has S+stem.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

(# (# (# (# (# (# (# (# (# (# (# (#

Vous aimerez peut-être aussi