Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
69 changes: 69 additions & 0 deletions Week 7/Linked_Hash_Set.java
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print some text to make the user know how to use the application and what to do next
for example
Enter the number of test cases
Enter the number of queries
etc

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import java.util.*;

public class Linked_Hash_Set {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of class shoudn't contain special characters

public static void main(String[] args) {
LinkedHashSet_operations operations = new LinkedHashSet_operations();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the variable name shouldn't contains special character only when it's constant variable

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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract to a new method and also handle when user enter the f
aslo what if user entered a letter that doesn't exist

}
}
}
static class LinkedHashSet_operations{
LinkedHashSet<Integer> operations;
public LinkedHashSet_operations(){
operations = new LinkedHashSet<>();
}
private void addValue(int number){
operations.add(number);
}
private void printSortedSet(){
List<Integer> 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());
}
}
}
66 changes: 66 additions & 0 deletions Week 7/Tree_Map.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import java.util.*;

public class Tree_Map {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here also the class name

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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract to a new method


case "c":
map.getSize();
break;

case "d":
key = scan.nextInt();
map.removeElement(key);
break;

case "e":
map.printElements();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract to a new method and also handle when user enter the f
aslo what if user entered a letter that doesn't exist

break;
}
}
}
}
static class TreeMap_operations{
Map<Integer,Integer> 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());
}
}
}
38 changes: 38 additions & 0 deletions Week 7/Word_Dictionary.java
Original file line number Diff line number Diff line change
@@ -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<Character,ArrayList<String>> 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make it add the word automatically to its coressponding key

}
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));
}
}
}


69 changes: 69 additions & 0 deletions Week 7/set.java
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same comments in other tasks

Original file line number Diff line number Diff line change
@@ -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<Integer> operations;
public Set_operations(){
operations = new HashSet<>();
}
private void addValue(int number){
operations.add(number);
}
private void printSortedSet(){
List<Integer> 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());
}
}
}