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!
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! 🎉
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".
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:
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; }
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:
new Pet()), Java will complain. All three parameters are required!
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! 🐱🐶
Now that you know how to create pets, try these challenges:
🏪 MY PET SHOP 🏪 - Buddy is a 3-year-old dog - Whiskers is a 5-year-old cat
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! 🦮
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*"; } } }
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()); }
Ready to put everything together? Let's build this step by step:
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); }
Add a nice header to your pet shop:
out.println("🏪 WELCOME TO JAVA PET SHOP 🏪\n"); out.println("Our adorable 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());
Instead of copying and pasting the same code for each pet, try creating a helper method! Here's how:
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);
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! 🐾
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! 👩💻👨💻