Skip to content
20 changes: 15 additions & 5 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4

String[] newArray = new String[4];
// Set the value of the array at each index to be a different String
// It's OK to do this one-by-one
newArray[0] = "Hello";
newArray[1] = "My";
newArray[2] = "Name's";
newArray[3] = "Tav";

// Get the value of the array at index 2

System.out.println(newArray[2]);
// Get the length of the array

System.out.println("The length of the array is: " + newArray.length);
// Iterate over the array using a traditional for loop and print out each item

for (int i = 0; i < newArray.length; i++) {
System.out.println(newArray[i]);
}
// Iterate over the array using a for-each loop and print out each item

// Breaking to show for each
System.out.println();
for (String words:newArray) {
System.out.println(words);
}
/*
* Reminder!
*
Expand Down
30 changes: 22 additions & 8 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ListPractice {


public static void main(String[] args) {
// Create an empty ArrayList of Strings and assign it to a variable of type List

List<String> stringList = new ArrayList<>();
// Add 3 elements to the list (OK to do one-by-one)

stringList.add("Pulp");
stringList.add("Round");
stringList.add("Strange");
// Print the element at index 1
System.out.println(stringList.get(1));

// Replace the element at index 1 with a new value
// (Do not insert a new value. The length of the list should not change)

stringList.set(1, "Juice");
System.out.println(stringList.get(1));
// Insert a new element at index 0 (the length of the list will change)

stringList.add(0, "Oh");
System.out.println(stringList.get(0));
// Check whether the list contains a certain string

System.out.println(stringList.contains("Juice"));
// Iterate over the list using a traditional for-loop.
// Print each index and value on a separate line

for (int i = 0; i < stringList.size(); i++) {
System.out.println("Index: " + i + ", Value: " +stringList.get(i));
}
// Sort the list using the Collections library

Collections.sort(stringList);
System.out.println(stringList);
// Iterate over the list using a for-each loop
// Print each value on a second line

for (String word : stringList) {
System.out.println(word);
}
/*
* Usage tip!
*
Expand Down
35 changes: 25 additions & 10 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@
import java.util.HashMap;
import java.util.Map;


public class MapPractice {
public static void main(String[] args) {
// Create a HashMap with String keys and Integer values and
// assign it to a variable of type Map

Map<String, Integer> newMap = new HashMap<>();
// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
newMap.put("For", 1);
newMap.put("Gas", 2);
newMap.put("Port", 3);

// Get the value associated with a given key in the Map

// Get the value associated with a given key in the Map
System.out.println(newMap.get("Port"));
// Find the size (number of key/value pairs) of the Map

// Replace the value associated with a given key (the size of the Map shoukld not change)

System.out.println(newMap.size());
// Replace the value associated with a given key (the size of the Map should not change)
newMap.replace("For", 9);
// Check whether the Map contains a given key

System.out.println(newMap.containsKey("Port"));
// Check whether the Map contains a given value

System.out.println(newMap.containsValue(4));
// Iterate over the keys of the Map, printing each key

System.out.println("Keys:");
for (String keys : newMap.keySet()) {
System.out.println(keys);
}
// Iterate over the values of the map, printing each value

System.out.println("Values:");
for (Integer values : newMap.values()) {
System.out.println(values);
}
// Iterate over the entries in the map, printing each key and value

System.out.println("Both Keys and Values");
for (Map.Entry<String, Integer> set : newMap.entrySet()) {
System.out.println("Keys: " + set.getKey() + ", Values: " + set.getValue());
}
/*
* Usage tip!
*
Expand Down
14 changes: 9 additions & 5 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable

float floatingNum = -2l;
// Create an int with a positive value and assign it to a variable

int regNum = 22;
// Use the modulo % operator to find the remainder when the int is divided by 3

System.out.println(regNum % 3);
// Use the modulo % operator to determine whether the number is even
// (A number is even if it has a remainder of zero when divided by 2)
// Use an if-else to print "Even" if the number is even and "Odd"
// if the number is odd.

if (regNum % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
// Divide the number by another number using integer division

System.out.println(regNum / 6);
/*
* Reminder!
*
Expand Down
29 changes: 19 additions & 10 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
public class Person {
// Declare a public String instance variable for the name of the person
// Declare a private int instance variable for the age of the person

public String name;
private int age;

// Create a constructor that takes the name and age of the person
// and assigns it to the instance variables

public Person(String name, int age) {
this.name = name;
this.age = age;
}

// Create a toString method that gives the name and age of the person

public String toString() {
return "This is " + name + ", they are " + age;
}

// Implement the below public instance method "birthYear"
// There should NOT be any print statement in this method.
Expand All @@ -28,26 +34,29 @@ public class Person {
* @return The year the person was born
*/
// (create the instance method here)
public int birthYear(int currentYear) {
return currentYear - age;
}


public static void main(String[] args) {
// Create an instance of Person

Person dude = new Person("Dante", 20);
// Create another instance of Person with a different name and age and
// assign it to a different variable

Person dudeTwo = new Person("Vergil", 21);
// Print the first person

System.out.println(dude);
// Print the second person

System.out.println(dudeTwo);
// Get the name of the first person and store it in a local variable

String fName = dude.name;
// Using the birthYear method, get the birth year of the first person
// and store it in a local variable. Input the actual current year (e.g. 2025)
// as the argument.

int fBirth = dude.birthYear(2025);
// In a separate statement, print the local variable holding the birth year.

System.out.println(fBirth);
/**
* Terminology!
*
Expand Down
22 changes: 17 additions & 5 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import java.util.HashSet;
import java.util.Set;

public class SetPractice {
public static void main(String[] args) {
// Create a HashSet of Strings and assign it to a variable of type Set
Set<String> newSet = new HashSet<>();

// Add 3 elements to the set
// (It's OK to do it one-by-one)

newSet.add("Hello");
newSet.add("World");
newSet.add("Smile?");
// Check whether the Set contains a given String

if (newSet.contains("Smile?")) {
System.out.println("True");
} else {
System.out.println("False");
}
// Remove an element from the Set

newSet.remove("Hello");
// Get the size of the Set

System.out.println(newSet.size());
// Iterate over the elements of the Set, printing each one on a separate line

for (String word : newSet) {
System.out.println(word);
}
/*
* Warning!
*
Expand Down
38 changes: 29 additions & 9 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
import java.util.ArrayList;
import java.util.List;

public class StringPractice {
public static void main(String[] args) {
// Create a string with at least 5 characters and assign it to a variable

String word = "Fives";
// Find the length of the string

System.out.println(word.length());
// Concatenate (add) two strings together and reassign the result

word += " high?";
System.out.println(word);
// Find the value of the character at index 3

System.out.println(word.charAt(3));
// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)

if (word.contains("high")) {
System.out.println("True");
} else {
System.out.println("False");
}
// Iterate over the characters of the string, printing each one on a separate line

for (Character letter : word.toCharArray()) {
System.out.println(letter);
}
// Create an ArrayList of Strings and assign it to a variable

List<String> newList = new ArrayList<>();
// Add multiple strings to the List (OK to do one-by-one)
newList.add("Apple");
newList.add("bottom");
newList.add("jeans");
newList.add("boots");
newList.add("with the?");
Comment on lines +28 to +32
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎶


// Join all of the strings in the list together into a single string separated by commas
// Use a built-in method to achieve this instead of using a loop

String lyric = String.join(", ", newList);
System.out.println(lyric);
// Check whether two strings are equal

if (lyric.equals(word)) {
System.out.println("True");
} else {
System.out.println("False");
}
/*
* Reminder!
*
Expand Down
3 changes: 2 additions & 1 deletion toRefresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

As you work through this exercise, write down anything that you needed to look up or struggled to remember here. It can be just a word or two (e.g. "joining strings"). You can use this as a guide of what to make extra sure you're refreshed on before exams and interviews.

-
- Refresh on methods used in general
- Refresh on java constructors and fields