diff --git a/CarQueue.java b/CarQueue.java new file mode 100644 index 0000000..b47483f --- /dev/null +++ b/CarQueue.java @@ -0,0 +1,36 @@ +import java.util.LinkedList; +import java.util.Queue; + +public class CarQueue { + + public static void main(String[] args) { + Queue carQueue = new LinkedList<>(); + + enqueue(carQueue, "Audi"); + enqueue(carQueue, "Honda"); + enqueue(carQueue, "Ford"); + enqueue(carQueue, "Rolls-Royce"); + + printQueueContents(carQueue); + + while (!carQueue.isEmpty()) { + String removedCar = dequeue(carQueue); + System.out.println(removedCar+" Removed "); + } + + if (carQueue.isEmpty()) { + System.out.println("The queue is empty."); + } else { + System.out.println("The queue is not empty."); + } + } + public static void enqueue(Queue queue, String car) { + queue.add(car); + } + public static String dequeue(Queue queue) { + return queue.poll(); + } + public static void printQueueContents(Queue queue) { + System.out.println("The queue: " + queue); + } +} \ No newline at end of file diff --git a/Detective.java b/Detective.java index 5707416..018f3be 100644 --- a/Detective.java +++ b/Detective.java @@ -25,11 +25,18 @@ public static void main(String[] args) { poirotsToDos.add("reveal the truth of the crime"); // Print the size of each ArrayList below: - + System.out.println("Sherlock's to-do list size: " + sherlocksToDos.size()); + System.out.println("Poirot's to-do list size: " + poirotsToDos.size()); // Print the name of the detective with the larger to-do list: - + if (sherlocksToDos.size() > poirotsToDos.size()) { + System.out.println("Sherlock Holmes has more tasks."); + } else if (sherlocksToDos.size() < poirotsToDos.size()) { + System.out.println("Hercules Poirot has more tasks."); + } else { + System.out.println("Both detectives have the same number of tasks."); + } } - + // Poirot kar bishtari darad. } diff --git a/HashCollisionChecker.java b/HashCollisionChecker.java index 80631a7..5214dbf 100644 --- a/HashCollisionChecker.java +++ b/HashCollisionChecker.java @@ -1,14 +1,22 @@ -import java.util.*; +import java.util.HashMap; +import java.util.HashSet; public class HashCollisionChecker { + public static int countOfUniqueHashCodes(HashSet set) { - // TODO: Implement - return 0; + HashSet uniqueHashCodes = new HashSet<>(); + for (T item : set) { + uniqueHashCodes.add(item.hashCode()); + } + return uniqueHashCodes.size(); } public static int countOfUniqueHashCodes(HashMap map) { - // TODO: Implement - return 0; + HashSet uniqueHashCodes = new HashSet<>(); + for (K key : map.keySet()) { + uniqueHashCodes.add(key.hashCode()); + } + return uniqueHashCodes.size(); } public static void main(String[] args) { diff --git a/Shuffle.java b/Shuffle.java new file mode 100644 index 0000000..94a4654 --- /dev/null +++ b/Shuffle.java @@ -0,0 +1,25 @@ +import java.util.Collections; +import java.util.LinkedList; +import java.util.Scanner; + +public class Shuffle { + + public static void main(String[] args) { + LinkedList list = new LinkedList<>(); + Scanner scanner = new Scanner(System.in); + + System.out.println("Enter the number of elements in the list:"); + int numberOfElements = scanner.nextInt(); + scanner.nextLine(); + + System.out.println("Enter the elements :"); + for (int i = 0; i < numberOfElements; i++) { + String element = scanner.nextLine(); + list.add(element); + } + + System.out.println("Original list: " + list); + Collections.shuffle(list); + System.out.println("Shuffled list: " + list); + } +} \ No newline at end of file diff --git a/StudentManager.java b/StudentManager.java new file mode 100644 index 0000000..a7a149e --- /dev/null +++ b/StudentManager.java @@ -0,0 +1,82 @@ +import java.util.Comparator; +import java.util.Scanner; +import java.util.TreeSet; + +class Student { + private final String name; + private final int studentNumber; + private final double gpa; + + public Student(String name, int studentNumber, double gpa) { + this.name = name; + this.studentNumber = studentNumber; + this.gpa = gpa; + } + + public int getStudentNumber() { + return studentNumber; + } + + @Override + public String toString() { + return "Name: " + name + ", Student Number: " + studentNumber + ", GPA: " + gpa; + } +} + +class StudentComparator implements Comparator { + @Override + public int compare(Student s1, Student s2) { + return Integer.compare(s1.getStudentNumber(), s2.getStudentNumber()); + } +} + +public class StudentManager { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + TreeSet students = new TreeSet<>(new StudentComparator()); + + // Receiving student details from user + System.out.print("Enter the number of students: "); + int numberOfStudents = scanner.nextInt(); + scanner.nextLine(); // Consume newline + + for (int i = 0; i < numberOfStudents; i++) { + System.out.println("Enter details for student " + (i + 1) + ":"); + + System.out.print("Name: "); + String name = scanner.nextLine(); + + System.out.print("Student Number: "); + int studentNumber = scanner.nextInt(); + + System.out.print("GPA: "); + double gpa = scanner.nextDouble(); + scanner.nextLine(); // Consume newline + + students.add(new Student(name, studentNumber, gpa)); + } + + // Searching for a student by student number + System.out.print("Enter student number to search: "); + int searchStudentNumber = scanner.nextInt(); + + Student searchStudent = null; + for (Student student : students) { + if (student.getStudentNumber() == searchStudentNumber) { + searchStudent = student; + break; + } + } + + if (searchStudent != null) { + System.out.println("Student found: " + searchStudent); + } else { + System.out.println("Student with student number " + searchStudentNumber + " not found."); + } + + scanner.close(); + } +} + + +