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
20 changes: 14 additions & 6 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4

String[] myArray = new String[3];
// Set the value of the array at each index to be a different String
// It's OK to do this one-by-one

myArray[0] = "one";
myArray[1] = "two";
myArray[2] = "three";
// Get the value of the array at index 2

System.out.println("Value at index 2 - " + myArray[2]);
// Get the length of the array

System.out.println("Length of myArray - " + myArray.length);
// Iterate over the array using a traditional for loop and print out each item

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

System.out.println("--Printing values in myArray (for each loop)--");
for (String word : myArray) {
System.out.println(word);
}
/*
* Reminder!
*
Expand Down
35 changes: 26 additions & 9 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
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> myList = new ArrayList<>();
// Add 3 elements to the list (OK to do one-by-one)

myList.add("one");
myList.add("two");
myList.add("three");
// Print the element at index 1

System.out.println(myList.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)

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

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

if (myList.contains("zero")) {
System.out.println("String 'zero' found");
}
else {
System.out.println("String 'zero' not found");
}
// Iterate over the list using a traditional for-loop.
// Print each index and value on a separate line

System.out.println("--Printing elements unsorted--");
for (int i = 0; i < myList.size(); i++) {
System.out.println(myList.get(i));
}
// Sort the list using the Collections library

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

System.out.println("--Printing elements sorted--");
for (String word : myList) {
System.out.println(word);
}
/*
* Usage tip!
*
Expand Down
45 changes: 34 additions & 11 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,52 @@

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

myMap.put("one", 1);
myMap.put("two", 2);
myMap.put("three", 3);
System.out.println(myMap);
// Get the value associated with a given key in the Map

System.out.println("The value for key 'one' is - " + myMap.get("one"));
// Find the size (number of key/value pairs) of the Map

System.out.println("The size of myMap is - " + myMap.size());
// Replace the value associated with a given key (the size of the Map shoukld not change)

myMap.put("three", 33);
System.out.println("New myMap values are - " + myMap);
// Check whether the Map contains a given key

if (myMap.containsKey("one")) {
System.out.println("myMap does contain the key 'one'");
}
else {
System.out.println("myMap does not contain the key 'one'");
}
// Check whether the Map contains a given value

if (myMap.containsValue(1)) {
System.out.println("myMap does contain the value 'one'");
}
else {
System.out.println("myMap does not contain the key 'one'");
}
// Iterate over the keys of the Map, printing each key

System.out.println("--Printing all keys in myMap--");
for (String key : myMap.keySet()) {
System.out.println(key);
}
// Iterate over the values of the map, printing each value

System.out.println("--Printing all values in myMap--");
for (int key : myMap.values()) {
System.out.println(key);
}
// Iterate over the entries in the map, printing each key and value

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

float negFloat = -1.5F;
System.out.println(negFloat);
// Create an int with a positive value and assign it to a variable

int posInt = 7;
System.out.println(posInt);
// Use the modulo % operator to find the remainder when the int is divided by 3

int remainder = 1 % 3;
System.out.println(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 (posInt % 2 == 0) {
System.out.println("Even");
}
else {
System.out.println("Odd");
}
// Divide the number by another number using integer division

int dividedInt = posInt / 2;
System.out.println(dividedInt);
/*
* 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: " + this.name + " - Age: " + this.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 - this.age;
}

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

Person personOne = new Person("Daniel", 27);
// Create another instance of Person with a different name and age and
// assign it to a different variable

Person personTwo = new Person("Gator", 30);
// Print the first person

System.out.println("personOne - " + personOne);
// Print the second person

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

String personOneName = personOne.name;
System.out.println("The name of personOne is - " + personOneName);
// 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 personOneBirthYear = personOne.birthYear(2025);
// In a separate statement, print the local variable holding the birth year.

System.out.println("The birth year of personOne is - " + personOneBirthYear);
/**
* Terminology!
*
Expand Down
26 changes: 20 additions & 6 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
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("one");
mySet.add("two");
mySet.add("three");
System.out.println(mySet);
// Check whether the Set contains a given String

if (mySet.contains("one")) {
System.out.println("mySet does contain string 'one'");
}
else {
System.out.println("mySet does not contain string 'one'");
}
// Remove an element from the Set

mySet.remove("three");
System.out.println(mySet);
// Get the size of the Set

System.out.println("The size of the set is - " + mySet.size());
// Iterate over the elements of the Set, printing each one on a separate line

System.out.println("--Printing all elements in mySet--");
for (String word : mySet) {
System.out.println(word);
}
/*
* Warning!
*
Expand Down
40 changes: 30 additions & 10 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
import java.util.*;

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 = "daniel";
System.out.println("myString is - " + myString);
// Find the length of the string

System.out.println("The length of myString is - " + myString.length());
// Concatenate (add) two strings together and reassign the result

String myStringTwo = "grabowski";
String fullname = myString + myStringTwo;
System.out.println("The concatenated string is - " + fullname);
// Find the value of the character at index 3

System.out.println("The value of the character at index 3 is - " + myString.charAt(3));
// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)

if (myString.contains("dan")) {
System.out.println("myString does contain substring 'dan'");
}
else {
System.out.println("myString does not contain substring 'dan'");
}
// Iterate over the characters of the string, printing each one on a separate line

System.out.println("--Printing all characters in myString");
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> myList = new ArrayList<>();
// Add multiple strings to the List (OK to do one-by-one)

myList.add("one");
myList.add("two");
myList.add("three");
// 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

System.out.println("All words in myList concatenated - " + myList.get(0) + ", ".concat(myList.get(1)) + ", ".concat(myList.get(2)));
// Check whether two strings are equal

if (myString.equals(myStringTwo)) {
System.out.println("myString is equal to myStringTwo");
}
else {
System.out.println("myString is not equal to myStringTwo");
}
/*
* Reminder!
*
Expand Down
48 changes: 48 additions & 0 deletions toRefresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,52 @@

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.


Lists
----------
ArrayList replace - myList.set(int index, E element)
Adding at index - myList.add(int index, E element)
Collections.sort(myList)


Arrays
----------
Creating an array {
String[] myArray = new String[5]; // Where 5 is the length of the array
}


Strings
----------
Concatenation method - myString.concat(myStringTwo)


Maps
----------
For itteration {
map.keySet() // To itterate keys
map.values() // To itterate values
map.entrySet() // To itterate both
}

entrySet() example {
for (Map.Entry<String, Integer> entry : map.entrySet()) {
String key = entry.getKey();
Integer value = entry.getValue();
// do something with key and value
}
}


Classes and objects/instances
----------
Overriding the toString method:
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}

-