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
44 changes: 42 additions & 2 deletions src/main/java/Collections/Practice/CollectionBasics.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

public class CollectionBasics {
public static void main(String[] args) {
Expand Down Expand Up @@ -34,6 +35,15 @@ public static int sum(Collection<Integer> numbers) {
// Loop through the collection
// Add each number to total


//loop through each item in numbers
for(int item :numbers){

//add each item in loop to the total
total = total + item;
}

//return final total value
return total;
}

Expand All @@ -50,6 +60,15 @@ public static int countEven(Collection<Integer> numbers) {
// Loop through the collection
// If the number is even, increase count

//loop through each item in numbers
for(int item : numbers){

//if item is divisible by 2 (remainder of item / 2 is equal to 0)
//then increment count by 1.
if(item % 2 ==0){
count++;
}
}
return count;
}

Expand All @@ -65,8 +84,16 @@ public static int findMax(Collection<Integer> numbers) {
// TODO:
// Loop through numbers
// Update max if current number is larger
for(int item : numbers){
//if item is greater than max
if(item > max){
//set max to item
max = item;
}
}


return max;
return max;
}


Expand All @@ -81,7 +108,20 @@ public static boolean hasDuplicates(Collection<Integer> numbers) {
// Hint:
// Compare the size of a collection with the size of a Set

return false;
//make new set, populate set with values in numbers.
//so this set will be a numbers list but without duplicates.

HashSet<Integer> set = new HashSet<>();

//add to set for each item in numbers
//but there is something that a
// set returns when you add something to a set, and its already in the set.
for(int item : numbers){


}

return false;
}


Expand Down
36 changes: 19 additions & 17 deletions src/main/java/Hierachy/PRACTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ B. Iterable
C. List
D. Queue

---
Answer :B.

## Question 2

Expand All @@ -27,7 +27,7 @@ B. Collection
C. Map
D. Deque

---
Answer :B.

## Question 3

Expand All @@ -38,7 +38,7 @@ B. Set
C. Queue
D. Map

---
Answer :D.

## Question 4

Expand All @@ -49,7 +49,8 @@ B. List
C. Queue
D. TreeSet

---
Answer :B.


## Question 5

Expand All @@ -60,17 +61,18 @@ B. Set
C. Queue
D. Map

---
Answer :D.


# True or False

Write **True** or **False**.

1. `ArrayList` implements the `List` interface.
2. `Set` allows duplicate elements.
3. `Map` stores elements using keys and values.
4. `Queue` typically follows FIFO behavior.
5. `Iterable` allows collections to be used in enhanced for-loops.
1. `ArrayList` implements the `List` interface. Answer : True
2. `Set` allows duplicate elements. Answer : False
3. `Map` stores elements using keys and values. Answer : True
4. `Queue` typically follows FIFO behavior. Answer : True
5. `Iterable` allows collections to be used in enhanced for-loops. Answer : True

---

Expand All @@ -80,13 +82,13 @@ Write **True** or **False**.

What is the difference between an **interface** and a **class** in the Java Collections Framework?

---
Answer: Interface defines behaviors and class implements those behaviors

## Question 2

Why is it recommended to declare variables using interfaces like `List` instead of concrete classes like `ArrayList`?

Example:
Answer: It provides better flexibilities, abstraction and better code design.

```java
List<String> list = new ArrayList<>();
Expand All @@ -97,8 +99,9 @@ List<String> list = new ArrayList<>();
## Question 3

Name three interfaces that extend `Collection`.
Answer: Lists, Set, and Queue.


---

# Code Reading Exercise

Expand All @@ -117,10 +120,9 @@ for (String fruit : fruits) {

Questions:

1. Which interface type is used for the variable?
2. Which class is used as the implementation?
3. Why can the enhanced for-loop be used here?

1. Which interface type is used for the variable? Answer: List
2. Which class is used as the implementation? Answer: ArrayList
3. Why can the enhanced for-loop be used here? Answer:Lists extends Collections which extends iterable and the enhanced for loop can be used for on all collections.
---

# Coding Exercise
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/Iterable/Examples/ForEachLoopDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

public class ForEachLoopDemo {
public static void main(String[] args) {

List<String> students = new ArrayList<>();

students.add("Alex");
Expand All @@ -15,23 +14,27 @@ public static void main(String[] args) {

System.out.println("Printing students using a for-each loop:");

// TODO:
// Use a for-each loop to print each student name

// Loop through each student and print their name
for (String student : students) {
System.out.println(student);
}

System.out.println("\nPrinting students in uppercase:");

// TODO:
// Use a for-each loop to print each name in uppercase

// Loop through each student and print name in uppercase
for (String student : students) {
System.out.println(student.toUpperCase());
}

System.out.println("\nCount the number of students:");

int count = 0;

// TODO:
// Use a for-each loop to count how many students are in the list
// Loop through students and increment count
for (String student : students) {
count++;
}

System.out.println("Total students: " + count);
}
}
}
24 changes: 15 additions & 9 deletions src/main/java/Iterable/Examples/IteratorDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

public class IteratorDemo {
public static void main(String[] args) {

List<Integer> numbers = new ArrayList<>();

numbers.add(10);
Expand All @@ -18,26 +17,33 @@ public static void main(String[] args) {
System.out.println("Original list:");
System.out.println(numbers);

// Create iterator
// Create iterator
Iterator<Integer> iterator = numbers.iterator();

System.out.println("\nIterating using Iterator:");

// TODO:
// Use iterator.hasNext() and iterator.next()
// Print each number
// Loop through list using iterator
while (iterator.hasNext()) {

int number = iterator.next(); // get next number
System.out.println(number); // print it
}

System.out.println("\nRemoving odd numbers using Iterator");

iterator = numbers.iterator();

// TODO:
// Use iterator to remove odd numbers
// Remember: use iterator.remove()
// Remove odd numbers
while (iterator.hasNext()) {

int number = iterator.next();

if (number % 2 != 0) { // check if number is odd
iterator.remove(); // remove it safely
}
}

System.out.println("\nUpdated list:");
System.out.println(numbers);
}
}
}
67 changes: 34 additions & 33 deletions src/main/java/Iterable/Practice/IterableWarmups.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,18 @@
import java.util.List;

public class IterableWarmups {
public static void main(String[] args) {

List<Integer> numbers = new ArrayList<>();

numbers.add(3);
numbers.add(7);
numbers.add(10);
numbers.add(4);
numbers.add(8);

System.out.println("Sum: " + sum(numbers));
System.out.println("Even count: " + countEven(numbers));
System.out.println("Max value: " + findMax(numbers));
}


/*
PROBLEM 1
Return the sum of all numbers in the iterable
*/
PROBLEM 1
Return the sum of all numbers in the iterable
*/
public static int sum(Iterable<Integer> numbers) {

int total = 0;

// TODO:
// Use a for-each loop to calculate the sum
// Loop through each number in the iterable
for (int num : numbers) {
total += num; // add each number to total
}

return total;
}
Expand All @@ -38,14 +24,19 @@ public static int sum(Iterable<Integer> numbers) {
/*
PROBLEM 2
Count how many numbers are even
*/
*/
public static int countEven(Iterable<Integer> numbers) {

int count = 0;

// TODO:
// Loop through numbers
// Increment count if number is even
// Loop through each number
for (int num : numbers) {

// Check if the number is even
if (num % 2 == 0) {
count++; // increase counter
}
}

return count;
}
Expand All @@ -54,14 +45,19 @@ public static int countEven(Iterable<Integer> numbers) {
/*
PROBLEM 3
Return the maximum value
*/
*/
public static int findMax(Iterable<Integer> numbers) {

int max = Integer.MIN_VALUE;

// TODO:
// Loop through numbers
// Update max if current number is larger
for (int num : numbers) {

// If current number is greater than max
if (num > max) {
max = num; // update max
}
}

return max;
}
Expand All @@ -70,15 +66,20 @@ public static int findMax(Iterable<Integer> numbers) {
/*
PROBLEM 4 (BONUS)
Count how many times a word appears
*/
*/
public static int countMatches(Iterable<String> words, String target) {

int count = 0;

// TODO:
// Loop through words
// Compare each word to target
// Loop through each word
for (String word : words) {

// Compare word to target
if (word.equals(target)) {
count++;
}
}

return count;
}
}
}
Loading