Lab 2 Error
Lab 2 Error
LAB 2 – Individual
LAB OBJECTIVE:
The main objective for this lab is to introduce student to types of error, display pattern, and
create simple program to calculate BMI. Student able to identify syntax error, logic error, and
run-time error based on given coding. Student also can understand how to display pattern
using syntax System.out.print or System.out.println. Student can write simple program to
calculate BMI.
DURATION:
1 week / 3 hours
INTRODUCTION:
In coding, errors are the mistake that programmers do, and it can be fixed. There are 3 types of
error; syntax error, logic error, and run-time error. (refer note slide 137 for details).
import java.util.Scanner;
public class BMICalculator {
public static void main(string[] args) {
// Create a Scanner object to read input from the console
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter their weight in kilograms
System.out.println("Enter your weight in kilograms:");
double weight = scanner.nextDouble();
// Prompt the user to enter their height in meters
System.out.println("Enter your height in meters:");
double height = scanner.nextDouble();
// Calculate the user's BMI
double bmi = weight / height;
// Display the user's BMI
System.out.println("Your BMI is " + bmi);
// Determine the user's BMI category
if (Bmi > 18.5) {
System.out.println("You are underweight.");
} else if (bmi < 25) {
System.out.println("You are normal weight.");
} else if (bmi < 30) {
System.out.println("You are overweight.");
} else {
System.out.println("You are obese.");
}
}
}
1