How JustAnswer Works:
  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.
Ask Arty Your Own Question
Arty
Arty, Computer Software Engineer
Category: Programming
Satisfied Customers: 575
Experience:  15+ years of programming, C, C++, Java, PHP, awk, ruby, shell, ASM, Forth, Raptor, M.A.R.I.E, Excel
31145473
Type Your Programming Question Here...
Arty is online now

adding methods to a map

Customer Question

In this part we want you to add some instance methods to the class you have written for part (ii). Each of these methods will involve the map object. You must comment your methods appropriately so that their purpose is clear. Use Javadoc tags, as introduced in Unit 8, such as @param and @return. Write separate public instance methods to implement all of the following three requirements for the class that models your chosen scenario (you will need to decide on return type and arguments): Method name Description Marks printMap() Iterates over the map to print out a meaningful textual representation of its contents. 3 printMapValue() If the argument is a key in the map, prints a meaningful textual representation of its associated value, otherwise displays a dialogue box announcing that the key is not present. 3 addMapEntry() Adds the key and value given as arguments to the map, checking first to see if the key already exists and giving the user the option of overwriting the existing value for that key, or doing nothing. Thank you

Submitted: 3 years ago.
Category: Programming
Expert:  Arty replied 3 years ago.

Arty :

Hello.
Probably this is Java, not JavaScript. What is not clear, is "you to add some instance methods to the class you have written for part (ii)". Could you provide class files, written previously, so I could expand them? Or provide previous questions, so I could write the code.


Regards,
Arty

JACUSTOMER-jgixcxki- :

I have managed to add some methods to the map but now I am having difficulty with a method called selectKeys() which returns a collection of all keys that satisfy some selection criterion and also a method called selectValues () which returns a selection of values which match a selection criterion and a final method called createReduceMap() which creates a new map of the same type but only including entries that also match a particular selection criterion. here is my program so far:

JACUSTOMER-jgixcxki- :

/**
* A golf club map called Members to keep track of its members
* whose keys are type Integer which represents membership number. The values associated with these keys
* are of type String and represent the members name and exact handicap.
*
* @author (Aidan O Sullivan)
* @version (14/05/2014)
*/
public class Members
{
// instance variables - replace the example below with your own
private Map<Integer, String> membersMap;

/**
* Constructor for objects of class Members
*/
public Members()
{
membersMap = new HashMap<>();
}

/**
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void populateMap()
{
membersMap.put(1, "Mark/5.5");
membersMap.put(2, "john/12.5");
membersMap.put(3, "Michael/17.8");
membersMap.put(4, "Peter/9.8");
membersMap.put(5, "Paul/10.5");
}

/**
* Iterates over map to print contents.
*/
public void printMap()
{
Set<Integer> mapKeys = membersMap.keySet();
for (Integer numbers: mapKeys)
{

System.out.println("The member number is " + numbers + " Name/Handicap " + membersMap.get(numbers));
}

}

/**
* If the argument is a key it prints its contents otherwise
* lets user know key is not present.
*/

public void printMapValue(Integer number)
{
if(membersMap.get(number) != null)
{ System.out.println("The member associated with this number is "+
membersMap.get(number));
}
else
{
OUDialog.alert("This membership number does not exist");
}
}

/**
* Adds the argument to the map, if the key already exists it gives
* the user the option to overwrite the entry or do nothing.
*/
public void addMapEntry(Integer num, String nameHandi)
{
if(membersMap.get(num)== null)
{
membersMap.put(num,nameHandi);
}
else
{
if( OUDialog.confirm("The number already exists, would you like to replace it?"))
{
membersMap.put(num,nameHandi);
}

}


}

/**
* If the key given as the argument exists the method deletes the key - value pair.
* Otherwise does nothing.
*/
public void deleteEntry(Integer key)
{
if(membersMap.get(key)!= null)
{
membersMap.remove(key);
}


}

Arty :

/**
* Test condition, use this interface to create search conditions
*
*/
public interface SearchCondition<String> {
/*
* Check for condition
*
* @param menber value for testing
* @return true if the member matches condition, false otherwise
*/
boolean test(String member);
}

/**
* Return a collection of all keys that satisfy some selection criterion
* read
* http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#approach5
*
* @param cond condition expression
* @return set of keys, where values match condition cond
*/
public Set<Integer> selectKeys(SearchCondition<String> cond){
Set <Integer> subset;
subset = new HashSet<Integer>();
for (Integer key: membersMap.keySet()){
if (cond.test(membersMap.get(key))) {
subset.add(key);
}
}
return subset;
}

/**
* Return a collection of values, that satisfy condition
*
* @param cond condition to match
* @return set of values that match condition
*/
public Set<String> selectValues(SearchCondition<String> cond){
Set <String> subset;
subset = new HashSet<String>();
for (String val: membersMap.values()){
if (cond.test(val)){
subset.add(val);
}
}
return subset;
}

/**
* Return key-value map on values, that match condition
*
* @param cond condition to match
* @return return (K,V) pair - all values, that match cond
*/
public Map<Integer, String> createReduceMap(SearchCondition<String> cond){
Map <Integer, String> reduction;
reduction = new HashMap();
for (Integer key: membersMap.keySet()){
if (cond.test(membersMap.get(key))){
reduction.put(key, membersMap.get(key));
}
}
return reduction;
}

Arty :

Hi.

Arty :

Sorry for delay. I'm in different timezone.

Arty :

you should create interface for test condition, then use it as a function parameter

Arty :

http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#approach5

Arty :

here is the basic idea of this approach

Arty :

I will put the code to pastebin in 1 minute

Arty :

I'll switch to Q&A mode if you don't need live chat

Arty :

http://pastebin.com/50RL944A

Expert:  Arty replied 3 years ago.
http://pastebin.com/50RL944A - here is a better formatted code