Welcome to Java! Let's Build a Pet Shop 🐾

Hey there! Ready to write your first Java program? We're going to build a simple pet shop system together. Don't worry - we'll take it step by step, and you'll be creating objects in no time!

Step 1: Your First Java Class

First things first - let's make sure your environment is ready. You should see a file called Main.java with some basic "Hello World" code. Let's start there:

package week1;

import static java.lang.System.out;

public class Main {
    public static void main(String[] args) {
        // This line prints to your console
        out.println("Hello World");
    }
}

Go ahead and run this code (click the green play button ▶️). You should see "Hello World" appear in your console. Congratulations - you've just run your first Java program! 🎉

Step 2: Creating Our Pet Class

Now for the fun part! We're going to create a new class to represent pets in our shop. Right-click on your week1 folder, select New → Java Class, and name it "Pet".

Pro Tip: In Java, we usually start class names with a capital letter (like "Pet" not "pet"). This is a naming convention that helps other programmers read your code better!

In your new Pet.java file, let's add some basic information that every pet should have:

package week1;

public class Pet {
    public String name;
    public String type;
    public int age;
}

Look at those three lines inside the class - we just created what we call "fields" or "properties". Each pet will have:

Step 3: Your First Constructor

Next up: let's add a way to create new pets! We'll add something called a "constructor" - it's like a special recipe for making new pets:

public Pet(String name, String type, int age) {
    this.name = name;
    this.type = type;
    this.age = age;
}
Watch Out! Make sure to put this constructor inside your Pet class, but not inside any other method. The placement matters!

Step 4: Creating Your First Pet!

Time to put your Pet class to work! Let's head back to Main.java and create some furry friends. Update your main method to look like this:

public static void main(String[] args) {
    Pet myPet = new Pet("Buddy", "dog", 3);
    out.println("My pet's name is " + myPet.name);
}

Run this code and... ta-da! 🎉 You should see "My pet's name is Buddy" in the console. You've just:

Common Gotcha! If you try to create a Pet without providing all three pieces of information (like new Pet()), Java will complain. All three parameters are required!

Step 5: Let's Add Another Pet!

One pet is nice, but a pet shop needs more than that! Let's create another pet and print information about both:

public static void main(String[] args) {
    Pet myPet = new Pet("Buddy", "dog", 3);
    Pet anotherPet = new Pet("Whiskers", "cat", 5);

    out.println("My pets:");
    out.println("- " + myPet.name + " (" + myPet.type + "), " + myPet.age + " years old");
    out.println("- " + anotherPet.name + " (" + anotherPet.type + "), " + anotherPet.age + " years old");
}

When you run this, you'll see a nice formatted list of your pets! 🐱🐶

Your Turn - Practice Time! 💪

Now that you know how to create pets, try these challenges:

  1. Create a third pet with different values
  2. Print all three pets in the same format as above
  3. Extra challenge: Can you modify the output format to show something like:
    🏪 MY PET SHOP 🏪
    - Buddy is a 3-year-old dog
    - Whiskers is a 5-year-old cat
Remember: You can use the + operator to combine text (strings) and numbers in your output. Java will automatically convert the numbers to text for you!

What We've Learned 🎓

Congratulations! You've just learned some fundamental concepts in Java:

Next, we'll learn how to add behavior to our pets - maybe teach them some tricks! 🦮

Adding Some Action - Pet Methods! 🎬

Our pets look great on paper, but they're a bit... quiet. Let's teach them how to make sounds! In Java, we can add behavior to our objects using methods.

Head back to your Pet class and let's add a makeSound() method:

public class Pet {
    public String name;
    public String type;
    public int age;
    
    public Pet(String name, String type, int age) {
        this.name = name;
        this.type = type;
        this.age = age;
    }

    public String makeSound() {
        if (type.equals("dog")) {
            return "Woof!";
        } else if (type.equals("cat")) {
            return "Meow!";
        } else {
            return "*mysterious pet noises*";
        }
    }
}
Quick Tip: When comparing Strings in Java, we use .equals() instead of ==. It's a quirk of Java that trips up even experienced programmers!

Now let's update our Main class to make our pets speak:

public static void main(String[] args) {
    Pet myPet = new Pet("Buddy", "dog", 3);
    Pet anotherPet = new Pet("Whiskers", "cat", 5);

    // Let's make them speak!
    out.println(myPet.name + " says: " + myPet.makeSound());
    out.println(anotherPet.name + " says: " + anotherPet.makeSound());
}

Final Challenge - Build Your Pet Shop! 🏪

Ready to put everything together? Let's build this step by step:

Step 1: Create Your Pets

First, create three different pets with different types and ages:

public static void main(String[] args) {
    Pet buddy = new Pet("Buddy", "dog", 3);
    Pet whiskers = new Pet("Whiskers", "cat", 5);
    Pet bubbles = new Pet("Bubbles", "fish", 1);
}

Step 2: Add Your Welcome Message

Add a nice header to your pet shop:

    out.println("🏪 WELCOME TO JAVA PET SHOP 🏪\n");
    out.println("Our adorable pets:");

Step 3: Display Your Pets

For each pet, print their details and make them speak. Try this first:

    // Print Buddy's details
    out.println("- " + buddy.name + " (" + buddy.type + "), " + 
               buddy.age + " years old");
    out.println("  " + buddy.name + " says: " + buddy.makeSound());
Notice something? You'll need to write similar code for Whiskers and Bubbles. Seems like a lot of repetition... is there a better way? 🤔

Step 4: Make It Better!

Instead of copying and pasting the same code for each pet, try creating a helper method! Here's how:

  1. Add this method outside of main (but still inside the Main class):
private static void printPetDetails(Pet pet) {
    out.println("- " + pet.name + " (" + pet.type + "), " + 
               pet.age + " years old");
    out.println("  " + pet.name + " says: " + pet.makeSound());
}

Now you can just use this method for each pet:

    printPetDetails(buddy);
    printPetDetails(whiskers);
    printPetDetails(bubbles);

Final Step: Add a Closing Message

    out.println("\nCome adopt a friend today! 🐾");

Run your code - you should see something like this:

🏪 WELCOME TO JAVA PET SHOP 🏪

Our adorable pets:
- Buddy (dog), 3 years old
  Buddy says: Woof!
- Whiskers (cat), 5 years old
  Whiskers says: Meow!
- Bubbles (fish), 1 years old
  Bubbles says: *mysterious pet noises*

Come adopt a friend today! 🐾
Extra Credit: Try creating a hamster! What sound should it make? You'll need to add another condition in the makeSound() method.

You Did It! 🎉

Look how far you've come! You've learned:

Next time we'll learn about:

Keep your code handy - we'll build on it in the next lesson! 👩‍💻👨‍💻