diff --git a/CarQueue.java b/CarQueue.java new file mode 100644 index 0000000..ca8ec50 --- /dev/null +++ b/CarQueue.java @@ -0,0 +1,26 @@ +import java.util.LinkedList; +import java.util.Queue; + +public class CarQueue { + public static void main(String[] args) { + Queue queue = new LinkedList<>(); + + queue.add("Car 1"); + queue.add("Car 2"); + queue.add("Car 3"); + queue.add("Car 4"); + + System.out.println(queue); + + while (!queue.isEmpty()) { + String removedCar = queue.poll(); + System.out.println("delete: " + removedCar); + } + + if (queue.isEmpty()) { + System.out.println("queue is empty."); + } else { + System.out.println("queue isn't empty."); + } + } +} \ No newline at end of file diff --git a/Detective.java b/Detective.java index 5707416..331e7b2 100644 --- a/Detective.java +++ b/Detective.java @@ -1,35 +1,45 @@ - + 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 a = sherlocksToDos.size(); + System.out.println("Sherlock's : " + a); + + int b = poirotsToDos.size(); + System.out.println("Poirot's : " + b); - - // Print the name of the detective with the larger to-do list: - + if (a > b) { + System.out.println("Sherlock"); + } else if (a < b) { + System.out.println("Poirot"); + } else { + System.out.println("Sherlock and Poirot"); + } + } - + } diff --git a/HashCollisionChecker.java b/HashCollisionChecker.java index 80631a7..5bf520b 100644 --- a/HashCollisionChecker.java +++ b/HashCollisionChecker.java @@ -2,13 +2,29 @@ public class HashCollisionChecker { public static int countOfUniqueHashCodes(HashSet set) { - // TODO: Implement - return 0; + HashSet h_c = new HashSet<>(); + + for (T element : set) { + int hashCode = element.hashCode(); + if (!h_c.contains(hashCode)) + h_c.add(hashCode); + + } + + return h_c.size(); } public static int countOfUniqueHashCodes(HashMap map) { - // TODO: Implement - return 0; + HashSet h_c = new HashSet<>(); + + for (K key : map.keySet()) { + int hashCode = key.hashCode(); + + if (!h_c.contains(hashCode)) + h_c.add(hashCode); + + } + return h_c.size(); } public static void main(String[] args) { diff --git a/Shuffle.java b/Shuffle.java new file mode 100644 index 0000000..19c74f2 --- /dev/null +++ b/Shuffle.java @@ -0,0 +1,24 @@ +import java.util.Collections; +import java.util.LinkedList; + +public class Shuffle { + public static void main(String[] args) { + + LinkedList linkedList = new LinkedList<>(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + linkedList.add(5); + + System.out.println(linkedList); + + shuffleLinkedList(linkedList); + + System.out.println(linkedList); + } + + public static void shuffleLinkedList(LinkedList linkedList) { + Collections.shuffle(linkedList); + } +} \ No newline at end of file diff --git a/Uni.java b/Uni.java new file mode 100644 index 0000000..0203932 --- /dev/null +++ b/Uni.java @@ -0,0 +1,88 @@ +import java.util.*; + +class Student implements Comparable { + private String name; + private String studentId; + private double gpa; + + public Student(String name, String studentId, double gpa) { + this.name = name; + this.studentId = studentId; + this.gpa = gpa; + } + + public String getName() { + return name; + } + + public String getStudentId() { + return studentId; + } + + public double getGpa() { + return gpa; + } + + @Override + public int compareTo(Student other) { + return this.studentId.compareTo(other.studentId); + } + + @Override + public String toString() { + return "Name: " + name + ", ID: " + studentId + ", GPA: " + gpa; + } +} + +public class Uni { + + private static TreeSet studentSet = new TreeSet<>(); + + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + while (true) { + System.out.println("\nMenu:"); + System.out.println("1. Add Student"); + System.out.println("2. Search Student by ID"); + System.out.println("3. Exit"); + + int choice = scanner.nextInt(); + scanner.nextLine(); + + switch (choice) { + case 1: + System.out.println("Enter student's name:"); + String name = scanner.nextLine(); + System.out.println("Enter student's ID:"); + String studentId = scanner.nextLine(); + System.out.println("Enter student's GPA:"); + double gpa = scanner.nextDouble(); + Student student = new Student(name, studentId, gpa); + studentSet.add(student); + System.out.println("Student added successfully."); + break; + case 2: + System.out.println("Enter student's ID to search:"); + String idToSearch = scanner.nextLine(); + boolean found = false; + for (Student s : studentSet) { + if (s.getStudentId().equals(idToSearch)) { + System.out.println("Student found: " + s); + found = true; + break; + } + } + if (!found) { + System.out.println("Student not found."); + } + break; + case 3: + System.out.println("Exiting..."); + return; + default: + System.out.println("Invalid choice. Please enter 1, 2, or 3."); + } + } + } +} \ No newline at end of file