# Ch 05: Extra-Strength Methods

**Source**: Head First Java, Second Edition | **Pages**: 129-158

## 🎯 Learning Objectives

Building complete programs

## 📚 Key Concepts

* for loops
* Enhanced for loop
* Type casting
* ArrayList basics
* Building the DotCom game
* Program flow and design

***

## 📖 Detailed Notes

### 1. for loops

*Essential concept for mastering Java and OOP.*

**Example**:

```java
void main
```

### 2. Enhanced for loop

*Essential concept for mastering Java and OOP.*

**Example**:

```java
void setLocationCells(int[] loc)
```

### 3. Type casting

*Essential concept for mastering Java and OOP.*

**Example**:

```java
class and a DotCom class. But before we build the full 
```

### 4. ArrayList basics

*Essential concept for mastering Java and OOP.*

**Example**:

```java
class has no instance variables, 
```

### 5. Building the DotCom game

*Essential concept for mastering Java and OOP.*

**Example**:

```java
whatever we want. And when we create a Java class as a 
```

### 6. Program flow and design

*Essential concept for mastering Java and OOP.*

**Example**:

```java
o	 Figure out what the class is supposed to do.
```

***

## 💡 Important Points to Remember

* part of prepcode is the method logic, because it defines what has to happen,
* to factor in the
* depending on the

***

## ✅ Self-Check Questions

Test your understanding:

1. Can you explain the main concepts covered in this chapter?
2. Can you write code examples demonstrating these concepts?
3. Do you understand when and why to use these features?
4. Can you explain the benefits and tradeoffs?

## 🔄 Quick Revision Points

* [ ] for loops
* [ ] Enhanced for loop
* [ ] Type casting
* [ ] ArrayList basics
* [ ] Building the DotCom game
* [ ] Program flow and design

***

## 📝 Practice Exercises

1. Write your own code examples for each key concept
2. Modify existing examples to test edge cases
3. Explain concepts to someone else
4. Create a small project using these concepts

## 🔗 Related Chapters

Review related concepts from other chapters to build comprehensive understanding.

***

*For complete details, diagrams, and all examples, refer to Head First Java Second Edition, pages 129-158.*

To prepare for your revision, here are the detailed study notes for Chapter 5: Extra-Strength Methods. This chapter focuses on building a program from scratch, flow control, and core Java operations.

<a class="button secondary">+1</a>

## Chapter 5: Extra-Strength Methods — Study Notes

This chapter moves beyond basic syntax to show how to build a real application by focusing on the logic and flow of "Extra-Strength" methods.

### 1. Building the "Simple Dot Com" Game

The chapter uses a battleship-style game to demonstrate the development process.

<a class="button secondary"></a>

* The Goal: Sink a "Dot Com" (a 1D array of 3 cells) in as few guesses as possible.

  <a class="button secondary"></a>
* Development Workflow:
  1. Prep Code: Pseudo-code to focus on logic without worrying about Java syntax.
  2. Test Code: Small programs to verify that the real code works as expected.
  3. Real Code: The actual implementation in Java.

     <a class="button secondary"></a>

***

### 2. Java Flow Control: The `for` Loop

While the `while` loop is for general repetition, the `for` loop is specialized for repeating a block of code a specific number of times.

<a class="button secondary">+1</a>

#### The Standard `for` Loop

Java

```
for (int i = 0; i < 10; i++) {
    // Body of the loop
}
```

* Initialization: `int i = 0` (runs once at the start).
* Boolean Test: `i < 10` (checked before every iteration).
* Iteration Expression: `i++` (runs at the end of every loop cycle).

#### The Enhanced `for` Loop (for-each)

Introduced in Java 5.0, this is the preferred way to iterate through every element in an array or collection.

<a class="button secondary">+1</a>Java

```
for (int cell : locationCells) {
    if (guess == cell) {
        result = "hit";
        break; 
    }
}
```

* `int cell`: Declares a variable that holds the current element during each iteration.

  <a class="button secondary">+1</a>
* `locationCells`: The array or collection being looped through.

  <a class="button secondary">+1</a>

***

### 3. Increments and Decrements

Java provides shorthand operators to increase or decrease a variable's value by 1.

<a class="button secondary">+1</a>

* Post-increment (`x++`): Use the current value of `x` first, then add 1 to it.
* Pre-increment (`++x`): Add 1 to `x` first, then use the new value.
* Post-decrement (`x--`) and Pre-decrement (`--x`) work similarly for subtraction.

***

### 4. Converting Data Types

Sometimes data comes in one format (like a `String` from user input) but needs to be another (like an `int` for calculations).

<a class="button secondary">+1</a>

* `Integer.parseInt()`: A static method used to convert a `String` of digits into a primitive `int`.

  <a class="button secondary">+1</a>Java

  ```
  int guess = Integer.parseInt(stringGuess);
  ```

***

### 5. Logical Operators

To create more complex boolean expressions, you can combine multiple tests:

<a class="button secondary">+2</a>

* `&&` (Short-circuit AND): True only if both sides are true. If the left side is false, the right side is never checked.
* `||` (Short-circuit OR): True if at least one side is true. If the left side is true, the right side is never checked.
* `!` (Not): Reverses the boolean value.
* `!=` (Not Equal): True if the two values being compared are different.

***

### 6. Revision Checklist

* The `break` statement: Used to jump out of a loop immediately.

  <a class="button secondary">+1</a>
* Cast vs. Parse: Remember that casting is for compatible types (like `long` to `int`), but `Integer.parseInt()` is for turning text into a number.

  <a class="button secondary">+1</a>
* Initialization: Ensure variables used in loops or conditionals are initialized, as Java's compiler is strict about "might not have been initialized" errors.
