Computational Thinking & Object-Oriented Design

গণনা চিন্তা ও অবজেক্ট-অরিয়েন্টেড ডিজাইন

Read: ~30 min Beginner 5 practice problems Live code runner

1. Think Before You Type

Before any syntax, great Java programs start in the head. Computational thinking is the habit of breaking a messy real-world problem into pieces a computer can handle — decomposition, abstraction, pattern recognition, and algorithm design. Object-Oriented Design (OOD) is the Java-flavoured way of doing that: you model the world as objects that carry state and respond to messages.

ভালো Java প্রোগ্রাম লিখতে শুরু করার আগে কীবোর্ডে হাত রাখা নয়, প্রশ্নটি মাথায় ভাঙতে হয়। গণনা চিন্তা (Computational Thinking) মানে একটি জটিল বাস্তব সমস্যাকে computer-বোধ্য ছোট ছোট অংশে ভাগ করা — decomposition, abstraction, pattern ও algorithm। Java-তে এটি করার উপায় হলো Object-Oriented Design (OOD): বাস্তব জগৎকে object-এর রূপে মডেল করা, যাদের state ও behavior আছে।

This module gives you the mental model you will use for the next 49 lectures — and the rest of your Java career.

2. The Four Pillars of Computational Thinking

Jeannette Wing's classic framing — four skills every programmer uses daily:

🧩 Decomposition (বিভাজন)

Break a big problem into small, self-contained sub-problems. "Build a bKash clone" becomes: user, wallet, transaction, ledger.

🎨 Abstraction (বিমূর্তকরণ)

Keep what matters, hide what doesn't. A Car for a ride-hailing app needs location and capacity — not tire pressure.

🔁 Pattern Recognition (প্যাটার্ন)

Different problems, same shape. "Sort users by name" and "sort products by price" are the same algorithm — only the comparator changes.

📜 Algorithm Design (অ্যালগরিদম)

Write the exact steps. Steps must be unambiguous, finite, and correct for all inputs — not just the easy ones.

চারটি চিন্তাশক্তি — (১) বিভাজন: বড় সমস্যাকে ছোট করুন। (২) abstraction: যা দরকার নেই বাদ দিন। (৩) pattern: একই রকম সমস্যা চিনুন। (৪) algorithm: ধাপগুলি স্পষ্ট করুন।

3. From Problem to Design — Nouns & Verbs

A classic OOD trick: read the problem statement, underline nouns — those become classes. Underline verbs — those become methods.

"A student at NSU can borrow a book from the library. Each book has a title and an ISBN. The library keeps track of how many copies are available."

  • Nouns: Student, Book, Library → candidate classes.
  • Verbs: borrow, keep track → candidate methods.
  • Adjectives (title, ISBN, copies): candidate fields.
একটি পুরনো কিন্তু শক্তিশালী কৌশল — সমস্যার বিবরণ পড়ে noun (বিশেষ্য)-গুলো class হিসেবে নিন, verb (ক্রিয়া)-গুলো method হিসেবে। adjective (বিশেষণ) হবে field। "Student NSU-এ book borrow করে" — Student, Book, Library class; borrow method; title, ISBN field।
Problem Statement → Classes & Methods Problem (English) "A student borrows a book from the library; each book has a title & ISBN." Classes (nouns) Student · Book · Library Methods (verbs) borrow(), return() addCopy(), count() Fields (adjectives) title, isbn, copies name, id (state each object holds) Figure 2.1 — Problem-এর noun → class, verb → method, adjective → field।

4. Class vs Instance — The Recipe and the Cake

A class is a blueprint. An instance (object) is a concrete thing built from that blueprint. A Book class describes what every book has; new Book(...) creates an actual book in memory.

Class হলো ব্লুপ্রিন্ট বা রেসিপি; object হলো সেই রেসিপি থেকে তৈরি আসল কেক। একই class থেকে অসংখ্য object বানানো যায়, প্রতিটির নিজস্ব state থাকে।
Main.java
// Class = blueprint. Instance = real object in memory.
class Book {
    String title;
    String isbn;
    int copies;

    Book(String t, String i, int c) {
        title = t; isbn = i; copies = c;
    }

    void describe() {
        System.out.println(title + " (" + isbn + ") — " + copies + " copies");
    }
}

class Main {
    public static void main(String[] args) {
        Book b1 = new Book("Effective Java", "978-0134685991", 3);
        Book b2 = new Book("Clean Code", "978-0132350884", 5);
        b1.describe();
        b2.describe();
    }
}

5. The Four OOP Pillars — A Preview

You will meet each of these in detail in Phase 3. For now, the shape of the road ahead:

  • Encapsulation — hide internal state; expose a safe API.
  • Inheritance — reuse a class by extending it (class Dog extends Animal).
  • Polymorphism — one interface, many behaviors at runtime.
  • Abstraction — describe what a type does, not how.
OOP-এর চারটি স্তম্ভ — Encapsulation (তথ্য লুকানো), Inheritance (বৃত্তি সম্প্রসারণ), Polymorphism (এক interface, বহু রূপ), Abstraction (কী করে তা বলুন, কীভাবে করে নয়)।

6. UML — A One-Page Design Language

Before writing code, sketch your classes on paper or a whiteboard. UML (Unified Modeling Language) gives a simple notation — you don't need the full spec, just the class box:

Book - title : String - isbn : String - copies : int + borrow() : boolean + returnCopy() : void + describe() : void Library - books : List<Book> - name : String + addBook(b) : void + find(isbn) : Book + count() : int has-a Figure 2.2 — Simple UML class notation: fields, methods, visibility (-/+), relationship (has-a)।
UML class box-এর তিনটি অংশ — class-এর নাম, field, method। - মানে private, + মানে public। Library-র কাছে একাধিক Book থাকে — তাই "has-a" সম্পর্ক।

7. A Small Real Design — bKash Wallet Sketch

Let's decompose "a simple bKash-style wallet" into classes and run a tiny prototype:

Main.java
// Nouns: User, Wallet, Transaction  —  Verbs: deposit, send
class Wallet {
    String ownerName;
    double balance;

    Wallet(String name, double opening) {
        ownerName = name; balance = opening;
    }

    void deposit(double amount) {
        balance += amount;
        System.out.println(ownerName + " deposited ৳" + amount);
    }

    boolean sendTo(Wallet other, double amount) {
        if (amount > balance) { System.out.println("Not enough balance"); return false; }
        balance -= amount;
        other.balance += amount;
        System.out.println(ownerName + " → " + other.ownerName + " : ৳" + amount);
        return true;
    }
}

class Main {
    public static void main(String[] args) {
        Wallet arif = new Wallet("Arif", 500.0);
        Wallet nila = new Wallet("Nila", 0.0);
        arif.deposit(200);
        arif.sendTo(nila, 300);
        System.out.println("Arif balance: ৳" + arif.balance);
        System.out.println("Nila balance: ৳" + nila.balance);
    }
}
Observation — fewer than 30 lines, but already a recognizable wallet: two objects exchanging money, each preserving its own balance. That is OOP's promise.

৩০ লাইনেরও কম, কিন্তু দুটি object নিজ নিজ balance ধরে রেখে লেনদেন করছে — এটাই OOP-এর শক্তি।

8. Vocabulary (শব্দভাণ্ডার)

TermMeaningবাংলায়
ClassA blueprint describing fields and behavior.object তৈরির blueprint।
Object / InstanceA concrete value produced from a class.class থেকে তৈরি বাস্তব object।
Field (attribute)A variable living inside an object.object-এর ভেতরে থাকা variable।
MethodA behavior an object can perform.object-এর কাজ বা ক্রিয়া।
ConstructorSpecial method that initializes a new object.নতুন object-এর প্রথম সেটআপ।
UMLUnified Modeling Language — visual design notation.design আঁকার আদর্শ notation।
AbstractionIgnoring irrelevant detail to focus on essentials.অপ্রয়োজনীয় detail বাদ দিয়ে মূল বিষয়ে মনোযোগ।

9. Practice Problems

Try each on your own first, then expand the answer.

  1. Read the statement "A student at NSU registers for a course taught by a teacher." List the classes, fields, and methods.
    উপরের বাক্য থেকে class, field এবং method খুঁজে বের করুন।
    ✨ Show Answer (উত্তর দেখুন)

    Classes: Student, Course, Teacher. Fields: Student{id, name}, Course{code, credits}, Teacher{name, department}. Methods: Student.register(Course), Course.addStudent(Student), Teacher.teach(Course).

    Class: Student, Course, Teacher। Field: id, name, code, credits, department। Method: register, addStudent, teach।

  2. Create a Student class with fields name and cgpa, and a method describe(). Create two students and print them.
    Student class তৈরি করে দুটি object বানান এবং তাদের তথ্য print করুন।
    ✨ Show Answer (উত্তর দেখুন)
    Main.java
    class Student {
        String name;
        double cgpa;
        Student(String n, double c) { name = n; cgpa = c; }
        void describe() {
            System.out.println(name + " — CGPA " + cgpa);
        }
    }
    class Main {
        public static void main(String[] args) {
            new Student("Arif", 3.72).describe();
            new Student("Nila", 3.91).describe();
        }
    }
  3. Extend the Wallet example to reject deposits of zero or negative amount. Run it.
    Wallet-এ শূন্য বা ঋণাত্মক deposit বন্ধ করুন।
    ✨ Show Answer (উত্তর দেখুন)
    Main.java
    class Wallet {
        String owner; double balance;
        Wallet(String o, double b) { owner = o; balance = b; }
        void deposit(double amt) {
            if (amt <= 0) { System.out.println("Invalid amount"); return; }
            balance += amt;
            System.out.println(owner + " balance: ৳" + balance);
        }
    }
    class Main {
        public static void main(String[] args) {
            Wallet w = new Wallet("Arif", 100);
            w.deposit(50);
            w.deposit(0);
            w.deposit(-10);
        }
    }
  4. Explain in 3 sentences why abstraction is essential when designing a ride-hailing app (like Pathao).
    Pathao-এর মতো অ্যাপে abstraction কেন জরুরি — ৩ বাক্যে লিখুন।
    ✨ Show Answer (উত্তর দেখুন)

    Answer: (1) A ride-hailing app must track vehicles, drivers, riders, and trips — without abstraction the code quickly becomes unmanageable. (2) Abstraction lets us treat every Vehicle the same way (get location, compute ETA) regardless of whether it is a bike, car, or CNG — the caller doesn't care. (3) That simplifies matching, pricing, and UI code, and lets a new vehicle type be added without rewriting the rest of the system.

  5. Design a class Transaction with from, to, amount, timestamp and a summary() method. Create one and print it.
    Transaction class design করে একটি object তৈরি ও print করুন।
    ✨ Show Answer (উত্তর দেখুন)
    Main.java
    class Transaction {
        String from, to; double amount; long ts;
        Transaction(String f, String t, double a, long s) {
            from = f; to = t; amount = a; ts = s;
        }
        void summary() {
            System.out.println("[#" + ts + "] " + from + " → " + to + " : ৳" + amount);
        }
    }
    class Main {
        public static void main(String[] args) {
            new Transaction("Arif", "Nila", 250.0, System.currentTimeMillis()).summary();
        }
    }

Summary — Module 02

Great Java starts in the head. Decompose the problem, abstract away noise, find patterns, design an algorithm — then map nouns to classes, verbs to methods, adjectives to fields. A class is a blueprint; an instance is a real object. UML is a one-page design language; use it. The four OOP pillars — encapsulation, inheritance, polymorphism, abstraction — are the tools we will spend the next several modules mastering.

কোড লেখার আগে চিন্তা করুন — decompose, abstract, pattern, algorithm। Noun → class, verb → method, adjective → field। UML দিয়ে design আঁকুন। OOP-এর চারটি স্তম্ভ — encapsulation, inheritance, polymorphism, abstraction।

Next Module → Setting up your JDK, IDE, Maven, and Gradle — the tools you will use every day.