Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
62552ef
Added a print statement to the Array file
SouthBennett Sep 29, 2025
0c4b977
Added a traditional for loop and a for each loop to the Array file
SouthBennett Sep 29, 2025
4dbd5a2
Imported java.util.ArrayList and List
SouthBennett Sep 29, 2025
a268f80
Replaced an element at index 1 of the list
SouthBennett Sep 29, 2025
6fe1bea
Created a for loop to iterate over the list
SouthBennett Sep 29, 2025
3a69fa8
Created a for each loop and printed each value on a second line
SouthBennett Sep 29, 2025
bd6f491
Created a HashMap, added 3 different key value pairs, and printed out…
SouthBennett Sep 30, 2025
c1b556a
Created a for loop and a for each loop to print out each item
SouthBennett Sep 30, 2025
9189ec7
Added a print statement to output the size of the map and replaced ke…
SouthBennett Sep 30, 2025
a77e206
Added a print statement checking if the map contain a given value, an…
SouthBennett Sep 30, 2025
42bcdbe
Added a for each loop to iterate through the map and print out key an…
SouthBennett Sep 30, 2025
8423fba
Used modulo to find the remainder when the int is divided by 3
SouthBennett Sep 30, 2025
15020ec
Created an if-else statement and performed integer division
SouthBennett Sep 30, 2025
b6a87c7
Created instance variables for name and age and a parameterized const…
SouthBennett Sep 30, 2025
f4c411a
Created a toString method for the name and age
SouthBennett Sep 30, 2025
ee07659
Added a birthYear method to return the birth year of the person
SouthBennett Sep 30, 2025
9ddcb62
Created two instances of Person and printed out both
SouthBennett Sep 30, 2025
1a9585d
Added a getter method for name and called the method in the main.
SouthBennett Sep 30, 2025
16871fe
Stored the birth year of the first person in a variable and printed o…
SouthBennett Sep 30, 2025
491ec74
Created a new HashSet and assigned it to a variable of type set
SouthBennett Sep 30, 2025
510357e
Added a for each loop to iterate over elements in the HashSet
SouthBennett Oct 1, 2025
13afa4a
Checked whether a string contains a given substring
SouthBennett Oct 1, 2025
369f5b9
Checked whether two strings are equal
SouthBennett Oct 1, 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
14 changes: 13 additions & 1 deletion src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4
String words[] = 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
words[0] = "Bike";
words[1] = "Car";
words[2] = "Boat";
words[3] = "Motorcycle";

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

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

// Iterate over the array using a traditional for loop and print out each item

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

/*
* Reminder!
Expand Down
28 changes: 25 additions & 3 deletions src/ListPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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> words = new ArrayList<>();


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

words.add("Marvel Comics");
words.add("Detective Comics");
words.add("Image Comics");
// Print the element at index 1
System.out.println(words.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)
words.set(1, "Dark Horse Comics");
System.out.println(words.get(1));

// Insert a new element at index 0 (the length of the list will change)
words.add(0, "IDW Publishing");

// Check whether the list contains a certain string
System.out.println(words.contains("Dark Horse Comics"));

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

// Print each index and value on a separate line
System.out.println(words.get(i));
}
// Sort the list using the Collections library
System.out.println("before sort: " + words);
Collections.sort(words);
System.out.println("after sort: " + words);

// Iterate over the list using a for-each loop
// Print each value on a second line
for (String word : words) {

// Print each value on a second line
System.out.println(word);
}
/*
* Usage tip!
*
Expand Down
25 changes: 24 additions & 1 deletion src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

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("Xavier", 1);
map.put("Jhoanna", 2);
map.put("Thor", 3);

// Get the value associated with a given key in the Map
int value = map.get("Xavier");
System.out.println("The value associated with key: 'Xavier' is " + value);

// Find the size (number of key/value pairs) of the Map
System.out.println("The size of the map is: " + map.size());

// Replace the value associated with a given key (the size of the Map shoukld not change)
map.replace("Thor", 23);
System.out.println(map.get("Thor"));
System.out.println(map.size());

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

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

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

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

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

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

// Create an int with a positive value and assign it to a variable
int positiveNum = 10;

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

// Divide the number by another number using integer division
int quotient = positiveNum / 5;
System.out.println("Answer: " + quotient);

/*
* Reminder!
Expand Down
33 changes: 28 additions & 5 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
// Declare a private int instance variable for the age of the person
public String name; // instance variable (can be accessed outside class )

// Declare a private int instance variable for the age of the person
private int age;// can only be accessed within the class (private)

// Create a constructor that takes the name and age of the person
public Person(String name, int age) {
// and assigns it to the instance variables


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 +35,41 @@ public class Person {
* @return The year the person was born
*/
// (create the instance method here)
public int birthYear(int currentYear) {
int yearOfBirth = currentYear - age;
return yearOfBirth;
}

// Getter Method
public String getName() {
return this.name;
}


public static void main(String[] args) {
// Create an instance of Person
Person p = new Person("Xavier", 35);

// Create another instance of Person with a different name and age and
// assign it to a different variable
Person pTwo = new Person("Jhoanna", 36);

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

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

// Get the name of the first person and store it in a local variable

System.out.println(p.getName());

// 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 bornYear = p.birthYear(2025);

// In a separate statement, print the local variable holding the birth year.
System.out.println("The year of birth for " + p.getName() + " is: " + bornYear);

/**
* Terminology!
Expand Down
15 changes: 14 additions & 1 deletion src/SetPractice.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import java.util.HashSet;
import java.util.Set;

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> words = new HashSet<>();

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

words.add("The");
words.add("Fantastic");
words.add("Four");

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

// Remove an element from the Set
words.remove("Four");

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

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

/*
* Warning!
Expand Down
31 changes: 29 additions & 2 deletions src/StringPractice.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
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 firstName = "Xavier";
// Find the length of the string
int nameLength = firstName.length();
System.out.println("Length of name: " + nameLength);

// Concatenate (add) two strings together and reassign the result
String lastName = "Lewis";

String fullName = firstName + " " + lastName;

System.out.println(fullName);

// Find the value of the character at index 3
char c = fullName.charAt(3);
System.out.println("The value of the character at index 3 is: " + c);

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

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

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

// Add multiple strings to the List (OK to do one-by-one)
words.add("Apple");
words.add("Banana");
words.add("Orange");

// 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 joinedStrings = String.join("," , words);
System.out.println(joinedStrings);

// Check whether two strings are equal

if (firstName.equals(lastName)) {
System.out.println("equal");
} else {
System.out.println("not equal");
}

/*
* Reminder!
*
Expand Down