Thuta Learning
ရှာဖွေရန်
ExercisesProgrammingbeginner

Practice Set 2: Collections, Inheritance & Exceptions

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Practice Set 2: Collections, Inheritance & Exceptions ကို ကိုယ်တိုင် လေ့ကျင့်ကြည့်မယ်
  • သင်ခဲ့ပြီးသား skill တွေကို practice လုပ်ပြီး ခိုင်မာအောင်လုပ်မယ်
  • အမှားရှာ၊ ပြင်တတ်၊ ကိုယ်တိုင် check လုပ်တတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

ဒီ lesson က Practice Set 1 ထက် တစ်ဆင့်ခက်တဲ့ task များဖြစ်ပြီး Intermediate/Advanced chapters တွေက collections, OOP inheritance/polymorphism, exception handling concepts တွေကို ပေါင်းစပ်ပြီး self-practice လုပ်ရမှာဖြစ်ပါတယ်။ ArrayList နဲ့ HashMap ကို real scenario တွေမှာ operate လုပ်နည်း၊ abstract class နဲ့ subclass တွေကို array ထဲထည့်ပြီး polymorphism နဲ့ loop ဖြတ်သန်းနည်း၊ ပြီးတော့ exception ကို try-catch-finally နဲ့ handle လုပ်နည်း တွေကို လက်တွေ့ လေ့ကျင့်ရမှာဖြစ်ပါတယ်။ Task တွေက ပိုခက်တာမို့ concept တစ်ခုချင်းစီကို ပြန်ပြီး review လုပ်ပြီးမှ စတင်ကြိုးစားဖို့ အကြံပြုပါတယ်။

လေ့ကျင့်ခန်းများ

Task 1: ArrayList<String> shopping list တစ်ခုဆောက်ပြီး item add/remove လုပ်ကာ enhanced for-loop နဲ့ print ထုတ်ပါ။ Task 2: HashMap<String, Integer> inventory တစ်ခုဆောက်ပြီး put(), get(), containsKey(), remove() တွေကို practice လုပ်ကာ entrySet() နဲ့ loop ဖြတ်သန်းပါ။ Task 3: abstract class Shape (abstract method area() ပါ) ဆောက်ပြီး Circle နဲ့ Rectangle subclass နှစ်ခုနဲ့ implement လုပ်ပါ - Shape[] array ထဲမှာ object နှစ်ခုထည့်ပြီး loop နဲ့ area() ကို ခေါ်ပြီး print ထုတ်ပါ (polymorphism)။ Task 4: divide(int a, int b) method ရေးပြီး b==0 ဖြစ်ရင် ArithmeticException throw လုပ်အောင် ရေးပါ - try-catch-finally block နဲ့ error ကို catch လုပ်ကာ finally block ထဲမှာ 'Operation complete' message print ထုတ်ပါ။

Code နမူနာ

java
import java.util.ArrayList;
import java.util.HashMap;

abstract class Shape {
    abstract double area();
}

class Circle extends Shape {
    double radius;
    Circle(double radius) { this.radius = radius; }
    double area() { return Math.PI * radius * radius; }
}

class Rectangle extends Shape {
    double width, height;
    Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }
    double area() { return width * height; }
}

public class Practice2 {
    public static void main(String[] args) {
        // Task 1: ArrayList shopping list
        ArrayList<String> shoppingList = new ArrayList<>();
        // TODO: add/remove items ပြီး enhanced for-loop နဲ့ print ပါ

        // Task 2: HashMap inventory
        HashMap<String, Integer> inventory = new HashMap<>();
        // TODO: put/get/containsKey/remove သုံးပြီး entrySet() နဲ့ loop ပါ

        // Task 3: Shape polymorphism
        Shape[] shapes = { new Circle(5), new Rectangle(4, 6) };
        // TODO: loop ပြီး each shape ရဲ့ area() ကို print ပါ

        // Task 4: Exception handling
        // TODO: divide(int a, int b) method ရေးပြီး b==0 ဖြစ်ရင် ArithmeticException throw လုပ်ပါ
        // try-catch-finally နဲ့ handle ပါ
    }
}
You should see
Task 4 ခုလုံးအတွက် shopping list update, inventory data, shape area နှစ်ခု, ပြီးတော့ exception error message + finally message တို့ကို console မှာ print ထုတ်ပေးရပါမယ်။

၅ မိနစ် စမ်းကြည့်

Task 3 ထဲကို Triangle subclass တစ်ခု ထပ်ထည့်ပြီး Shape[] array ထဲမှာ ထည့်ကာ polymorphism ဆက်လက် အလုပ်လုပ်ကြောင်း 5 minutes အတွင်း စမ်းကြည့်ပါ။

သတိလေးတစ်ချက်

finally block က exception ဖြစ်ဖြစ်၊ မဖြစ်ဖြစ် အမြဲ run မှာဖြစ်တာကို သတိထားပြီး resource cleanup (ဥပမာ file/connection close) လုပ်ရာမှာ အသုံးဝင်ကြောင်း မှတ်ထားပါ။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • abstract class ကို directly new Shape() နဲ့ instantiate လုပ်ဖို့ ကြိုးစားပြီး compile error တွေ့ခြင်း (subclass ကိုသာ instantiate လုပ်ရမည်)
  • try block ထဲမှာ divide() ခေါ်ခြင်းကို မထည့်ဘဲ catch block ရေးထားလို့ ArithmeticException ကို မမိခြင်း

အခု ကိုယ်တိုင် စမ်းကြည့်

Task 3 ထဲကို Triangle subclass တစ်ခု ထပ်ထည့်ပြီး Shape[] array ထဲမှာ ထည့်ကာ polymorphism ဆက်လက် အလုပ်လုပ်ကြောင်း 5 minutes အတွင်း စမ်းကြည့်ပါ။

You'll know it worked when: Task 4 ခုလုံးအတွက် shopping list update, inventory data, shape area နှစ်ခု, ပြီးတော့ exception error message + finally message တို့ကို console မှာ print ထုတ်ပေးရပါမယ်။

Practice Set 2: Collections, Inheritance & Exceptions | Thuta Learning