Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
GENERAL INSTRUCTIONS:
1. Answer all the questions.
2. Your will have to demo ONE answer of any of the questions to your instructor in class. So,
please answer ALL questions.
3. You will be considered as ATTEND if you can demonstrate the output correctly.
QUESTION 1
Correct and add some codes the following program to produce the output as shown below:
Answer = 0.56
NOTE:
1. You must print the word “Answer = ”.
2. You are NOT allowed to change the highlighted number 5 and 9.
3. You can choose to edit the given codes especially when to format the output.
class LabTest
{
public static void main (String[ ] args)
{
int result ;
result = 5/9;
System.out.print(result);
1
}
}
QUESTION 2
Complete the below program. The program should solve the following problem:
The volume of a sphere is computed by the equation
where V is the volume and r is the radius of the sphere. Write a program that computes the volume
of a sphere with a given radius r (input from user).
NOTE:
1. Use Math class for π value. Find the appropriate Math class for this purpose.
2. The output has to be formatted to 3 decimal places.
class
{
public static void main (String[] args)
{
Scanner scanner = new Scanner (System.in);
double r, v;
//Get input
System.out.print("Radius of the sphere: ");
2
//Compute the volume
v =
QUESTION 3
import java.util.*;
class Exercise3_27
{
//Get input
System.out.print("Enter your birthday in the format MM/DD/YYYY: ");
inputStr = scanner.next();
3
calBirthday = new GregorianCalendar(year, month-1, day);
QUESTION 4
A quantity known as the body mass index (BMI) is used to calculate the risk of weight-related
health problems. BMI is computed by the formula
NOTE:
1. Accept the WHOLE NUMBER (example: 67) only as the input.
4
2. You can use the below sample program to expedite the process of finding standard codes
like getting input, printing the output and declarations.
import java.util.*;
class Exercise3_10
{
//Get input
System.out.print("Enter temperature in fahrenheit (e.g. 87): ");
fahrenheit = scanner.nextDouble();
Input -
Weight: 45kg
Height: 1.51m
Output -
5
BMI: 19
QUESTION 5
According to Newton’s universal law of gravitation, the force F between two bodies with masses
M1 and M2 is computed as
where d is the distance between the two bodies and k is a positive real number called the
gravitational constant. The gravitational constant k is approximately equal to 6.67E-8 dyn cm2/g2.
Write an application that (1) accepts the mass for two bodies in grams and the distance between
the two bodies in centimeters and (2) computes the force F. Use the standard input and output, and
format the output appropriately. For your information, the force between the Earth and the Moon
is 1.984E25 dyn. The mass of the earth is 5.983E27 g, the mass of the moon is 7.347E25 g, and
the distance between the two is 3.844E10 cm.
NOTE:
1. Use E (exponential) to represent the blue texts values.
6
Force between the two bodies: 1.1726693749999999E-5 dynes
QUESTION 6
The following code generates random numbers in range 0 to 100 (both inclusive).
import java.util.*;
class RandomNumbers {
public static void main(String[] args) {
int c;
Random t = new Random();
Sample of output:
NOTE: Since we want to generate random numbers, your output might be different from the output
shown below.
2
5
7
7
3