Repository navigation
Sheffield | 26-Java-June | Hassan Osman | Sprint 4 | Prep: Data Structures #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HassanOHOsman
wants to merge
30
commits into
CodeYourFuture:main
Choose a base branch
from
HassanOHOsman:prep/data-structures-exercises
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
ed9f927
Answer the automated testing reflection questions
HassanOHOsman f4c8d0e
Create StudentGrades class class with 3 fields (arrays) and a constru…
HassanOHOsman f4626c5
Create a method getDistinctFirstNames that returns ONLY unique first …
HassanOHOsman 1f7b594
Create a method getDistinctFirstNamesInAlphabeticalOrder that returns…
HassanOHOsman 53f7bf1
Create a method getLastNameToGrade that returns last names and grades…
HassanOHOsman 5ce0ade
1. Update getDistinctFirstNames methods by using HashSet constructor …
HassanOHOsman 8993c8d
1. Correct getLastNameToGrade to use HashMap instead and then loop th…
HassanOHOsman 245d1f1
Answer the reflection question from exercise 2.1
HassanOHOsman 3d1dba7
Create a Student class with fields firstName and lastName
HassanOHOsman fd81f97
Add a constructor, and getters.
HassanOHOsman c2457bf
1.Correct the fields of Student to the correct data type (arrays of s…
HassanOHOsman d97c3c7
1. implement getStudentToGrade method.
HassanOHOsman bc957e4
1. Copy the solution (so far) of exercise 2.2 to me merged with exerc…
HassanOHOsman 18e7ebf
Update data sets (arrays)
HassanOHOsman 942d30a
Correct the fields in the Student class to be Strings and not arrays …
HassanOHOsman 1e57066
Override the hashCode() and equals() methods so that this map to work…
HassanOHOsman 1db9cf9
Override the toString() method in order to get a meaningful and reada…
HassanOHOsman 16a838b
exercise 2.3: Call each of the methods already created and assign the…
HassanOHOsman e147b51
Correct some variables names and data types
HassanOHOsman edd083b
Modify each of the collections created to include the new student and…
HassanOHOsman 0d92343
1. Update the collections adding a new student.
HassanOHOsman 70f9168
Modify the collections that handle grades to update the grade from th…
HassanOHOsman 09a3428
Answer all reflection questions to exercise 3.2
HassanOHOsman 990ad78
Answer all reflection questions to exercise 2.3 (not 3.2)
HassanOHOsman 7006396
Add statements to help find the highest and lowest grades.
HassanOHOsman 846af64
Add statements to help find the first and last names of the student w…
HassanOHOsman 896db5d
Add statements to help find duplicate first names.
HassanOHOsman 437461f
Create .gitignore file, containting all the files and folders to be u…
HassanOHOsman cf37b7b
untrack .idea folder and its content
HassanOHOsman f5a553a
update .gitignore
HassanOHOsman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,12 @@ | ||
| # IntelliJ | ||
| .idea/ | ||
| *.iml | ||
|
|
||
| # IntelliJ build output | ||
| out/ | ||
|
|
||
| # Compiled class files | ||
| *.class | ||
|
|
||
| # Maven build output | ||
| target/ |
This file contains hidden or 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,17 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <groupId>org.example</groupId> | ||
| <artifactId>2</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
|
|
||
| <properties> | ||
| <maven.compiler.source>26</maven.compiler.source> | ||
| <maven.compiler.target>26</maven.compiler.target> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| </properties> | ||
|
|
||
| </project> |
This file contains hidden or 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,80 @@ | ||
| package org.example; | ||
|
|
||
|
|
||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| //TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or | ||
| // click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter. | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| String[] firstNames = {"Anuket", "Eryn", "Filip", "Raquel", "Eryn", "Ali", "Sam"}; | ||
| String[] lastNames = {"Hassan", "Doe", "Pappas", "Clarke", "Jones", "Hassan", "Clarke"}; | ||
| int[] grades = {22, 45, 35, 22, 41, 48, 39}; | ||
|
|
||
| StudentGrades studentGrades = new StudentGrades(firstNames, lastNames, grades); | ||
|
|
||
| System.out.println(Arrays.toString(studentGrades.getFirstNames())); | ||
| System.out.println(studentGrades.getDistinctFirstNames(firstNames)); | ||
| System.out.println(studentGrades.getDistinctFirstNamesInAlphabeticalOrder(firstNames)); | ||
| System.out.println(studentGrades.getLastNameToGrade(lastNames, grades)); | ||
|
|
||
| System.out.println(studentGrades.getStudentToGrade()); | ||
|
|
||
|
|
||
| List<String> firstNamesList = new ArrayList<>(Arrays.asList(studentGrades.getFirstNames())); | ||
| Set<String> distinctFirstNamesSet = studentGrades.getDistinctFirstNames(firstNames); | ||
| Set<String> distinctFirstNamesInAlphabeticalOrderSet = studentGrades.getDistinctFirstNamesInAlphabeticalOrder(firstNames); | ||
| Map<String, Integer> lastNamesToGradesMap = studentGrades.getLastNameToGrade(lastNames, grades); | ||
| Map<Student, Integer> studentToGradeMap = studentGrades.getStudentToGrade(); | ||
|
|
||
|
|
||
| firstNamesList.add("Patrick"); | ||
| distinctFirstNamesSet.add("Patrick"); | ||
| distinctFirstNamesInAlphabeticalOrderSet.add("Patrick"); | ||
| lastNamesToGradesMap.put("Smith", 49); | ||
| studentToGradeMap.put(new Student("Patrick", "Smith"), 49); | ||
|
|
||
| firstNamesList.remove("Patrick"); | ||
| distinctFirstNamesSet.remove("Patrick"); | ||
| distinctFirstNamesInAlphabeticalOrderSet.remove("Patrick"); | ||
| lastNamesToGradesMap.remove("Smith", 49); | ||
| studentToGradeMap.remove(new Student("Patrick", "Smith"), 49); | ||
|
|
||
| lastNamesToGradesMap.replace("Smith", 49, 0); | ||
| studentToGradeMap.replace(new Student("Patrick", "Smith"), 49, 0); | ||
|
|
||
| int highestGrade = Arrays.stream(grades).max().getAsInt(); | ||
| System.out.println(highestGrade); // Highest grade is 48 | ||
|
|
||
| int lowestGrade = Arrays.stream(grades).min().getAsInt(); | ||
| System.out.println(lowestGrade); // Lowest grade is 22 | ||
|
|
||
|
|
||
|
|
||
| Student highestGradeStudent = studentToGradeMap.entrySet() | ||
| .stream() | ||
| .max(Map.Entry.comparingByValue()) | ||
| .get() | ||
| .getKey(); | ||
|
|
||
| System.out.println(highestGradeStudent.getFirstName()); | ||
| System.out.println(highestGradeStudent.getLastName()); | ||
|
|
||
|
|
||
|
|
||
|
|
||
| Set<String> duplicateFirstNames = firstNamesList.stream() | ||
| .filter(firstName -> Collections.frequency(firstNamesList, firstName) > 1) | ||
| .collect(Collectors.toSet()); | ||
|
|
||
| System.out.println(duplicateFirstNames); | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
|
|
This file contains hidden or 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,50 @@ | ||
| package org.example; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| public class Student { | ||
| private String firstName; | ||
| private String lastName; | ||
|
|
||
| public Student(String firstName, String lastName) { | ||
| this.firstName = firstName; | ||
| this.lastName = lastName; | ||
| } | ||
|
|
||
| public String getFirstName() { | ||
| return firstName; | ||
| } | ||
|
|
||
| public String getLastName() { | ||
| return lastName; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object object) { | ||
| if (this == object) { | ||
| return true; | ||
| } | ||
| if (object == null || getClass() != object.getClass()) { | ||
| return false; | ||
| } | ||
|
|
||
| Student student = (Student) object; | ||
|
|
||
| return firstName.equals(student.firstName) && lastName.equals(student.lastName); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(firstName, lastName); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(){ | ||
| return firstName + " " + lastName; | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } |
This file contains hidden or 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,109 @@ | ||
| package org.example; | ||
|
|
||
| import java.util.*; | ||
|
|
||
| public class StudentGrades { | ||
| private String[] firstNames; | ||
| private String[] lastNames; | ||
| private int[] grades; | ||
|
|
||
| public StudentGrades(String[] firstNames, String[] lastNames, int[] grades) { | ||
| this.firstNames = firstNames; | ||
| this.lastNames = lastNames; | ||
| this.grades = grades; | ||
| } | ||
|
|
||
| public String[] getFirstNames() { | ||
| return firstNames; | ||
| } | ||
|
|
||
| public String[] getLastNames() { | ||
| return lastNames; | ||
| } | ||
|
|
||
| public int[] getGrades() { | ||
| return grades; | ||
| } | ||
|
|
||
| public Set<String> getDistinctFirstNames(String[] firstNames) { | ||
| Set<String> set = new HashSet<>(); | ||
| set.addAll(Arrays.asList(firstNames)); | ||
| return set; | ||
| } | ||
|
|
||
| public Set<String> getDistinctFirstNamesInAlphabeticalOrder(String[] firstNames) { | ||
| Set<String> set = new TreeSet<>(); | ||
| set.addAll(Arrays.asList(firstNames)); | ||
| return set; | ||
|
|
||
|
|
||
| } | ||
|
|
||
| public Map<String, Integer> getLastNameToGrade(String[] lastNames, int[] grades) { | ||
| Map<String, Integer> map = new HashMap<>(); | ||
|
|
||
| for (int i = 0; i < lastNames.length; i++) { | ||
| map.put(lastNames[i], grades[i]); | ||
| } | ||
| return map; | ||
| } | ||
|
|
||
|
|
||
| public Map<Student, Integer> getStudentToGrade() { | ||
| Map<Student, Integer> map = new HashMap<>(); | ||
|
|
||
| for (int i = 0; i < firstNames.length; i ++) { | ||
| Student student = new Student(firstNames[i], lastNames[i]); | ||
| map.put(student, grades[i]); | ||
| } | ||
| return map; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /* | ||
| Reflection - Exercise 2.1 | ||
|
|
||
| Question: Suppose there are a set of twins in the class, so there are some duplicate lastNames. Can we still build a | ||
| map of students to their grades? | ||
|
|
||
| Answer: perhaps we might need a 4th array that provide unique values that could be associated with each student. | ||
| Something like an array of student's ID numbers can work. Otherwise, maybe there's a way for the key to be | ||
| customized to include both the first and last names of each student in such cases. | ||
|
|
||
|
|
||
|
|
||
| Reflections - Exercise 2.3 | ||
| Think about the following questions, make notes and be prepared to talk through your thoughts in the workshop. | ||
|
|
||
| Q1. Would it have been possible to use Arrays to model the same data? | ||
| A1. Yes, it would. | ||
|
|
||
| Q2. Would it have been easier, harder or the same? Why? | ||
| A2. It would have been harder. This is because arrays are not dynamic, they are of fixed size, therefore, adding and | ||
| removing elements would have been more challenging. | ||
|
|
||
| Q3. What was your thought process when deciding which data structure was most appropriate for modelling particular data? | ||
| What things did you consider? | ||
| A3. If the goal was to have data represented in key-value pair, then I'd use HashMap (other implementations of Map | ||
| interface can work. If the goal was to have data as distinct values, then I'd use HashSet (other Set interface | ||
| implementations can work), and if order mattered, then I'd go for TreeSet instead. If the data insertion order and | ||
| storing order matters, then, I'd go for an ArrayList (other List implementation can work). | ||
|
|
||
| Q4. Suppose the initial data set was input incorrectly and the same student’s name and grade was input twice. How would | ||
| your Map<Student, Grade> handle this? Why? | ||
| A4. Since Student is key here, and Student class has implemented both equals() and hashCode() methods, then they will | ||
| consider duplicate student objects as the same object. Therefore, Map<Student, Grade> treats them as the same key. | ||
|
|
||
| Q5. What is the difference between variables: a) List firstNames, b) List<String> firstNames, | ||
| c) ArrayList<String> firstNames ? Which is preferable? Bonus: how about List<?> firstNames ? | ||
| A5. a) is a list interface that may contain different data types | ||
| b) is a list interface of Strings ONLY | ||
| c) is an array list - implementing the List interface - and cntains Strings only | ||
|
|
||
| I prefer List<String> firstNames because interfaces are more flexible, allowing loose coupling and multiple | ||
| inheritance. Also having the input type - String - explicitly stated ensure that no other data type can be passed. | ||
|
|
||
| Regarding List<?> firstNames perhaps it's a list of certain type which is yet to be confirmed. | ||
|
|
||
| */ | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this in order?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is.