Question

Consider the following client class: import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; public...

Consider the following client class:

import java.util.Collection;

import java.util.Collections;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

public class PresidentsMain {

public static void main(String[] args) {

Map<String, String> PresidentsOfTheUnitedStates = new HashMap<String, String>();

PresidentsOfTheUnitedStates.put("George Washington", "Unaffiliated");

PresidentsOfTheUnitedStates.put("John Adams", "Federalist");

PresidentsOfTheUnitedStates.put("Thomas Jefferson", "Democratic-Republican");

PresidentsOfTheUnitedStates.put("James Madison", "Democratic-Republican");

PresidentsOfTheUnitedStates.put("James Monroe", "Democratic-Republican");

PresidentsOfTheUnitedStates.put("John Quincy Adams", "Democratic-Republican");

PresidentsOfTheUnitedStates.put("Andrew Jackson", "Democratic");

PresidentsOfTheUnitedStates.put("Martin Van Buren", "Democratic");

PresidentsOfTheUnitedStates.put("William Henry Harrison", "Whig");

PresidentsOfTheUnitedStates.put("John Tyler", "Whig");

     }

}

}

Extend given client class:

  • Implement a static method called FilterMapByValue, that takes 2 parameters:
    • Map<String, String> InMap;
    • String TargetValue;

Method should print out all map elements, for which Value == TargetValue. Test the implementation by filtering PresidentsOfTheUnitedStates map so that only presidents, affiliated with Democratic-Republican party are printed. Note: use the following to iterate over a map:

  for (Map.Entry<String,String> Entry : InMap.entrySet())   

  • Implement a method PrintValues, that prints all values for a given map (use map’s values() function). Test the implementation on PresidentsOfTheUnitedStates map.
  • Implement a method PrintKeys, that prints all keys for a given map (use map’s keySet() function). Test the implementation on PresidentsOfTheUnitedStates map.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Declare both these classes under the same package name and run the code from PresidentsMain class

package com.tavant.training; //PresidentsMain class

import java.util.Collection;

import java.util.Collections;

import java.util.HashMap;

import java.util.Map;

import java.util.Set;

public class PresidentsMain {

   public static void main(String[] args) { //RUN THE PROGRAM FROM HERE

       Map<String, String> PresidentsOfTheUnitedStates = new HashMap<String, String>();

       PresidentsOfTheUnitedStates.put("George Washington", "Unaffiliated");

       PresidentsOfTheUnitedStates.put("John Adams", "Federalist");

       PresidentsOfTheUnitedStates.put("Thomas Jefferson", "Democratic-Republican");

       PresidentsOfTheUnitedStates.put("James Madison", "Democratic-Republican");

       PresidentsOfTheUnitedStates.put("James Monroe", "Democratic-Republican");

       PresidentsOfTheUnitedStates.put("John Quincy Adams", "Democratic-Republican");

       PresidentsOfTheUnitedStates.put("Andrew Jackson", "Democratic");

       PresidentsOfTheUnitedStates.put("Martin Van Buren", "Democratic");

       PresidentsOfTheUnitedStates.put("William Henry Harrison", "Whig");

       PresidentsOfTheUnitedStates.put("John Tyler", "Whig");
      
       ExtendedClass object=new ExtendedClass(); // object of newly created extendedClass is made
      
       object.FilterMapByValue(PresidentsOfTheUnitedStates, "Democratic-Republican"); //FilterMapByValue method call
      
       object.PrintValues(PresidentsOfTheUnitedStates); //PrintValues method call
      
       object.PrintKeys(PresidentsOfTheUnitedStates); //PrintKeys method call    

       }

       }

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package com.tavant.training; //  ExtendedClass

import java.util.Map;

public class ExtendedClass {
  
   public void FilterMapByValue(Map<String, String> InMap,String TargetValue) // accepts map to InMap and the string
   {
       for (Map.Entry<String,String> Entry : InMap.entrySet())
       {
           if(Entry.getValue()==TargetValue) // check if value equals target value or not
           System.out.println("key:"+Entry.getKey()+"and value:"+Entry.getValue()); //if condition satisfies, key
       } //value pair is printed
   }
  
   public void PrintValues(Map<String, String> InMap)
   {
       System.out.println(InMap.values()); // values() function implemented
   }

   public void PrintKeys(Map<String, String> InMap)
   {  
       System.out.println(InMap.keySet()); // keySet() function implemented
   }

}

      

Add a comment
Know the answer?
Add Answer to:
Consider the following client class: import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; public...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Here is my assignment: --------------------------------------------------- Consider the following client class: import java.util.Collection; import java.util.Collections; import java.util.HashMap;...

    Here is my assignment: --------------------------------------------------- Consider the following client class: import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Set; public class PresidentsMain {       public static void main(String[] args) {             Map<String, String> PresidentsOfTheUnitedStates = new HashMap<String, String>();             PresidentsOfTheUnitedStates.put("George Washington", "Unaffiliated");             PresidentsOfTheUnitedStates.put("John Adams", "Federalist");             PresidentsOfTheUnitedStates.put("Thomas Jefferson", "Democratic-Republican");             PresidentsOfTheUnitedStates.put("James Madison", "Democratic-Republican");             PresidentsOfTheUnitedStates.put("James Monroe", "Democratic-Republican");             PresidentsOfTheUnitedStates.put("John Quincy Adams", "Democratic-Republican");             PresidentsOfTheUnitedStates.put("Andrew Jackson", "Democratic");             PresidentsOfTheUnitedStates.put("Martin Van Buren", "Democratic");             PresidentsOfTheUnitedStates.put("William Henry Harrison", "Whig");             PresidentsOfTheUnitedStates.put("John Tyler", "Whig");            }       } } Extend given client class: Implement a static...

  • What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class...

    What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test implements Comparable<Test> private String[] cast; public Test( String[] st) cast = st; public int compareTo( Test t) if ( cast.length >t.cast.length ) t return +1; else if cast.length < t.cast.length return -1; else return 0; public static void main( String[] args String[] a"Peter", "Paul", "Mary" String[] b_ { "Мое", ''Larry", "Curly", String [ ] c = { ·Mickey", "Donald" }; "Shemp" }; List<Test>...

  • Convert Code from Java to C++ Convert : import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class...

    Convert Code from Java to C++ Convert : import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Template { public static void main(String args[]) { Scanner sc1 = new Scanner(System.in); //Standard input data String[] line1 = sc1.nextLine().split(","); //getting input and spliting on basis of "," String[] line2 = sc1.nextLine().split(" "); //getting input and spiliting on basis of " "    Map<String, String> mapping = new HashMap<String, String>(); for(int i=0;i<line1.length;i++) { mapping.put(line1[i].split("=")[0], line1[i].split("=")[1]); //string in map where in [] as key and...

  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

  • HW60.1. Using Maps Implement a public final class named wordCounter. WordCounter should provide a single static...

    HW60.1. Using Maps Implement a public final class named wordCounter. WordCounter should provide a single static method countwords, which takes a string and returns a Map from strings to Integers. Each entry in the map should have a value (Integer) representing how many times that key (String) appeared in the String that was passed to countWords. For example: MapString, Integer» counts = Wordcounter.countilords ("cs 125 is awesome"); System.out.println(counts.get("CS")); // prints 1 System.out.println(counts.get( "125")); // prints 1 System.out.println(counts.get("225")); // prints null...

  • import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private...

    import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class FindWordInMaze { private char grid[][]; private ArrayList<String> words; private HashSet<String> foundWords = new HashSet<String>(); public FindWordInMaze(char[][] grid) { this.grid = grid; this.words = new ArrayList<>();    // add dictionary words words.add("START"); words.add("NOTE"); words.add("SAND"); words.add("STONED");    Collections.sort(words); } public void findWords() { for(int i=0; i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) { findWordsFromLocation(i, j); } }    for(String w: foundWords) { System.out.println(w); } } private boolean isValidIndex(int i, int j, boolean visited[][]) { return...

  • ANNOTATE BRIEFLY LINE BY LINE THE FOLLOWING CODE: package shop.data; import junit.framework.Assert; import junit.framework.TestCase; public class...

    ANNOTATE BRIEFLY LINE BY LINE THE FOLLOWING CODE: package shop.data; import junit.framework.Assert; import junit.framework.TestCase; public class DataTEST extends TestCase { public DataTEST(String name) { super(name); } public void testConstructorAndAttributes() { String title1 = "XX"; String director1 = "XY"; String title2 = " XX "; String director2 = " XY "; int year = 2002; Video v1 = Data.newVideo(title1, year, director1); Assert.assertSame(title1, v1.title()); Assert.assertEquals(year, v1.year()); Assert.assertSame(director1, v1.director()); Video v2 = Data.newVideo(title2, year, director2); Assert.assertEquals(title1, v2.title()); Assert.assertEquals(director1, v2.director()); } public void testConstructorExceptionYear()...

  • My client java a simple client program java .net import java.io*i public class MyClient (public static...

    My client java a simple client program java .net import java.io*i public class MyClient (public static void main(String args() thrown IO Exception (part I: initialize rocket and stream BufferedReader inFromUser - new BufferedReader(new inputStreamReader(System in)); Socket clientSocket - new Socket(___. ___); DataOutputStream oldToServer - new DataOutputStream(clientSocket.getOutputStrea, ())); part 2: interact with server

  • complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class...

    complete this in java import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class WordDetective { /** * Picks the first unguessed word to show. * Updates the guessed array indicating the selected word is shown. * * @param wordSet The set of words. * @param guessed Whether a word has been guessed. * @return The word to show or null if all have been guessed. */    public static String pickWordToShow(ArrayList<String> wordSet, boolean []guessed) { return null;...

  • import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements...

    import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements Iterable<Pair<K, V>> {    private static final long serialVersionUID = -6229569372944782075L;       public void add(K k, V v) {        if (!containsKey(k)) { put(k, new LinkedList<V>()); } get(k).add(v);    }       public V removeFirst(K k) {               if (!containsKey(k)) { return null;        } V value = get(k).removeFirst(); if (get(k).isEmpty()) { super.remove(k); } return value;    }...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT