Java Arrays with Zombie Apocalypse Example

Jul 21, 2024

Java Arrays Tutorial with Zombie Apocalypse Example

Introduction

  • Presenter: Alex
  • Channel Focus: Weekly Java programming tutorials for beginners
  • Current Topic: Understanding and using arrays in Java through a fun zombie apocalypse scenario.

Setting up the Java Project

  1. Create New Java Project:
    • Name: ZombieKiller
    • Finish and open.
  2. Create New Java Class: ZombieKiller
    • Check the first checkbox.
    • Finish to set up the Java file.
  3. Welcome Message:
    • System.out.println("Welcome to our Zombie Killer program");
      
    • Run to see the welcome message printed.

Understanding Arrays

Introduction to Arrays

  • Definition: A list to keep track of items (e.g., weapons, zombies).
  • Syntax:
    • Example of a String array for weapons:
      String[] backpack = {"shotgun", "assault rifle", "sniper"};
      
    • Example of a String array for zombies:
      String[] zombies = {"close range zombie", "mid range zombie", "long range zombie"};
      

Accessing Array Elements

  • Arrays are zero-indexed (first element is at index 0).
  • Example:
    • Access shotgun (first element): backpack[0]
    • Access assault rifle (second element): backpack[1]
    • Access sniper (third element): backpack[2]

Printing Array Elements

  • Use System.out.println to print array elements:
    • Print weapons in the backpack:
      System.out.println(backpack[0]); // shotgun
      System.out.println(backpack[1]); // assault rifle
      System.out.println(backpack[2]); // sniper
      
    • Print zombie types:
      System.out.println(zombies[0]); // close range zombie
      System.out.println(zombies[1]); // mid range zombie
      System.out.println(zombies[2]); // long range zombie
      

Example Scenario: Killing Zombies

Scenario Setup

-Problem: Different weapons for different ranges of zombies.

  • Zombie Types and Required Weapon:
    • Close-range zombie: Shotgun
    • Mid-range zombie: Assault rifle
    • Long-range zombie: Sniper

Coding the Solution

  1. Identify Long-range Zombies:
    • Get sniper from the backpack:
    • backpack[2]; // sniper
      
    • Take 3 sniper shots for 3 long-range zombies:
    • System.out.println("sniper");
      System.out.println("sniper");
      System.out.println("sniper");
      
  2. Identify Close-range Zombie:
    • Get shotgun from the backpack:
    • backpack[0]; // shotgun
      
    • Take 1 shotgun shot:
    • System.out.println("shotgun");
      
  3. Run the Program: Output should be:
    • sniper sniper sniper shotgun
      

Practical Applications

  • Games: Weapon cycling (e.g., pressing number keys to change weapons).
  • Flexibility: Arrays can hold different types of variables (not just strings).

Example with Integers

  • Array of Integers:
    • int[] numbersZombiesHate = {4, 90, 70, 123, 561};
      
  • Access Example:
    • Access number 561 (at index 4):
    • System.out.println(numbersZombiesHate[4]); // 561
      

Customization Challenge

  • Suggestion: Customize your backpack and zombie arrays.
  • Example Weapons: Spas-12 (MW2), ACR, L96A1 (BO)
  • Question of the Day: How did you customize your arrays? What challenges did you face?

Conclusion

  • Engage with Community: Comment and share your custom arrays.
  • Subscribe: Stay updated with weekly tutorials.
  • Like and Share: If you found the video helpful.
  • Appreciation: Thanks for watching and until next time!