← Back to Home

Code-Alongs

Overview

Join us for interactive coding sessions where we'll work through real-world examples and practice implementing object-oriented programming concepts in Java.

Available Code-Along Topics

Code-Along 1: Writing Classes

Learning Objectives Covered

  • Define instance variables to store the internal state of an object
  • Implement a class with multiple constructors to allow optional constructor arguments
  • Implement code to instantiate an object by calling a constructor with and without parameters
  • Explain the scope of visibility of a class's public and private member variables
  • Implement instance methods that read and modify instance variables

Key Code Examples

// Creating a basic class
public class Person {
    // Instance variables
    private String name;
    private int age;
    private String email;
    
    // Default constructor
    public Person() {
        this.name = "Unknown";
        this.age = 0;
        this.email = "";
    }
    
    // Constructor with name parameter
    public Person(String name) {
        this.name = name;
        this.age = 0;
        this.email = "";
    }
    
    // Constructor with all parameters
    public Person(String name, int age, String email) {
        this.name = name;
        this.age = age;
        this.email = email;
    }
    
    // Getter methods
    public String getName() {
        return this.name;
    }
    
    public int getAge() {
        return this.age;
    }
    
    public String getEmail() {
        return this.email;
    }
    
    // Setter methods
    public void setName(String name) {
        this.name = name;
    }
    
    public void setAge(int age) {
        if (age >= 0) {
            this.age = age;
        }
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    // Business method
    public boolean isAdult() {
        return this.age >= 18;
    }
}
                

Instantiating Objects

// Creating objects using different constructors
Person person1 = new Person(); // Uses default constructor
Person person2 = new Person("Alice"); // Uses constructor with name parameter
Person person3 = new Person("Bob", 25, "bob@example.com"); // Uses constructor with all parameters

// Using getters to access state
System.out.println(person3.getName()); // Outputs: Bob
System.out.println(person3.getAge()); // Outputs: 25

// Using setters to modify state
person1.setName("Charlie");
person1.setAge(30);
person1.setEmail("charlie@example.com");

// Using business method
if (person1.isAdult()) {
    System.out.println(person1.getName() + " is an adult.");
}
                

Code-Along 2: Enums and Exceptions

Learning Objectives Covered

  • Implement an enum type to represent a set of predefined constants
  • Implement code that uses a defined enum type
  • Implement code that throws an exception when a method input is invalid
  • Handle NullPointerException and other runtime exceptions

Enum Examples

// Simple enum definition
public enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

// Enum with properties and methods
public enum Status {
    PENDING("Pending", 0),
    IN_PROGRESS("In Progress", 1),
    COMPLETED("Completed", 2),
    CANCELLED("Cancelled", 3);
    
    private final String displayName;
    private final int code;
    
    // Constructor
    Status(String displayName, int code) {
        this.displayName = displayName;
        this.code = code;
    }
    
    // Getters
    public String getDisplayName() {
        return displayName;
    }
    
    public int getCode() {
        return code;
    }
    
    // Static method to find by code
    public static Status findByCode(int code) {
        for (Status status : values()) {
            if (status.getCode() == code) {
                return status;
            }
        }
        return null;
    }
}
                

Using Enums

// Using simple enum
Day today = Day.MONDAY;

// Switch statement with enum
switch (today) {
    case MONDAY:
        System.out.println("Start of work week");
        break;
    case FRIDAY:
        System.out.println("End of work week");
        break;
    case SATURDAY:
    case SUNDAY:
        System.out.println("Weekend!");
        break;
    default:
        System.out.println("Mid-week");
}

// Using enum with properties
Status taskStatus = Status.IN_PROGRESS;
System.out.println("Task status: " + taskStatus.getDisplayName());
System.out.println("Status code: " + taskStatus.getCode());

// Finding enum by value
Status foundStatus = Status.findByCode(2);
if (foundStatus != null) {
    System.out.println("Found status: " + foundStatus.name());
}
                

Exception Handling Examples

// Method that throws an exception for invalid input
public void processAge(int age) {
    if (age < 0) {
        throw new IllegalArgumentException("Age cannot be negative");
    }
    
    if (age > 150) {
        throw new IllegalArgumentException("Age is unrealistically high");
    }
    
    // Process valid age...
    System.out.println("Processing age: " + age);
}

// Handling potential exceptions
try {
    processAge(-5);
} catch (IllegalArgumentException e) {
    System.err.println("Invalid age provided: " + e.getMessage());
}

// Handling NullPointerException
String name = null;
try {
    int length = name.length(); // Will throw NullPointerException
} catch (NullPointerException e) {
    System.err.println("Name is null, cannot get length");
    name = "Default";
}

// Using null check to avoid exception
if (name != null) {
    int length = name.length();
} else {
    System.out.println("Name is null");
}
                

How to Join a Code-Along

  1. Log in to your BloomTech platform account
  2. Navigate to the "Live Events" section
  3. Look for available Backend Development code-along sessions
  4. If no sessions are currently scheduled, please reach out to the Backend Development team to request one

Preparation Checklist

  • Review the module content before attending
  • Set up your Java development environment
  • Have your questions ready
  • Test your audio and video setup

Contact the BD Team

Need to schedule a code-along session? Have questions? Contact the Backend Development team:

  • Use the #help-backend channel in Slack
  • Reach out to your Team Lead
  • Contact your Section Lead