Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions hw0b/src/Dessert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
public class Dessert {
int flavor;
int price;
static int numDesserts = 0;

public Dessert(int flavor, int price) {
this.flavor = flavor;
this.price = price;
numDesserts++;
}

public void printDessert() {
System.out.println(flavor + " " + price + " " + numDesserts);
}

public static void main(String[] args) {
System.out.println("I love dessert!");
}
}
42 changes: 34 additions & 8 deletions hw0b/src/JavaExercises.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,43 @@ public class JavaExercises {

/** Returns an array [1, 2, 3, 4, 5, 6] */
public static int[] makeDice() {
// TODO: Fill in this function.
return null;
int[] arr = new int[6];
for (int i = 0; i < 6; i++) {
arr[i] = i + 1;
}
return arr;
}

/** Returns the order depending on the customer.
* If the customer is Ergun, return ["beyti", "pizza", "hamburger", "tea"].
* If the customer is Erik, return ["sushi", "pasta", "avocado", "coffee"].
* In any other case, return an empty String[] of size 3. */
public static String[] takeOrder(String customer) {
// TODO: Fill in this function.
return null;
String[] arr;
if (customer.equals("Ergun")) {
arr = new String[] {"beyti", "pizza", "hamburger", "tea"};
} else if (customer.equals("Erik")) {
arr = new String[] {"sushi", "pasta", "avocado", "coffee"};
} else {
arr = new String[3];
}
return arr;
}

/** Returns the positive difference between the maximum element and minimum element of the given array.
* Assumes array is nonempty. */
public static int findMinMax(int[] array) {
// TODO: Fill in this function.
return 0;
if (array.length < 1) throw new IllegalArgumentException("There are no elements in the array!");

int min = array[0];
int max = array[0];

for (int i = 1; i < array.length; i++) {
if (array[i] < min) min = array[i];
if (array[i] > max) max = array[i];
}

return Math.abs(max - min);
}

/**
Expand All @@ -38,8 +57,15 @@ public static List<Integer> hailstone(int n) {
}

private static List<Integer> hailstoneHelper(int x, List<Integer> list) {
// TODO: Fill in this function.
return null;
list.add(x);
if (x == 1) {
return list;
}
if (x % 2 == 0) {
return hailstoneHelper(x / 2, list);
} else {
return hailstoneHelper(x * 3 + 1, list);
}
}

}
35 changes: 27 additions & 8 deletions hw0b/src/ListExercises.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ListExercises {

/** Returns the total sum in a list of integers */
public static int sum(List<Integer> L) {
// TODO: Fill in this function.
return 0;
int sum = 0;
for (int item : L) {
sum += item;
}
return sum;
}

/** Returns a list containing the even numbers of the given list */
public static List<Integer> evens(List<Integer> L) {
// TODO: Fill in this function.
return null;
List<Integer> output = new ArrayList<>();
for (int item : L) {
if (item % 2 == 0) output.add(item);
}
return output;
}

/** Returns a list containing the common item of the two given lists */
public static List<Integer> common(List<Integer> L1, List<Integer> L2) {
// TODO: Fill in this function.
return null;
List<Integer> output = new ArrayList<>();
Set<Integer> l2Set = new HashSet<>(L2);
for (int item : L1) {
if (l2Set.contains(item)) output.add(item);
}
return output;
}


/** Returns the number of occurrences of the given character in a list of strings. */
public static int countOccurrencesOfC(List<String> words, char c) {
// TODO: Fill in this function.
return 0;
int count = 0;
for (String word : words) {
char[] wordArr = word.toCharArray();
for (char character : wordArr) {
if (character == c) count++;
}
}
return count;
}
}
33 changes: 27 additions & 6 deletions hw0b/src/MapExercises.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand All @@ -6,21 +7,41 @@ public class MapExercises {
* 1, 'b' is 2, 'c' is 3, ..., 'z' is 26.
*/
public static Map<Character, Integer> letterToNum() {
// TODO: Fill in this function.
return null;
int counter = 1;
Map<Character, Integer> map = new HashMap<>();

for (int i = 97; i <= 122; i++) {
map.put((char) i, counter);
counter++;
}
return map;
}

/** Returns a map from the integers in the list to their squares. For example, if the input list
* is [1, 3, 6, 7], the returned map goes from 1 to 1, 3 to 9, 6 to 36, and 7 to 49.
*/
public static Map<Integer, Integer> squares(List<Integer> nums) {
// TODO: Fill in this function.
return null;
Map<Integer, Integer> map = new HashMap<>();

for (int num : nums) {
map.put(num, num * num);
}

return map;
}

/** Returns a map of the counts of all words that appear in a list of words. */
public static Map<String, Integer> countWords(List<String> words) {
// TODO: Fill in this function.
return null;
Map<String, Integer> map = new HashMap<>();

for (String word : words) {
if (map.containsKey(word)) {
map.put(word, map.get(word) + 1);
} else {
map.put(word, 1);
}
}

return map;
}
}
4 changes: 0 additions & 4 deletions hw0b/tests/DessertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ public class DessertTest {
@Order(0)
@DisplayName("Test Dessert class")
public void testDessert() {
// TODO: Uncomment this test when you've created and completed Dessert.java!
// TODO: Delete lines 24 and 60 of this file to uncomment.
boolean completed = false;

/*
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));

Expand Down Expand Up @@ -57,7 +54,6 @@ public void testDessert() {
.isEqualTo("I love dessert!");

completed = true;
*/

// Check that assertions were run
if (!completed) {
Expand Down