You are on page 1of 3

LAB EXERCISE WEEK 2 (BASIC JAVA PROGRAM)

Question 1

Complete the following Java program by answering Question 1 (a), (b) and (c).

import java.util.Scanner;
import java.lang.Math;
import java.text.*;

public class BMI


{
public static void main(String args[])
{
Scanner keyboard = new Scanner(System.in);
int age;
double weight,bmi;
float height;
char gender;
String name,status;

// 1 (a)

//1 (b)

//1 (c)

DecimalFormat df = new DecimalFormat("0.00");


// format double
System.out.println("Bmi:"+ df.format(bmi) +"(" +status+ ")");
}

a) Write Java statements to receive all the input from user.


System.out.print(“Enter your age: ”);
age=keyboard.nextInt();
System.out.print(“Enter your weight: ”);
weight=keyboard.nextDouble();
System.out.print(“Enter your height: ”);
height=keyboard.nextFloat();
System.out.print(“Enter your name:”);
name= keyboard.nextLine();
System.out.print(“Enter your gender: ”);
gender=keyboard.nextLine();
b) Write Java statements to calculate the BMI based on the values entered by user in
Question 1(a). The formula is:
BMI = weight / height2
bmi=(weight)/(height*height);
c) Determine the BMI status based on the BMI value calculated in Question 1(b) by referring
to the following table:
BMI value BMI status

< 18.5 Underweight

18.5 – 24.9 Normal

25 – 29.9 Overweight

30 – 34.9 Obese

>35 Extremely Obese

if(bmi>=35){status=”Extremely Obese”;}
else if (bmi>=30 && bmi <=34.9){status=”Obese”;}
else if (bmi>=25 && bmi<=29.9){status=”Overweight”;}
else if (bmi>=18.5&& bmi <= 24.9){status=”Normal”;}
else if (bmi<=18.5 && bmi >0){status=”Underweight”;}
else{status=”Invalid”;}

Question 2

Given the following program:

import java.util.Scanner;

public class scanner


{
public static void main(String args[])
{
int num1, num2, total;

Scanner insert = new Scanner(System.in);

System.out.print("Enter number 1: ");


num1 = insert.nextInt();

System.out.print("Enter a number 2: ");


num2 = insert.nextInt();

total = num1 + num2;

System.out.println(num1 + " + " + num2 + " = " + total);


}
}
Rewrite the program using Java dialog box input and output statements.

You might also like