Key Learnings

Can loop through strings, since they can be thought of as arrays of chars.

for-each loop iterates through arrays with specified datatypes. Sometimes called enhanced for loops.

Related info: Do While Loop

A Do While loop is similar to a while loop. However, a while loop checks the condition before executing for the first time. A Do While loop checks the condition after each execution, meaning that it is guaranteed to execute at least one time. Both loops continue to execute until the condition is fulfilled.

// Example of for-each loop

public class ForEach {
    public static void main(String[] args) {
        String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};

        for (String letter: letters) {
            System.out.print(" " + letter);
        }
    }
}
ForEach.main(null);
 a b c d e f g h i j k l m n o p q r s t u v w x y z

Hack 1

public class WhileLoops {
    public double money = 0;
    public double profit = 5450000;
    public double goal = 30000000;
    public double years = 0;

    public void Calc() {
        while (this.money < this.goal) {
            this.money = this.money + this.profit;
            this.profit = this.profit * 1.05;
            this.years = this.years + 1;
        }
        System.out.println(this.years);
    }

    public static void main(String[] args) {
        WhileLoops obj = new WhileLoops();
        obj.Calc();
    }
}

WhileLoops.main(null);
5.0

Hack 2

public class ForLoops {
    public double temp = 0;
    public void Calc() {
        System.out.println("Numbers 10-15");
        for (int x = 10; x <= 15; x++) {
            System.out.println(x);
        }
        
        System.out.println("Convert temperature");
        for (int x = 0; x<=100; x+=10) {
            temp = 0;
            temp = x + 273.15;
            System.out.println(x + "c -> " + temp + "k");
        }
    }

    public static void main(String[] args) {
        ForLoops obj = new ForLoops();
        obj.Calc();

    }
}

ForLoops.main(null);
Numbers 10-15
10
11
12
13
14
15
Convert temperature
0c -> 273.15k
10c -> 283.15k
20c -> 293.15k
30c -> 303.15k
40c -> 313.15k
50c -> 323.15k
60c -> 333.15k
70c -> 343.15k
80c -> 353.15k
90c -> 363.15k
100c -> 373.15k

Caesar Cipher Homework

public class CaesarCipher {


    public String[] letters = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
    public String temp;

    // Method which takes a string and swaps a character
    static char[] swap(String str, int i, char j)
    {
        char ch[] = str.toCharArray();
        ch[i] = j;
        return ch;
    }

    public String Calc(String message) {
        temp = message;
        // Looping through each character in the message
        for (int i=0; i<temp.length(); i++) {  
            // Cast the character to ascii to make substitutions much more efficient
            int ascii = (int) temp.charAt(i);

            // Letters at the end of the alphabet behave differently, so we create two separate conditionals
            if (ascii > 64 && ascii < 88 || (ascii > 96 && ascii < 120)) {
                ascii = ascii + 3;
                String tempSwap = new String(swap(temp, i, (char) ascii));
                temp = tempSwap;
            }
            // This is for the last three letters of the alphabet
            else if (ascii > 87 && ascii < 91 || ascii > 119 && ascii < 123) {
                ascii = ascii - 23;
                String tempSwap = new String(swap(temp, i, (char) ascii));
                temp = tempSwap;
            }
        } 
        return temp;
    }
    public static void main(String[] args) {
        CaesarCipher cipherCalc = new CaesarCipher();
        String message1 = "Kfzb gly!";
        String message2 = "zlab zlab zlab";
        String message3 = "prmbozxifcoxdfifpqfzbumfxifalzflrp";
        System.out.println(cipherCalc.Calc(message1));
        System.out.println(cipherCalc.Calc(message2));
        System.out.println(cipherCalc.Calc(message3));


    }
}
CaesarCipher.main(null);
Nice job!
code code code
supercalifragilisticexpialidocious