diff --git a/Cars.java b/Cars.java new file mode 100644 index 0000000..2b63bb0 --- /dev/null +++ b/Cars.java @@ -0,0 +1,22 @@ +import java.util.*; +public class Main { + public static void main(String[] args) { + Queue Cars = new LinkedList<>(); + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + for (int i = 0; i < n; i++){ + Cars.add(sc.next()); + } + + for (int i = 0; i < n; i++){ + System.out.println(Cars.poll()); + } + + if (Cars.isEmpty()) { + System.out.println("is empty"); + } + else { + System.out.println("is not empty"); + } + } +} diff --git a/Detective.java b/Detective.java index 5707416..a1c074a 100644 --- a/Detective.java +++ b/Detective.java @@ -1,35 +1,44 @@ - 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: - - } - + + 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 Sherlock = sherlocksToDos.size(); + int Poirot = poirotsToDos.size(); + System.out.println(Sherlock); + System.out.println(Poirot); + + + // Print the name of the detective with the larger to-do list: + + if(Sherlock > Poirot) { + System.out.println("Sherlock"); + } + else { + System.out.println("Poirot"); + } + + } + } diff --git a/HashCollisionChecker.java b/HashCollisionChecker.java index 80631a7..4edc570 100644 --- a/HashCollisionChecker.java +++ b/HashCollisionChecker.java @@ -2,14 +2,20 @@ 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) { HashSet set = new HashSet<>(); @@ -18,12 +24,12 @@ public static void main(String[] args) { 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 + System.out.println(HashCollisionChecker.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 + System.out.println(HashCollisionChecker.countOfUniqueHashCodes(map)); // 2 } } diff --git a/Shuffle.java b/Shuffle.java new file mode 100644 index 0000000..eb68aa9 --- /dev/null +++ b/Shuffle.java @@ -0,0 +1,13 @@ +import java.util.LinkedList; +import java.util.Collections; +public class Main { + public static void main(String[] args) { + LinkedList linkedList = new LinkedList<>(); + linkedList.add(1); + linkedList.add(2); + linkedList.add(3); + linkedList.add(4); + Collections.shuffle(linkedList); + System.out.println(linkedList); + } +} diff --git a/University.java b/University.java new file mode 100644 index 0000000..d799697 --- /dev/null +++ b/University.java @@ -0,0 +1,73 @@ +public class Student implements Comparable { + private String Name; + private int ID; + private Double GPA; + public Student (String Name, int ID, Double GPA){ + this.Name = Name; + this.ID = ID; + this.GPA = GPA; + } + public String getName() { + return Name; + } + public void setName(String Name){ + this.Name = Name; + } + public int getID() { + return ID; + } + public void setID(int ID) { + this.ID = ID; + } + public Double getGPA() { + return GPA; + } + public void setGPA(Double GPA) { + this.GPA = GPA; + } + + public int compareTo(Student other) { + return Integer.compare(this.ID, other.ID); + } + public String toString() { + return "Name: " + Name + ", Student ID: " + ID + ", GPA: " + GPA; + } +} + +import java.util.Scanner; +import java.util.TreeSet; + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + TreeSet students = new TreeSet<>(); + + System.out.println("Enter number of students:"); + int numberOfStudents = sc.nextInt(); + sc.nextLine(); + for (int i = 0; i < numberOfStudents; i++) { + System.out.println("Student " + (i + 1) + ":"); + System.out.print("Name: "); + String Name = sc.nextLine(); + System.out.print("Student ID: "); + int ID = sc.nextInt(); + System.out.print("GPA: "); + double GPA = sc.nextDouble(); + sc.nextLine(); + + students.add(new Student(Name, ID, GPA)); + } + + System.out.print("Enter student ID to search: "); + int searchID = sc.nextInt(); + Student searchStudent = new Student("", searchID, 0.0); + + Student result = students.ceiling(searchStudent); + + if (result != null && result.getID() == searchID) { + System.out.println("Student found: " + result); + } else { + System.out.println("Student not found."); + } + } +}