💻 Learning Java with Storytelling


🪙 1. 7 Hills & Gold Deduction

I crossed 7 hills, and at every hill I gave 50% of my gold to the security.

Finally, I reached with 64 gold coins in my bag.

What was the actual gold I had before crossing the first hill?

public class HillGold {
    public static void main(String[] args) {
        int hills = 7;
        double gold = 64;

        while (hills > 0) {
            gold = gold * 2;
            hills = hills - 1;
        }

        System.out.println("Initially, I had gold: " + gold);
    }
}

🧠 Logic: We know the final gold, but each hill took half → we reverse multiply by 2, seven times.

✅ Output: 8192

📌 Thanks to Neelakandan bro for this magical loop trick!


🍫 2. Chocolate Wrapper Exchange"The Sweet Tooth Story"

I bought 15 chocolates. For every 3 wrappers, I get 1 chocolate.

How many chocolates can I totally eat?

public class ChocolateWrapper {
    public static void main(String[] args) {
        int chocolate = 15;
        int wrapper = 15;

        while (wrapper >= 3) {
            int extra = wrapper / 3;
            chocolate = chocolate + extra;
            wrapper = extra + (wrapper % 3);
        }

        System.out.println("Total chocolates I can eat: " + chocolate);
    }
}

✅ Output: 22


🥞 3. Dosa Division Logic"Mom’s Dosa Mystery" [TBD]

I cooked N dosas. I ate 1/3 in morning, 1/3 in afternoon, 1/3 in night.

After 3 meals, 8 dosas are still left. How many dosas I originally cooked?

public class DosaMystery {
    public static void main(String[] args) {
        int dosa = 1;

        while ((dosa - dosa/3 - dosa/3 - dosa/3) != 8) {
            dosa = dosa + 1;
        }

        System.out.println("Originally cooked dosas: " + dosa);
    }
}

✅ Output: 27


🌳 4. Tree Plantation with 3ft & 5ft Rules"The Raja’s Final Test"

Raja gave 100ft land.

One prince said: “Plant tree every 3 ft

Another: “Plant every 5 ft

Trees should be placed only if distance is divisible by 3 or 5

public class RajaTrees {
    public static void main(String[] args) {
        int feet = 1;

        while (feet <= 100) {
            if (feet % 3 == 0 && feet % 5 == 0) {
                System.out.println("Plant tree at: " + feet + "ft");
            }
            feet = feet + 1;
        }
    }
}

✅ Trees planted at: 3, 5, 6, 9, 10... up to 100


✍️ Class Learnings

🔸 print vs println vs printf

Method Use Case
print() Same line output
println() Goes to next line after print
printf() Format based (like %d, %.2f)

🛠 printf() is mostly used when printing formatted values like percentages, decimals, etc.


🔍 What is “Segregated”?

Segregated = separated or grouped apart

E.g. Boys and Girls are segregated in hostel rooms.


🧥 What is a Wrapper (commonly)?

Wrapper means covering or boxing.

In Java: Wrapper Classes like Integer, Double are used to wrap primitive data types into objects.


🪙 In the Hill-Gold Story…

Why condition on hills, not gold?

Because we don’t know how much gold we had, but we know how many hills (7) — so loop is based on count of hills, not gold.

✅ This logic was super useful. Thanks to Neelakandan bro once again 🙏


🧾 What is Indentation?

Proper alignment of code for better readability

Like:

while (true) {
    System.out.println("Indented!");
}

📜 Descriptive Actions

Clear steps written in story or explainable format.

E.g., Instead of saying "Run loop", say “Check each hill and double the gold”.


🗣️ What is Toastmaster?

Toastmasters is a global club to improve public speaking and leadership skills

Official Site: toastmasters.org


🌐 Learning & Practice Sites for Java & Logic:


💬 Fun Fact – Spaces in Different Languages

Language Space Style
Java Use " " inside strings
Python Use , or " " in print()
JavaScript Use " "
C Use " " inside printf()

Also:

  • Java: default end = println means \n
  • Python: default end = space (end=' ' in print())
  • C: uses \n manually with printf