Consider a class KeyedArrayBag where data values are associated with keys-think
ID: 3842050 • Letter: C
Question
Consider a class KeyedArrayBag where data values are associated with keys-think student name as the value and student number as the key. Values may be repeated in the bag but keys must be unique (think of having several John Doe's each with a unique student number). Assume that you have three data fields in your KeyedArrayBag class-a String array named data [], an integer array named keys [], and an integer named manyItems. Keys and their associated values are stored in corresponding elements in the two arrays i.e., data [0] stores the value associated with key [0]. State any assumptions you are making in your implementation. Write add and remove methods according to the following specifications: public void add (String value, int key)//Precondition: size ()Explanation / Answer
import java.util.Scanner;
public class KeyeArrayBag {
/*
* manyItems is the size of data and keys
*/
Integer manyItems;
String[] data = null;
Integer[] keys = null;
/*
* Initializing data and keys while creating constructor
*/
public KeyeArrayBag() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter No of Items:");
this.manyItems = Integer.parseInt(sc.nextLine());
data = new String[manyItems];
keys = new Integer[manyItems];
}
public void add(String value, Integer key) {
boolean flag = true;
if (flag) {
/*
* Checking for duplicate key
*/
for (int k = 0; k < keys.length; k++) {
if (keys[k] != null) {
if (keys[k] == key) {
System.out.println("Duplicate Key:" + key);
flag = false;
}
}
}
}
/*
* if key is unique adding values to data and keys arrays like
* data[n],keys[n] where n is index
*/
if (flag) {
for (int i = 0; i < keys.length; i++) {
if (keys[i] == null) {
data[i] = value;
keys[i] = key;
System.out.println("Data Added: value:" + value + ", " + "Key:" + key);
;
}
}
}
}
public String remove(int key) {
/*
* Checking for key exist or not
*/
for (int j = 0; j < keys.length; j++) {
if (keys[j] != key) {
return "Key is not Exist:" + key;
}
}
/*
* if key is present data and key will be removed
*/
for (int i = 0; i < keys.length; i++) {
if (keys[i] == key) {
data[i] = null;
keys[i] = null;
return "Data is Removed:";
}
}
return ".";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
KeyeArrayBag keyeArrayBag = new KeyeArrayBag();
/*
* Switch case until pressing 3 it will iterate
*/
int choice = 0;
while (true) {
System.out.println("Enter your Choice:");
System.out.println("Press 1 for **add**");
System.out.println("Press 2 for **remove**");
System.out.println("press 3 for **exit**");
choice = Integer.parseInt(sc.nextLine());
switch (choice) {
case 1:
System.out.println("Enter Value");
String value = sc.nextLine();
System.out.println("Enter Key (Must be Integer)");
int key = Integer.parseInt(sc.nextLine());
keyeArrayBag.add(value, key);
break;
case 2:
System.out.println("Enter Key to remove:");
int key1 = Integer.parseInt(sc.nextLine());
System.out.println(keyeArrayBag.remove(key1));
break;
case 3:
System.out.println("Application stoped");
System.exit(0);
default:
System.out.println("Enter 1 or 2 only");
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.