Skip to content

Commit

Permalink
Add ArrayLists and while loops assignments (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahsnider authored Jun 29, 2023
1 parent 22d03b6 commit 65a6ed2
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 0 deletions.
42 changes: 42 additions & 0 deletions app/src/main/java/assignments/lists1/FilterList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package assignments.lists1;

import java.util.ArrayList;
import java.util.List;

public class FilterList {
// Create a function that accepts two lists of strings.
// Modify the first list so that it only contains strings that are also in the second list.
// You probably want to use List#contains() for this.
// Don't return anything.

// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv YOUR CODE vvvvvvvvvvvvvvvvvvvvvvvvvvvvv

private static void filterList(ArrayList<String> input, ArrayList<String> filter) {}

// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ YOUR CODE ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

private static void testFilterList(
List<String> input, List<String> filter, List<String> expected) {
System.out.println("");
ArrayList<String> result = new ArrayList<>(input);
filterList(result, new ArrayList<>(filter));
boolean pass = result.equals(expected);
__testResults +=
String.format(
"filterList(%s, %s) -> %s | %s | %s \n",
input, filter, expected, result, pass ? "OK " : "X ");
}

private static String __testResults = "";

public static void main(String[] args) {
testFilterList(List.of("a", "b", "c"), List.of("a", "b", "c"), List.of("a", "b", "c"));
testFilterList(List.of("a", "b", "c"), List.of("a", "b", "d"), List.of("a", "b"));
testFilterList(List.of("a", "b", "c"), List.of("d", "e", "f"), List.of());
testFilterList(List.of("a", "b", "c"), List.of(), List.of());
testFilterList(List.of(), List.of("a", "b", "c"), List.of());
testFilterList(List.of(), List.of(), List.of());
System.out.println("Function Call -> Expected | Yours | Pass?\n");
System.out.println(__testResults);
}
}
35 changes: 35 additions & 0 deletions app/src/main/java/assignments/lists1/MultiplyUntil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package assignments.lists1;

public class MultiplyUntil {
// Create a function that accepts an int and multiplies it by 3.
// Store that number, and keep multiplying by 3 until the number is greater than 100.
// Count the number of times you multiplied, and return that number.

// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv YOUR CODE vvvvvvvvvvvvvvvvvvvvvvvvvvvvv

private static int multiplyUntil(int value) {
return 0;
}

// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ YOUR CODE ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

private static void testMultiplyUntil(int input, int expected) {
System.out.println("");
int result = multiplyUntil(input);
boolean pass = result == expected;
__testResults +=
String.format(
"multiplyUntil(%d) -> %d | %d | %s \n", input, expected, result, pass ? "OK " : "X ");
}

private static String __testResults = "";

public static void main(String[] args) {
testMultiplyUntil(1, 5);
testMultiplyUntil(2, 4);
testMultiplyUntil(40, 1);
testMultiplyUntil(200, 0);
System.out.println("Function Call -> Expected | Yours | Pass?\n");
System.out.println(__testResults);
}
}
36 changes: 36 additions & 0 deletions app/src/main/java/assignments/lists1/SplitString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package assignments.lists1;

import java.util.ArrayList;
import java.util.List;

public class SplitString {
// Create a function that accepts a string, splits it into each character, and then returns a list
// of the characters

// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv YOUR CODE vvvvvvvvvvvvvvvvvvvvvvvvvvvvv

private static ArrayList<String> splitString(String input) {
return new ArrayList<String>();
}

// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ YOUR CODE ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

private static void testSplitString(String input, List<String> expected) {
System.out.println("");
List<String> result = splitString(input);
boolean pass = result.equals(expected);
__testResults +=
String.format(
"splitString(\"%s\") -> %s | %s | %s \n", input, expected, result, pass ? "OK " : "X ");
}

private static String __testResults = "";

public static void main(String[] args) {
testSplitString("Hello", List.of("H", "e", "l", "l", "o"));
testSplitString("World", List.of("W", "o", "r", "l", "d"));
testSplitString("Hello World", List.of("H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"));
System.out.println("Function Call -> Expected | Yours | Pass?\n");
System.out.println(__testResults);
}
}
36 changes: 36 additions & 0 deletions app/src/main/java/assignments/lists1/SquareList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package assignments.lists1;

import java.util.ArrayList;
import java.util.List;

public class SquareList {
// Create a function that accepts a list of ints and returns a list of the squares of those ints.

// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv YOUR CODE vvvvvvvvvvvvvvvvvvvvvvvvvvvvv

private static ArrayList<Integer> squareList(ArrayList<Integer> input) {
return new ArrayList<Integer>();
}

// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ YOUR CODE ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

private static void testSquareList(List<Integer> input, List<Integer> expected) {
System.out.println("");
List<Integer> result = squareList(new ArrayList(input));
boolean pass = result.equals(expected);
__testResults +=
String.format(
"squareList(%s) -> %s | %s | %s \n", input, expected, result, pass ? "OK " : "X ");
}

private static String __testResults = "";

public static void main(String[] args) {
testSquareList(List.of(1, 2, 3), List.of(1, 4, 9));
testSquareList(List.of(2, 4, 6), List.of(4, 16, 36));
testSquareList(List.of(0, 0, 0), List.of(0, 0, 0));
testSquareList(List.of(-1, -2, -3), List.of(1, 4, 9));
System.out.println("Function Call -> Expected | Yours | Pass?\n");
System.out.println(__testResults);
}
}
37 changes: 37 additions & 0 deletions app/src/main/java/assignments/lists1/SumUntil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package assignments.lists1;

import java.util.ArrayList;
import java.util.List;

public class SumUntil {
// Create a function that calculates the sum of numbers in a list.
// Once the sum exceeds 20, or you run out of numbers, stop adding numbers and return the sum.

// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv YOUR CODE vvvvvvvvvvvvvvvvvvvvvvvvvvvvv

private static int sumUntil(ArrayList<Integer> input) {
return 0;
}

// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ YOUR CODE ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

private static void testSumUntil(List<Integer> input, int expected) {
System.out.println("");
int result = sumUntil(new ArrayList(input));
boolean pass = result == expected;
__testResults +=
String.format(
"sumUntil(%s) -> %s | %s | %s \n", input, expected, result, pass ? "OK " : "X ");
}

private static String __testResults = "";

public static void main(String[] args) {
testSumUntil(List.of(5, 10, 6, 5), 21);
testSumUntil(List.of(2, 4, 6), 12);
testSumUntil(List.of(10, 10, 10), 30);
testSumUntil(List.of(0, 0, 0), 0);
System.out.println("Function Call -> Expected | Yours | Pass?\n");
System.out.println(__testResults);
}
}

0 comments on commit 65a6ed2

Please sign in to comment.