DEV Community

Vigneshwaralingam
Vigneshwaralingam

Posted on

๐Ÿง  Day 5 โ€” Programming With Stories & Logic

Java Programming Session


โ˜• Day 5 Java Programming Session

  • ๐Ÿ“œ Story format for every problem
  • ๐Ÿง  Real-world inspiration
  • ๐Ÿ“š Bonus facts (Shakuntala Devi, Python versions, Arvind Gupta)

๐ŸŒŸ 1. Printing the First N Numbers โ€” "The Birthday Party Story"

Ravi wanted to invite his 10 friends to his birthday party.

He wrote each of their invitation numbers on cards using his robot โ€” one by one.

public class PartyInviter {
    public static void main(String[] args) {
        int i = 1;
        int n = 10;

        while (i <= n) {
            System.out.println("Invitation sent to Friend #" + i);
            i++;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ Moral: Simple repetitions can solve daily problems smartly.


๐Ÿธ 2. The Frog Wheel Theory โ€” "The Frog in the Well"

A frog was stuck inside a 20-meter well.

Each day it jumps up 3 meters, but by the next morning, itโ€™s back 2 meters lower.

Letโ€™s simulate its daily journey.

public class FrogInWell {
    public static void main(String[] args) {
        int height = 20;
        int pos = 0;
        int day = 0;

        while (pos < height) {
            day = day + 1;
            pos = pos + 3;
            pos = pos - 2;
        }

        System.out.println("Frog escapes in " + day + " days.");
    }
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Realization: Progress is still progress, even if slow!


๐Ÿ‘‘ 3. Raja Manthiri & Magical Paddy Story โ€” "The Grain Doubling Tale"

The minister asked King Manghiri for 1 grain on Day 1, doubling it each day.

The king agreed for 10 days, unaware of the magic of doubling.

public class GrainDoubling {
    public static void main(String[] args) {
        int day = 1;
        int totalDays = 10;
        long grain = 1;
        long total = 0;

        while (day <= totalDays) {
            total = total + grain;
            grain = grain * 2;
            day = day + 1;
        }

        System.out.println("Total grains after 10 days: " + total);
    }
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ Moral: Even the smallest request, when doubled, becomes massive!


๐Ÿ•ต๏ธ 4. Shakuntala Devi Puzzle โ€” "The Thief & Police Story"

A thief runs at 2 m/s, and a police officer at 5 m/s, starting 40 meters apart.

Letโ€™s track the seconds until the police catches up.

public class ThiefPolice {
    public static void main(String[] args) {
        int thief = 0;
        int police = -40;
        int time = 0;

        while (police < thief) {
            thief = thief + 2;
            police = police + 5;
            time = time + 1;
        }

        System.out.println("Police catches thief in " + time + " seconds.");
    }
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Œ Moral: Speed difference matters more than distance.


๐Ÿ” Extra Learnings

๐ŸŽฏ Shakuntala Devi โ€“ โ€œThe Human Computerโ€

  • Born: November 4, 1929 (India)
  • Solved 13-digit math in just 28 seconds
  • Guinness World Record holder
  • Book: Figuring: The Joy of Numbers

โ€œNumbers have life; theyโ€™re not just symbols on paper.โ€


๐Ÿ Python on Linux โ€“ Whatโ€™s Fabricated?

  • Linux often includes Python 2 and Python 3
  • You may need to install Python 3 manually:
  sudo apt install python3
Enter fullscreen mode Exit fullscreen mode
  • Fabricated = artificially constructed or created.

๐Ÿ‘จโ€๐Ÿ”ง Who is Arvind Gupta?

  • Indian scientist, teacher & toy inventor
  • Builds science toys from trash โ™ป๏ธ
  • TED Speaker, inspires learning through fun
  • Website: arvindguptatoys.com

โ€œChildren learn by doing, not by mugging.โ€


Top comments (0)