From f13b416c1d1f1c209dec52023d9f36f16283e966 Mon Sep 17 00:00:00 2001 From: amira921 Date: Fri, 11 Aug 2023 18:39:17 +0300 Subject: [PATCH 1/2] week 7 tasks added --- Week 7/Linked_Hash_Set.java | 69 +++++++++++++++++++++++++++++++++++++ Week 7/Tree_Map.java | 66 +++++++++++++++++++++++++++++++++++ Week 7/Word_Dictionary.java | 38 ++++++++++++++++++++ Week 7/set.java | 69 +++++++++++++++++++++++++++++++++++++ 4 files changed, 242 insertions(+) create mode 100644 Week 7/Linked_Hash_Set.java create mode 100644 Week 7/Tree_Map.java create mode 100644 Week 7/Word_Dictionary.java create mode 100644 Week 7/set.java diff --git a/Week 7/Linked_Hash_Set.java b/Week 7/Linked_Hash_Set.java new file mode 100644 index 0000000..1421279 --- /dev/null +++ b/Week 7/Linked_Hash_Set.java @@ -0,0 +1,69 @@ +import java.util.*; + +public class Linked_Hash_Set { + public static void main(String[] args) { + LinkedHashSet_operations operations = new LinkedHashSet_operations(); + Scanner scan = new Scanner(System.in); + int testNumber = scan.nextInt(); + + for (int i = 0; i < testNumber; i++) { + + int number_of_operations = scan.nextInt(); + + for (int j = 0; j < number_of_operations; j++) { + + String operation = scan.next(); + int number; + + switch (operation) { + case "a": + number = scan.nextInt(); + operations.addValue(number); + break; + + case "b": + operations.printSortedSet(); + break; + + case "c": + number = scan.nextInt(); + operations.removeElement(number); + break; + + case "d": + number = scan.nextInt(); + operations.findNumber(number); + break; + + case "e": + operations.getSize(); + break; + } + } + } + } + static class LinkedHashSet_operations{ + LinkedHashSet operations; + public LinkedHashSet_operations(){ + operations = new LinkedHashSet<>(); + } + 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()); + } + } +} diff --git a/Week 7/Tree_Map.java b/Week 7/Tree_Map.java new file mode 100644 index 0000000..b5d4f7f --- /dev/null +++ b/Week 7/Tree_Map.java @@ -0,0 +1,66 @@ +import java.util.*; + +public class Tree_Map { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + TreeMap_operations map = new TreeMap_operations(); + int testNumber = scan.nextInt(); + + for (int i = 0; i < testNumber; i++) { + + int number_of_operations = scan.nextInt(); + + for (int j = 0; j < number_of_operations; j++) { + String operation = scan.next(); + int value , key; + + 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; + } + } + } + } + static class TreeMap_operations{ + Map operations = new TreeMap<>(); + public TreeMap_operations() { + 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/Word_Dictionary.java b/Week 7/Word_Dictionary.java new file mode 100644 index 0000000..721049d --- /dev/null +++ b/Week 7/Word_Dictionary.java @@ -0,0 +1,38 @@ +import java.util.*; + +public class Word_Dictionary { + + public static void main(String[]args){ + WordDictionary dict = new WordDictionary(); + dict.addWord('a',"apple"); + dict.addWord('a',"astronaut"); + dict.addWord('b',"ball"); + dict.addWord('c',"camera"); + dict.addWord('d',"dress"); + dict.printValues('a'); + dict.printDictionay(); + } + + static class WordDictionary { + Map> word_dectionary; + public WordDictionary(){ + word_dectionary = new HashMap<>(); + for(char c='a';c<'z';c++){ + word_dectionary.put(c,new ArrayList<>()); + } + } + public void addWord(char key,String word){ + word_dectionary.get(key).add(word); + } + public void printDictionay(){ + for(Map.Entry m : word_dectionary.entrySet()){ + System.out.println(m.getKey() + ": " + m.getValue()); + } + } + public void printValues(char key){ + System.out.println(word_dectionary.get(key)); + } + } +} + + diff --git a/Week 7/set.java b/Week 7/set.java new file mode 100644 index 0000000..f9114e9 --- /dev/null +++ b/Week 7/set.java @@ -0,0 +1,69 @@ +import java.util.*; + +public class set { + public static void main(String[] args) { + Set_operations operations = new Set_operations(); + Scanner scan = new Scanner(System.in); + int testNumber = scan.nextInt(); + + for (int i = 0; i < testNumber; i++) { + + int number_of_operations = scan.nextInt(); + + for (int j = 0; j < number_of_operations; j++) { + + String operation = scan.next(); + int number; + + switch (operation) { + case "a": + number = scan.nextInt(); + operations.addValue(number); + break; + + case "b": + operations.printSortedSet(); + break; + + case "c": + number = scan.nextInt(); + operations.removeElement(number); + break; + + case "d": + number = scan.nextInt(); + operations.findNumber(number); + break; + + case "e": + operations.getSize(); + break; + } + } + } + } + static class Set_operations{ + Set operations; + public Set_operations(){ + 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()); + } + } +} From 33260d66933a8c770c920739122c48ba3d12731a Mon Sep 17 00:00:00 2001 From: amira921 Date: Sun, 13 Aug 2023 02:19:32 +0300 Subject: [PATCH 2/2] changes requested done --- Week 7/LinkedHashSetOperations.java | 83 +++++++++++++++++++++++++++++ Week 7/Linked_Hash_Set.java | 69 ------------------------ Week 7/TreeMapOperations.java | 75 ++++++++++++++++++++++++++ Week 7/Tree_Map.java | 66 ----------------------- Week 7/WordDictionary.java | 39 ++++++++++++++ Week 7/Word_Dictionary.java | 38 ------------- Week 7/set.java | 74 +++++++++++++------------ 7 files changed, 237 insertions(+), 207 deletions(-) create mode 100644 Week 7/LinkedHashSetOperations.java delete mode 100644 Week 7/Linked_Hash_Set.java create mode 100644 Week 7/TreeMapOperations.java delete mode 100644 Week 7/Tree_Map.java create mode 100644 Week 7/WordDictionary.java delete mode 100644 Week 7/Word_Dictionary.java 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/Linked_Hash_Set.java b/Week 7/Linked_Hash_Set.java deleted file mode 100644 index 1421279..0000000 --- a/Week 7/Linked_Hash_Set.java +++ /dev/null @@ -1,69 +0,0 @@ -import java.util.*; - -public class Linked_Hash_Set { - public static void main(String[] args) { - LinkedHashSet_operations operations = new LinkedHashSet_operations(); - Scanner scan = new Scanner(System.in); - int testNumber = scan.nextInt(); - - for (int i = 0; i < testNumber; i++) { - - int number_of_operations = scan.nextInt(); - - for (int j = 0; j < number_of_operations; j++) { - - String operation = scan.next(); - int number; - - switch (operation) { - case "a": - number = scan.nextInt(); - operations.addValue(number); - break; - - case "b": - operations.printSortedSet(); - break; - - case "c": - number = scan.nextInt(); - operations.removeElement(number); - break; - - case "d": - number = scan.nextInt(); - operations.findNumber(number); - break; - - case "e": - operations.getSize(); - break; - } - } - } - } - static class LinkedHashSet_operations{ - LinkedHashSet operations; - public LinkedHashSet_operations(){ - operations = new LinkedHashSet<>(); - } - 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()); - } - } -} 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/Tree_Map.java b/Week 7/Tree_Map.java deleted file mode 100644 index b5d4f7f..0000000 --- a/Week 7/Tree_Map.java +++ /dev/null @@ -1,66 +0,0 @@ -import java.util.*; - -public class Tree_Map { - public static void main(String[] args) { - Scanner scan = new Scanner(System.in); - TreeMap_operations map = new TreeMap_operations(); - int testNumber = scan.nextInt(); - - for (int i = 0; i < testNumber; i++) { - - int number_of_operations = scan.nextInt(); - - for (int j = 0; j < number_of_operations; j++) { - String operation = scan.next(); - int value , key; - - 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; - } - } - } - } - static class TreeMap_operations{ - Map operations = new TreeMap<>(); - public TreeMap_operations() { - 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/Word_Dictionary.java b/Week 7/Word_Dictionary.java deleted file mode 100644 index 721049d..0000000 --- a/Week 7/Word_Dictionary.java +++ /dev/null @@ -1,38 +0,0 @@ -import java.util.*; - -public class Word_Dictionary { - - public static void main(String[]args){ - WordDictionary dict = new WordDictionary(); - dict.addWord('a',"apple"); - dict.addWord('a',"astronaut"); - dict.addWord('b',"ball"); - dict.addWord('c',"camera"); - dict.addWord('d',"dress"); - dict.printValues('a'); - dict.printDictionay(); - } - - static class WordDictionary { - Map> word_dectionary; - public WordDictionary(){ - word_dectionary = new HashMap<>(); - for(char c='a';c<'z';c++){ - word_dectionary.put(c,new ArrayList<>()); - } - } - public void addWord(char key,String word){ - word_dectionary.get(key).add(word); - } - public void printDictionay(){ - for(Map.Entry m : word_dectionary.entrySet()){ - System.out.println(m.getKey() + ": " + m.getValue()); - } - } - public void printValues(char key){ - System.out.println(word_dectionary.get(key)); - } - } -} - - diff --git a/Week 7/set.java b/Week 7/set.java index f9114e9..738baa9 100644 --- a/Week 7/set.java +++ b/Week 7/set.java @@ -1,50 +1,56 @@ import java.util.*; - -public class set { +class set { public static void main(String[] args) { - Set_operations operations = new Set_operations(); - Scanner scan = new Scanner(System.in); - int testNumber = scan.nextInt(); + 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(); - int number_of_operations = scan.nextInt(); - - for (int j = 0; j < number_of_operations; j++) { - - String operation = scan.next(); - int number; + switch (query) { + case "a": + number = scan.nextInt(); + operation.addValue(number); + break; - switch (operation) { - case "a": - number = scan.nextInt(); - operations.addValue(number); - break; + case "b": + operation.printSortedSet(); + break; - case "b": - operations.printSortedSet(); - break; + case "c": + number = scan.nextInt(); + operation.removeElement(number); + break; - case "c": - number = scan.nextInt(); - operations.removeElement(number); - break; + case "d": + number = scan.nextInt(); + operation.findNumber(number); + break; - case "d": - number = scan.nextInt(); - operations.findNumber(number); - break; + case "e": + operation.getSize(); + break; - case "e": - operations.getSize(); - break; - } - } + default: + System.out.println("Choose a valid option"); } } - static class Set_operations{ + private static class SetOperations{ Set operations; - public Set_operations(){ + public SetOperations(){ operations = new HashSet<>(); } private void addValue(int number){