Sei sulla pagina 1di 7

LAB EXERCISE 3

Assessment: Lab exercise


Weightage: 15% (include other lab exercises and lab tests)
Course Outcome to Achieve:
CO1: Describe the principles of object-oriented programming.
CO2: Apply the concepts of an object-oriented programming in implementing the solution.
CO3: Develop object-oriented computer programs to solve various problems.

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.

Sample of input and output:

Radius of the sphere: 15


The volume of the sphere is: 14137.167

Radius of the sphere: 5


The volume of the sphere is: 523.599
import java.util.*;

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 =

//Display the result


System.out.println("The volume of the sphere is: "
+

QUESTION 3

Complete the program that:


Ask the user to enter his or her birthday in the MM/DD/YYYY format and output the number of days
between the birthday and today. This gives the person’s age in days.
NOTE:
1. You just need to edit the statements with substring method.

To know more about substring method:


http://www.javatpoint.com/substring

import java.util.*;

class Exercise3_27
{

public static void main (String[] args)


{
Scanner scanner = new Scanner(System.in);
GregorianCalendar calBirthday = new GregorianCalendar();
GregorianCalendar calToday = new GregorianCalendar();
String inputStr;
int year, month, day;
long result;

//Get input
System.out.print("Enter your birthday in the format MM/DD/YYYY: ");
inputStr = scanner.next();

month = Integer.parseInt( inputStr.substring( 0, 1 ) );


day = Integer.parseInt( inputStr.substring( 2, 5 ) );
year = Integer.parseInt( inputStr.substring( 2 ) );

3
calBirthday = new GregorianCalendar(year, month-1, day);

//Calculate how many days have passed


//1 day = 24 hours = 8.64e+7 = 86400000
result = (calToday.getTime().getTime() -
calBirthday.getTime().getTime())/ 86400000; // 86400000 --> milliseconds in a day

//Display the result


System.out.println("You are " + result + " days old.");

Sample of input and output:


***The yellow highlighted values are input from user

Enter your birthday in the format MM/DD/YYYY: 04/30/1979


You are 13569 days old.

QUESTION 4

Write a program that:

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

where w is weight in kilograms and h is height in centimeters. A BMI of about 20 to 25 is


considered “normal.” Write an application that accepts weight and height (both integers) and
outputs the BMI.

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
{

public static void main (String[] args)


{
Scanner scanner;
double celsius, fahrenheit;

scanner = new Scanner( System.in );

//Get input
System.out.print("Enter temperature in fahrenheit (e.g. 87): ");
fahrenheit = scanner.nextDouble();

//Convert into fahrenheit


celsius = 5.00/9.00 * (fahrenheit - 32.00);

//Display the result


System.out.println();
System.out.println("Input: " + fahrenheit + "F is equivalent to");
System.out.println("Output: " + celsius + "C");

Sample of input and output:


***The yellow highlighted values are input from user

Enter your weight in kilograms (whole number e.g. 67): 45


Enter your height in centimeters (whole number e.g. 176): 151

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.

If you write double v= 3.0E-4, this is actually double v= 0.0003

Sample of input and output:


Mass of Body 1: 14.5
Mass of body 2: 48.5
Distance between Bodies 1 and 2: 2
Mass of Body 1: 14.5 grams
Mass of Body 2: 48.5 grams
Distance between the two bodies: 2.0 cm

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();

// random integers in [0, 100]

for (c = 1; c <= 10; c++) {


System.out.println(t.nextInt(100));
}
}
}

Modify the above program to generate 5 random integers between 2 to 7 (inclusive).

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

Potrebbero piacerti anche