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 2 Introduction <strong>to</strong> <strong>Java</strong> Applications 99<br />

b) Error: The relational opera<strong>to</strong>r => is incorrect.<br />

Correction: Change => <strong>to</strong> >=.<br />

2.5 a) // Calculate the product of three integers<br />

b) int x, y, z, result;<br />

c) String xVal, yVal, zVal;<br />

d) xVal = JOptionPane.showInputDialog(<br />

"Enter first integer:" );<br />

e) yVal = JOptionPane.showInputDialog(<br />

"Enter second integer:" );<br />

f) zVal = JOptionPane.showInputDialog(<br />

"Enter third integer:" );<br />

g) x = Integer.parseInt( xVal );<br />

h) y = Integer.parseInt( yVal );<br />

i) z = Integer.parseInt( zVal );<br />

j) result = x * y * z;<br />

k) JOptionPane.showMessageDialog( null,<br />

"The product is " + result );<br />

l) System.exit( 0 );<br />

2.6 The solution <strong>to</strong> Exercise 2.6 is as follows:<br />

1 // Calculate the product of three integers<br />

2<br />

3 // <strong>Java</strong> extension packages<br />

4 import javax.swing.JOptionPane;<br />

5<br />

6 public class Product {<br />

7<br />

8 public static void main( String args[] )<br />

9 {<br />

10 int x, y, z, result;<br />

11 String xVal, yVal, zVal;<br />

12<br />

13 xVal = JOptionPane.showInputDialog(<br />

14 "Enter first integer:" );<br />

15 yVal = JOptionPane.showInputDialog(<br />

16 "Enter second integer:" );<br />

17 zVal = JOptionPane.showInputDialog(<br />

18 "Enter third integer:" );<br />

19<br />

20 x = Integer.parseInt( xVal );<br />

21 y = Integer.parseInt( yVal );<br />

22 z = Integer.parseInt( zVal );<br />

23<br />

24 result = x * y * z;<br />

25 JOptionPane.showMessageDialog( null,<br />

26 "The product is " + result );<br />

27<br />

28 System.exit( 0 );<br />

29 }<br />

30 }<br />

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

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

Saved successfully!

Ooh no, something went wrong!