From 17808bd1f78fef0771ba01930094b8e1ba3ad3de Mon Sep 17 00:00:00 2001 From: ArianSdg <166846398+ArianSdg@users.noreply.github.com> Date: Sat, 29 Jun 2024 18:31:01 +0330 Subject: [PATCH 1/4] Add files via upload --- Car.java | 44 ++++++++++++++++++++++ HashCollisionChecker.java | 74 ++++++++++++++++++++++--------------- Shuffle.java | 23 ++++++++++++ ToDos.java | 40 ++++++++++++++++++++ University.java | 77 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 229 insertions(+), 29 deletions(-) create mode 100644 Car.java create mode 100644 Shuffle.java create mode 100644 ToDos.java create mode 100644 University.java diff --git a/Car.java b/Car.java new file mode 100644 index 0000000..e9cb12a --- /dev/null +++ b/Car.java @@ -0,0 +1,44 @@ +import java.util.*; + +public class Car { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + Queue cars = new LinkedList<>(); + + String car; + System.out.println("Type the car you want! if you are finished typing the cars, type \"Finish\"."); + do { + car = scan.next(); + if (car.equals("Finish")) { + break; + } + cars.add(car); + } while (!car.equals("Finish")); + + Iterator carIterator = cars.iterator(); + + System.out.print("Cars: "); + while (carIterator.hasNext()) { + System.out.print(carIterator.next() + ", "); + } + System.out.println(); + + while (!cars.isEmpty()) { // polling cars from the queue + String removedCar = dequeue(cars); + System.out.println(removedCar+ " is removed!"); + } + + if (cars.isEmpty()) { + System.out.println("All of the cars have left!"); + } else { + System.out.println(cars); + } + } + + public static void enqueue(Queue cars,String car) { + cars.add(car); + } + public static String dequeue(Queue cars) { + return cars.poll(); + } +} diff --git a/HashCollisionChecker.java b/HashCollisionChecker.java index 80631a7..d53044f 100644 --- a/HashCollisionChecker.java +++ b/HashCollisionChecker.java @@ -1,29 +1,45 @@ -import java.util.*; - -public class HashCollisionChecker { - public static int countOfUniqueHashCodes(HashSet set) { - // TODO: Implement - return 0; - } - - public static int countOfUniqueHashCodes(HashMap map) { - // TODO: Implement - return 0; - } - - public static void main(String[] args) { - HashSet set = new HashSet<>(); - set.add("c#c#c#c#c#c#bBc#c#c#c#bBc#"); - set.add("abcd"); - set.add("c#c#c#c#c#c#bBc#c#c#c#c#aa"); - set.add("1234"); - set.add("c#c#c#c#c#c#bBc#c#c#c#c#bB"); - System.out.println(countOfUniqueHashCodes(set)); // 3 - - HashMap map = new HashMap<>(); - map.put("c#c#c#c#c#c#c#aaaaaaaabBbB", 14); - map.put("c#c#c#c#c#c#c#aaaaaaaac#c#", 12); - map.put("c#c#c#c#c#c#c#aaaaaaaac#cc", 16); - System.out.println(countOfUniqueHashCodes(map)); // 2 - } -} +import java.util.*; + +public class HashCollisionChecker { + public static int countOfUniqueHashCodes(HashSet set) { + // TODO: Implement + Set newSet = new HashSet<>(); + Set hashCodeCollector = new HashSet<>(); + Iterator it = set.iterator(); + while ( it.hasNext() ) { + newSet.add(it.next()); + hashCodeCollector.add(newSet.hashCode()); + newSet.clear(); + } + return hashCodeCollector.size(); + } + + public static int countOfUniqueHashCodes(HashMap map) { + // TODO: Implement + Set set = new HashSet<>(); + Set hashCodeCollector = new HashSet<>(); + Iterator it = map.keySet().iterator(); + while (it.hasNext()) { + set.add(it.next()); + hashCodeCollector.add(set.hashCode()); + set.clear(); + } + return hashCodeCollector.size(); + } + + public static void main(String[] args) { + HashSet set = new HashSet<>(); + set.add("c#c#c#c#c#c#bBc#c#c#c#bBc#"); + set.add("abcd"); + set.add("c#c#c#c#c#c#bBc#c#c#c#c#aa"); + set.add("1234"); + set.add("c#c#c#c#c#c#bBc#c#c#c#c#bB"); + System.out.println(countOfUniqueHashCodes(set)); // 3 + + HashMap map = new HashMap<>(); + map.put("c#c#c#c#c#c#c#aaaaaaaabBbB", 14); + map.put("c#c#c#c#c#c#c#aaaaaaaac#c#", 12); + map.put("c#c#c#c#c#c#c#aaaaaaaac#cc", 16); + System.out.println(countOfUniqueHashCodes(map)); // 2 + } +} \ No newline at end of file diff --git a/Shuffle.java b/Shuffle.java new file mode 100644 index 0000000..b0d6c6c --- /dev/null +++ b/Shuffle.java @@ -0,0 +1,23 @@ +import java.util.*; + +public class Shuffle { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + LinkedList myLinkedList = new LinkedList<>(); + + Object value; + System.out.println("Add your elements! if you don't want to add more, type \"Finish\"."); + do { + value = scan.next(); + if (value.equals("Finish")) { + break; + } + myLinkedList.add(value); + } while (!value.equals("Finish")); + + System.out.println("Main LinkedList: "+myLinkedList); + + Collections.shuffle(myLinkedList); + System.out.println("Shuffled LinkedList: "+myLinkedList); + } +} diff --git a/ToDos.java b/ToDos.java new file mode 100644 index 0000000..b81f9ab --- /dev/null +++ b/ToDos.java @@ -0,0 +1,40 @@ +import java.util.ArrayList; + + class ToDos { + + public static void main(String[] args) { + + // Sherlock + ArrayList sherlocksToDos = new ArrayList(); + + sherlocksToDos.add("visit the crime scene"); + sherlocksToDos.add("play violin"); + sherlocksToDos.add("interview suspects"); + sherlocksToDos.add("solve the case"); + sherlocksToDos.add("apprehend the criminal"); + + // Poirot + ArrayList poirotsToDos = new ArrayList(); + + poirotsToDos.add("visit the crime scene"); + poirotsToDos.add("interview suspects"); + poirotsToDos.add("let the little grey cells do their work"); + poirotsToDos.add("trim mustache"); + poirotsToDos.add("call all suspects together"); + poirotsToDos.add("reveal the truth of the crime"); + + // Print the size of each ArrayList below: + int sherlocksToDosSize = sherlocksToDos.size(); + int poirotsToDosSize = poirotsToDos.size(); + + System.out.println("Sherlock's amount of to dos: "+ sherlocksToDosSize); + System.out.println("Poirot's amount of to dos: "+ poirotsToDosSize); + + // Print the name of the detective with the larger to-do list: + if (sherlocksToDosSize > poirotsToDosSize) { + System.out.println("Sherlock"); + } else { + System.out.println("Poirot"); + } + } +} diff --git a/University.java b/University.java new file mode 100644 index 0000000..cc62e8b --- /dev/null +++ b/University.java @@ -0,0 +1,77 @@ +import java.util.*; + +class Student { + private String name; + private int studentId; + private double gpa; + + Student (String name, int studentId, double gpa) { + this.name = name; + this.studentId = studentId; + this.gpa = gpa; + } + + public void setName(String name) { + this.name = name; + } + public String getName() { + return name; + } + public void setStudentId(int studentId) { + this.studentId = studentId; + } + public int getStudentId() { + return studentId; + } + public void setGpa(double gpa) { + this.gpa = gpa; + } + public double getGpa() { + return gpa; + } + public String showDetails(int studentId) { + return "\nname: "+ this.name +"\nID: "+ this.studentId + "\nGPA: "+ this.gpa; + } +} +class CompareStudents implements Comparator { + @Override + public int compare(Student student1, Student student2) { + return Integer.compare(student1.getStudentId(),student2.getStudentId()); + } +} + + +public class University { + public static void main(String[] args) { + TreeSet students = new TreeSet<>(new CompareStudents()); + Scanner scan = new Scanner(System.in); + + System.out.print("Type the number of students you want to add: "); + int studentNum = scan.nextInt(); + for (int i = 0; studentNum > i; i++) { + System.out.print("Enter student's name: "); + String studentName = scan.next(); + + System.out.print("Enter student's ID: "); + int studentId = scan.nextInt(); + + System.out.print("Enter student's GPA: "); + double studentGpa = scan.nextDouble(); + + students.add(new Student(studentName, studentId, studentGpa)); + System.out.println("---------------------------"); + + } + + System.out.print("Enter the student ID you want to search: "); + int search = scan.nextInt(); + + List studentList = new ArrayList<>(students); + for (int i = 0; studentNum > i; i++) { + int searchedStudentID = studentList.get(i).getStudentId(); + if (search == searchedStudentID) { + System.out.println(studentList.get(i).showDetails(searchedStudentID)); + } + } + } +} From 5666c8162acce7f9ac22dd4355c633855f63fb4a Mon Sep 17 00:00:00 2001 From: ArianSdg <166846398+ArianSdg@users.noreply.github.com> Date: Sat, 29 Jun 2024 18:41:05 +0330 Subject: [PATCH 2/4] Delete University.java --- University.java | 77 ------------------------------------------------- 1 file changed, 77 deletions(-) delete mode 100644 University.java diff --git a/University.java b/University.java deleted file mode 100644 index cc62e8b..0000000 --- a/University.java +++ /dev/null @@ -1,77 +0,0 @@ -import java.util.*; - -class Student { - private String name; - private int studentId; - private double gpa; - - Student (String name, int studentId, double gpa) { - this.name = name; - this.studentId = studentId; - this.gpa = gpa; - } - - public void setName(String name) { - this.name = name; - } - public String getName() { - return name; - } - public void setStudentId(int studentId) { - this.studentId = studentId; - } - public int getStudentId() { - return studentId; - } - public void setGpa(double gpa) { - this.gpa = gpa; - } - public double getGpa() { - return gpa; - } - public String showDetails(int studentId) { - return "\nname: "+ this.name +"\nID: "+ this.studentId + "\nGPA: "+ this.gpa; - } -} -class CompareStudents implements Comparator { - @Override - public int compare(Student student1, Student student2) { - return Integer.compare(student1.getStudentId(),student2.getStudentId()); - } -} - - -public class University { - public static void main(String[] args) { - TreeSet students = new TreeSet<>(new CompareStudents()); - Scanner scan = new Scanner(System.in); - - System.out.print("Type the number of students you want to add: "); - int studentNum = scan.nextInt(); - for (int i = 0; studentNum > i; i++) { - System.out.print("Enter student's name: "); - String studentName = scan.next(); - - System.out.print("Enter student's ID: "); - int studentId = scan.nextInt(); - - System.out.print("Enter student's GPA: "); - double studentGpa = scan.nextDouble(); - - students.add(new Student(studentName, studentId, studentGpa)); - System.out.println("---------------------------"); - - } - - System.out.print("Enter the student ID you want to search: "); - int search = scan.nextInt(); - - List studentList = new ArrayList<>(students); - for (int i = 0; studentNum > i; i++) { - int searchedStudentID = studentList.get(i).getStudentId(); - if (search == searchedStudentID) { - System.out.println(studentList.get(i).showDetails(searchedStudentID)); - } - } - } -} From 7de60d8099490c80678dc29b5a09c9210f4dc0a8 Mon Sep 17 00:00:00 2001 From: ArianSdg <166846398+ArianSdg@users.noreply.github.com> Date: Sat, 29 Jun 2024 18:43:39 +0330 Subject: [PATCH 3/4] Add files via upload --- University.java | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 University.java diff --git a/University.java b/University.java new file mode 100644 index 0000000..e3c8b0d --- /dev/null +++ b/University.java @@ -0,0 +1,72 @@ +import java.util.*; + +class Student { + private String name; + private int studentId; + private double gpa; + + Student (String name, int studentId, double gpa) { + this.name = name; + this.studentId = studentId; + this.gpa = gpa; + } + + public void setName(String name) { + this.name = name; + } + public String getName() { + return name; + } + public void setStudentId(int studentId) { + this.studentId = studentId; + } + public int getStudentId() { + return studentId; + } + public void setGpa(double gpa) { + this.gpa = gpa; + } + public double getGpa() { + return gpa; + } + public String showDetails(int studentId) { + return "\nname: "+ this.name +"\nID: "+ this.studentId + "\nGPA: "+ this.gpa; + } +} + + +public class University { + public static void main(String[] args) { + TreeSet students = new TreeSet<>(); + Scanner scan = new Scanner(System.in); + + System.out.print("Type the number of students you want to add: "); + int studentNum = scan.nextInt(); + for (int i = 0; studentNum > i; i++) { + System.out.print("Enter student's name: "); + String studentName = scan.next(); + + System.out.print("Enter student's ID: "); + int studentId = scan.nextInt(); + + System.out.print("Enter student's GPA: "); + double studentGpa = scan.nextDouble(); + + Student student = new Student(studentName,studentId,studentGpa); + students.add(student); + System.out.println("---------------------------"); + + } + + System.out.print("Enter the student ID you want to search: "); + int search = scan.nextInt(); + + List studentList = new ArrayList<>(students); + for (int i = 0; studentNum > i; i++) { + int searchedStudentID = studentList.get(i).getStudentId(); + if (search == searchedStudentID) { + System.out.println(studentList.get(i).showDetails(searchedStudentID)); + } + } + } +} From d81b08369f53d3832a189ed2b32730c39414c4a4 Mon Sep 17 00:00:00 2001 From: ArianSdg <166846398+ArianSdg@users.noreply.github.com> Date: Sat, 29 Jun 2024 18:45:53 +0330 Subject: [PATCH 4/4] Delete Detective.java --- Detective.java | 35 ----------------------------------- 1 file changed, 35 deletions(-) delete mode 100644 Detective.java diff --git a/Detective.java b/Detective.java deleted file mode 100644 index 5707416..0000000 --- a/Detective.java +++ /dev/null @@ -1,35 +0,0 @@ - -import java.util.ArrayList; - -class ToDos { - - public static void main(String[] args) { - - // Sherlock - ArrayList sherlocksToDos = new ArrayList(); - - sherlocksToDos.add("visit the crime scene"); - sherlocksToDos.add("play violin"); - sherlocksToDos.add("interview suspects"); - sherlocksToDos.add("solve the case"); - sherlocksToDos.add("apprehend the criminal"); - - // Poirot - ArrayList poirotsToDos = new ArrayList(); - - poirotsToDos.add("visit the crime scene"); - poirotsToDos.add("interview suspects"); - poirotsToDos.add("let the little grey cells do their work"); - poirotsToDos.add("trim mustache"); - poirotsToDos.add("call all suspects together"); - poirotsToDos.add("reveal the truth of the crime"); - - // Print the size of each ArrayList below: - - - - // Print the name of the detective with the larger to-do list: - - } - -}