Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
65a6427
negative float
DarienArthur-Gocken Sep 25, 2025
1c97eb2
positive int
DarienArthur-Gocken Sep 25, 2025
ccbf171
modulo remainder
DarienArthur-Gocken Sep 25, 2025
7306a78
Even/Odd numbers.
DarienArthur-Gocken Sep 25, 2025
4a00923
Dividing ints
DarienArthur-Gocken Sep 25, 2025
9ffe1cd
Make arraylist
DarienArthur-Gocken Sep 25, 2025
636ebda
adding elements to list
DarienArthur-Gocken Sep 25, 2025
8f1efd3
setting at specific indexes
DarienArthur-Gocken Sep 25, 2025
b644faf
Check for specific string
DarienArthur-Gocken Sep 25, 2025
8e0d8ce
Iterate over list
DarienArthur-Gocken Sep 25, 2025
6bbf327
sort & iterate foreach
DarienArthur-Gocken Sep 26, 2025
cbdad62
make new strings array
DarienArthur-Gocken Sep 26, 2025
da85422
string values set and checked.
DarienArthur-Gocken Sep 26, 2025
5a6f52c
print array length
DarienArthur-Gocken Sep 26, 2025
ca720b5
iterate array loops
DarienArthur-Gocken Sep 26, 2025
3ca8cc6
string init & length
DarienArthur-Gocken Sep 26, 2025
6ed3c5d
concat strings & find value of char
DarienArthur-Gocken Sep 26, 2025
ecfc8db
check contains, check each char in string, new arraylist
DarienArthur-Gocken Sep 26, 2025
fe71163
adding to array, tostring, checking equals.
DarienArthur-Gocken Sep 26, 2025
61bbd15
init hashset, add elements
DarienArthur-Gocken Sep 26, 2025
a485bb2
hashset contains, remove, size
DarienArthur-Gocken Sep 26, 2025
edc69f4
iterate over set
DarienArthur-Gocken Sep 26, 2025
fb033e8
init hashmap and fill
DarienArthur-Gocken Sep 26, 2025
2b271d5
get value, size, replace, and check contains
DarienArthur-Gocken Sep 26, 2025
b59de3f
iterating over hashmap
DarienArthur-Gocken Sep 26, 2025
c44bd3b
Person class impl
DarienArthur-Gocken Sep 26, 2025
087de3e
main impl
DarienArthur-Gocken Sep 26, 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
13 changes: 13 additions & 0 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4
String[] strings = 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
strings[0] = "Hey";
strings[1] = "Test";
strings[2] = "Third";
strings[3] = "Fourth";

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

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

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

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

/*
* Reminder!
Expand Down
25 changes: 24 additions & 1 deletion src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
import java.util.ArrayList;
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
ArrayList<String> strings = new ArrayList<String>();
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.

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 list (OK to do one-by-one)
strings.add("Hey");
strings.add("hey2");
strings.add("whats up");

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

// Insert a new element at index 0 (the length of the list will change)
strings.add(0, "even newer value");
System.out.println(strings.get(0));


// Check whether the list contains a certain string
if(strings.indexOf("new value") >= 0) {
System.out.println("Found.");
} else {
System.out.println("Not found.");
}

// Iterate over the list using a traditional for-loop.
// Print each index and value on a separate line
for(int i = 0; i < strings.size(); i++) {
System.out.println(i + strings.get(i));
}

// Sort the list using the Collections library
Collections.sort(strings);

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

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

import java.util.HashMap;

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
HashMap<String, Integer> strIntHashMap = new HashMap<>();

// Put 3 different key/value pairs in the Map
// (it's OK to do this one-by-one)
strIntHashMap.put("test1", 1);
strIntHashMap.put("test2", 2);
strIntHashMap.put("test3", 3);

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

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

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

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

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

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

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

// Iterate over the entries in the map, printing each key and value
for(HashMap.Entry<String, Integer> entry : strIntHashMap.entrySet()) {
System.out.println(entry.getKey() + " - " + entry.getValue());
}

/*
* Usage tip!
Expand Down
16 changes: 16 additions & 0 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
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.5f;
//System.out.println(negativeFloat);

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

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

// Divide the number by another number using integer division
int number1 = 20;
int number2 = 10;
int result = number1 / number2;
System.out.println(result);

/*
* Reminder!
Expand Down
20 changes: 20 additions & 0 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +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) {
age = _age;
name = _name;
}


// Create a toString method that gives the name and age of the person
public String toString() {
return name + " - " + age;
}


// Implement the below public instance method "birthYear"
Expand All @@ -28,25 +37,36 @@ public class Person {
* @return The year the person was born
*/
// (create the instance method here)
public int birthYear(int currentYear) {
int bornOn = currentYear - age;
return bornOn;
}


public static void main(String[] args) {
// Create an instance of Person
Person person = new Person("Person1", 20);

// Create another instance of Person with a different name and age and
// assign it to a different variable
Person person2 = new Person("Person2", 25);

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

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

// Get the name of the first person and store it in a local variable
String firstPersonName = person.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 bornOn = person.birthYear(2025);

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

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

// Add 3 elements to the set
// (It's OK to do it one-by-one)
hashStrings.add("test1");
hashStrings.add("test2");
hashStrings.add("test3");

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

// Remove an element from the Set
hashStrings.remove("test3");

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

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

/*
* Warning!
Expand Down
23 changes: 23 additions & 0 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
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 fiveCharacterString = "Tests";

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

// Concatenate (add) two strings together and reassign the result
String stringOne = "String1";
String stringTwo = "String2";
stringOne = stringOne.concat(stringTwo);
System.out.println(stringOne);

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

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

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

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

// Add multiple strings to the List (OK to do one-by-one)
arrayOfStrings.add("string1");
arrayOfStrings.add("string2");
arrayOfStrings.add("String3");

// 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(arrayOfStrings.toString());

// Check whether two strings are equal
if(stringOne.equals(stringTwo)) {
System.out.println("equal");
} else {
System.out.println("Not equal.");
}

/*
* Reminder!
Expand Down