Thuta Learning
ExercisesProgrammingbeginner

Practice Set 2: Collections, Inheritance & Exceptions

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Work through Practice Set 2: Collections, Inheritance & Exceptions on your own
  • Practice the skills you've already learned to make them solid
  • Get better at finding bugs, fixing them, and checking your own work

Take a Moment to Think

These tasks are a step up from Practice Set 1 — this time you'll practice on your own by combining the collections, OOP inheritance/polymorphism, and exception handling concepts from the Intermediate/Advanced chapters. You'll get hands-on practice working with ArrayList and HashMap in real-world scenarios, putting abstract classes and subclasses into an array and looping through them with polymorphism, and handling exceptions with try-catch-finally. Since these tasks are harder, we recommend reviewing each concept before diving in.

Exercises

Task 1: Build an ArrayList<String> shopping list, add/remove items, and print it out using an enhanced for-loop. Task 2: Build a HashMap<String, Integer> inventory, practice put(), get(), containsKey(), and remove(), and loop through it with entrySet(). Task 3: Build an abstract class Shape (with an abstract area() method) and implement it with two subclasses, Circle and Rectangle — put both objects into a Shape[] array, then loop through it, calling area() and printing the result (polymorphism). Task 4: Write a divide(int a, int b) method that throws an ArithmeticException when b==0 — catch the error with a try-catch-finally block, and print an 'Operation complete' message in the finally block.

Code Example

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
For all 4 tasks, your program should print the updated shopping list, the inventory data, the two shape areas, and the exception error message plus the finally message to the console.

Try It in 5 Minutes

Add one more Triangle subclass to Task 3, drop it into the Shape[] array, and spend 5 minutes confirming that polymorphism still works as expected.

A Quick Heads-Up

Keep in mind that the finally block always runs, whether an exception occurs or not — that's exactly why it's useful for resource cleanup, like closing a file or connection.

Easy traps

  • Trying to instantiate an abstract class directly with new Shape() and hitting a compile error (only its subclasses can be instantiated)
  • Writing a catch block without actually calling divide() inside the try block, so the ArithmeticException never gets caught

Now Try It Yourself

Add one more Triangle subclass to Task 3, drop it into the Shape[] array, and spend 5 minutes confirming that polymorphism still works as expected.

You'll know it worked when: For all 4 tasks, your program should print the updated shopping list, the inventory data, the two shape areas, and the exception error message plus the finally message to the console.

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