• Home
  • Textbooks
  • Introduction to Java Programming
  • Elementary Programming

Introduction to Java Programming

Y. Daniel Liang

Chapter 2

Elementary Programming - all with Video Answers

Educators


Chapter Questions

03:52

Problem 1

(Convert Celsius to Fahrenheit) Write a program that reads a Celsius degree in a double value from the console, then converts it to Fahrenheit and displays the result. The formula for the conversion is as follows:
fahrenheit $=(9 / 5)$ Celsius +32
Hint: In Java, 9 / 5 is 1, but 9.0 / 5 is 1.8.
Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
03:31

Problem 2

(Compute the volume of a cylinder) Write a program that reads in the radius and length of a cylinder and computes the area and volume using the following formulas:
area = radius $\quad$ radius $* \pi$ volume $=$ area
Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
03:26

Problem 3

(Convert feet into meters) Write a program that reads a number in feet, converts it to meters, and displays the result. One foot is 0.305 meter. Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
03:48

Problem 4

(Convert pounds into kilograms) Write a program that converts pounds into kilograms. The program prompts the user to enter a number in pounds, converts it to kilograms, and displays the result. One pound is 0.454 kilograms. Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
03:47

Problem 5

(Financial application: calculate tips) Write a program that reads the subtotal and the gratuity rate, then computes the gratuity and total. For example, if the user enters 10 for subtotal and $15 \%$ for gratuity rate, the program displays $\$ 1.5$ as gratuity and $\$ 11.5$ as total. Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
07:22

Problem 6

(Sum the digits in an integer) Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example, if an integer is $932,$ the sum of all its digits is 14

Hint: Use the $\%$ operator to extract digits, and use the / operator to remove the extracted digit. For instance, $932 \% 10=2$ and $932 / 10=93$
Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
03:58

Problem 7

(Find the number of years) Write a program that prompts the user to enter the minutes (e.g., 1 billion), and displays the number of years and days for the minutes. For simplicity, assume a year has 365 days. Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
03:51

Problem 8

(Current time) Listing $2.6,$ ShowCurrentTime.java, gives a program that displays the current time in GMT. Revise the program so that it prompts the user to enter the time zone offset to GMT and displays the time in the specified time zone. Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
01:48

Problem 9

(Physics: acceleration) Average acceleration is defined as the change of velocity divided by the time taken to make the change, as shown in the following formula:
$$a=\frac{v_{1}-v_{0}}{t}$$
Write a program that prompts the user to enter the starting velocity $v_{0}$ in meters/second, the ending velocity $v_{1}$ in meters/second, and the time span $t$ in seconds, and displays the average acceleration. Here is a sample run:

Akash M
Akash M
Numerade Educator
05:09

Problem 10

(Science: calculating energy) Write a program that calculates the energy needed to heat water from an initial temperature to a final temperature. Your program should prompt the user to enter the amount of water in kilograms and the initial and final temperatures of the water. The formula to compute the energy is $Q=M *(f \text { inalTemperature }-\text { initialTemperature }) * 4184$
where $M$ is the weight of water in kilograms, temperatures are in degrees Celsius, and energy $Q$ is measured in joules. Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
05:50

Problem 11

(Population projection) Rewrite Exercise 1.11 to prompt the user to enter the number of years and displays the population after the number of years. Here is a sample run of the program:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
05:03

Problem 12

(Physics: finding runway length) Given an airplane's acceleration $a$ and take-off speed $v,$ you can compute the minimum runway length needed for an airplane to take off using the following formula: $$\text {length}=\frac{v^{2}}{2 a}$$
Write a program that prompts the user to enter $v$ in meters/second (m/s) and the acceleration $a$ in meters/second squared $\left(\mathrm{m} / \mathrm{s}^{2}\right),$ and displays the minimum runway length. Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
04:21

Problem 13

(Sum the digits in an integer) Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example, if an integer is $932,$ the sum of all its digits is 14

Hint: Use the $\%$ operator to extract digits, and use the / operator to remove the extracted digit. For instance, $932 \% 10=2$ and $932 / 10=93$
Here is a sample run:(Financial application: compound value) Suppose you save $\$ 100$ each month into a savings account with the annual interest rate $5 \% .$ Thus, the monthly interest rate is $0.05 / 12=0.00417 .$ After the first month, the value in the account becomes

$$100 *(1+0.00417)=100.417$$
After the second month, the value in the account becomes
$$(100+100.417) \text { is }(1+0.00417)=201.252$$
After the third month, the value in the account becomes
$$(100+201.252) *(1+0.00417)=302.507$$
and so on.
Write a program that prompts the user to enter a monthly saving amount and displays the account value after the sixth month. (In Exercise $4.30,$ you will use a loop to simplify the code and display the account value for any month.)

Harriet O'Brien
Harriet O'Brien
Numerade Educator
05:50

Problem 14

(Health application: computing $B M I$ ) Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms and dividing by the square of your height in meters. Write a program that prompts the user to enter a weight in pounds and height in inches and displays the BMI. Note that one pound is 0.45359237 kilograms and one inch is 0.0254 meters. Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
07:28

Problem 15

(Geometry: area of a triangle) Write a program that prompts the user to enter three points $(x 1, y 1),(x 2, y 2),(x 3, y 3)$ of a triangle and displays its area. The formula for computing the area of a triangle is
$$s=(\text {side} 1+\text {side} 2+\text {side} 3) / 2$$
$$\text {area}=\sqrt{s(s-\text {side} 1)(s-\text {side} 2)(s-\text {side} 3)}$$
Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
03:08

Problem 16

(Geometry: area of a hexagon) Write a program that prompts the user to enter the side of a hexagon and displays its area. The formula for computing the area of a hexagon is $$\text { Area }=\frac{3 \sqrt{3}}{2} s^{2}$$
where $s$ is the length of a side. Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
05:11

Problem 17

(Science: wind-chill temperature) How cold is it outside? The temperature alone is not enough to provide the answer. Other factors including wind speed, relative humidity, and sunshine play important roles in determining coldness outside. In $2001,$ the National Weather Service (NWS) implemented the new wind-chill temperature to measure the coldness using temperature and wind speed. The formula is:
$$t_{w c}=35.74+0.6215 t_{a}-35.75 v^{0.16}+0.4275 t_{a} v^{0.16}$$
where $t_{a}$ is the outside temperature measured in degrees Fahrenheit and $v$ is the speed measured in miles per hour. $t_{\mathrm{w} c}$ is the wind-chill temperature. The formula cannot be used for wind speeds below 2 mph or temperatures below $-58^{\circ} \mathrm{F}$ or above $41^{\circ} \mathrm{F}$
Write a program that prompts the user to enter a temperature between $-58^{\circ} \mathrm{F}$ and $41^{\circ} \mathrm{F}$ and a wind speed greater than or equal to 2 and displays the wind-chill temperature. Use Math .pow(a,
b) to compute $v^{0.16} .$ Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
03:41

Problem 18

(Print a table) Write a program that displays the following table:
$$\begin{array}{lll}
\mathrm{a} & \mathrm{b} & \text { pow }(\mathrm{a}, \mathrm{b}) \\
1 & 2 & 1 \\
2 & 3 & 8 \\
3 & 4 & 81 \\
4 & 5 & 1024 \\
5 & 6 & 15625
\end{array}$$

Willis James
Willis James
Numerade Educator
05:17

Problem 19

(Geometry: distance of two points) Write a program that prompts the user to enter two points $(x 1, y 1)$ and $(x 2, y 2)$ and displays their distance between them. The formula for computing the distance is $\sqrt{\left(x_{2}-x_{1}\right)^{2}+\left(y_{2}-y_{1}\right)^{2}}$. Note that you can use Math.pow(a, 0.5 ) to compute $\sqrt{a}$. Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
03:13

Problem 20

(Financial application: calculate interest) If you know the balance and the annual percentage interest rate, you can compute the interest on the next monthly payment using the following formula: interest $=$ balance $\times$ (annual Interest Rate $/ 1200$ )
Write a program that reads the balance and the annual percentage interest rate and displays the interest for the next month. Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
06:22

Problem 21

(Financial application: calculate future investment value) Write a program that reads in investment amount, annual interest rate, and number of years, and displays the future investment value using the following formula:
futureInvestmentValue $=$ investmentAmount $\times(1+\text { monthlyInterestRate })^{\text {numberofYears }^{*} 12}$
For example, if you enter amount 1000 , annual interest rate $3.25 \%$, and number of years $1,$ the future investment value is 1032.98
Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
03:10

Problem 22

(Random character) Write a program that displays a random uppercase letter using the System. CurrentTimeMil 7 is () method.

Harriet O'Brien
Harriet O'Brien
Numerade Educator
03:14

Problem 23

(Find the character of an ASCII code) Write a program that receives an ASCII code (an integer between 0 and 127) and displays its character. For example, if the user enters $97,$ the program displays character a. Here is a sample run:

Harriet O'Brien
Harriet O'Brien
Numerade Educator
04:28

Problem 24

(Financial application: monetary units) Rewrite Listing 2.10 ComputeChange.java, to fix the possible loss of accuracy when converting a double value to an int value. Enter the input as an integer whose last two digits represent the cents. For example, the input 1156 represents 11 dollars and 56 cents.

Harriet O'Brien
Harriet O'Brien
Numerade Educator
05:47

Problem 25

(Financial application: payroll) Write a program that reads the following information and prints a payroll statement: Employee's name (e.g., Smith) Number of hours worked in a week (e.g., 10 ) Hourly pay rate (e.g., 6.75 ) Federal tax withholding rate (e.g., $20 \%$ ) State tax withholding rate (e.g., 9\%)

Harriet O'Brien
Harriet O'Brien
Numerade Educator
06:37

Problem 26

(Use input dialog) Rewrite Listing 2.10 , ComputeChange.java, using input and output dialog boxes.

Harriet O'Brien
Harriet O'Brien
Numerade Educator
06:04

Problem 27

(Financial application: payroll) Rewrite Exercise 2.25 using GUI input and output dialog boxes.

Harriet O'Brien
Harriet O'Brien
Numerade Educator