Exercises - chapter 2

Mr. Gottsacker
Computer Programming


Note: In the example code, keyboard input from the user will be represented using bold, bright green.


2.101

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

Note: In Java, 9 / 5 is 1, but 9.0 / 5 is 1.8.

Here is a sample run:


Enter a temperature in degrees Celsius: 28
28.0 Celsius is 82.4 Fahrenheit.
                


2.102

Write a program that prompts a user for for the radius and length of a cylinder. Compute the area and volume of the cylinder using the formulas below. Use a named constant for Pi with its first 8 digits after the decimal point in your program.
area = radius * radius * \(\pi\)
volume = area * length

Sample run:


Enter the cylinder's radius: 6
Enter the cylinder's length: 3
The area is: 113.0973354
The volume is: 339.2920062
                


2.103

From Wikipedia:
The speed of light in vacuum, commonly denoted \(c\), is a universal physical constant important in many areas of physics. Its exact value is 299,792,458 metres per second. It is exact because by international agreement a metre is defined as the length of the path travelled by light in vacuum during a time interval of 1/299792458 second. According to special relativity, \(c\) is the upper limit for the speed at which conventional matter and information can travel.

Write a program that prompts the user for a number of miles. Compute how many minutes it would take light to travel that distance, and display the result to the user. One mile is 1.60934 kilometers.

Use test distances similar in size to the sample run below, which is the distance from the Sun to the Earth.

Sample run:


Enter a distance in miles: 92957130.4
It takes light 8.316880735067306 minutes to travel 9.29571304E7 miles.
                


2.104

Write a program that calculates tip at a restaurant. The program should prompt the user for the cost of the meal and for a gratuity percentage. The program should compute the gratuity and total cost, and then display the results to the user.

Sample run:


Enter the subtotal: 20.0
Enter the gratuity percentage: 20
The gratuity is $4.0 and the total is $24.0
                


2.105

Write a program that prompts the user to enter a number of minutes. Compute the number of whole years and days occur over the user's input. You may assume that each year has exactly 365 days (no leap years).

Sample run:


Enter the number of minutes: 1061190000
1061190000 minutes is approximately 2019 years and 2 days.
                


2.106

Objects in orbit around Earth follow a uniform and repeating trajectory around the planet. The speed of this orbit depends on the distance from the object to the center of the Earth. Often, artifical satellites maintain a consistent distance from the Earth, forming a circular orbit. Then, at any time, the distance from the object to the center of the Earth is the radius of that circle. In this case, the orbital velocity of the satellite can be computed with the following equation:

\( \sqrt{G * m_E \over r} \)

where:
\(G\) = the universal gravitational constant = \(6.673 * 10^{-11} {N\cdot m^2/kg^2}\)
\(m_E\) = the mass of Earth = \(5.98 * 10^{24} kg\)
\(r\) = the distance from the object to the center of the Earth

The radius of the Earth is \(6.38 * 10^6 m\).

Write a program that prompts the user for a satellite's distance from the Earth's crust. Assume the satellite follows the circular orbit described above. Compute its orbital speed and display the result to the user. The example below is the orbital speed of the International Space Station.

Hint: Use the Math.pow() method or the Math.sqrt() method.

Sample run:


Enter the distance between the satellite and Earth's surface (in km): 400
A satellite that is 400000.0 meters from Earth's surface travels at a speed of 7671.782953610956 m/s
                



Exercises from textbook (pg. 66 - 68)


2.12

Aviation: finding required runway length for airplanes


2.13

Finances: calculating compound value


2.15

Calculating geometric distances


2.17

Meteorology: computing coldness