Skip to content
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
83 changes: 83 additions & 0 deletions Week 7/LinkedHashSetOperations.java
Original file line number Diff line number Diff line change
@@ -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<Integer> operations;
public linkedhashset(){
operations = new LinkedHashSet<>();
}
private void addValue(int number){
operations.add(number);
}
private void printSortedLinkedHashSet(){
List<Integer> 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());
}
}
}
75 changes: 75 additions & 0 deletions Week 7/TreeMapOperations.java
Original file line number Diff line number Diff line change
@@ -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<Integer,Integer> 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());
}
}
}
39 changes: 39 additions & 0 deletions Week 7/WordDictionary.java
Original file line number Diff line number Diff line change
@@ -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<Character,ArrayList<String>> 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));
}
}
}


75 changes: 75 additions & 0 deletions Week 7/set.java
Original file line number Diff line number Diff line change
@@ -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<Integer> operations;
public SetOperations(){
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());
}
}
}