Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

MyInformationSystem Class that uses an array list and an inner class using java

You will be writing a class called MyInformationSystem that uses an array list and an inner class called Pair. Both MyInformationSystem and Pair are generic. Copy and paste the main method from below into MyInformationSystem in order to test your class. Sample output is given and MyInformationSystem is described below.
Class MyInformationSystem:
This class is generic, taking a single type parameter which you can call T. It has an inner class called Pair, which is also generic. Be sure to use a different type parameter name in Pair, U, instead of T. This is because you want this type parameter name to conflict with that of the outer class. The Pair class has a U called value and a String called key. Pair objects represent a key-value pair. It also has a method called keyMatches which takes a String and returns true if the key instance variable matches the String received. Otherwise, it returns false.
Outside of the Pair class (still in the MyInformationSystem class), MyInformationSystem has an ArrayList of Pair objects called myList. This list will store a series of Pair objects, as per the main method below. MyInformationSystem has a method called insert which takes a T object and a String called key. This method creates a pair from the given value and key and adds it to the end of myList. Finally, MyInformationSystem has a method called retrieve which returns a T object and receives a String called key. This method iterates through the objects of myList and uses keyMatches to determine if it has found a match. If a match is found, return its value. Otherwise, return null.

Answer:

import java.util.*;
public class MyInformationSystem <T> {
	private class Pair <U>{
		private U value;
		private String key;
		boolean keyMatches (String keyValue){
			if (key.equals(keyValue)) return true;
			else return false;
		}
		private Pair (U value, String key){
			this.value =  value;
			this.key = key;
		}
		Object getT(){
			return value;
		}
	}

	private ArrayList <Pair> myList = new ArrayList <Pair> ();

	public void insert (T value, String key){
		Pair thisPair = new Pair (value,key);
		myList.add(thisPair);
	}

	public T retrieve (String keyValue) throws Exception{
		for (Pair x:myList){
			if (x.keyMatches(keyValue))
				return (T)x.getT();
		}
		return null;
	}
	
	/* main method */
	public static void main(String a[]){
		String result;
		String keyList[] = {"126", "536", "428", "245"};
		MyInformationSystem<String> nameStudentNumberList;
		nameStudentNumberList = new MyInformationSystem<String>();
		nameStudentNumberList.insert("John", "245");// John has
		// student number 245
		nameStudentNumberList.insert("Tom", "126");
		nameStudentNumberList.insert("Mary", "536");
		nameStudentNumberList.insert("Mark", "821");
		for (int i = 0; i < keyList.length; i++){
		try{
		result = nameStudentNumberList.retrieve(keyList[i]);
		System.out.println("For key " + keyList[i] +
		" the matching value is " + result);
		}
		catch(Exception e){
		System.out.println("No Match with " + keyList[i]);
		}
		}
		}
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!