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++;
}
}
}
π 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.");
}
}
π§ 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);
}
}
π 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.");
}
}
π 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
- 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)