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: 15 additions & 4 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[] arr = 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
arr[0] = "Hello";
arr[1] = "World";
arr[2] = "Java";
arr[3] = "Arrays";

// Get the value of the array at index 2
System.out.println("Index 2: " + arr[2]);

// Get the length of the array

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

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

System.out.println("For-each loop:");
for (String s : arr) {
System.out.println(s);
}
/*
* Reminder!
*
Expand Down
35 changes: 26 additions & 9 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
public class ListPractice {
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;

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("Apple");
list.add("Banana");
list.add("Cherry");
// Print the element at index 1
System.out.println("Index 1: " + 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, "Blueberry");
// Insert a new element at index 0 (the length of the list will change)

list.add(0, "Date");
// Check whether the list contains a certain string

System.out.println("Contains Apple? " + list.contains("Apple"));
// 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
for (String item : list) {
System.out.println(item);
}
// Print each value on a second line
for (String item : list) {
System.out.println(item);
}

/*
* Usage tip!
*
* Use a traditional for-loop when you need to use the index or you need to iterate in an
* Use a traditional for-loop when you need to use the index or you need to
* iterate in an
* unconventional order (e.g. backwards)
*
* Otherwise, if you're iterating the in the conventional order and don't need the
* Otherwise, if you're iterating the in the conventional order and don't need
* the
* index values a for-each loop is cleaner.
*/
}
Expand Down
44 changes: 30 additions & 14 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,56 @@

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

map.put("Apples", 5);
map.put("Bananas", 10);
map.put("Cherries", 15);
// Get the value associated with a given key in the Map

System.out.println("Value for 'Bananas': " + map.get("Bananas"));
// 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("Map size: " + map.size());
// Replace the value associated with a given key (the size of the Map shoukld
// not change)
map.put("Bananas", 12);
System.out.println("Updated value for 'Bananas': " + map.get("Bananas"));
// Check whether the Map contains a given key

System.out.println("Contains key 'Apples'? " + map.containsKey("Apples"));
// Check whether the Map contains a given value

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

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

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

System.out.println("Entries in map:");
for (Map.Entry<String, Integer> entry : map.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
/*
* Usage tip!
*
* Maps are great when you want a specific key to value mapping.
* Example: The key could be a person's name, and the value could be their phone number
* Example: The key could be a person's name, and the value could be their phone
* number
*
* However if your keys are simple ascending 0-indexed integers with no gaps
* (0, 1, 2, 3, 4...) then an array or List is likely a better choice.
* Example: If you want to store the order of songs in a playlist.
*
* If you're finding that you're just wanting to store unordered values and the keys
* If you're finding that you're just wanting to store unordered values and the
* keys
* are unimportant, a Set may be a better choice.
* Example: If you want to hold the student ID numbers of everyone in a course,
* and you don't care about any ordering.
Expand Down
20 changes: 14 additions & 6 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable

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

int positiveInt = 10;
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 of " + positiveInt + " % 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 divided = positiveInt / 4;
System.out.println(positiveInt + " / 4 = " + divided);
/*
* Reminder!
*
* When dividing ints, the result is rounded down.
* Example:
* Example:
* 7 / 3 = 2 when performing int division
*/

Expand Down
40 changes: 26 additions & 14 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@

public class Person {
// Declare a public String instance variable for the name of the person
public String name;
// Declare a private int instance variable for the age of the person

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 "Person{name='" + name + "', age=" + age + "}";
}

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

Person person2 = new Person("Bob", 30);
// 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(firstName + " was born in " + birthYear);
/**
* Terminology!
*
* A class is the overall definition, like a blueprint.
* An instance is a specific object made according to that definition.
* We use "instance" and "object" to mean the same thing.
*
* For example, if there is a Person class, we can make an instance of a specific person: Auberon.
* For example, if there is a Person class, we can make an instance of a
* specific person: Auberon.
*
* There can be many instances for the same class. For example: Auberon, Xinting, Baya are all
* There can be many instances for the same class. For example: Auberon,
* Xinting, Baya are all
* different instances of the Person class.
*
* Each instance has its own instance variables: Auberon's age can be different from Baya's age.
* Each instance has its own instance variables: Auberon's age can be different
* from Baya's age.
*/
}
}
27 changes: 19 additions & 8 deletions src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,37 @@
import java.util.Set;
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

Set<String> set = new HashSet<>();
// Add 3 elements to the set
// (It's OK to do it one-by-one)

set.add("Apple");
set.add("Banana");
set.add("Cherry");
// Check whether the Set contains a given String

System.out.println("Contains 'Banana'? " + set.contains("Banana"));
// Remove an element from the Set

set.remove("Apple");
System.out.println("Removed 'Apple'");
// Get the size of the Set

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

System.out.println("Elements in set:");
for (String item : set) {
System.out.println(item);
}
/*
* Warning!
*
* The iteration order over the items in a HashSet is NOT GUARANTEED.
*
* Even running the exact same program multiple times may give different results.
* Even running the exact same program multiple times may give different
* results.
* Do not use a HashSet if order is important! You can use a TreeSet if you
* want items in sorted order, or an array or List if you want them in a specified
* want items in sorted order, or an array or List if you want them in a
* specified
* order.
*
* Also remember that sets do NOT have duplicates.
Expand Down
42 changes: 29 additions & 13 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
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 text = "HelloWorld";
// Find the length of the string

System.out.println("Length: " + text.length());
// Concatenate (add) two strings together and reassign the result

text = text + "123";
System.out.println("After concatenation: " + text);
// Find the value of the character at index 3

// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)

// Iterate over the characters of the string, printing each one on a separate line

System.out.println("Character at index 3: " + text.charAt(3));
// Check whether the string contains a given substring (i.e. does the string
// have "abc" in it?)
System.out.println("Contains 'abc'? " + text.contains("abc"));
// 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 < text.length(); i++) {
System.out.println(text.charAt(i));
}
// Create an ArrayList of Strings and assign it to a variable

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

// Join all of the strings in the list together into a single string separated by commas
words.add("Apple");
words.add("Banana");
words.add("Cherry");
// 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 joined = String.join(", ", words);
System.out.println("Joined list: " + joined);
// Check whether two strings are equal

String s1 = "Hello";
String s2 = "hello";
System.out.println("s1 equals s2? " + s1.equals(s2));
System.out.println("s1 equalsIgnoreCase s2? " + s1.equalsIgnoreCase(s2));
/*
* Reminder!
*
Expand Down