Java Programming Day 8

HimanshiHimanshi
5 min read

✨ Java Day 8 – Slot Machine Game 🎰 + Car Class πŸš— + Real OOP Stuff πŸ› οΈ

Hey guys! So today was honestly one of the most fun + productive Java days so far. I played around with three different programs β€” and guess what? Each one taught me something completely different while still connecting the dots around OOP, game logic, and real-life implementation.

Let me walk you through everything I coded today β€” trust me, you’ll relate if you’re just starting out like me.


🎰 1. SLOT MACHINE GAME – My First Java Game!

Okay so this one? Pure fun. I built a slot machine game using basic Java concepts, and wow, it was so satisfying watching it work.

πŸ”Ή What it does:

You basically start with β‚Ή100, and you place bets. Then 3 random symbols (emojis) pop up.
If two or three of them match, you win! If not… well, you lose that round's bet. But the game keeps going until your balance is gone or you decide to quit.

🧠 What I used:

  • Random for generating the symbols

  • Scanner for user input (bet, continue/stop)

  • Basic loops and if-else conditions

  • And of course… emojis for ✨aesthetic✨

πŸ–₯️ Code:

javaCopyEditimport java.util.*;

public class SlotMachine {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Random rand = new Random();

        String[] symbols = {"πŸ‡", "🍈", "πŸ‰", "🍊", "πŸ‹"};
        int balance = 100;

        System.out.println("🎰 Welcome to JAVA Slot Machine!");
        System.out.println("Symbols: πŸ‡πŸˆπŸ‰πŸŠπŸ‹");

        while (balance > 0) {
            System.out.println("πŸ’° Current Balance: β‚Ή" + balance);
            System.out.print("Place your bet: ");
            int bet = scanner.nextInt();

            if (bet > balance || bet <= 0) {
                System.out.println("❌ Invalid bet amount!");
                continue;
            }

            String sym1 = symbols[rand.nextInt(symbols.length)];
            String sym2 = symbols[rand.nextInt(symbols.length)];
            String sym3 = symbols[rand.nextInt(symbols.length)];

            System.out.println("Spinning...");
            System.out.println(sym1 + " | " + sym2 + " | " + sym3);

            if (sym1.equals(sym2) && sym2.equals(sym3)) {
                int winnings = bet * 5;
                balance += winnings;
                System.out.println("πŸŽ‰ Jackpot! You won β‚Ή" + winnings);
            } else if (sym1.equals(sym2) || sym2.equals(sym3)) {
                int winnings = bet * 2;
                balance += winnings;
                System.out.println("😊 Nice! You won β‚Ή" + winnings);
            } else {
                balance -= bet;
                System.out.println("πŸ’” You lost this round.");
            }

            System.out.print("Play again? (yes/no): ");
            String choice = scanner.next();
            if (!choice.equalsIgnoreCase("yes")) {
                break;
            }
        }

        System.out.println("Game over! Final balance: β‚Ή" + balance);
    }
}

πŸ’­ What I learned:

This helped me practice loops, input/output, and conditions in a real-world game format.
Also, the logic for checking symbol matches made me think through the steps properly. πŸ”


πŸš— 2. CAR CLASS – Understanding OOP Better

I know I’ve done basic classes before, but today I made my first meaningful class for a real-world object: a Car.

πŸ”Ή What this class does:

  • Has 4 main attributes: make, model, year, price

  • Uses 4 methods: start(), stop(), drive(), brake()

  • Uses a boolean variable isRunning to track car status

It literally feels like I made my own car in Java β€” I can start it, drive it, and even brake it πŸ˜­πŸ’–

πŸ–₯️ Car Class Code:

javaCopyEditpublic class Car {
    String make = "Toyota";
    String model = "Corolla";
    int year = 2020;
    double price = 1500000.0;
    boolean isRunning = false;

    void start() {
        isRunning = true;
        System.out.println("πŸš— Car started.");
    }

    void stop() {
        isRunning = false;
        System.out.println("πŸ›‘ Car stopped.");
    }

    void drive() {
        if (isRunning) {
            System.out.println("🏁 Car is driving.");
        } else {
            System.out.println("⚠️ Start the car first!");
        }
    }

    void brake() {
        System.out.println("🚦 Car is braking.");
    }
}

πŸ’­ What I learned:

  • How to design a class like an actual real-world entity

  • Importance of boolean flags (like isRunning) to maintain states

  • It helped me get more comfortable with method calling + object behavior


πŸ› οΈ 3. MAIN CLASS – Connecting the Car with the Main Program

This one is short, but so important.
I used this Main class to create an object of the Car class and call all its methods in a sequence. This gave me full confidence in how classes and objects interact in Java.

πŸ–₯️ Main Class Code:

javaCopyEditpublic class Main {
    public static void main(String[] args) {
        Car car = new Car();

        System.out.println("🚘 Car Details:");
        System.out.println("Make: " + car.make);
        System.out.println("Model: " + car.model);
        System.out.println("Year: " + car.year);
        System.out.println("Price: β‚Ή" + car.price);
        System.out.println("Running Status: " + car.isRunning);

        car.start();
        System.out.println("Running Status: " + car.isRunning);

        car.drive();
        car.brake();
        car.stop();

        System.out.println("Running Status: " + car.isRunning);
    }
}

πŸ’­ What I learned:

  • How to use the Car object inside another file

  • Got super clear on how data and behavior travel with objects

  • Also practiced the use of dot operator to access variables/methods


πŸ“˜ Summary: What Java Day 8 Gave Me

ConceptMy Takeaway
Game LogicLoops, Random, Conditions – all came together in a fun way!
OOP BasicsI finally feel confident creating and using my own custom class ✨
Real-life MappingWhether it’s a car or a slot machine, Java is powerful for simulations

🫢 Final Thoughts

I feel really proud of Day 8, not because I did something super complex β€” but because I actually enjoyed it, saw the output, and understood everything I wrote.

I also realized how Java is not just theory, it's literally a way to build small cool things from imagination. From today onwards, I’ll try to blend creativity + code every day πŸ’‘πŸ’»

0
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