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 src/ArrayPractice.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,40 @@ public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4

String[] words = 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

words[0] = "Array";
words[1] = "Practice";
words[2] = "For";
words[3] = "Class";

// Get the value of the array at index 2

String valueAtIndex2 = words[2];
System.out.println("Value at index 2: " + valueAtIndex2);

// Get the length of the array

int length = words.length;
System.out.println("Length of the array: " + length);

// Iterate over the array using a traditional for loop and print out each item

System.out.println("Using a for-each loop:");
for (String word : words) {
System.out.println(word);
}
// Iterate over the array using a for-each loop and print out each item

/*
* Reminder!
*
* Arrays start at index 0
*/


}
}
32 changes: 23 additions & 9 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
import java.util.*;

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> items = new ArrayList<>();
// Add 3 elements to the list (OK to do one-by-one)

items.add("Number 1");
items.add("Number 2");
items.add("Number 3");
// Print the element at index 1

System.out.println("Element at index 1: " + items.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)

items.set(1, "Practice");
System.out.println("Updated element at index 1: " + items.get(1));
// Insert a new element at index 0 (the length of the list will change)

items.add(0, "Start");
System.out.println("Index 0: " + items);
// Check whether the list contains a certain string

boolean containsCode = items.contains("Code");
System.out.println("Does this lest contain 'Code'? " + containsCode);
// Iterate over the list using a traditional for-loop.
// Print each index and value on a separate line

System.out.println("Using a traditional for-loop:");
for (int i = 0; i < items.size(); i++) {
System.out.println("Index " + i + ": " + items.get(i));
}
// Sort the list using the Collections library

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

System.out.println("Using a for-each loop:");
for (String item : items) {
System.out.println(item);
}
/*
* Usage tip!
*
Expand Down
34 changes: 23 additions & 11 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@

import java.util.*;

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> pointsScored = new HashMap<>();
// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)

pointsScored.put("Alex", 28);
pointsScored.put("Samuel", 30);
pointsScored.put("Jimmy", 42);
// Get the value associated with a given key in the Map

System.out.println("Score for Alex: " + pointsScored.get("Alex"));
// Find the size (number of key/value pairs) of the Map

System.out.println("Number of entries in the map: " + pointsScored.size());
// Replace the value associated with a given key (the size of the Map shoukld not change)

pointsScored.put("Alex", 31);
System.out.println("Updated score for Alex: " + pointsScored.get("Alex"));
// Check whether the Map contains a given key

System.out.println("Does the map contain the key 'Jimmy'? " + pointsScored.containsKey("Jimmy"));
// Check whether the Map contains a given value

System.out.println("Does the map contain the value 42? " + pointsScored.containsValue(42));
// Iterate over the keys of the Map, printing each key

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

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

System.out.println("Entries in the map:");
for (Map.Entry<String, Integer> entry : pointsScored.entrySet()) {
System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
}
/*
* Usage tip!
*
Expand Down
17 changes: 13 additions & 4 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable

float negativeFloat = -15.5f;
System.out.println("Negative float: " + negativeFloat);
// Create an int with a positive value and assign it to a variable

int positiveInt = 19;
System.out.println("Positive int: " + positiveInt);
// Use the modulo % operator to find the remainder when the int is divided by 3

int remainder = positiveInt % 3;
System.out.println("Remainder when " + positiveInt + " is divided by 3: " + remainder);
// 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 (positiveInt % 2 == 0) {
System.out.println(positiveInt + " is Even");
} else {
System.out.println(positiveInt + " is Odd");
}
// Divide the number by another number using integer division
int result = positiveInt / 4; // Divide by 4
System.out.println("Result of integer division of " + positiveInt + " by 4: " + result);

/*
* Reminder!
Expand Down
32 changes: 21 additions & 11 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
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

@Override
public String toString() {
return "Name: " + name + ", Age: " + age;
}

// Implement the below public instance method "birthYear"
// There should NOT be any print statement in this method.
Expand All @@ -28,26 +35,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 person1 = new Person("Jimmy", 23);
// Create another instance of Person with a different name and age and
// assign it to a different variable

Person person2 = new Person("John", 29);
// Print the first person

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

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

String firstName = person1.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 birthYear = person1.birthYear(2025);
// In a separate statement, print the local variable holding the birth year.

System.out.println("Birth year of " + firstName + ": " + birthYear);

/**
* Terminology!
*
Expand Down
25 changes: 19 additions & 6 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import java.util.*;
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> mySet = new HashSet<>();
// Add 3 elements to the set
// (It's OK to do it one-by-one)

mySet.add("Cookies");
mySet.add("Cupcakes");
mySet.add("Brownies");
// Check whether the Set contains a given String

String searchElement = "Cupcakes";
if (mySet.contains(searchElement)) {
System.out.println(searchElement + " is in the set.");
} else {
System.out.println(searchElement + " is not in the set.");
}
// Remove an element from the Set

mySet.remove("Brownies");
System.out.println("After removing 'Brownies': " + mySet);
// Get the size of the Set

int setSize = mySet.size();
System.out.println("Size of the set: " + setSize);
// Iterate over the elements of the Set, printing each one on a separate line

System.out.println("Elements in the set:");
for (String element : mySet) {
System.out.println(element);
}
/*
* Warning!
*
Expand Down
35 changes: 25 additions & 10 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
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 myString = "Hello";
// Find the length of the string

int length = myString.length();
System.out.println("Length of the string: " + length);
// Concatenate (add) two strings together and reassign the result

String newString = myString + " World";
System.out.println("Concatenated string: " + newString);
// Find the value of the character at index 3

char charAtIndex3 = myString.charAt(3);
System.out.println("Character at index 3: " + charAtIndex3);
// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)

boolean containsSubstring = myString.contains("abc");
System.out.println("Does the string contain 'abc'? " + containsSubstring);
// Iterate over the characters of the string, printing each one on a separate line

System.out.println("Characters in the string:");
for (int i = 0; i < myString.length(); i++) {
System.out.println(myString.charAt(i));
}
// Create an ArrayList of Strings and assign it to a variable

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

stringList.add("Apples");
stringList.add("Carrots");
stringList.add("Pomegranites");
// 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 joinedString = String.join(", ", stringList);
System.out.println("Joined list into a single string: " + joinedString);
// Check whether two strings are equal

String anotherString = "Hello";
boolean areStringsEqual = myString.equals(anotherString);
System.out.println("Are the two strings equal? " + areStringsEqual);
/*
* Reminder!
*
Expand Down
6 changes: 5 additions & 1 deletion toRefresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

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.

-
- String Practice
- Set Practice
- List Practice
- Overall, I do need to look over all of these a little more to get them down
- Because I am a little shaky on them.