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)