forked from davidbernson/Java-Programmering-23
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# Java Collections - Practice Exercises | ||
_Disclaimer_: The following exercises have been created using AI Tools, they are based on information from lecture 7 of the course "Programmering för testare" at ITHS. | ||
|
||
## Warm-up Exercise 🌅 | ||
Before diving into collections, let's warm up our programming skills! | ||
|
||
Create a simple array of integers with the values 1, 2, 3, 4, 5. Write code to: | ||
1. Print all numbers | ||
2. Print the sum of all numbers | ||
3. Print all numbers in reverse order | ||
|
||
This will help you remember array basics before we move to collections! | ||
|
||
## "Easy" Exercises 🌱 | ||
|
||
### B1: List Basics | ||
Create an `ArrayList<String>` that contains your favorite colors. Write code to: | ||
- Add at least 3 colors | ||
- Print all colors | ||
- Check if "blue" exists in your list | ||
- Remove one color and print the updated list | ||
|
||
### B2: Set Introduction | ||
Create a `HashSet<Integer>` and demonstrate how it handles duplicates: | ||
- Add the numbers: 1, 2, 2, 3, 3, 3 | ||
- Print the set | ||
- Print the size of the set | ||
- Try to explain why the size is what it is | ||
|
||
### B3: Queue Practice | ||
Create a `Queue<String>` representing people waiting in line at a coffee shop: | ||
- Add at least 3 people to the queue | ||
- Print who is first in line | ||
- Serve (remove) the first person | ||
- Add another person to the queue | ||
- Print the entire queue | ||
|
||
## Intermediate Exercises 🌿 | ||
|
||
### I1: List Operations | ||
Create an `ArrayList<Integer>` with numbers 1-10. Write methods to: | ||
- Remove all even numbers | ||
- Double all remaining numbers | ||
- Calculate the average of the remaining numbers | ||
- Print the final list and the average | ||
|
||
### I2: Set Comparison | ||
Create two `HashSet<String>` objects containing names of fruits: | ||
- Set 1: "apple", "banana", "orange", "mango" | ||
- Set 2: "apple", "mango", "grape", "pear" | ||
Write code to find: | ||
- Which fruits are in both sets? | ||
- Which fruits are unique to Set 1? | ||
- Which fruits are unique to Set 2? | ||
|
||
### I3: Map Usage | ||
Create a `HashMap<String, Integer>` to store student names and their scores: | ||
- Add at least 5 student-score pairs | ||
- Print all students who scored above 90 | ||
- Find and print the student with the highest score | ||
- Calculate the average score of all students | ||
|
||
## Hard Exercises 🌳 | ||
|
||
### H1: List Manipulation | ||
Create an `ArrayList<Integer>` with 20 random numbers between 1-100. | ||
Write methods to: | ||
- Sort the list in ascending order without using Collections.sort() | ||
- Remove all numbers that appear more than once | ||
- Find the second-highest number | ||
- Calculate the median value | ||
|
||
### H2: Custom Object Collections | ||
Create a class `Book` with properties: | ||
- title (String) | ||
- author (String) | ||
- year (int) | ||
- isbn (String) | ||
|
||
Then create a `HashSet<Book>` and: | ||
- Add several books (including some duplicates with same ISBN) | ||
- Override equals() and hashCode() to prevent duplicate ISBNs | ||
- Create a method to find all books by a specific author | ||
- Sort the books by year using a TreeSet | ||
|
||
### H3: Advanced Map Operations | ||
Create a `HashMap<String, List<Integer>>` to store student names and their test scores throughout the year: | ||
- Add at least 3 students with 4 test scores each | ||
- Calculate each student's average score | ||
- Find the student with the most improved scores (biggest difference between first and last test) | ||
- Create a method to get all students whose average is above a given threshold | ||
|
||
## Challenge Exercises 🏆 | ||
|
||
### C1: Library Management System | ||
Create a simple library management system using various collections: | ||
- Use a `Map<String, Book>` to store books by ISBN | ||
- Use a `Set<String>` to store member IDs | ||
- Use a `Map<String, List<String>>` to track which books are borrowed by which members | ||
- Implement methods for: | ||
* Adding new books | ||
* Registering new members | ||
* Borrowing books | ||
* Returning books | ||
* Listing all books borrowed by a member | ||
* Finding most popular books (most times borrowed) | ||
|
||
### C2: Tournament Bracket Generator | ||
Create a tournament bracket system using various collections: | ||
- Use a `Queue<String>` for participants waiting to play | ||
- Use a `Map<String, Integer>` to store player scores | ||
- Use a `List<List<String>>` to represent tournament rounds | ||
- Implement methods for: | ||
* Adding participants | ||
* Generating random matches | ||
* Recording match results | ||
* Advancing winners to next round | ||
* Displaying current tournament status | ||
* Determining tournament winner | ||
|
||
## Tips for All Exercises 💡 | ||
- Start with simpler tasks and gradually move to more complex ones | ||
- Test your code with different inputs | ||
- Consider edge cases (empty collections, null values, etc.) | ||
- Use appropriate exception handling where needed | ||
- Document your code with comments | ||
- Try to make your code as clean and readable as possible | ||
|
||
## Need Help? 🤔 | ||
Remember these resources: | ||
- Java Documentation for Collections | ||
- Your lecture notes | ||
- Draw diagrams if you're stuck | ||
- Break down complex problems into smaller steps | ||
- Ask for help after trying to solve it yourself first | ||
|
||
Good luck with your exercises! Remember, practice makes perfect! 🚀 |