Table of Contents

    Lab Activity - Class 8 - Decision Control Structure

    Lab Activity - Class 8 - Decision Control Structure
    Figure: Lab Activity - Class 8 - Decision Control Structure

    LAB ACTIVITY — CLASS 8

    Lab Activity — Decision Control Structure

    Learn by Doing — Code, Test and Grow! This hands-on lab activity contains 10 real Java programs to help you master decision control statements (if, if-else, nested if, else-if ladder) using BlueJ IDE. Complete all tasks and become a confident Java programmer!

    Introduction

    Welcome to your Decision Control Structure Lab Activity! The best way to learn programming is by writing and running actual code. In this lab, you'll code 10 different programs covering all types of decision statements.

    By the end of this activity, you'll be comfortable with if, if-else, nested if, and else-if ladder — the building blocks of almost every program!

    Lab Details:
    • Duration: 2-3 lab periods
    • Total Marks: 100
    • Format: BlueJ IDE + Notebook
    • Language: Java

    Motivational Tip

    Coding is like riding a bicycle — you can watch tutorials all day, but you'll only really learn by doing it yourself. Every program you write makes you a better programmer!

    Objective

    To understand and implement decision control statements (if, if-else, nested if, else-if ladder) using Java in BlueJ IDE.

    Requirements

    What You Need

    • ✅ Computer with BlueJ IDE installed.
    • JDK (Java Development Kit).
    • ✅ Notepad or Text Editor.
    • ✅ Working Internet (optional).
    • ✅ Notebook and Pen.

    Learning Outcomes

    After This Lab, You Can

    • ⭐ Write if statements
    • ⭐ Use if-else statements
    • ⭐ Implement nested if
    • ⭐ Create else-if ladder
    • ⭐ Test edge cases
    • ⭐ Debug decision programs

    10 Lab Tasks (Complete All Programs)

    Task 1: Check Positive or Negative Number

    1

    Positive / Negative / Zero

    Uses: if-else

    Requirement: Take a number as input. If positive, print "Positive". If negative, print "Negative". If zero, print "Zero".

    import java.util.Scanner;
    
    public class CheckNumber {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter a number: ");
            int num = sc.nextInt();
    
            if (num > 0) {
                System.out.println("Positive");
            } else if (num < 0) {
                System.out.println("Negative");
            } else {
                System.out.println("Zero");
            }
        }
    }
    Test Cases Try: 5 (Positive), -3 (Negative), 0 (Zero)

    Task 2: Even or Odd Number

    2

    Even or Odd

    Uses: if-else, % operator

    Requirement: Take a number input. Check if divisible by 2. Print "Even" or "Odd".

    import java.util.Scanner;
    
    public class EvenOdd {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter a number: ");
            int num = sc.nextInt();
    
            if (num % 2 == 0) {
                System.out.println("Even");
            } else {
                System.out.println("Odd");
            }
        }
    }

    Task 3: Find Largest of Two Numbers

    3

    Largest of Two

    Uses: if-else

    Requirement: Input two numbers. Compare them. Print the larger one.

    import java.util.Scanner;
    
    public class LargestOfTwo {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter first number: ");
            int a = sc.nextInt();
            System.out.print("Enter second number: ");
            int b = sc.nextInt();
    
            if (a > b) {
                System.out.println(a + " is larger");
            } else if (b > a) {
                System.out.println(b + " is larger");
            } else {
                System.out.println("Both are equal");
            }
        }
    }

    Task 4: Find Largest of Three Numbers

    4

    Largest of Three

    Uses: Nested if or logical operators

    Requirement: Input three numbers. Find and print the largest.

    import java.util.Scanner;
    
    public class LargestOfThree {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter three numbers: ");
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
    
            if (a >= b && a >= c) {
                System.out.println(a + " is largest");
            } else if (b >= c) {
                System.out.println(b + " is largest");
            } else {
                System.out.println(c + " is largest");
            }
        }
    }

    Task 5: Voting Eligibility Check

    5

    Voting Eligibility

    Uses: if-else

    Requirement: Input age. If >= 18 print "Can Vote", else print "Cannot Vote".

    import java.util.Scanner;
    
    public class VotingEligibility {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter your age: ");
            int age = sc.nextInt();
    
            if (age >= 18) {
                System.out.println("Can Vote");
            } else {
                System.out.println("Cannot Vote");
            }
        }
    }

    Task 6: Grade Calculator

    6

    Grade Calculator

    Uses: else-if ladder

    Requirement: Input marks (0-100). Print Grade: A (>=90), B (>=75), C (>=60), D (>=40), F (<40).

    import java.util.Scanner;
    
    public class GradeCalculator {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter marks: ");
            int marks = sc.nextInt();
    
            if (marks >= 90) {
                System.out.println("Grade: A");
            } else if (marks >= 75) {
                System.out.println("Grade: B");
            } else if (marks >= 60) {
                System.out.println("Grade: C");
            } else if (marks >= 40) {
                System.out.println("Grade: D");
            } else {
                System.out.println("Grade: F");
            }
        }
    }

    Task 7: Leap Year Checker

    7

    Leap Year Checker

    Uses: if-else with modulo

    Requirement: Input year. Check if divisible by 4 (leap year). Print "Leap Year" or "Not Leap Year".

    import java.util.Scanner;
    
    public class LeapYear {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter year: ");
            int year = sc.nextInt();
    
            if (year % 4 == 0) {
                System.out.println("Leap Year");
            } else {
                System.out.println("Not Leap Year");
            }
        }
    }

    Task 8: Positive/Negative and Even/Odd

    8

    Combined Check

    Uses: Nested if

    Requirement: Input number. Check both positive/negative AND even/odd.

    import java.util.Scanner;
    
    public class CombinedCheck {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter a number: ");
            int num = sc.nextInt();
    
            if (num > 0) {
                if (num % 2 == 0) {
                    System.out.println("Positive and Even");
                } else {
                    System.out.println("Positive and Odd");
                }
            } else if (num < 0) {
                if (num % 2 == 0) {
                    System.out.println("Negative and Even");
                } else {
                    System.out.println("Negative and Odd");
                }
            } else {
                System.out.println("Zero");
            }
        }
    }

    Task 9: Simple Calculator

    9

    Simple Calculator

    Uses: else-if ladder

    Requirement: Input two numbers and operator (+, -, *, /). Perform operation and display result.

    import java.util.Scanner;
    
    public class Calculator {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter first number: ");
            double a = sc.nextDouble();
            System.out.print("Enter operator (+, -, *, /): ");
            char op = sc.next().charAt(0);
            System.out.print("Enter second number: ");
            double b = sc.nextDouble();
            double result = 0;
    
            if (op == '+') {
                result = a + b;
            } else if (op == '-') {
                result = a - b;
            } else if (op == '*') {
                result = a * b;
            } else if (op == '/') {
                if (b != 0) {
                    result = a / b;
                } else {
                    System.out.println("Cannot divide by zero!");
                    return;
                }
            } else {
                System.out.println("Invalid operator!");
                return;
            }
            System.out.println("Result: " + result);
        }
    }

    Task 10: Traffic Light Signal

    10

    Traffic Light Signal

    Uses: else-if ladder

    Requirement: Input color (RED, YELLOW, GREEN). Print action (Stop / Ready / Go).

    import java.util.Scanner;
    
    public class TrafficLight {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter light color (RED/YELLOW/GREEN): ");
            String color = sc.next().toUpperCase();
    
            if (color.equals("RED")) {
                System.out.println("Stop");
            } else if (color.equals("YELLOW")) {
                System.out.println("Ready");
            } else if (color.equals("GREEN")) {
                System.out.println("Go");
            } else {
                System.out.println("Invalid color!");
            }
        }
    }

    Submission Format

    For each task, include:

    What to Submit

    • ✅ Program title
    • ✅ Java source code (.java file)
    • ✅ Screenshot of output
    • ✅ Test cases with results
    • ✅ Observations / Notes

    Evaluation Criteria (Total: 100 Marks)

    Criteria Marks
    Program compilation20
    Correct output30
    Code quality (indentation, naming)20
    Test cases handled15
    Comments & documentation10
    Punctuality5
    Total100 Marks

    Tips for Success

    Success Tips

    • 📖 Read task carefully before coding.
    • 📝 Write pseudocode first.
    • 🧪 Test with different inputs.
    • 🏷️ Use meaningful variable names.
    • 💬 Add comments to explain logic.
    • 👨‍🏫 Ask your teacher if stuck.
    • 🔄 Practice regularly.
    • 🐛 Debug patiently.
    • 🎯 Test edge cases.
    • 📋 Keep track of your progress.

    Common Errors to Avoid

    Watch Out for These!

    • ❌ Using = instead of ==
    • ❌ Missing braces { }
    • ❌ Wrong operator precedence
    • ❌ Forgetting else block
    • ❌ Not handling zero/negative
    • ❌ Case-sensitive issues (Java is case-sensitive!)
    • ❌ Not testing edge cases
    • ❌ Using == for strings (use .equals())

    How to Run Your Programs in BlueJ

    1

    Create New Project

    Set up your workspace

    Open BlueJ → Project → New Project → Give a name → OK

    2

    Create New Class

    Add your program

    Click "New Class" → Enter class name (e.g., CheckNumber) → OK

    3

    Write Code

    Enter your Java code

    Double-click the class → Delete default code → Type your program.

    4

    Compile

    Check for errors

    Click "Compile" button. Fix any errors that appear.

    5

    Run Program

    Execute your code

    Right-click class → "void main(String[] args)" → OK → Enter inputs in Terminal.

    6

    Take Screenshot

    Document your output

    Capture the terminal output showing your program working correctly.

    Suggested Timeline

    Day Tasks to Complete
    Lab 1 - Day 1Tasks 1-3 (Positive/Negative, Even/Odd, Largest of 2)
    Lab 1 - Day 2Tasks 4-5 (Largest of 3, Voting Eligibility)
    Lab 2 - Day 1Tasks 6-7 (Grade Calculator, Leap Year)
    Lab 2 - Day 2Tasks 8-9 (Positive Even/Odd, Calculator)
    Lab 3Task 10 (Traffic Light) + Testing + Documentation

    Did You Know?

    Fun Fact

    Practice makes a programmer perfect! The more you code, the better you become at solving problems. Famous programmers write thousands of lines of code before they master programming. Every line you write today builds your future skills!

    Bonus Challenge Tasks (Optional)

    Extra Practice

    • 🌟 Check if a triangle is Equilateral, Isosceles, or Scalene (from 3 sides).
    • 🌟 Find the largest of 4 numbers.
    • 🌟 Check if a character is vowel or consonant.
    • 🌟 Print the day name based on day number (1-7).
    • 🌟 Simple ATM Program (check PIN and withdraw).
    • 🌟 BMI Calculator (Underweight/Normal/Overweight).
    • 🌟 Season Finder based on month number.
    • 🌟 Number to word conversion (1-10).

    Frequently Asked Questions

    Q1. What if my program doesn't compile?

    Read the error message carefully. Common issues: missing semicolon, missing braces, misspelled keywords. Fix and try again!

    Q2. Should I use Scanner or command-line arguments?

    For beginners, use Scanner. It's easier to test interactively. Command-line arguments are for advanced programs.

    Q3. What if I get wrong output?

    Check your logic. Print variable values to see what's happening. Use test cases to verify.

    Q4. Can I skip a task?

    You need to complete all 10 tasks for full marks. Ask for help if you're stuck.

    Q5. Should I add comments?

    Yes! Comments help you and others understand the code. They also earn marks.

    Q6. What's the best way to test?

    Test with normal cases, edge cases (0, negative), maximum values, and invalid inputs.

    Q7. Can I work with a friend?

    You can discuss ideas, but write your own code. Copying is not learning!

    Q8. What if I don't have BlueJ at home?

    BlueJ is free! Download from bluej.org. It works on Windows, Mac, and Linux.

    Key Takeaways

    • Complete all 10 Java programs.
    • Use BlueJ IDE for coding.
    • Test with multiple inputs.
    • Take screenshots of outputs.
    • Follow best practices.
    • Handle edge cases.
    • Add comments to code.
    • Total marks: 100.
    • Try bonus challenges for extra practice.
    • Have fun coding!

    Key Takeaway

    Lab activities help you learn programming concepts by hands-on practice! Code, test, debug and learn. That's the programming way!

    This lab isn't just about scoring marks — it's about becoming a real programmer. Every task teaches you something new about decision-making in code.

    Complete all 10 programs. Ask for help when needed. You will become a Java expert!

    CODE SMART, THINK CLEAR, WRITE BETTER!

    Best of Luck! Practice makes perfect. Every program you write brings you closer to being a great programmer! 👍😊

    Flowchart → Visual Thinking → Smart Solutions → Better Results! 🚀