π» 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 likeInteger
,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:
- β LeetCode
- β HackerRank
- β HackerEarth
- β CodeChef
π¬ 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=' '
inprint()
) - C: uses
\n
manually withprintf
Top comments (0)