Skip to content

Last assignment #1

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);
}
}
75 changes: 40 additions & 35 deletions Detective.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@

import java.util.ArrayList;

class ToDos {

public static void main(String[] args) {

// 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:



// Print the name of the detective with the larger to-do list:

}

}
import java.util.ArrayList;

class ToDos {

public static void main(String[] args) {

// 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("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.");
}
}
}
// پوارو کار بیشتری دارد
66 changes: 37 additions & 29 deletions HashCollisionChecker.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
import java.util.*;

public class HashCollisionChecker {
public static <T> int countOfUniqueHashCodes(HashSet<T> set) {
// TODO: Implement
return 0;
}

public static <K, V> int countOfUniqueHashCodes(HashMap<K, V> map) {
// TODO: Implement
return 0;
}

public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("c#c#c#c#c#c#bBc#c#c#c#bBc#");
set.add("abcd");
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

HashMap<String, Integer> 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
}
}
import java.util.HashMap;
import java.util.HashSet;

public class HashCollisionChecker {

public static <T> int countOfUniqueHashCodes(HashSet<T> set) {
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) {
HashSet<Integer> uniqueHashCodes = new HashSet<>();
for (K key : map.keySet()) {
uniqueHashCodes.add(key.hashCode());
}
return uniqueHashCodes.size();
}

public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("c#c#c#c#c#c#bBc#c#c#c#bBc#");
set.add("abcd");
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

HashMap<String, Integer> 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
}
}
25 changes: 25 additions & 0 deletions ShuffleLinkedList.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 ShuffleLinkedList {

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);
}
}
88 changes: 88 additions & 0 deletions University.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import java.util.*;

class Student implements Comparable<Student> {
private String name;
private String studentId;
private double gpa;

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

public String getName() {
return name;
}
public String getStudentId() {
return studentId;
}
public double getGpa() {
return gpa;
}

@Override
public int compareTo(Student other) {
return this.studentId.compareTo(other.studentId);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Student student = (Student) obj;
return studentId.equals(student.studentId);
}

@Override
public int hashCode() {
return Objects.hash(studentId);
}

@Override
public String toString() {
return "name = " + name +
", studentId = " + studentId +
", gpa = " + gpa ;

}
}

public class University {

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

System.out.println("Enter the number of students:");
int numberOfStudents = scanner.nextInt();
scanner.nextLine();

for (int i = 0; i < numberOfStudents; i++) {
System.out.println("Enter Info for student " + (i + 1) + ":");
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Student ID: ");
String studentId = scanner.nextLine();
System.out.print("GPA: ");
double gpa = scanner.nextDouble();
scanner.nextLine();

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

System.out.print("Enter the Student ID to search for: ");
String searchId = scanner.nextLine();


Student searchStudent = new Student("", searchId, 0);
Student foundStudent = students.ceiling(searchStudent);

if (foundStudent != null && foundStudent.getStudentId().equals(searchId)) {
System.out.println(foundStudent);
} else {
System.out.println("Student with ID " + searchId + " not found.");
}

}
}