• Final Hack
  • Homework down below…
  • Enhanced For Loop (For-each)

    An enhanced For Loop is a simplified version of a regular for loop, where the coder doesn’t need to specify the increment and can access each element directly.

    Pros

    • Can access the elements of a array instad of a counter
    • Enhanced readability
    • Less likely to be buggy

    Cons

    • Can only traverse the array in order
    • Accesses a copy of the array, so modifications to the variables are not saved in the original array

    Regular For Loop

    Regular For Loop

    Enhanced For Loop

    Enhanced For Loop

    int[] numbers = {10, 20, 30, 40, 50};
    for (int number : numbers) {
        number += 1;
        System.out.println(number);
    };
    
    System.out.println(Arrays.toString(numbers))
    
    11
    21
    31
    41
    51
    [10, 20, 30, 40, 50]
    

    Comparing a regular for loop with the enhanced for loop

    Popcorn Hack: Rewrite this code to use an enhanced for loop instead. make comments explaining what you added/changed

    String[] languages = {"Java", "Python", "Markdown", "C++", "Go", "JavaScript", "HTML"};
    
    for (int i = 0; i<languages.length; i++) {
        System.out.println(languages[i]);
    };
    
    Java
    Python
    Markdown
    C++
    Go
    JavaScript
    HTML
    

    Hacks

    1. Build the loop
    2. Multiple Choice
    3. Find each index of the letter ‘a’ in each word
    4. Find the error
    5. HW Hack
    public class Main {
        public static void main(String[] args) {
            // Add an array here
            String[] myArray = {"Math", "PE", "English", "CSA", "Science"};
            
            // Add a loop to go through the array here
            for (String element : myArray) {
                System.out.println(element);
            }
        }
    }
    Main.main(null);
    
    
    Math
    PE
    English
    CSA
    Science
    
    private String[] myArray = {
        "And", "Table", "Shirts", "Tea", "School Day"
    };
    
    for (String currentWord : myArray) {
        // Add code here
    }
    
    A: System.out.println(myArray.currentWord.length());
    B: System.out.println(myArray[index].length());
    C: System.out.println(myArray[currentWord].length());
    D: System.out.println(currentWord.length());
    //*** IT'S OPTION D ***
    
    String[] fruits = {"Apple", "Banana", "Orange"};
    
    for (String fruit: fruits) {
        // Find the index of the letter 'a' in each fruit
    };
    
    public class Main {
        public static void main(String[] args) {
            String[] fruits = {"Apple", "Banana", "Orange"};
            
            for (String fruit : fruits) {
                // Find the index of the letter 'a' in each fruit
                int index = fruit.toLowerCase().indexOf('a');
                System.out.println("The index of 'a' in " + fruit + " is: " + index);
            }
        }
    }
    Main.main(null);
    
    The index of 'a' in Apple is: 0
    The index of 'a' in Banana is: 1
    The index of 'a' in Orange is: 2
    
    String[] myArray = {"Object 1", "Object 2", "Object 3", "Object 4", "Object 5"};
    
    for (currentWord: myArray) {
        System.out(currentWord)
    };
    
    // There are 3 errors, can you find them?
    
    public class Main {
        public static void main(String[] args) {
            String[] myArray = {"Object 1", "Object 2", "Object 3", "Object 4", "Object 5"};
            
            for (String currentWord : myArray) {
                System.out.print(currentWord);  // You can use println() if you want each on a new line
            }
        }
    }
    Main.main(null);
    
    Object 1Object 2Object 3Object 4Object 5
    

    Final Hack

    Add to the code below to create a average grade calculator (using an enhanced for loop)

    Integer[] grades = {88, 93, 55, 68, 77};
    
    Scanner userGrades = new Scanner(System.in);
    System.out.print("Enter a grade: ");
    int grade = Integer.parseInt(userGrades.nextLine());
    
    // Add code here to take the average
    
    grades = Arrays.copyOf(grades, grades.length + 1);
    grades[grades.length - 1] = grade;
    System.out.println(Arrays.toString(grades));
    
    Enter a grade: [88, 93, 55, 68, 77, 88]
    

    Homework down below…

    public class AverageGradeCalculator {
        public static void main(String[] args) {
            Integer[] grades = {88, 93, 55, 68, 77};
    
            Scanner userGrades = new Scanner(System.in);
            System.out.print("Enter a grade: ");
            int grade = Integer.parseInt(userGrades.nextLine());
    
            // Add code here to take the average
            int sum = 0;
            for (int g : grades) {
                sum += g;  
            }
            double average = (double) sum / grades.length;
            System.out.println("Average grade before adding new grade: " + average);
    
            grades = Arrays.copyOf(grades, grades.length + 1);
            grades[grades.length - 1] = grade;
            System.out.println("Updated grades: " + Arrays.toString(grades));
        }
    }
    
    AverageGradeCalculator.main(null);
    
    
    Enter a grade: 
    
    Average grade before adding new grade: 76.2
    Updated grades: [88, 93, 55, 68, 77, 85]