Skip to content

Mohadese #5

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 5 commits 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
36 changes: 36 additions & 0 deletions CarQueue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.LinkedList;
import java.util.Queue;

public class CarQueue {

public static void main(String[] args) {
Queue<String> 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<String> queue, String car) {
queue.add(car);
}
public static String dequeue(Queue<String> queue) {
return queue.poll();
}
public static void printQueueContents(Queue<String> queue) {
System.out.println("The queue: " + queue);
}
}
13 changes: 10 additions & 3 deletions Detective.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +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'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.");
}
}

// Poirot kar bishtari darad.
}
18 changes: 13 additions & 5 deletions HashCollisionChecker.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import java.util.*;
import java.util.HashMap;
import java.util.HashSet;

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 Down
25 changes: 25 additions & 0 deletions Shuffle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;

public class Shuffle {

public static void main(String[] args) {
LinkedList<String> 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);
}
}
82 changes: 82 additions & 0 deletions StudentManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

class Student {
private final String name;
private final int studentNumber;
private final double gpa;

public Student(String name, int studentNumber, double gpa) {
this.name = name;
this.studentNumber = studentNumber;
this.gpa = gpa;
}

public int getStudentNumber() {
return studentNumber;
}

@Override
public String toString() {
return "Name: " + name + ", Student Number: " + studentNumber + ", GPA: " + gpa;
}
}

class StudentComparator implements Comparator<Student> {
@Override
public int compare(Student s1, Student s2) {
return Integer.compare(s1.getStudentNumber(), s2.getStudentNumber());
}
}

public class StudentManager {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
TreeSet<Student> students = new TreeSet<>(new StudentComparator());

// Receiving student details from user
System.out.print("Enter the number of students: ");
int numberOfStudents = scanner.nextInt();
scanner.nextLine(); // Consume newline

for (int i = 0; i < numberOfStudents; i++) {
System.out.println("Enter details for student " + (i + 1) + ":");

System.out.print("Name: ");
String name = scanner.nextLine();

System.out.print("Student Number: ");
int studentNumber = scanner.nextInt();

System.out.print("GPA: ");
double gpa = scanner.nextDouble();
scanner.nextLine(); // Consume newline

students.add(new Student(name, studentNumber, gpa));
}

// Searching for a student by student number
System.out.print("Enter student number to search: ");
int searchStudentNumber = scanner.nextInt();

Student searchStudent = null;
for (Student student : students) {
if (student.getStudentNumber() == searchStudentNumber) {
searchStudent = student;
break;
}
}

if (searchStudent != null) {
System.out.println("Student found: " + searchStudent);
} else {
System.out.println("Student with student number " + searchStudentNumber + " not found.");
}

scanner.close();
}
}