Fun For Loop Hack: Create a program that iterates through a list of numbers (int_list = {0, 4, 51, 83, 92, 10, 123, 145}) using both a for loop and a for each loop, then split the numbers in the list into even/odd lists, and output them.
int[] int_list = {0, 4, 51, 83, 92, 10, 123, 145};
List<Integer> evenNumbers = new ArrayList<>();
List<Integer> oddNumbers = new ArrayList<>();
// Using a for loop
for (int i = 0; i < int_list.length; i++) {
if (int_list[i] % 2 == 0) {
evenNumbers.add(int_list[i]);
} else {
oddNumbers.add(int_list[i]);
}
}
// Using a for-each loop
for (int num : int_list) {
if (num % 2 == 0) {
evenNumbers.add(num);
} else {
oddNumbers.add(num);
}
}
// Output the even and odd lists
System.out.println("Even Numbers: " + evenNumbers);
System.out.println("Odd Numbers: " + oddNumbers);
Even Numbers: [0, 4, 92, 10, 0, 4, 92, 10]
Odd Numbers: [51, 83, 123, 145, 51, 83, 123, 145]
Popcorn Hack: Iterate through the characters a string with a while loop
String str = "popcorn";
int i = 0;
while (i < str.length()) {
System.out.println(str.charAt(i));
i++;
}
p
o
p
c
o
r
n
Homework Hack! code a caesar cipher that will encrypt any string with any key provided. your code should go into the encrypt() method, and should successfully pass the test cases provided as a bonus, try to use StringBuilder
public class CaesarCipher {
private int key;
private String phrase;
public CaesarCipher(int key, String phrase) {
this.key = key;
this.phrase = phrase;
}
public String encrypt() {
String encrypted = "";
for (int i = 0; i < phrase.length(); i++) {
char c = phrase.charAt(i);
if (Character.isLetter(c)) {
if (Character.isUpperCase(c)) {
char newChar = (char) (((int) c + key - 65) % 26 + 65);
encrypted += newChar;
} else {
char newChar = (char) (((int) c + key - 97) % 26 + 97);
encrypted += newChar;
}
} else {
encrypted += c;
}
}
return encrypted;
}
}
CaesarCipher test1 = new CaesarCipher(3, "hello world");
CaesarCipher test2 = new CaesarCipher(10, "abcdefg");
CaesarCipher test3 = new CaesarCipher(20, "i love csa");
System.out.println("test 1: " + test1.encrypt());
System.out.println("test 2: " + test2.encrypt());
System.out.println("test 3: " + test3.encrypt());
test 1: khoor zruog
test 2: klmnopq
test 3: c fipy wmu
What is wrong with this code cell(Hack) //Hint: Check the Syntax and look at the equals to signs on the example above
import java.util.Scanner;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
System.out.println();
int rows = scanner.nextInt();
for (int i = rows; i>0; i--) { // to go backwards you need a i-- not an i- and also to go to 0 you should exlude 0 and use i>0 not i>1
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
scanner.close();
Enter the number of rows:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
MC:
- B because if you run the code cell, it starts at 3 and goes till 8 then it starts at 1 and goes till 5 which together equals 20
- the code would print out 20 * each on a new line
HW MC:
- What does the following code print?
A. 5 6 7 8 9
B. 4 5 6 7 8 9 10 11 12
C. 3 5 7 9 11
D. 3 4 5 6 7 8 9 10 11 12
for (int i = 3; i <= 12; i++) {
System.out.print(i + " ");
}
Answer: D because the code starts at 3 and goes till 12 printing each number on the same line with a space between them. 1.1. Explain the difference between using a variable like i inside a for loop, vs. using a variable that exists in the code itself for a while loop
- the difference between using a variable like i inside a for loop vs using a variable that exists in the code itself for a while loop is that the variable i inside the for loop is only used for the for loop and is not used outside of the for loop. The variable that exists in the code itself for a while loop is used throughout the code and can be used outside of the while loop.
- How many times does the following method print a “*” ?
A. 9
B. 7
C. 8
D. 6
for (int i = 3; i < 11; i++) {
System.out.print("*");
}
Answer: A because the code starts at 3 and goes till 10 printing a * which would print * 9 times.
- Question 3: What does the following code print?
A. -4 -3 -2 -1 0
B. -5 -4 -3 -2 -1
C. 5 4 3 2 1
int x = -5;
while (x < 0)
{
x++;
System.out.print(x + " ");
}
Answer: B because the code starts at -5 and goes till -1 printing each number on the same line with a space between them.
- What does the following code print?
A. 20
B. 21
C. 25
D. 30
Click to reveal answer: Explain your answer. (explanation is graded not answer)
int sum = 0;
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) {
sum += i * 2;
} else {
sum += i;
}
}
System.out.println(sum);
Answer: C because the code starts at 1 and goes till 5 and if the number is even it multiplies it by 2 and adds it to the sum, if the number is odd it adds it to the sum. The sum is then printed out.
Loops HW Hack Easy Hack Use a while loop to find the numbers from 1-50 that are divisible by 3 or 5, then store them into a list (make sure to print it out at the end) Use a for loop to do the same thing detailed above
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> divisibleBy3Or5 = new ArrayList<>();
int i = 1;
while (i <= 50) {
if (i % 3 == 0 || i % 5 == 0) {
divisibleBy3Or5.add(i);
}
i++;
}
System.out.println(divisibleBy3Or5);
}
}
Main.main(new String[0]);
[3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27, 30, 33, 35, 36, 39, 40, 42, 45, 48, 50]
Harder Hack Palindromes are numbers that have the same value when reversed (ex: “123321” or “323”). Create a program that uses a while loop that outputs all palindromes in any given list. Sample Input: test_list = [5672, 235, 5537, 6032, 317, 8460, 1672, 8104, 7770, 4442, 913, 2508, 1116, 9969, 9091, 522, 8756, 9527, 7968, 1520, 4444, 515, 2882, 6556, 595]
Sample Output: 4444, 515, 2882, 6556, 595
import java.util.ArrayList;
import java.util.List;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String[] list = {"5672", "235", "5537", "6032", "317", "8460", "1672", "8104", "7770", "4442", "913", "2508", "1116", "9969", "9091", "522", "8756", "9527", "7968", "1520", "4444", "515", "2882", "6556", "595"};
// Convert the list of strings to an array of integers
int[] intList = new int[list.length];
for (int i = 0; i < list.length; i++) {
intList[i] = Integer.parseInt(list[i]);
}
// Find palindromes
String[] palindromes = palindromes(intList);
// Print palindromes
for (String palindrome : palindromes) {
System.out.println(palindrome);
}
}
public static String[] palindromes(int[] test_list) {
List<String> palindromes = new ArrayList<>();
for (int i = 0; i < test_list.length; i++) {
int num = test_list[i];
int reversed = 0;
int original = num;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
if (original == reversed) {
palindromes.add(String.valueOf(original));
}
}
return palindromes.toArray(new String[0]);
}
}
Main.main(new String[0]);
4444
515
2882
6556
595
Bonus Hack (for above 0.9) Use a for loop to output a spiral matrix with size n Example:
Sample Input: n = 3
Output: [[1, 2, 3], [8, 9, 4], [7, 6, 5]]
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int n = 3;
int[][] matrix = spiralMatrix(n);
for (int i = 0; i < n; i++) {
System.out.println(Arrays.toString(matrix[i]));
}
}
public static int[][] spiralMatrix(int n) {
int[][] matrix = new int[n][n];
int value = 1;
int minRow = 0;
int maxRow = n - 1;
int minCol = 0;
int maxCol = n - 1;
while (value <= n * n) {
for (int i = minCol; i <= maxCol; i++) {
matrix[minRow][i] = value;
value++;
}
minRow++;
for (int i = minRow; i <= maxRow; i++) {
matrix[i][maxCol] = value;
value++;
}
maxCol--;
for (int i = maxCol; i >= minCol; i--) {
matrix[maxRow][i] = value;
value++;
}
maxRow--;
for (int i = maxRow; i >= minRow; i--) {
matrix[i][minCol] = value;
value++;
}
minCol++;
}
return matrix;
}
}
Main.main(new String[0]);
[1, 2, 3]
[8, 9, 4]
[7, 6, 5]