diff --git a/CarQueue.java b/CarQueue.java new file mode 100644 index 0000000..e3380df --- /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..ea6869a 100644 --- a/Detective.java +++ b/Detective.java @@ -1,35 +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: - - - - // Print the name of the detective with the larger to-do list: - - } - -} +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: + 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."); + } + } +} +// پوارو کار بیشتری دارد \ No newline at end of file diff --git a/HashCollisionChecker.java b/HashCollisionChecker.java index 80631a7..e555138 100644 --- a/HashCollisionChecker.java +++ b/HashCollisionChecker.java @@ -1,29 +1,37 @@ -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.HashMap; +import java.util.HashSet; + +public class HashCollisionChecker { + + public static int countOfUniqueHashCodes(HashSet set) { + HashSet uniqueHashCodes = new HashSet<>(); + for (T item : set) { + uniqueHashCodes.add(item.hashCode()); + } + return uniqueHashCodes.size(); + } + + public static int countOfUniqueHashCodes(HashMap map) { + HashSet uniqueHashCodes = new HashSet<>(); + for (K key : map.keySet()) { + uniqueHashCodes.add(key.hashCode()); + } + return uniqueHashCodes.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/ShuffleLinkedList.java b/ShuffleLinkedList.java new file mode 100644 index 0000000..5a8791c --- /dev/null +++ b/ShuffleLinkedList.java @@ -0,0 +1,25 @@ +import java.util.Collections; +import java.util.LinkedList; +import java.util.Scanner; + +public class ShuffleLinkedList { + + 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/University.java b/University.java new file mode 100644 index 0000000..dc71365 --- /dev/null +++ b/University.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 boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + Student student = (Student) obj; + return studentId.equals(student.studentId); + } + + @Override + public int hashCode() { + return Objects.hash(studentId); + } + + @Override + public String toString() { + return "name = " + name + + ", studentId = " + studentId + + ", gpa = " + gpa ; + + } +} + +public class University { + + public static void main(String[] args) { + TreeSet students = new TreeSet<>(); + Scanner scanner = new Scanner(System.in); + + System.out.println("Enter the number of students:"); + int numberOfStudents = scanner.nextInt(); + scanner.nextLine(); + + for (int i = 0; i < numberOfStudents; i++) { + System.out.println("Enter Info for student " + (i + 1) + ":"); + System.out.print("Name: "); + String name = scanner.nextLine(); + System.out.print("Student ID: "); + String studentId = scanner.nextLine(); + System.out.print("GPA: "); + double gpa = scanner.nextDouble(); + scanner.nextLine(); + + students.add(new Student(name, studentId, gpa)); + } + + System.out.print("Enter the Student ID to search for: "); + String searchId = scanner.nextLine(); + + + Student searchStudent = new Student("", searchId, 0); + Student foundStudent = students.ceiling(searchStudent); + + if (foundStudent != null && foundStudent.getStudentId().equals(searchId)) { + System.out.println(foundStudent); + } else { + System.out.println("Student with ID " + searchId + " not found."); + } + + } +} \ No newline at end of file