DEV Community

Cover image for 3 Java Mini Programs to Try Today!
Mohamad mhana
Mohamad mhana

Posted on

3 Java Mini Programs to Try Today!

Looking to practice Java hands-on and strengthen your coding skills? Here are three small programs, broken into three chunks each, with clear explanations and key skills highlighted.


1️⃣ Simple Calculator

What it does: Perform addition, subtraction, multiplication, and division between two numbers.

Chunk 1 – Input from the User

Scanner sc = new Scanner(System.in);

System.out.print("Enter first number: ");
double num1 = sc.nextDouble();

System.out.print("Enter operator (+, -, *, /): ");
char operator = sc.next().charAt(0);

System.out.print("Enter second number: ");
double num2 = sc.nextDouble();

Enter fullscreen mode Exit fullscreen mode

Explanation: This chunk takes two numbers and an operator from the user. It prepares the program to perform calculations based on user input.

This enforces: Handling user input and understanding data types in Java.


Chunk 2 – Calculate the Result


double result;

switch(operator) {
    case '+': result = num1 + num2; break;
    case '-': result = num1 - num2; break;
    case '*': result = num1 * num2; break;
    case '/': 
        if(num2 != 0) result = num1 / num2; 
        else { System.out.println("Cannot divide by zero!"); return; }
        break;
    default: System.out.println("Invalid operator"); return;
}
Enter fullscreen mode Exit fullscreen mode

Explanation: This chunk decides which arithmetic operation to perform using a switch statement. It also handles invalid input and division by zero.

This enforces: Conditional logic and basic arithmetic operations.


Chunk 3 – Display the Result

System.out.println("Result: " + result);

Enter fullscreen mode Exit fullscreen mode

Explanation: This final chunk prints the calculated result to the console so the user can see it.

This enforces: Output handling and program completion.

💡 Try this: Modify it to handle multiple operations in a row.

Switch Statements in Java - GeeksforGeeks


2️⃣ Number Guessing Game

What it does: Guess a randomly generated number between 1–100.

Chunk 1 – Setup

Scanner sc = new Scanner(System.in);
Random rand = new Random();
int number = rand.nextInt(100) + 1;
int guess = 0;

System.out.println("Guess a number between 1 and 100:");
Enter fullscreen mode Exit fullscreen mode

Explanation: This chunk initializes the program, generates a random number, and prepares to take guesses from the user.

This enforces: Random number generation, variable initialization, and console setup.


Chunk 2 – Loop Through Guesses

while(guess != number) {
    guess = sc.nextInt();
    if(guess < number) System.out.println("Too low! Try again:");
    else if(guess > number) System.out.println("Too high! Try again:");

Enter fullscreen mode Exit fullscreen mode

Explanation: This chunk repeatedly asks the user for guesses until the correct number is entered, providing hints for each attempt.

This enforces: Loops and conditional branching for interactive programs.


Chunk 3 – Correct Guess

    else System.out.println("🎉 Congrats! You guessed it!");
}
Enter fullscreen mode Exit fullscreen mode

Explanation: Once the user guesses correctly, the program prints a congratulatory message and ends.

This enforces: Program termination handling and proper feedback to the user.

💡 Try this: Limit the number of attempts and show a “Game Over” message.

Random Number in Java


3️⃣ Palindrome Checker

What it does: Checks whether a word or number reads the same forwards and backwards.

Chunk 1 – Take Input

Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
String reversed = "";
Enter fullscreen mode Exit fullscreen mode

Explanation: This chunk takes a string input from the user and initializes a variable for the reversed version of the string.

This enforces: String handling and preparing variables for processing.


Chunk 2 – Reverse the String

for(int i = str.length() - 1; i >= 0; i--) {
    reversed += str.charAt(i);
}
Enter fullscreen mode Exit fullscreen mode

Explanation: This chunk reverses the string by looping backwards through each character.

This enforces: Loops and string manipulation skills.


Chunk 3 – Compare and Output

if(str.equalsIgnoreCase(reversed))
    System.out.println("✅ " + str + " is a palindrome!");
else
    System.out.println("❌ " + str + " is not a palindrome.");
Enter fullscreen mode Exit fullscreen mode

Explanation: This final chunk compares the original and reversed strings to determine if the input is a palindrome, then prints the result.

This enforces: Conditional checks, logic building, and output formatting.

💡 Try this: Ignore spaces, punctuation, and capitalization to make it more robust.

Palindrome Program in Java


🎯 Why Try Mini Programs?

  • Hands-on learning: Apply Java concepts in small, digestible chunks.
  • Build confidence: Completing these programs prepares you for larger projects.
  • Experiment: Modify, improve, or extend each program to make it your own.

💬Questions:

Which mini program will you try first?

How would you improve or expand any of these programs?

Have you created your own mini Java project? Share it in the comments!

Top comments (0)