19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

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

Sections 4.2–4.7<br />

*4.1 (Count positive and negative numbers and <strong>com</strong>pute the average of numbers) Write<br />

a program that reads an unspecified number of integers, determines how many<br />

positive and negative values have been read, and <strong>com</strong>putes the <strong>to</strong>tal and average of<br />

the input values (not counting zeros). Your program ends with the input 0. Display<br />

the average as a floating-point number. Here is a sample run:<br />

<strong>Programming</strong> Exercises 167<br />

Enter an integer, the input ends if it is 0:<br />

The number of positives is 3<br />

The number of negatives is 1<br />

The <strong>to</strong>tal is 5<br />

The average is 1.25<br />

1 2 -1 3 0<br />

Enter an integer, the input ends if it is 0:<br />

No numbers are entered except 0<br />

0<br />

4.2 (Repeat additions) Listing 4.4, SubtractionQuizLoop.java, generates five random<br />

subtraction questions. Revise the program <strong>to</strong> generate ten random addition<br />

questions for two integers between 1 and 15. Display the correct count and test<br />

time.<br />

4.3 (Conversion from kilograms <strong>to</strong> pounds) Write a program that displays the following<br />

table (note that 1 kilogram is 2.2 pounds):<br />

Kilograms Pounds<br />

1 2.2<br />

3<br />

...<br />

6.6<br />

197 433.4<br />

199 437.8<br />

4.4 (Conversion from miles <strong>to</strong> kilometers) Write a program that displays the following<br />

table (note that 1 mile is 1.609 kilometers):<br />

Miles Kilometers<br />

1 1.609<br />

2 3.218<br />

...<br />

9 14.481<br />

10 16.090<br />

4.5 (Conversion from kilograms <strong>to</strong> pounds and pounds <strong>to</strong> kilograms) Write a program<br />

that displays the following two tables side by side (note that 1 kilogram is 2.2<br />

pounds and that 1 pound is .453 kilograms):<br />

Kilograms Pounds | Pounds Kilograms<br />

1 2.2 | 20 9.09<br />

3<br />

...<br />

6.6 | 25 11.36<br />

197 433.4 | 510 231.82<br />

199 437.8 | 515 234.09

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

Saved successfully!

Ooh no, something went wrong!