JAVA PROGRAMMING
Exercises 1
Elementary Programming
1.1 Identify and fix the errors in the following code:
1 public class Test {
2 public void main(string[] args) {
3 int i;
4 int k = 100.0;
5 int j = i + 1;
6 [Link]("j is " + j + " and
8 k is " + k);
9 }
10 }
1.2 Write a program that reads a Celsius degree in double 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
1.3 Write a program that reads in the radius and length of a cylinder and computes
volume using the following formulas:
area = radius * radius * π
volume = area * length
1.4 Write a program that reads a number in feet, converts it to meters, and displays
the result. One foot is 0.305 meter.
1.5 Write a program that reads the subtotal and the tip rate, then computes the tip
and total. For example, if the user enters 10 for subtotal and 15% for tip rate, the
program displays $1.5 as tip and $11.5 as total.
1.6 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.
1.7 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.
1.8 Write a program that receives an ASCII code (an integer between 0 and 128)
and displays its character. For example, if the user enters 97, the program
displays character a.
1.9 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 display the BMI. Note that one pound is 0.45359237
kilograms and one inch is 0.0254 meters.
1.10 Write a program that prompts the user to enter three sides, side1, side2, and
side3, of a triangle and displays its area. The formula for computing the area of
a triangle is: