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 bitsdouble
- Double precision floating point numbers, 64 bitsboolean
- true or false valueschar
- Single characters like 'A', '*', '7'float
- Single precision floating point numbers, 32 bitslong
- Integers, 64 bitsshort
- Integers, 16 bitsbyte
- 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;
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 stringcharAt(int index)
- Returns the character at the specified indexsubstring(int beginIndex)
- Returns a substring starting from the specified indexsubstring(int beginIndex, int endIndex)
- Returns a substring between specified indexesindexOf(String str)
- Returns the index of the first occurrence of the specified substringtoLowerCase()
- Converts all characters to lowercasetoUpperCase()
- Converts all characters to uppercasetrim()
- Removes whitespace from both ends of the stringreplace(char oldChar, char newChar)
- Replaces all occurrences of a characterequals(Object obj)
- Compares this string to another object for equalityequalsIgnoreCase(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
GitHub Repositories
Schedule
Additional Resources
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.
- Did you review the core competencies before coming to class?
- Did you watch the guided projects before coming to class?
- Did you take the checks for understanding before coming to class?
- Did you finish your module projects before coming to class?
How to Get the Most from Code-Alongs
Follow these tips to maximize your learning during code-along sessions:
Before the Session
- Clone both the starter and solution repositories to your local machine
- Review the related module content and learning objectives
- Try to identify any concepts you're struggling with
- Prepare specific questions you'd like addressed during the session
During the Session
- Code along with the instructor, don't just watch
- Take brief notes on key concepts or techniques
- Ask questions when you don't understand something
- Try to understand the why behind each step, not just the how
After the Session
- Review your code and compare it with the solution
- Experiment with variations of the code to deepen your understanding
- Apply what you've learned to your module projects
- Discuss concepts with classmates to reinforce learning