+ 3

Implementing Key-Value-Store

Hey guys! I have to implement the basic functionalities of a KeyValueStore class. It’s supposed to store Key-Value-Pairs as objects of the KeyValuePair class (It contains keys and values as String objects). The objects are stored in an array, the size of which is to be transferred to the constructor of the Key-Value-Store. The Key Value Store should provide four methods for accessing the key value pairs: - newKVP(String key, String value) - getKVP(String key) - updateKVP(String key, String newValue) - deleteKVP(String key) In addition, these methods have to throw an IllegalArgumentException to invalid parameters, such as: - zero was passed for one of the parameters - KVP with given key already exists at newKVP - there is no KVP for the given key with get, update, delete Now I know it’s a lot but I’m fairly new to programming and these tasks that I’m given are really lowering my motivation due to my incapability of fulfilling them. I’m not asking for a finished code, all I’m asking for is a basic guideline. How would you explain this task to, for example, a young child?

12th May 2020, 10:38 PM
Terminatez
9 Antworten
+ 2
it is an interesting oop practice. what is the language requirement? C?
13th May 2020, 12:44 AM
Gordon
Gordon - avatar
+ 1
Use a hashmap data structure.....then it will be too easy to implement....... For more info try this: https://www.w3schools.com/java/java_hashmap.asp
13th May 2020, 4:05 AM
[ ]
[     ] - avatar
+ 1
This is something what i have written....its just a basic structure for your question.....make it as fancy as u want it to be.....I haven't dealt with the exception here so take care of it yourself..... hope it helps !!! import java.util.HashMap; class KeyValueStore { HashMap<String,String> map=new HashMap<String ,String>(); void newKVP(String key,String value) { if(!(map.containsKey(key))) { map.put(key,value); } } String getKVP(String key) { if(map.containsKey(key)); { return map.get(key); } } void updateKVP(String key,String value) { if(map.containsKey(key)) { String ovalue=map.get(key); map.replace(key,ovalue,value); } } void deleteKVP(String key) { if(map.containsKey(key)) { map.remove(key); } } } class Main { public static void main(String args[]) { KeyValueStore obj =new KeyValueStore(); obj.newKVP("KING","[ ]"); System.out.println(obj.getKVP("KING")); obj.updateKVP("KING","A"); System.out.println(obj.getKVP("KING")); obj.deleteKVP("KING"); } }
13th May 2020, 11:09 AM
[ ]
[     ] - avatar
0
I'm required to use Java
13th May 2020, 1:25 AM
Terminatez
0
class List { private Integer[] myList = new Integer[10]; private int next = 0; public List() { } public Integer get(int idx) { return this.myList[idx]; } public void insert(Integer value) { this.myList[this.next] = value; next++; } } This is what I have so far
13th May 2020, 9:32 AM
Terminatez
0
import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; class ListTests { private List list; @BeforeEach void init() { this.list = new List(); } @Test void regularSet() { this.list.insert(Integer.valueOf(3)); assertEquals(this.list.get(0), 3); } @Test void outOfBoundsSet() { assertThrows(ArrayIndexOutOfBoundsException.class, () -> this.list.get(11)); } } And this is what I'll use for my JUnit tests
13th May 2020, 9:33 AM
Terminatez
0
Thank you lots! What do you mean by "ovalue" in the updateKVP method?
13th May 2020, 11:32 AM
Terminatez
0
It's old value..... which is already stored in that key.... Reference :https://www.javatpoint.com/java-map
13th May 2020, 12:02 PM
[ ]
[     ] - avatar