Description
Programming Assignment-II (Elementary Programming)
Using Keyboard Input
1. Write a java 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:
Enter a degree in Celsius: 43
43 Celsius is 109.4 Fahrenheit
2. Write a java program that reads in the radius and length of a cylinder and computes the area and volume using the following formulas:
area = radius * radius * Ο volume = area * length
Here is a sample run:
Enter the radius and length of a cylinder: 5.5 12
The area is 95.0331
The volume is 1140.4
3. Write a java 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:
Enter a value for feet: 16.5
16.5 feet is 5.0325 meters
4. Write a java 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:
Enter a number between 0 and 1000: 999
The sum of the digits is 27
5. Average acceleration is defined as the change of velocity divided by the time taken to make the change, as shown in the following formula:
π£1βπ£0
π =
π‘
Write a java program that prompts the user to enter the starting velocity v0 in meters/second, the ending velocity v1 in meters/second, and the time span t in seconds, and displays the average acceleration.
Here is a sample run:
Enter v0, v1, and t: 5.5 50.9 4.5
The average acceleration is 10.0889
6. 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 java 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:
Enter weight in pounds: 95.5
Enter height in inches: 50
BMI is 26.8573
7. Write a java 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
π΄πππ
where s is the length of a side.
Here is a sample run:
Enter the side: 5.5
The area of the hexagon is 78.5895
8. Write a java program that displays the following table. Cast floating-point numbers into integers.
a b pow(a, b)
1 2 1
2 3 8
3 4 81
4 5 1024
5 6 15625
9. Write a java program that prompts the user to enter two points (x1, y1) and (x2, y2) and displays their distance between them. The formula for computing the distance is
β(π₯2 β π₯1)2 + (π¦2 β π¦1)2. Note that you can use Math.pow (a, 0.5) to compute .
Here is a sample run:
Enter x1 and y1: 1.5 -3.4
Enter x2 and y2: 4 5
The distance between the two points is 8.764131445842194
10. Write a java program that prompts the user to enter three points (x1, y1), (x2, y2), (x3, y3) of a triangle and displays its area. The formula for computing the area of a triangle is
s = (side1 + side2 + side3)/2;
ππππ = βπ β (π β π) β (π β π) β (π β π)
Here is a sample run:
Enter three points for a triangle: 1.5 -3.4 4.6 5 9.5 -3.4
The area of the triangle is 33.6
11. Write a java 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 x(1 + ππππ‘ππ¦πΌππ‘ππππ π‘π ππ‘π)ππ’πππππππππππ β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:
Enter investment amount: 1000.56 Enter annual interest rate in percentage: 4.25
Enter number of years: 1 Accumulated value is $1043.92
12. If you have N eggs, then you have N/12 dozen eggs, with N%12 eggs left over. (This is essentially the definition of the / and % operators for integers.) Write a java program that asks the user how many eggs she has and then tells the user how many dozen eggs she has and how many extra eggs are left over. A gross of eggs is equal to 144 eggs. Extend your program so that it will tell the user how many gross, how many dozen, and how many left over eggs she has. For example, if the user says that she has 1342 eggs, and then your program would respond with
Your number of eggs is 9 gross, 3 dozen, and 10.
13. Write a java 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:
Enter the number of minutes: 1000000000
1000000000 minutes is approximately 1902 years and 214 days
Using Command-Line Arguments
14. Write a java program that takes two positive integers as command-line arguments and prints true if either evenly divides the other.
15. Write a java program that takes two int values a and b from the command line and prints a random integer between a and b.
16. Write a java program that prints the sum of two random integers between 1 and 6 (such as you might get when rolling dice).
17. Write a java program that takes three positive integers as command-line arguments and prints true if any one of them is greater than or equal to the sum of the other two and false otherwise.
18. Write a java program that takes three double values x, y, and z as command-line arguments and prints true if the values are strictly ascending or descending (x < y < z or x > y > z), and false otherwise.
19. Enter the basic salary of an employee of an organization through the command prompt. His dearness allowance (DA) is 40% of basic salary, and house rent allowance (HRA) is 20% of basic salary. Write a java program to calculate his gross salary.
20. Write a java program that takes two int values m and d from the command line and prints true if day d of month m is between 3/20 and 6/20, false otherwise.
21. Write a java program that takes a double value t from the command line and prints the value of sin (2t) + sin (3t).
22. Write a java program that calculates the monthly payments you would have to make over a given number of years to pay off a loan at a given interest rate compounded continuously, taking the number of years t, the principal P, and the annual interest rate r as command-line arguments. The desired value is given by the formula ππππ‘. Use Math.exp ().
23. Write a java program that takes three int values from the command line and prints them in ascending order. Use Math.min() and Math.max().
24. Write a java program that prints five uniform random values between 0 and 1, their average value, and their minimum and maximum value. Use Math.random(), Math.min(), and Math.max().
********************
Reviews
There are no reviews yet.