← Back to Home

Code-Alongs - BD Unit 1 Sprint 1

Code-Alongs Overview

Code-Alongs are live experiences taught by our expert instructors designed to prepare you for concepts found in the sprint challenges. They're your opportunity to work on complex job-ready problems in a live and engaging environment.

Code-Alongs are live classes 50 minutes in length designed to offer deeper insights into learning your core competencies and are offered seven days a week in the morning, afternoon, and evening.

Code-Along 1: JavaDoc, Variables and Arithmetic Operations

Learn about JavaDoc, variable declarations, and how to perform arithmetic operations in Java.

JavaDoc Documentation

JavaDoc is a documentation generator for Java that extracts comments from Java source code and generates formatted HTML documentation. JavaDoc comments help other developers understand your code's functionality.

JavaDoc Comment Format

/**
 * This is a JavaDoc comment.
 * @param paramName description of the parameter
 * @return description of the return value
 * @throws ExceptionType description of when this exception is thrown
 */

Variables in Java

Variables are used to store data in your programs. In Java, each variable has a specific type that determines what kind of data it can hold.

Variable Declaration and Initialization

  • Declaration: type variableName;
  • Initialization: variableName = value;
  • Declaration and Initialization: type variableName = value;

Java Primitive Data Types

  • int - Integers (whole numbers), 32 bits
  • double - Double precision floating point numbers, 64 bits
  • boolean - true or false values
  • char - Single characters like 'A', '*', '7'
  • float - Single precision floating point numbers, 32 bits
  • long - Integers, 64 bits
  • short - Integers, 16 bits
  • byte - Integers, 8 bits

Arithmetic Operations

Java supports standard arithmetic operations between numeric values.

Basic Operators

  • + - Addition (also used for String concatenation)
  • - - Subtraction
  • * - Multiplication
  • / - Division (be careful with integer division)
  • % - Modulus (remainder after division)

Integer vs. Floating Point Division

When you divide two integers, the result is an integer (decimal part is truncated):

int result = 5 / 2;  // result is 2, not 2.5

To get a floating-point result, at least one operand must be a floating-point type:

double result = 5.0 / 2;  // result is 2.5
double result2 = (double) 5 / 2;  // result is 2.5 using casting

Example Code

// Variable declarations
int count = 10;
double price = 29.99;
String name = "Java Programming";

// Arithmetic operations
int sum = count + 5;  // 15
double total = price * count;  // 299.9
int remainder = count % 3;  // 1 (remainder of 10/3)

// String concatenation
String message = "The total price for " + count + " items is $" + total;

GitHub Repositories

Schedule

Code-Along 2: String Methods

Explore string manipulation and methods available for working with strings in Java.

Working with Strings in Java

Strings in Java are objects that represent sequences of characters. The String class provides many methods for examining individual characters, comparing strings, searching strings, extracting parts of strings, and creating copies with modified content.

Creating Strings

// String literal
String greeting = "Hello, World!";

// Using the String constructor
String message = new String("Welcome to Java");

Common String Methods

  • length() - Returns the number of characters in the string
  • charAt(int index) - Returns the character at the specified index
  • substring(int beginIndex) - Returns a substring starting from the specified index
  • substring(int beginIndex, int endIndex) - Returns a substring between specified indexes
  • indexOf(String str) - Returns the index of the first occurrence of the specified substring
  • toLowerCase() - Converts all characters to lowercase
  • toUpperCase() - Converts all characters to uppercase
  • trim() - Removes whitespace from both ends of the string
  • replace(char oldChar, char newChar) - Replaces all occurrences of a character
  • equals(Object obj) - Compares this string to another object for equality
  • equalsIgnoreCase(String anotherString) - Compares strings, ignoring case

String Concatenation

There are several ways to concatenate strings in Java:

// Using + operator
String fullName = firstName + " " + lastName;

// Using concat() method
String result = str1.concat(str2);

// Using StringBuilder (more efficient for many concatenations)
StringBuilder builder = new StringBuilder();
builder.append("Hello");
builder.append(" ");
builder.append("World");
String message = builder.toString();

String Formatting

Java provides powerful string formatting capabilities using the String.format() method:

String formatted = String.format("Hello, %s! You have $%.2f in your account.", name, balance);

// Alternatively, using printf (for printing)
System.out.printf("Hello, %s! You have $%.2f in your account.", name, balance);

Common Format Specifiers

  • %s - Formats strings
  • %d - Formats decimal integers
  • %f - Formats floating-point numbers
  • %n - Inserts a platform-specific line separator
  • %.2f - Formats a floating-point number with 2 decimal places

Example Code

String message = "Hello, Java Programming!";

// String methods in action
int length = message.length();  // 25
char firstChar = message.charAt(0);  // 'H'
String sub = message.substring(7, 11);  // "Java"
boolean containsJava = message.contains("Java");  // true
String lower = message.toLowerCase();  // "hello, java programming!"
String modified = message.replace('a', 'A');  // "Hello, JAvA ProgrAmming!"

// Finding occurrence
int indexOfJava = message.indexOf("Java");  // 7

Preparation Checklist

The best Code-Along experiences happen when you are ready before coming to class. Your instructors created a starting point and a solution for each of your Code-Alongs to ensure you have what you need to succeed.

How to Get the Most from Code-Alongs

Follow these tips to maximize your learning during code-along sessions:

Before the Session

  1. Clone both the starter and solution repositories to your local machine
  2. Review the related module content and learning objectives
  3. Try to identify any concepts you're struggling with
  4. Prepare specific questions you'd like addressed during the session

During the Session

  1. Code along with the instructor, don't just watch
  2. Take brief notes on key concepts or techniques
  3. Ask questions when you don't understand something
  4. Try to understand the why behind each step, not just the how

After the Session

  1. Review your code and compare it with the solution
  2. Experiment with variations of the code to deepen your understanding
  3. Apply what you've learned to your module projects
  4. Discuss concepts with classmates to reinforce learning