DEV Community

Vigneshwaralingam
Vigneshwaralingam

Posted on

Day 6 Java Programming Session:

πŸ’» 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

🧠 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… 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;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

βœ… 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!");
}
Enter fullscreen mode Exit fullscreen mode

πŸ“œ 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

Top comments (0)