Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8b48d15
Created a float variable with a negative value
Jan 3, 2025
49125c2
Created an int variable with a positive value
Jan 3, 2025
3d8ef77
Created a remainder variable to store the remainder of the int variab…
Jan 3, 2025
886beea
Wrote an if-else statements to determine whether an integer number is…
Jan 3, 2025
be06136
Created a divison variable and performed an integer division, also sh…
Jan 3, 2025
076ffa1
Started working on second set of problems, created an empty ArrayList…
Jan 3, 2025
1fb96f2
Printing the element at index 1 of the ArrayList
Jan 3, 2025
e725aff
Inserted elements at certain spots without changing the original size…
Jan 3, 2025
1f50926
Checking whether the ArrayList contains a certain string in the console
Jan 3, 2025
62d2bd1
Wrote a regular for-loop to iterate through an ArrayList and use a pr…
Jan 3, 2025
ba9c1f6
Sorted an ArrayList using Collections library and imported a statemen…
Jan 3, 2025
31b7e99
Wrote a for-each loop for the ArrayList and printed each item in sort…
Jan 3, 2025
7fe3cdb
Started a third set of problems, created a basic String array and pop…
Jan 3, 2025
63f987e
Console the value from the array at index 2, and showed the length of…
Jan 3, 2025
308ff89
Finished up third set of problems, and wrote traditional and for-each…
Jan 3, 2025
063f4c7
Started fourth set of problems, and created and assigned values to va…
Jan 3, 2025
2ade961
Wrote additional print statements
Jan 3, 2025
3f82e1f
Wrote a for-loop for the String to print each character on a seperate…
Jan 3, 2025
8e0b53b
Finished up the problems in fourth set
Jan 3, 2025
1777173
Started a fifth set of problems, created a HashSet and added some val…
Jan 3, 2025
03fdca1
Practiced with HashSet and looped over the Set
Jan 3, 2025
c9ffd43
Started sixth set of problems, created a HashMap and populated it wit…
Jan 3, 2025
5b669e8
Practiced with HashMap and wrote additional print statements for check
Jan 3, 2025
dbdf939
Wrote a loop to iterate over the Keys for the HashMap and additional …
Jan 3, 2025
548fb58
Wrote for loops for keys, values, and both
Jan 3, 2025
d5f5ff7
Started last set of problems, creating public and private instance va…
Jan 3, 2025
7b87e0e
Wrote a toString method
Jan 3, 2025
c6e7195
Created an instance method for birth year
Jan 3, 2025
203f329
Created two instances of Person class to test the code
Jan 3, 2025
c99d377
Finished up the problems, wrote main method
Jan 3, 2025
d7a20d6
Added topics to Refresh later on
Jan 3, 2025
e5d9e2e
Final Touches and double checking all the directions and files
Jan 3, 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: 13 additions & 5 deletions src/ArrayPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
public class ArrayPractice {
public static void main(String[] args) {
// Create an array of Strings of size 4

String[] myArr = 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
myArr[0] = "Hello";
myArr[1] = "Bonjour";
myArr[2] = "Hola";
myArr[3] = "Salam";

// Get the value of the array at index 2

System.out.println("Value at index 2: " + myArr[2]);
// Get the length of the array

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

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

for (String item : myArr) {
System.out.println(item);
}
/*
* 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.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> myList = new ArrayList<>();
// Add 3 elements to the list (OK to do one-by-one)

myList.add("Apple");
myList.add("Cherry");
myList.add("Banana");
// Print the element at index 1

System.out.println("Element at index 1: " + myList.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)

myList.set(1, "Strawberry");
System.out.println("Size of the List after the first insertion: " + myList.size());
// Insert a new element at index 0 (the length of the list will change)

myList.set(0, "Kiwi");
System.out.println("Size of the List after the second insertion: " + myList.size());
// Check whether the list contains a certain string

System.out.println(myList.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 < myList.size(); i++) {
System.out.println("Index " + i + ": " + myList.get(i));
}
// Sort the list using the Collections library

Collections.sort(myList);
System.out.println("Sorted List: " + myList);
// Iterate over the list using a for-each loop
// Print each value on a second line

for (String fruit : myList) {
System.out.println(fruit);
}
/*
* Usage tip!
*
Expand Down
34 changes: 22 additions & 12 deletions src/MapPractice.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@

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

myMap.put("Alice", 75);
myMap.put("Brandon", 88);
myMap.put("Emily", 96);
// Get the value associated with a given key in the Map

System.out.println("Valued assosiated with a given key of 'Alice': " + myMap.get("Alice"));
// 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("Size of the Map: " + myMap.size());
// Replace the value associated with a given key (the size of the Map should not change)
myMap.put("Alice", 76);
System.out.println("Updated value for 'Alice' key: " + myMap.get("Alice"));
// Check whether the Map contains a given key

System.out.println("Does the Map contains the key 'Edward'? " + myMap.containsKey("Edward"));
// Check whether the Map contains a given value

System.out.println("Does the Map contains the value 88? " + myMap.containsValue(88));
// Iterate over the keys of the Map, printing each key

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

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

for (Map.Entry<String, Integer> entry : myMap.entrySet()) {
System.out.println("Key: " + entry.getKey() + " | Value: " + entry.getValue());
}
/*
* Usage tip!
*
Expand Down
14 changes: 10 additions & 4 deletions src/NumberPractice.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
public class NumberPractice {
public static void main(String args[]) {
// Create a float with a negative value and assign it to a variable
float floatNum = -7.7f;

// Create an int with a positive value and assign it to a variable

int intNum = 9;
// Use the modulo % operator to find the remainder when the int is divided by 3

int remainder = intNum % 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 (intNum % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
// Divide the number by another number using integer division

int divResult = 6 / 2;
System.out.println("Result of the division by 2: " + divResult);
/*
* Reminder!
*
Expand Down
34 changes: 21 additions & 13 deletions src/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,56 @@
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.
/**
* birthYear returns the year the person was born.
*
* The birth year is calculated by subtracting the person's age from currentYear
* The birth year is calculated by subtracting the person's age from current Year
* that's passed in as an int. It assumes that the person's birthday has already
* passed this year.
*
* @param currentYear an int for the current year
* @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 firstPerson = new Person("Ali", 22);
// Create another instance of Person with a different name and age and
// assign it to a different variable

Person secondPerson = new Person("Mark", 18);
// Print the first person

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

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

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

System.out.println(firstPersonName + "'s Birth year: " + firstPersonBirthYear);
/**
* 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;
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> mySet = new HashSet<>();
// Add 3 elements to the set
// (It's OK to do it one-by-one)

mySet.add("Math");
mySet.add("English");
mySet.add("Music");
// Check whether the Set contains a given String

System.out.println("Does the Set contains Math? " + mySet.contains("Math"));
// Remove an element from the Set

mySet.remove("Math");
// Get the size of the Set

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

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

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

myWord = myWord + " Friend";
System.out.println("Concatenated String: " + myWord);
// Find the value of the character at index 3

System.out.println("Value of the character at index 3 is " + myWord.charAt(3));
// Check whether the string contains a given substring (i.e. does the string have "abc" in it?)

System.out.println("Does the String have 'abc' in it? " + myWord.contains("abc"));
// Iterate over the characters of the string, printing each one on a separate line

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

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

stringList.add("Cucumber");
stringList.add("Carrot");
stringList.add("Pepper");
// 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("Joined String: " + String.join(", ", stringList));
// Check whether two strings are equal

String anotherWord = "Welcome Friend";
System.out.println("Are the Strings equal to each other? " + myWord.equals(anotherWord));
/*
* Reminder!
*
Expand Down
3 changes: 2 additions & 1 deletion toRefresh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

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 Variables
- Java Classes and instance fields