Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
e11b182
float datatype
liliann19 Oct 1, 2025
e93e6a0
Positive int number
liliann19 Oct 1, 2025
9eb9164
Find remainder using modulo
liliann19 Oct 1, 2025
477b58e
Determine whether the num is even or odd
liliann19 Oct 1, 2025
4d908f2
Divide num by another number
liliann19 Oct 1, 2025
6844f34
Created an empty ArrayList of Strings
liliann19 Oct 1, 2025
76c31c5
Added 3 elements to the list and updated toRefresh
liliann19 Oct 1, 2025
97094ec
Print element at index, updated toRefresh
liliann19 Oct 1, 2025
e3bb283
replaced element at index, updated toRefresh
liliann19 Oct 1, 2025
a245bb3
Inserted new element at index
liliann19 Oct 1, 2025
47cbc1f
Checked list for certain string
liliann19 Oct 1, 2025
e265ccb
Using traditional for-loop to iterate list and print index-value, upd…
liliann19 Oct 1, 2025
7a52071
Used the Collections library, updated toRefresh
liliann19 Oct 1, 2025
249b9d4
Used for-each loop to print each value
liliann19 Oct 1, 2025
701eb23
Created array of Strings, update toRefresh
liliann19 Oct 1, 2025
f376119
Added values to each index in array
liliann19 Oct 1, 2025
be3dab2
Get value at index 2
liliann19 Oct 1, 2025
827ed3d
Find the length of the array
liliann19 Oct 1, 2025
c5fb9c3
Traditional for-loop to print each item, updated toRefresh
liliann19 Oct 1, 2025
1106f23
Used for-each loop to print out items
liliann19 Oct 1, 2025
9365beb
Assigned String to variable
liliann19 Oct 1, 2025
2ee4e8e
Find length of the string
liliann19 Oct 1, 2025
8f680c1
Concatenate two strings together
liliann19 Oct 1, 2025
4f37863
Find character at index
liliann19 Oct 1, 2025
b7e4637
Added a space between the two strings
liliann19 Oct 1, 2025
ce8bcea
Check String for substring
liliann19 Oct 1, 2025
0c28e0d
Iterate through characters of the string, updated toRefresh
liliann19 Oct 1, 2025
4650784
Created an ArrayList of Strings
liliann19 Oct 1, 2025
f7fa0f1
Add multiple strings to arraylist
liliann19 Oct 1, 2025
0ce987b
Used built in method to join strings together, updated toRefresh
liliann19 Oct 1, 2025
ba695b4
Check if strings are equal
liliann19 Oct 1, 2025
58ebbf0
Edit code on line 27, if string contains substring
liliann19 Oct 1, 2025
1caadcf
Created HashSet
liliann19 Oct 1, 2025
3215d82
add 3 elements to set
liliann19 Oct 1, 2025
c9bb46c
check if set contains certain string
liliann19 Oct 1, 2025
836a624
Removed element from set
liliann19 Oct 1, 2025
2376040
Get size of set
liliann19 Oct 1, 2025
a3b8d6e
Iterate the set, updated toRefresh
liliann19 Oct 1, 2025
a61b1ed
Created HashMap with String, Integer
liliann19 Oct 1, 2025
3ec0925
Added key/value pairs in Map, updated toRefresh
liliann19 Oct 1, 2025
1be48c5
Get value to a give key, updated toRefresh
liliann19 Oct 1, 2025
76e8a24
Find size of map
liliann19 Oct 1, 2025
8d547ec
Replaced value with given key, updated toRefresh
liliann19 Oct 1, 2025
15a6bd9
Check map if contains given key
liliann19 Oct 1, 2025
15b8f6f
Check map if contains given value
liliann19 Oct 1, 2025
45f4481
Iterate keys of Map, updated toRefresh
liliann19 Oct 1, 2025
1f89273
Iterate values in map
liliann19 Oct 1, 2025
15dec6c
Iterate over key/value pairs, updated toRefresh
liliann19 Oct 1, 2025
7783699
Declared instance variables
liliann19 Oct 1, 2025
c1f3dd4
Created constructor, updated toRefresh
liliann19 Oct 1, 2025
c973a11
Created toString, updated toRefresh
liliann19 Oct 1, 2025
c482443
Created birthYear method
liliann19 Oct 1, 2025
c12ae7a
Created an instance of Person
liliann19 Oct 1, 2025
686ff42
Created second intance of Person
liliann19 Oct 1, 2025
69432e7
Print first person
liliann19 Oct 1, 2025
c976449
Print second person
liliann19 Oct 1, 2025
e48dc70
Store first person's name in variable
liliann19 Oct 1, 2025
8272fcd
Find the persons birth year using method
liliann19 Oct 2, 2025
05b34b8
Print first persons birth year, updated toRefresh
liliann19 Oct 2, 2025
e606dbc
Reviewed code and edited some
liliann19 Oct 2, 2025
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
18 changes: 18 additions & 0 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@

public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4
String[] array = 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
array[0] = "Noodles";
array[1] = "Rice";
array[2] = "Pasta";
array[3] = "Bread";
// System.out.println(array[0]);
// System.out.println(array[1]);
// System.out.println(array[2]);
// System.out.println(array[3]);

// Get the value of the array at index 2
System.out.println(array[2]);

// Get the length of the array
System.out.println(array.length);

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

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

/*
* Reminder!
Expand Down
23 changes: 22 additions & 1 deletion src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,48 @@
public class ListPractice {
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> list = new ArrayList<>();

// Add 3 elements to the list (OK to do one-by-one)
list.add("Hi");
list.add("Hello");
list.add("Good morning");
System.out.println(list);

// Print the element at index 1
System.out.println(list.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)
list.set(1,"Hey");
System.out.println(list);

// Insert a new element at index 0 (the length of the list will change)
list.add(0, "Good night");
System.out.println(list);

// Check whether the list contains a certain string
System.out.println(list.contains("Hey"));

// 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++){
String word = list.get(i);
System.out.println("Value at index " + i + ": " + word);
}

// Sort the list using the Collections library
Collections.sort(list);
System.out.println(list);

// Iterate over the list using a for-each loop
// Print each value on a second line
for (String str : list){
System.out.println(str);
}

/*
* Usage tip!
Expand Down
22 changes: 21 additions & 1 deletion src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@

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> dict = new HashMap<>();

// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
dict.put("Pooh", 1);
dict.put("Tigger", 3);
dict.put("Eeyore", 6);
System.out.println(dict);

// Get the value associated with a given key in the Map
System.out.println(dict.get("Eeyore"));

// Find the size (number of key/value pairs) of the Map
System.out.println(dict.size());

// Replace the value associated with a given key (the size of the Map shoukld not change)
dict.replace("Pooh", 9);
System.out.println(dict);

// Check whether the Map contains a given key
System.out.println(dict.containsKey("Tigger"));

// Check whether the Map contains a given value
System.out.println(dict.containsValue(9));

// Iterate over the keys of the Map, printing each key
for (String str : dict.keySet()){
System.out.println(str);
}

// Iterate over the values of the map, printing each value
for (int v : dict.values()){
System.out.println(v);
}

// Iterate over the entries in the map, printing each key and value
for (String s : dict.keySet()){
System.out.println("Key: " + s + ", Value: " + dict.get(s));
}

/*
* Usage tip!
Expand Down
11 changes: 11 additions & 0 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable
float num = -2.345f;
System.out.println(num);

// Create an int with a positive value and assign it to a variable
int positiveNum = 5;
System.out.println(positiveNum);

// Use the modulo % operator to find the remainder when the int is divided by 3
System.out.println(positiveNum % 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 (positiveNum % 2 == 0){
System.out.println("Even");
} else {
System.out.println("Odd");
}

// Divide the number by another number using integer division
System.out.println(positiveNum / 5);

/*
* Reminder!
Expand Down
25 changes: 22 additions & 3 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 "Name: " + name + " Age: " + age;
}

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


public static void main(String[] args) {
// Create an instance of Person
Person p1 = new Person("Harry Potter", 27);

// Create another instance of Person with a different name and age and
// assign it to a different variable
Person p2 = new Person("Ron Weasley", 28);

// Print the first person
System.out.println(p1);

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

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

// 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 age1 = p1.age; - don't need this
int birthYear1 = p1.birthYear(2025);

// In a separate statement, print the local variable holding the birth year.
System.out.println(birthYear1);

/**
* Terminology!
Expand Down
14 changes: 14 additions & 0 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +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> set = new HashSet<>();

// Add 3 elements to the set
// (It's OK to do it one-by-one)
set.add("Pen");
set.add("Pencil");
set.add("Eraser");
System.out.println(set);

// Check whether the Set contains a given String
System.out.println(set.contains("Pen"));

// Remove an element from the Set
System.out.println(set.remove("Eraser"));
System.out.println(set);

// Get the size of the Set
System.out.println(set.size());

// Iterate over the elements of the Set, printing each one on a separate line
for (String str : set) {
System.out.println(str);
}

/*
* Warning!
Expand Down
33 changes: 32 additions & 1 deletion src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,56 @@
import java.util.ArrayList;

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 = "October";
System.out.println(word);

// Find the length of the string
System.out.println(word.length());

// Concatenate (add) two strings together and reassign the result

String word2 = "November";
String newWord = word + " " + word2;
System.out.println(newWord);

// Find the value of the character at index 3
System.out.println(newWord.charAt(3));

// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)
// if (newWord.contains("Nov")){
// System.out.println("True");
// } else {
// System.out.println("False");
// }

System.out.println(newWord.contains("Nov"));

// Iterate over the characters of the string, printing each one on a separate line
for (int i = 0; i < newWord.length(); i++){
char c = newWord.charAt(i);
System.out.println(c);
}

// Create an ArrayList of Strings and assign it to a variable
ArrayList<String> list = new ArrayList<>();

// Add multiple strings to the List (OK to do one-by-one)
list.add("Blue");
list.add("White");
list.add("Green");
list.add("Red");
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 joinedString = String.join(", ", list);
System.out.println(joinedString);

// Check whether two strings are equal
String s = list.get(1);
String s2 = list.get(2);
System.out.println(s.equals(s2));

/*
* Reminder!
Expand Down
21 changes: 20 additions & 1 deletion toRefresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,23 @@

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.

-
- float datatype
- Syntax to create empty ArrayList/List
- .get() to find element at index
- .set() to replace an element at index
- .size() to get number of elements in list
- Collections.sort()
- Arrays vs Lists
- traditional for-loop for arrays
- charAt() to return the character at index
- .join() to join strings seperated by commas
- .equals() syntax
- iterate a HashSet
- .put() for Maps
- .get() access an item in HashMap
- HashMap ordering
- iterate a HashMap
- print both keys and values in Hashmap
- creating a constructor
- creating toString()
- review Java OOP