Skip to content

final Task #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CarQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.*;

public class CarQueue {
public static void main(String[] args) {
Queue<String> 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.");
}
}
}
43 changes: 43 additions & 0 deletions Detectives.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.util.ArrayList;
class ToDos {
public static void main(String[] args) {
int sherDos;
int poiDos;

// Sherlock
ArrayList<String> sherlocksToDos = new ArrayList<String>();

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<String> poirotsToDos = new ArrayList<String>();

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!");
}

}

}
16 changes: 13 additions & 3 deletions HashCollisionChecker.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import com.sun.source.tree.CompilationUnitTree;

import java.util.*;

public class HashCollisionChecker {
public static <T> int countOfUniqueHashCodes(HashSet<T> set) {
// TODO: Implement
return 0;
HashSet<Integer> uniqueHashCodes = new HashSet<>();
for (T item : set) {
uniqueHashCodes.add(item.hashCode());
}
return uniqueHashCodes.size();
}

public static <K, V> int countOfUniqueHashCodes(HashMap<K, V> map) {
// TODO: Implement
return 0;
HashSet<Integer> uniqueHashCodes = new HashSet<>();
for (K key : map.keySet()) {
uniqueHashCodes.add(key.hashCode());
}
return uniqueHashCodes.size();
}

public static void main(String[] args) {
Expand All @@ -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
}
}
}
13 changes: 13 additions & 0 deletions Shuffle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.*;

public class Shuffle {
public static void main(String[] args) {
LinkedList<Integer> 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);
}
}
119 changes: 119 additions & 0 deletions University.java
Original file line number Diff line number Diff line change
@@ -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<Student> {
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<Student> 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.");
}
}
}
}