diff --git a/Week 7/LinkedHashSetOperations.java b/Week 7/LinkedHashSetOperations.java new file mode 100644 index 0000000..24f2293 --- /dev/null +++ b/Week 7/LinkedHashSetOperations.java @@ -0,0 +1,83 @@ +import java.util.*; +public class LinkedHashSetOperations { + public static void main(String[] args) { + linkedhashset operation = new linkedhashset(); + Scanner scanner = new Scanner(System.in); + + System.out.print("Enter the number of tests: "); + int testNumber = scanner.nextInt(); + for (int i = 0; i < testNumber; i++) { + System.out.print("Enter the number of operations: "); + int numberOfOperations = scanner.nextInt(); + for (int j = 0; j < numberOfOperations; j++) { + chooseOperation(operation); + } + } + } + private static void chooseOperation(linkedhashset operations){ + int number; + Scanner scan = new Scanner(System.in); + + System.out.print("Enter an operation: "); + String operation = scan.next(); + + switch (operation) { + case "a": + number = scan.nextInt(); + operations.addValue(number); + break; + + case "b": + operations.printSortedLinkedHashSet(); + break; + + case "c": + number = scan.nextInt(); + operations.removeElement(number); + break; + + case "d": + number = scan.nextInt(); + operations.findNumber(number); + break; + + case "e": + operations.getSize(); + break; + + case "f": + operations.printElement(); + + default: + System.out.println("Choose a valid option"); + } + } + private static class linkedhashset{ + LinkedHashSet operations; + public linkedhashset(){ + operations = new LinkedHashSet<>(); + } + private void addValue(int number){ + operations.add(number); + } + private void printSortedLinkedHashSet(){ + List sortedNumbers = new ArrayList<>(operations); + Collections.sort(sortedNumbers); + for (Integer n : sortedNumbers) System.out.print(n + " "); + System.out.println(); + } + private void printElement(){ + for(Integer item: operations) System.out.print(item + " "); + System.out.println(); + } + private void removeElement(int number){ + operations.remove(number); + } + private void findNumber(int number){ + System.out.println(operations.contains(number)); + } + private void getSize(){ + System.out.println(operations.size()); + } + } +} diff --git a/Week 7/TreeMapOperations.java b/Week 7/TreeMapOperations.java new file mode 100644 index 0000000..511e1ff --- /dev/null +++ b/Week 7/TreeMapOperations.java @@ -0,0 +1,75 @@ +import java.util.*; +public class TreeMapOperations { + public static void main(String[] args) { + treemap map = new treemap(); + Scanner scanner = new Scanner(System.in); + System.out.print("Enter the number of tests: "); + int testNumber = scanner.nextInt(); + + for (int i = 0; i < testNumber; i++) { + System.out.print("Enter the number of operations: "); + int numberOfOperations = scanner.nextInt(); + for (int j = 0; j < numberOfOperations; j++) { + chooseOperation(map); + } + } + } + + public static void chooseOperation(treemap map) { + int value, key; + Scanner scan = new Scanner(System.in); + System.out.print("Enter an operation: "); + String operation = scan.next(); + + switch (operation) { + case "a": + key = scan.nextInt(); + value = scan.nextInt(); + map.addValue(key,value); + break; + + case "b": + key = scan.nextInt(); + map.findKey(key); + break; + + case "c": + map.getSize(); + break; + + case "d": + key = scan.nextInt(); + map.removeElement(key); + break; + + case "e": + map.printElements(); + break; + + default: + System.out.println("Choose a valid option"); + } + } + + static class treemap{ + Map operations; + public treemap() { + operations= new TreeMap<>(); + } + private void addValue(int key,int value){ + operations.put(key,value); + } + private void findKey(int key){ + if(operations.containsKey(key)) System.out.println(operations.get(key)); else System.out.println(-1); + } + private void getSize(){ + System.out.println(operations.size()); + } + private void removeElement(int key){ + System.out.println(operations.remove(key)); + } + private void printElements(){ + for(Map.Entry m: operations.entrySet()) System.out.println(m.getKey() + ":" + m.getValue()); + } + } +} diff --git a/Week 7/WordDictionary.java b/Week 7/WordDictionary.java new file mode 100644 index 0000000..30081ac --- /dev/null +++ b/Week 7/WordDictionary.java @@ -0,0 +1,39 @@ +import java.util.*; + +public class WordDictionary { + + public static void main(String[]args){ + wordDictionary dict = new wordDictionary(); + dict.addWord("apple"); + dict.addWord("astronaut"); + dict.addWord("ball"); + dict.addWord("camera"); + dict.addWord("dress"); + dict.printValues('a'); + dict.printDictionary(); + } + + static class wordDictionary { + Map> wordDictionary; + public wordDictionary(){ + wordDictionary = new HashMap<>(); + for(char c='a';c<'z';c++){ + wordDictionary.put(c,new ArrayList<>()); + } + } + public void addWord(String word){ + for(Map.Entry m : wordDictionary.entrySet()) + if(m.getKey().equals(word.charAt(0))) wordDictionary.get(m.getKey()).add(word); + } + public void printDictionary(){ + for(Map.Entry m : wordDictionary.entrySet()){ + System.out.println(m.getKey() + ": " + m.getValue()); + } + } + public void printValues(char key){ + System.out.println(wordDictionary.get(key)); + } + } +} + + diff --git a/Week 7/set.java b/Week 7/set.java new file mode 100644 index 0000000..738baa9 --- /dev/null +++ b/Week 7/set.java @@ -0,0 +1,75 @@ +import java.util.*; +class set { + public static void main(String[] args) { + SetOperations operation = new SetOperations(); + Scanner scanner = new Scanner(System.in); + + System.out.print("Enter the number of tests: "); + int testNumber = scanner.nextInt(); + for (int i = 0; i < testNumber; i++) { + System.out.print("Enter the number of operations: "); + int numberOfOperations = scanner.nextInt(); + for (int j = 0; j < numberOfOperations; j++) { + chooseOperation(operation); + } + } + } + private static void chooseOperation(SetOperations operation){ + int number; + Scanner scan = new Scanner(System.in); + System.out.print("Enter an operation: "); + String query = scan.next(); + + switch (query) { + case "a": + number = scan.nextInt(); + operation.addValue(number); + break; + + case "b": + operation.printSortedSet(); + break; + + case "c": + number = scan.nextInt(); + operation.removeElement(number); + break; + + case "d": + number = scan.nextInt(); + operation.findNumber(number); + break; + + case "e": + operation.getSize(); + break; + + default: + System.out.println("Choose a valid option"); + } + } + private static class SetOperations{ + Set operations; + public SetOperations(){ + operations = new HashSet<>(); + } + private void addValue(int number){ + operations.add(number); + } + private void printSortedSet(){ + List sortedNumbers = new ArrayList<>(operations); + Collections.sort(sortedNumbers); + for (Integer n : sortedNumbers) System.out.print(n + " "); + System.out.println(); + } + private void removeElement(int number){ + operations.remove(number); + } + private void findNumber(int number){ + System.out.println(operations.contains(number)); + } + private void getSize(){ + System.out.println(operations.size()); + } + } +}