Programming Exercises
2.1 (Converting Fahrenheit to Celsius) Write a program that reads a Fahrenheit degree in double from an input dialog box, then converts it to Celsius and displays the result in a message dialog box. The formula for the conversion is as follows:
celsius = (5/9) * (fahrenheit - 32)
Hint
In Java, 5 / 9 is 0, so you need to write 5.0 / 9 in the program to obtain the correct result.
2.2 (Computing the volume of a cylinder) Write a program that reads in the radius and length of a cylinder and computes its volume using the following formulas:
area = radius * radius * p
volume = area * length
2.3 (Converting 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 meters.
2.4 (Converting 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.
2.5* (Calculating tips) Write a program that reads the subtotal and the gratuity rate, and 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.
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
No comments:
Post a Comment