From a78d2cc3f92928010ac2e79f83ee3e031ba9945b Mon Sep 17 00:00:00 2001 From: AtParhamAzizi Date: Sat, 29 Jun 2024 23:35:29 +0330 Subject: [PATCH] Add files via upload --- CarQueue.java | 19 ++++++ Detectives.java | 43 ++++++++++++++ HashCollisionChecker.java | 16 ++++- Shuffle.java | 13 +++++ University.java | 119 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 CarQueue.java create mode 100644 Detectives.java create mode 100644 Shuffle.java create mode 100644 University.java diff --git a/CarQueue.java b/CarQueue.java new file mode 100644 index 0000000..ddc9781 --- /dev/null +++ b/CarQueue.java @@ -0,0 +1,19 @@ +import java.util.*; + +public class CarQueue { + public static void main(String[] args) { + Queue carQueue = new LinkedList<>(); + for (int i = 10; i >= 0; i--) { + carQueue.add("Car " + String.valueOf(i)); + } + while (!carQueue.isEmpty()) { + String car = carQueue.poll(); + System.out.println("item " + car + " removed"); + } + if (carQueue.isEmpty()) { + System.out.println("The queue is empty now!"); + } else { + System.out.println("The queue is not empty."); + } + } +} diff --git a/Detectives.java b/Detectives.java new file mode 100644 index 0000000..0ffe1c6 --- /dev/null +++ b/Detectives.java @@ -0,0 +1,43 @@ +import java.util.ArrayList; +class ToDos { + public static void main(String[] args) { + int sherDos; + int poiDos; + + // 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(sherDos = sherlocksToDos.size()); + System.out.println(poiDos = poirotsToDos.size()); + + // Print the name of the detective with the larger to-do list: + + if ( sherDos > poiDos) { + System.out.println("Sherlock"); + } else if (sherDos < poiDos) { + System.out.println("Poirot"); + }else { + System.out.println("Poirot and Sherlock have same number in to-dos!"); + } + + } + +} \ No newline at end of file diff --git a/HashCollisionChecker.java b/HashCollisionChecker.java index 80631a7..b128032 100644 --- a/HashCollisionChecker.java +++ b/HashCollisionChecker.java @@ -1,14 +1,24 @@ +import com.sun.source.tree.CompilationUnitTree; + import java.util.*; 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) { @@ -26,4 +36,4 @@ public static void main(String[] args) { 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..cc3024b --- /dev/null +++ b/Shuffle.java @@ -0,0 +1,13 @@ +import java.util.*; + +public class Shuffle { + public static void main(String[] args) { + LinkedList list = new LinkedList<>(); + for (int i = 0; i < 15; i++) { + list.add(i); + } + System.out.println("Original: " + list); + Collections.shuffle(list); + System.out.println("Final: " + list); + } +} diff --git a/University.java b/University.java new file mode 100644 index 0000000..69ead2e --- /dev/null +++ b/University.java @@ -0,0 +1,119 @@ +import java.util.*; +public class University { + public static void main(String[] args) { + StudentManagement management = new StudentManagement(); + management.showMenu(); + } +} + +class Student implements Comparable { + private String name; + private String studentId; + private double avragePoint; + + public Student(String name, String studentId, double gpa) { + setName(name); + setStuId(studentId); + setAvgPoint(gpa); + } + + private void setAvgPoint(double gpa) { + this.avragePoint = gpa; + } + + private void setStuId(String studentId) { + this.studentId = studentId; + } + + private void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + public String getStudentId() { + return studentId; + } + public double getAvragePoint() { + return avragePoint; + } + public int compareTo(Student other) { + return this.studentId.compareTo(other.studentId); + } + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Student student = (Student) o; + return studentId.equals(student.studentId); + } + public int hashCode() { + return Objects.hash(studentId); + } + public String toString() { + return "Student: \n" + + "name='" + this.getName() + '\'' + "\n" + + "student Id='" + this.getStudentId() + '\'' + "\n" + + "avrage point=" + this.getAvragePoint() + "\n" ; + } +} + +class StudentManagement { + private TreeSet students; + + public StudentManagement() { + students = new TreeSet<>(); + } + public void addStudent(String name, String studentId, double gpa) { + Student student = new Student(name, studentId, gpa); + students.add(student); + } + public Student findStudentById(String studentId) { + for (Student student : students) { + if (student.getStudentId().equals(studentId)) { + return student; + } + } + return null; + } + public void showMenu() { + Scanner scanner = new Scanner(System.in); + while (true) { + System.out.println("Menu:"); + System.out.println("1. Add Student"); + System.out.println("2. Search Student by ID"); + System.out.println("3. Exit"); + System.out.print("Choose an option: "); + int option = scanner.nextInt(); + scanner.nextLine(); + + switch (option) { + case 1: + System.out.print("Enter student name: "); + String name = scanner.nextLine(); + System.out.print("Enter student ID: "); + String studentId = scanner.nextLine(); + System.out.print("Enter student Average Point: "); + double avg = scanner.nextDouble(); + addStudent(name, studentId, avg); + break; + case 2: + System.out.print("Enter student ID to search: "); + String searchId = scanner.nextLine(); + Student student = findStudentById(searchId); + if (student != null) { + System.out.println("Student found: \n" + student.toString()); + } else { + System.out.println("Student not found."); + } + break; + case 3: + System.out.println("Exiting!!!"); + return; + default: + System.out.println("Invalid option. Please try again another one."); + } + } + } +} +