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:
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())
------------------------------------------
I got this today and it's due tomorrow. I'm lost in this class and don't understand how to do this :(
ANSWER:-
CODE:-
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
filterMapByValue(Map<String,String> inMap, String
targetValue){
for (Map.Entry<String,String> entry :
inMap.entrySet()){
if(targetValue.equals(entry.getValue())){
System.out.println(entry.getKey()+" - "+entry.getValue());
}
}
}
public static void
printValues(Map<String,String> map){
for(String value : map.values()){
System.out.println(value);
}
}
public static void
printKeys(Map<String,String> map){
for(String key : map.keySet()){
System.out.println(key);
}
}
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");
System.out.println("Presidents of Democratic-Republican party:
");
filterMapByValue(PresidentsOfTheUnitedStates,"Democratic-Republican");
System.out.println();
System.out.println("Keys: ");
printKeys(PresidentsOfTheUnitedStates);
System.out.println();
System.out.println("Values: ");
printValues(PresidentsOfTheUnitedStates);
}
}
NOTE:- If you need any modifications in the code,please give positive rating.Please give positive rating.THUMBS UP.
THANK YOU!!!
OUTPUT:-

Here is my assignment: --------------------------------------------------- Consider the following client class: import java.util.Collection; import java.util.Collections; import java.util.HashMap;...
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...
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...
Please Modify TestPart2 to test the correctness and
efficiency of FasterDefaultList. Thanks
import java.util.List;
import java.util.AbstractList;
import java.util.Map;
import java.util.HashMap;
public class DumbDefaultList<T> extends AbstractList<T> {
Map<Integer,T> map;
public DumbDefaultList() {
map = new HashMap<Integer,T>();
}
public int size() {
return Integer.MAX_VALUE;
}
public T get(int i) {
return map.get(i);
}
public T set(int i, T x) {
return map.put(i, x);
}
public void add(int i, T x) {
Map<Integer, T> map2 = new HashMap<Integer,T>();
for (Integer k : map.keySet())...
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; }...
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()...
I need help with my homework. The task: Actually, you find flying very good, but you do not trust the whole new-fangled flying stuff and the infrastructure it has built up. As a diehard medieval metal fan you prefer to travel from A to B but rather the good old catapult. Since one can not easily take the favorite cat on vacation with it (cats do not get drafts, which is why ICEs are also eliminated), they let themselves be...
1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...
Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...
import java.util.ArrayList;
import java.util.Scanner;
public class AssertDemo {
/* Work on this in a piecewise fashion by
uncommenting and focusing on one section at a time
* in isolation rather than running everything at
once.
*/
public static void main(String[] args) {
assert(true);
assert(false);
warmUpAsserts();
assertWithPrimitives();
assertWithObjects();
homeworkRelatedAsserts();
}
/*
* Just a...
I created a program to display information about the presidents
in Java. When I click the start button, it starts to move, but when
I stop and click the start button again, it advance two pages at a
time. It has to change one page at a time. Please correct this
error.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
// Defines a class Map2 derived from JFrame
public class...