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: 2 years ago.
Category: Programming
Expert:  Arty replied 2 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 2 years ago.
http://pastebin.com/50RL944A - here is a better formatted code

What Customers are Saying:

 
 
 
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer
< Previous | Next >
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C.
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C.
 
 
 

Meet The Experts:

 
 
 
  • ATLPROG

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7260
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
< Last | Next >
  • http://ww2.justanswer.com/uploads/SP/spatlanta2010/2011-6-23_12450_photo.64x64.gif ATLPROG's Avatar

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7260
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Computer Software Engineer

    Satisfied Customers:

    5514
    Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.
  • http://ww2.justanswer.com/uploads/lifesaver333/2010-10-17_191349_ls.jpeg lifesaver's Avatar

    lifesaver

    Computer Software Engineer

    Satisfied Customers:

    936
    Several years of intensive programming and application development experience in various platforms.
  • http://ww2.justanswer.com/uploads/EH/ehabtutor/2012-8-2_202016_1.64x64.jpg ehabtutor's Avatar

    ehabtutor

    Computer Software Engineer

    Satisfied Customers:

    766
    Bachelor of computer science, 5+ years experience in software development, software company owner
  • http://ww2.justanswer.com/uploads/eljonis/2010-01-06_130406_eljon2.jpg Eljon's Avatar

    Eljon

    Consultant

    Satisfied Customers:

    558
    Founder of StockCanvas.com
  • http://ww2.justanswer.com/uploads/FA/fael/2011-1-15_104041_profile500x500.64x64.jpg Rafael Martins's Avatar

    Rafael Martins

    Computer Software Engineer

    Satisfied Customers:

    446
    Desktop, Mobile and Web Developer. 7+ years of experience. Creative solutions provider.
  • http://ww2.justanswer.com/uploads/CH/chutz747/2011-11-6_14231_me.64x64.jpg The-PC-Guy's Avatar

    The-PC-Guy

    Software Engineer

    Satisfied Customers:

    320
    Extensive Knowledge in PHP, MYSQL, CSS & Javascript