Skip to content

Final exercises #8

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

public class Car {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Queue<String> cars = new LinkedList<>();

String car;
System.out.println("Type the car you want! if you are finished typing the cars, type \"Finish\".");
do {
car = scan.next();
if (car.equals("Finish")) {
break;
}
cars.add(car);
} while (!car.equals("Finish"));

Iterator<String> carIterator = cars.iterator();

System.out.print("Cars: ");
while (carIterator.hasNext()) {
System.out.print(carIterator.next() + ", ");
}
System.out.println();

while (!cars.isEmpty()) { // polling cars from the queue
String removedCar = dequeue(cars);
System.out.println(removedCar+ " is removed!");
}

if (cars.isEmpty()) {
System.out.println("All of the cars have left!");
} else {
System.out.println(cars);
}
}

public static void enqueue(Queue<String> cars,String car) {
cars.add(car);
}
public static String dequeue(Queue<String> cars) {
return cars.poll();
}
}
35 changes: 0 additions & 35 deletions Detective.java

This file was deleted.

74 changes: 45 additions & 29 deletions HashCollisionChecker.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
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.*;

public class HashCollisionChecker {
public static <T> int countOfUniqueHashCodes(HashSet<T> set) {
// TODO: Implement
Set<T> newSet = new HashSet<>();
Set<Integer> hashCodeCollector = new HashSet<>();
Iterator<T> it = set.iterator();
while ( it.hasNext() ) {
newSet.add(it.next());
hashCodeCollector.add(newSet.hashCode());
newSet.clear();
}
return hashCodeCollector.size();
}

public static <K, V> int countOfUniqueHashCodes(HashMap<K, V> map) {
// TODO: Implement
Set<K> set = new HashSet<>();
Set<Integer> hashCodeCollector = new HashSet<>();
Iterator<K> it = map.keySet().iterator();
while (it.hasNext()) {
set.add(it.next());
hashCodeCollector.add(set.hashCode());
set.clear();
}
return hashCodeCollector.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
}
}
23 changes: 23 additions & 0 deletions Shuffle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.*;

public class Shuffle {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
LinkedList<Object> myLinkedList = new LinkedList<>();

Object value;
System.out.println("Add your elements! if you don't want to add more, type \"Finish\".");
do {
value = scan.next();
if (value.equals("Finish")) {
break;
}
myLinkedList.add(value);
} while (!value.equals("Finish"));

System.out.println("Main LinkedList: "+myLinkedList);

Collections.shuffle(myLinkedList);
System.out.println("Shuffled LinkedList: "+myLinkedList);
}
}
40 changes: 40 additions & 0 deletions ToDos.java
Original file line number Diff line number Diff line change
@@ -0,0 +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:
int sherlocksToDosSize = sherlocksToDos.size();
int poirotsToDosSize = poirotsToDos.size();

System.out.println("Sherlock's amount of to dos: "+ sherlocksToDosSize);
System.out.println("Poirot's amount of to dos: "+ poirotsToDosSize);

// Print the name of the detective with the larger to-do list:
if (sherlocksToDosSize > poirotsToDosSize) {
System.out.println("Sherlock");
} else {
System.out.println("Poirot");
}
}
}
72 changes: 72 additions & 0 deletions University.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import java.util.*;

class Student {
private String name;
private int studentId;
private double gpa;

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

public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public int getStudentId() {
return studentId;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
public double getGpa() {
return gpa;
}
public String showDetails(int studentId) {
return "\nname: "+ this.name +"\nID: "+ this.studentId + "\nGPA: "+ this.gpa;
}
}


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

System.out.print("Type the number of students you want to add: ");
int studentNum = scan.nextInt();
for (int i = 0; studentNum > i; i++) {
System.out.print("Enter student's name: ");
String studentName = scan.next();

System.out.print("Enter student's ID: ");
int studentId = scan.nextInt();

System.out.print("Enter student's GPA: ");
double studentGpa = scan.nextDouble();

Student student = new Student(studentName,studentId,studentGpa);
students.add(student);
System.out.println("---------------------------");

}

System.out.print("Enter the student ID you want to search: ");
int search = scan.nextInt();

List<Student> studentList = new ArrayList<>(students);
for (int i = 0; studentNum > i; i++) {
int searchedStudentID = studentList.get(i).getStudentId();
if (search == searchedStudentID) {
System.out.println(studentList.get(i).showDetails(searchedStudentID));
}
}
}
}