Java Programming Day 5

Day 5: My Journey with Java Methods, Overloading, Variable Scope & Simple Projects
Today was all about really understanding how methods work in Java and how I can use method overloading to write cleaner and more flexible code. I also got to grips with the tricky concept of variable scope β something Iβd heard about but now see clearly how it works in practice. To put things into practice, I built two beginner-friendly projects: a simple banking system and a dice roller that even prints dice faces using ASCII art! It was a great way to combine theory with fun.
1. Utility Methods Demo: Square, Cube, Full Name, and Age Check
I started with a simple program to create reusable methods:
javaCopyEdit/*
Program: Utility Methods Demonstration
Concept: Writing methods with different return types in Java
*/
public class UtilityMethodsDemo {
public static void main(String[] args) {
// I called the square method and printed the result
double result = square(6);
System.out.println("π The square of 6 is: " + result);
// Then I called the cube method
System.out.println("π¦ The cube of 6 is: " + cube(6));
// I combined two strings to form a full name
System.out.println("π€ Full name is: " + getFullName("Bro", "Code"));
// Finally, I checked if someone of age 4 is an adult
System.out.println("π Is a 4-year-old an adult? " + ageCheck(4));
}
// This method returns the square of a number
static double square(double number) {
return number * number;
}
// This method returns the cube of a number
static double cube(double number) {
return number * number * number;
}
// This method combines first and last names with a space
static String getFullName(String firstName, String lastName) {
return firstName + " " + lastName;
}
// This method returns true if age >= 18, false otherwise
static boolean ageCheck(int age) {
return age >= 18;
}
}
What I learned: Writing separate methods for tasks makes the code easier to manage and reuse. It was fun to see different return types like double
, String
, and boolean
all in one program.
2. Playing with Method Overloading β Adding Numbers
Next, I explored method overloading by writing several versions of an add
method that take different numbers of arguments:
javaCopyEditpublic class Adder {
public static void main(String[] args) {
System.out.println("Sum of 1 and 2: " + add(1, 2));
System.out.println("Sum of 1, 2 and 3: " + add(1, 2, 3));
System.out.println("Sum of 1, 2, 3 and 4: " + add(1, 2, 3, 4));
}
static double add(double a, double b) {
return a + b;
}
static double add(double a, double b, double c) {
return a + b + c;
}
static double add(double a, double b, double c, double d) {
return a + b + c + d;
}
}
My takeaway: Itβs amazing how Java knows which method to call just by looking at the number of parameters. This really makes writing flexible code easy.
3. Making Pizza with Overloaded Methods
I also created a pizza builder that changes the output based on how many ingredients I provide:
javaCopyEditpublic class PizzaBuilder {
public static void main(String[] args) {
System.out.println(makePizza("flat bread"));
System.out.println(makePizza("thin crust", "cheddar"));
System.out.println(makePizza("flat bread", "mozzarella", "pepperoni"));
}
static String makePizza(String bread) {
return bread + " pizza";
}
static String makePizza(String bread, String cheese) {
return cheese + " " + bread + " pizza";
}
static String makePizza(String bread, String cheese, String toppings) {
return toppings + " " + cheese + " " + bread + " pizza";
}
}
This helped me see practical use cases of overloading β itβs like creating different "recipes" with the same method name.
4. Understanding Variable Scope by Shadowing Variables
One tricky but important concept was variable scope β where variables exist and which one Java picks when names clash:
javaCopyEditpublic class VariableScopeDemo {
static int x = 3; // Class-level variable
public static void main(String[] args) {
int x = 1; // Local to main method, shadows class-level x
System.out.println("main-local x = " + x);
doSomething();
// Referencing class-level variable explicitly
System.out.println("class-static x = " + VariableScopeDemo.x);
}
static void doSomething() {
int x = 2; // Local to doSomething method, shadows class variable
System.out.println("doSomething-local x = " + x);
}
}
I was able to see how local variables with the same name shadow the class variable, and Java uses the closest variable in scope.
5. Building a Simple Banking Application
I wrote a simple interactive banking program that:
Shows balance
Accepts deposits
Allows withdrawals with checks for sufficient funds
Exits cleanly
Hereβs the full code:
javaCopyEditimport java.util.Scanner;
public class SimpleBankApp {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
double balance = 0;
boolean isRunning = true;
while (isRunning) {
System.out.println("\n***************");
System.out.println("BANKING PROGRAM");
System.out.println("***************");
System.out.println("1. Show Balance");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.println("4. Exit");
System.out.println("***************");
System.out.print("Enter your choice (1-4): ");
int choice = scanner.nextInt();
switch (choice) {
case 1 -> showBalance(balance);
case 2 -> balance += deposit();
case 3 -> balance -= withdraw(balance);
case 4 -> isRunning = false;
default -> System.out.println("INVALID CHOICE");
}
}
System.out.println("****************************");
System.out.println("Thank You! Have a nice day!!");
System.out.println("****************************");
scanner.close();
}
static void showBalance(double balance) {
System.out.printf("Your current balance is: $%.2f\n", balance);
}
static double deposit() {
System.out.print("Enter an amount to be deposited: ");
double amount = scanner.nextDouble();
if (amount < 0) {
System.out.println("Amount can't be negative");
return 0;
}
return amount;
}
static double withdraw(double balance) {
System.out.print("Enter the amount to be withdrawn: ");
double amount = scanner.nextDouble();
if (amount > balance) {
System.out.println("Insufficient funds");
return 0;
} else if (amount < 0) {
System.out.println("Amount can't be negative");
return 0;
}
return amount;
}
}
This project helped me learn how to create menus, validate user inputs, and use methods to keep the program organized.
6. Dice Roller Program with Cool ASCII Art
For some fun, I created a dice roller that:
Lets you choose how many dice to roll
Rolls each die randomly
Prints a cool ASCII representation of the dice face
Shows the total rolled sum
Hereβs the program:
javaCopyEditimport java.util.Scanner;
import java.util.Random;
public class DiceRoller {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.print("Enter the number of Dice to roll: ");
int numOfDice = scanner.nextInt();
if (numOfDice > 0) {
int total = 0;
for (int i = 0; i < numOfDice; i++) {
int roll = random.nextInt(1, 7);
printDie(roll);
System.out.println("You Rolled " + roll);
total += roll;
}
System.out.println("Total : " + total);
} else {
System.out.println("Number of dice must be greater than 0.");
}
scanner.close();
}
static void printDie(int roll) {
String dice1 = """
---------
| |
| β |
| |
---------
""";
String dice2 = """
---------
| β |
| |
| β |
---------
""";
String dice3 = """
---------
| β |
| β |
| β |
---------
""";
String dice4 = """
---------
| β β |
| |
| β β |
---------
""";
String dice5 = """
---------
| β β |
| β |
| β β |
---------
""";
String dice6 = """
---------
| β β |
| β β |
| β β |
---------
""";
switch (roll) {
case 1 -> System.out.println(dice1);
case 2 -> System.out.println(dice2);
case 3 -> System.out.println(dice3);
case 4 -> System.out.println(dice4);
case 5 -> System.out.println(dice5);
case 6 -> System.out.println(dice6);
default -> System.out.println("Invalid roll");
}
}
}
It was really cool to see my dice rolls displayed with those dot patterns β made the console feel a little more like a game.
Final Thoughts on Day 5
Todayβs lessons were crucial in shaping how I think about writing organized, reusable, and flexible code. Methods help me break down complex problems into manageable chunks, and overloading keeps my code clean when handling similar tasks with slight variations.
Understanding variable scope helped me avoid common bugs with variable naming conflicts β I now know exactly which variable Java uses when. The banking and dice programs gave me hands-on experience working with user input, loops, validation, and even ASCII art output.
Iβm excited to keep building on these concepts. Next, I plan to explore classes and objects in Java to make my programs even more powerful and modular.
Subscribe to my newsletter
Read articles from Himanshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Himanshi
Himanshi
Hi! I'm a curious and self-driven programmer currently pursuing my BCA π and diving deep into the world of Java β from Day 1. I already have a foundation in programming, and now I'm expanding my skills one concept, one blog, and one project at a time. Iβm learning Java through Bro Codeβs YouTube tutorials, experimenting with code, and documenting everything I understand β from basic syntax to real-world applications β to help others who are just starting out too. I believe in learning in public, progress over perfection, and growing with community support. Youβll find beginner-friendly Java breakdowns, hands-on code snippets, and insights from my daily coding grind right here. π‘ Interests: Java, Web Dev, Frontend Experiments, AI-curiosity, Writing & Sharing Knowledge π οΈ Tools: Java β’ HTML/CSS β’ JavaScript β’ Python (basics) β’ SQL π― Goal: To become a confident Full Stack Developer & AI Explorer π Based in India | Blogging my dev journey #JavaJourney #100DaysOfCode #CodeNewbie #LearnWithMe