diff --git a/CarQueue.java b/CarQueue.java new file mode 100644 index 0000000..3510ca8 --- /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 carQueue = new LinkedList<>(); + + carQueue.add("BMW"); + carQueue.add("Toyota"); + carQueue.add("Honda"); + + System.out.println("Queue: " + carQueue); + String firstCar = carQueue.poll(); + System.out.println("Queue: " + carQueue); + String secondCar = carQueue.poll(); + System.out.println("Queue: " + carQueue); + String thirdCar = carQueue.poll(); + System.out.println("Queue: " + carQueue); + + if (carQueue.isEmpty()) { + System.out.println("Empty."); + } else { + System.out.println("Not Empty."); + } + } +} diff --git a/HashCollisionChecker.java b/HashCollisionChecker.java index 80631a7..783ae2f 100644 --- a/HashCollisionChecker.java +++ b/HashCollisionChecker.java @@ -2,13 +2,19 @@ public class HashCollisionChecker { public static int countOfUniqueHashCodes(HashSet set) { - // TODO: Implement - return 0; + HashSet hashCodes = new HashSet<>(); + for (T item : set) { + hashCodes.add(item.hashCode()); + } + return hashCodes.size(); } public static int countOfUniqueHashCodes(HashMap map) { - // TODO: Implement - return 0; + HashSet hashCodes = new HashSet<>(); + for (K key : map.keySet()) { + hashCodes.add(key.hashCode()); + } + return hashCodes.size(); } public static void main(String[] args) { diff --git a/ShuffleLinkedList.java b/ShuffleLinkedList.java new file mode 100644 index 0000000..176ac7b --- /dev/null +++ b/ShuffleLinkedList.java @@ -0,0 +1,17 @@ +import java.util.Collections; +import java.util.LinkedList; + +public class ShuffleLinkedList { + public static void main(String[] args) { + LinkedList list = new LinkedList<>(); + list.add(45); + list.add("GFG"); + list.add(2.56f); + list.add(3.14); + list.add("Ali"); + list.add(true); + System.out.println("Before: " + list.toString()); + Collections.shuffle(list); + System.out.println("After: " + list.toString()); + } +} diff --git a/Detective.java b/ToDos.java similarity index 69% rename from Detective.java rename to ToDos.java index 5707416..3757e3d 100644 --- a/Detective.java +++ b/ToDos.java @@ -1,7 +1,7 @@ import java.util.ArrayList; -class ToDos { +public class ToDos { public static void main(String[] args) { @@ -25,10 +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: " + sherlocksToDos.size()); + System.out.println("Poirot: " + poirotsToDos.size()); // Print the name of the detective with the larger to-do list: + if (sherlocksToDos.size() > poirotsToDos.size()) { + System.out.println("Sherlock."); + } else if (sherlocksToDos.size() < poirotsToDos.size()) { + System.out.println("Poirot."); + } else { + System.out.println("Equal."); + } } diff --git a/University.java b/University.java new file mode 100644 index 0000000..221efdd --- /dev/null +++ b/University.java @@ -0,0 +1,61 @@ +import java.util.TreeSet; + +class Student implements Comparable { + private int id; + private String name; + private double moadel; + + public Student(int id, String name, double moadel) { + this.id = id; + this.name = name; + this.moadel = moadel; + } + + public int getId() { + return id; + } + + public String getName() { + return name; + } + + public double getMoadel() { + return moadel; + } + + @Override + public int compareTo(Student other) { + return Integer.compare(this.id, other.id); + } +} + +public class University { + private TreeSet students; + + public University() { + students = new TreeSet<>(); + } + + public void addStudent(int id, String name, double moadel) { + students.add(new Student(id, name, moadel)); + } + + public String searchStudentById(int id) { + for (Student student : students) { + if (student.getId() == id) { + return "Name: " + student.getName() + ", Moadel: " + student.getMoadel(); + } + } + return "Student not found."; + } + + public static void main(String[] args) { + University university = new University(); + university.addStudent(1, "Hasan", 3.5); + university.addStudent(2, "Shima", 3.8); + university.addStudent(3, "Amir", 3.9); + + System.out.println(university.searchStudentById(2)); + System.out.println(university.searchStudentById(4)); + } +}