0% found this document useful (0 votes)
2 views7 pages

Week 2 Lab Tutorial (Step-By-Step)

The Week 2 Lab Tutorial for the BIC1224 course focuses on decision-making in Java programming, covering concepts such as if statements, switch statements, and input validation. Students will learn to write conditions, validate user input, and build simple decision-based programs like grading systems and calculators. The tutorial includes step-by-step tasks and exercises to reinforce these concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Week 2 Lab Tutorial (Step-By-Step)

The Week 2 Lab Tutorial for the BIC1224 course focuses on decision-making in Java programming, covering concepts such as if statements, switch statements, and input validation. Students will learn to write conditions, validate user input, and build simple decision-based programs like grading systems and calculators. The tutorial includes step-by-step tasks and exercises to reinforce these concepts.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Week 2 Lab Tutorial (Step-by-Step)

Course: BIC1224 – Object-Oriented Programming (Java)

Topic: Decision Making — if, if-else, else-if, switch, Logical Operators, Input

Learning Outcomes (Week 2 Lab)


By the end of this lab, students will be able to:

● Write conditions using relational and logical operators


● Use if, if-else, nested if, and else-if ladder correctly
● Use switch for menu-based programs
● Validate user input (empty input, range check)
● Build simple real-world decision-based programs (grading, login, menu calculator)

Part A — Setup & Starter Template


Step 1: Create a New Project
1. Open your IDE (NetBeans/IntelliJ/Eclipse).
2. Create a new Java project:
○ Project Name: BIC1224_Week2
○ Main Class: Main

Step 2: Use This Starter Template


Paste this skeleton first (you will reuse it in tasks):

import [Link];

public class Main {


public static void main(String[] args) {
Scanner input = new Scanner([Link]);

// Your code here

[Link]();
}
}
Part B — Core Concepts Practice (Step-
by-Step)
Task 1: Relational Operators Practice
Step 3: Read Two Numbers and Compare
1. Take two integers as input.
2. Print which one is larger (or equal).

[Link]("Enter first number: ");


int a = [Link]();

[Link]("Enter second number: ");


int b = [Link]();

if (a > b) {
[Link]("First number is larger.");
} else if (a < b) {
[Link]("Second number is larger.");
} else {
[Link]("Both numbers are equal.");
}

✅ You practiced: > < ==

Task 2: if-else with Range Checking


Step 4: Validate Marks (0 to 100)
1. Input marks
2. If marks < 0 or > 100 → invalid
3. Otherwise → valid marks

[Link]("Enter marks (0-100): ");


int marks = [Link]();
if (marks < 0 || marks > 100) {
[Link]("Invalid marks! Must be between 0 and 100.");
} else {
[Link]("Valid marks.");
}

✅ You practiced: || (OR)

Task 3: else-if Ladder (Grading System)


Step 5: Build Grade Calculator
Rules (example):

● 80–100: A
● 70–79: B
● 60–69: C
● 50–59: D
● Below 50: F

[Link]("Enter marks (0-100): ");


int m = [Link]();

if (m < 0 || m > 100) {


[Link]("Invalid marks!");
} else if (m >= 80) {
[Link]("Grade: A");
} else if (m >= 70) {
[Link]("Grade: B");
} else if (m >= 60) {
[Link]("Grade: C");
} else if (m >= 50) {
[Link]("Grade: D");
} else {
[Link]("Grade: F");
}

✅ You practiced: ordered decision logic


Task 4: Nested if (Login Validation)
Step 6: Validate Username and PasswordStep 6: Validate Username and
Password

Use equals() for Strings.

Rules:

● Username must be "admin"


● Password must be "1234"

[Link](); // consume newline if needed

[Link]("Enter username: ");


String user = [Link]();

[Link]("Enter password: ");


String pass = [Link]();

if ([Link]("admin")) {
if ([Link]("1234")) {
[Link]("Login successful!");
} else {
[Link]("Wrong password!");
}
} else {
[Link]("Unknown username!");
}

✅ Key lesson: use .equals() not ==

Part C — switch Statement & Menus


Task 5: Menu-driven Calculator using switch
Step 7: Display Menu and Take Choice
1. Input two numbers
2. Input operation choice
3. Use switch
[Link]("Enter first number: ");
double x = [Link]();

[Link]("Enter second number: ");


double y = [Link]();

[Link]("Choose operation:");
[Link]("1. Add");
[Link]("2. Subtract");
[Link]("3. Multiply");
[Link]("4. Divide");

[Link]("Enter choice (1-4): ");


int choice = [Link]();

switch (choice) {
case 1:
[Link]("Result: " + (x + y));
break;
case 2:
[Link]("Result: " + (x - y));
break;
case 3:
[Link]("Result: " + (x * y));
break;
case 4:
if (y == 0) {
[Link]("Error: Cannot divide by zero.");
} else {
[Link]("Result: " + (x / y));
}
break;
default:
[Link]("Invalid choice!");
}

✅ You practiced: switch, break, default, and safe division


Part D — Input Validation (Important
Practice)
Task 6: Validate Non-negative AgeTask 6: Validate Non-
negative Age
Step 8: Age Check
[Link]("Enter age: ");
int age = [Link]();

if (age < 0) {
[Link]("Invalid age!");
} else if (age < 18) {
[Link]("Minor");
} else {
[Link]("Adult");
}

Exercises (Week 2)
Exercise 1 — Leap Year Checker
Input year and determine leap year:

● Leap if divisible by 400 OR divisible by 4 but not by 100

Hint:

if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) ...

Exercise 2 — Electricity Bill Calculator


Rates (example):

● 1–100 units → 5 BDT/unit


● 101–200 units → 7 BDT/unit
● Above 200 → 10 BDT/unit

Input units → output bill amount.


Exercise 3 — BMI Category
Input:

● weight (kg)
● height (m)

Compute BMI:

● bmi = weight / (height*height)

Category:

● bmi ≥ 30 → Obese
● 25–29.9 → Overweight
● 18.5–24.9 → Normal
● < 18.5 → Underweight

Exercise 4 — Day Finder using switch


Input number (1–7) → output:

● 1: Sunday … 7: Saturday

Exercise 5 (Challenge) — ATM Menu (switch + if)


Menu:

1. Balance Check
2. Deposit
3. Withdraw
4. Exit

Rules:

● Balance starts with 1000


● Withdraw must not exceed balance
● Deposit must be positive

You might also like