practical Assignment1 (1)
practical Assignment1 (1)
You are required to practice handling inputs in Java using the classes Scanner (java.util package) ,
Console and BufferedReader(both from java.io package)
a) You can convert temperature from degrees Celsius to degrees Fahrenheit by multiplying by 9/5
and adding 32. Write a java application that allows the user to enter a floating-point number
representing degrees Celsius, and then displays the corresponding degrees Fahrenheit.
Write an application that reads the user’s weight in kilograms and height in meters, then calculates and
displays the user’s body mass index. In addition the application should display the following information
to the user:
BMI VALUES
Obese: 30 or greater
c) Write a program that requests from the user the radius and the height of a cylinder then calculates
and displays the surface area and volume of the cylinder.
d) A large company pays its salespeople on a commission basis.The salespeople receive Ksh2000 per
week plus 9% of their gross sales for that week. For example, a salesperson who sells Ksh150000 worth
of merchandise in a week receives Ksh2000 plus 9% of Ksh150000. You’ve been supplied with a list of
the items sold by each salesperson. The values of these items are as follows:
Item Value
1 1390.99
2 1290.75
3 1990.95
4 1350.89
Develop a java application that inputs one salesperson’s items sold for last week and calculates and
displays that salesperson’s earnings. There’s no limit to the number of items that can be sold.
Control Structures
1) Using nested if else statements write a java program that implements the following grading
policy: 90 and above-A, 80-89-B, 70-79-C, 60-69-D, 50-59-E, else –F. The program should ask for
input from the user and display the letter grade on the screen
2) Write a java program using switch statement that implements a simple calculator program. The
program should receive input of two numbers and an arithmetic operator(+,-,*,/ ,%) and
perform the corresponding operation.
3) Explain, with aid of examples, the use of break and continue statements in Java. Write an
application that presents to the user several operations in the form of a menu. The user can
continue to perform any of the operation until he/she presses a certain key.
4) Write a java application that calculates the product of the odd integers from 1 to 15.
5) The factorial of a positive integer n(written n! ) is equal to the product of the positive integers
from1 to n. Write an application that calculates the factorials of 1 through 20. Use type long.
Display the results in tabular format.
6) Write a java application that uses nested looping to print the following table of values:
N 10*N 100*N 1000*N
1 10 100 1000
2 20 200 2000
3 30 300 3000z
4 40 400 4000
5 50 500 5000
7) Write an application that displays the following patterns separately, one below the other. Use
nested for loops to generate the patterns.
* ********** ********** *
** ********* ********* **
*** ******** ******** ***
**** ******* ******* ****
***** ****** ****** *****
****** ***** ***** ******
******* **** **** *******
******** *** *** ********
********* ** ** *********
********** * * **********
Methods
Q1) Write a program including a method quadratic that uses the formula below to determine the roots
of a quadratic equation. Use the main method to input the values(use Scanner, Console or
BufferedReader) that represent the coefficients in a quadratic equation (a, b, and c), call the quadratic
method and display the roots.
Q2) Write and test a class named MyStaticMethods that has the following static methods:
i) prime(): Takes an integer as argument and returns true if it’s a prime number
ii) gcd(): Takes two integer parameters and returns their greatest common divisor
iii) sphereVolume(): Takes a double radius as argument and returns the volume of a sphere.
Use the formula:4/3*PI*r3 where r is the radius
iv) isLeapYear(): takes an integer argument to represent a year and returns true if the year
is a leap year and false otherwise.
v) factorial(): Takes an integer argument and returns its factorial. Create both the iterative
and recursive versions of the method.