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
21 changes: 16 additions & 5 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@

public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4

String[] list = 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
list[0] = "hola";
list[1] = "mi";
list[2] = "nombre";
list[3] = "Kevin";

// Get the value of the array at index 2

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

System.out.println(list.length);
// Iterate over the array using a traditional for loop and print out each item

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

for (String str: list){
System.out.println(str);
}
System.out.println();
/*
* Reminder!
*
Expand Down
31 changes: 22 additions & 9 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
public class ListPractice {


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

// Add 3 elements to the list (OK to do one-by-one)

List<String> list = new ArrayList<>();
// Add 3 elements to the lisst (OK to do one-by-one)
list.add("hello");
list.add("I'm");
list.add("Kevin");
// Print the element at index 1

System.out.println(list.get(1));
// Replace the element at index 1 with a new value
list.set(1, "replaced");
System.out.println(list);
// (Do not insert a new value. The length of the list should not change)

// Insert a new element at index 0 (the length of the list will change)

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

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

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

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

System.out.println();
for (String str : list){
System.out.print(str + " ");
}
/*
* 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.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> mapStr = new HashMap<>();
// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)

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

System.out.println(mapStr.get("one"));
// Find the size (number of key/value pairs) of the Map

System.out.println(mapStr.size());
// Replace the value associated with a given key (the size of the Map shoukld not change)

mapStr.replace("one", 5);
System.out.println(mapStr);
// Check whether the Map contains a given key

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

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

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

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

for (Map.Entry<String, Integer> entry : mapStr.entrySet()){
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
/*
* Usage tip!
*
Expand Down
18 changes: 13 additions & 5 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +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 = -1;
System.out.println(negativeFloat);
// Create an int with a positive value and assign it to a variable

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

positiveInt = positiveInt % 3;
System.out.println(positiveInt);
// 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("true");
} else {
System.out.println("false");
}
// Divide the number by another number using integer division

positiveInt = positiveInt / 3;
System.out.println(positiveInt);
/*
* Reminder!
*
Expand Down
31 changes: 21 additions & 10 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@
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 +36,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("Gary", 2);
// Create another instance of Person with a different name and age and
// assign it to a different variable
Person person2 = new Person("Spongebob", 20);

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

System.out.println(birthYear1);
/**
* Terminology!
*
Expand Down
19 changes: 13 additions & 6 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import java.util.HashSet;

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

HashSet<String> setStr = new HashSet<>();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Here and elsewhere, use interface types (List, Map, etc.) where appropriate.
For example:
List<String> strings = new ArrayList<>();
Note that the type on the left is List, not ArrayList. When we do this, we're more flexible to be able to change our code to use a different type of list later.

Similarly for maps:
Map<String, String> myMap = new HashMap<>();
Note that on the left we use Map instead of HashMap.

And sets:
Set<String> strings = new HashSet<>();
Note that on the left we use Set instead of HashSet.

In summary:

  • interface type on left to declare type (List, Map, etc.)
  • Concrete type on right to instantiate instance (HashMap, ArrayList etc.)

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

setStr.add("hi");
setStr.add("im");
setStr.add("Kevin");
System.out.println(setStr);
// Check whether the Set contains a given String

System.out.println(setStr.contains("hi"));
// Remove an element from the Set

setStr.remove("hi");
// Get the size of the Set

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

for (String str : setStr) {
System.out.println(str);
}
/*
* Warning!
*
Expand Down
31 changes: 21 additions & 10 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
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 str = "hello";
// Find the length of the string

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

String newStr = str + "Kevin";
System.out.println(newStr);
// Find the value of the character at index 3

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

System.out.println(str.contains("abc"));
// Iterate over the characters of the string, printing each one on a separate line

for (int i = 0; i<str.length(); i++){
System.out.println(str.charAt(i));
}
// Create an ArrayList of Strings and assign it to a variable

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

list.add("my");
list.add("name");
list.add("is");
list.add("Kevin");
System.out.println(list);
// 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 combinedList = String.join(", ", list);
System.out.println(combinedList);
// Check whether two strings are equal

System.out.println(str.equals(combinedList));
/*
* Reminder!
*
Expand Down
4 changes: 3 additions & 1 deletion toRefresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

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.

-
- how to insert at certain points in ArrayList
- .join() for string
- how to interate over maps and entries