26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6 Methods 307<br />

6.16 Write a method multiple that determines for a pair of integers whether the second integer<br />

is a multiple of the first. The method should take two integer arguments and return true if the second<br />

is a multiple of the first and false otherwise. Incorporate this method in<strong>to</strong> an applet that inputs a<br />

series of pairs of integers (one pair at a time using JTextFields). [Note: Register for event handling<br />

on only the second JTextField. The user should interact with the program by typing numbers<br />

in both JTextFields and pressing Enter only in the second JTextField.]<br />

6.17 Write a method isEven that uses the modulus opera<strong>to</strong>r <strong>to</strong> determine if an integer is even.<br />

The method should take an integer argument and return true if the integer is even and false otherwise.<br />

Incorporate this method in<strong>to</strong> an applet that inputs a series integers (one at a time using a<br />

JTextField).<br />

6.18 Write a method squareOfAsterisks that displays a solid square of asterisks whose side<br />

is specified in integer parameter side. For example, if side is 4, the method displays<br />

****<br />

****<br />

****<br />

****<br />

Incorporate this method in<strong>to</strong> an applet that reads an integer value for side from the user at the keyboard<br />

and performs the drawing with the squareOfAsterisks method. Note that this method<br />

should be called from the applet’s paint method and should be passed the Graphics object from<br />

paint.<br />

6.19 Modify the method created in Exercise 6.18 <strong>to</strong> form the square out of whatever character is<br />

contained in character parameter fillCharacter. Thus, if side is 5 and fillCharacter is<br />

“#”, the method should print<br />

#####<br />

#####<br />

#####<br />

#####<br />

#####<br />

6.20 Use techniques similar <strong>to</strong> those developed in Exercise 6.18 and Exercise 6.19 <strong>to</strong> produce a<br />

program that graphs a wide range of shapes.<br />

6.21 Modify the program of Exercise 6.18 <strong>to</strong> draw a solid square with the fillRect method of<br />

the Graphics class. Method fillRect receives four arguments: x-coordinate, y-coordinate,<br />

width and height. Allow the user <strong>to</strong> input the coordinates at which the square should appear.<br />

6.22 Write program segments that accomplish each of the following tasks:<br />

a) Calculate the integer part of the quotient when integer a is divided by integer b.<br />

b) Calculate the integer remainder when integer a is divided by integer b.<br />

c) Use the program pieces developed in parts a) and b) <strong>to</strong> write a method displayDigits<br />

that receives an integer between 1 and 99999 and prints it as a series of digits, each<br />

pair of which is separated by two spaces. For example, the integer 4562 should be printed<br />

as<br />

4 5 6 2<br />

d) Incorporate the method developed in part c) in<strong>to</strong> an applet that inputs an integer from an<br />

input dialog and invokes displayDigits by passing the method the integer entered.<br />

Display the results in a message dialog.<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!