In: Computer Science
This homework will check your ability to declare and use classes and object, and how to use both Maps and sets in Java. The assignment will also serve as an introduction to Junit Testing.
Here is the general layout. Some more detailed instructions are also included below. Pay attention to the details and the naming conventions, since we will be running your code through testing code the grading team will develop. That code will require your method signatures to be consistent with these instructions.
Please call your project LegislatorFinder
inside your project, please have a package called LegislatorFinder
create your java files inside the LegislatorFinder package.
We will be simulating the system by which people can find who their legislators are:
There is a class called Legislator, with the following attributes: ▪ lastName ▪ firstName ▪ politicalParty ▪ Each of those attributes has getter and setter functions therefore, this class has the following methods: getLastName(), getFirstName, getPoliticalParty(), setFirstName(string), setLatName(string), setPoliticalParty(string) ▪ in addition, the class has two constructors: one with no arguments. One that takes arguments string, string, string
There is a class called LegislatorFinder ▪ this class has only one attribute, a TreeMap ▪ for methods, you should implement addLegislator(string state, Legislator l) ◦ if the state already exists in the TreeMap, it adds this legislator to the set the state maps to. ◦ If the state doe not exist in the TreeMap, it adds a new key:value pair to the map, with this legislator in the corresponding set. getLegislators(string state) ◦ returns the set of legislators from the given state. If there is no such state key in the map, returns an empty set. substituteLegislators(String state, Set newLegislators) ◦ substitutes the legislators for the indicated state with the one in newLegislators. If there was no such state key in the Map, it creates a new key:value pair. If there was a key:value pair for this state already, it substitutes the previous set of legislators with the one passed as an input parameter.
◦ Through JUnit tests, test the following operations: ▪ adding legislators, both for a state key that is already in the map, and for a state key not yet in the map. ▪ Retrieving legislators, both for a state key that is already in the map, and for a state key not yet in the map. ▪ Substituting the set of legislators for a given state for the following cases: the state was not previously being used as a key in the map. The key was being used as a key in the map.
(Need to implement a TreeMap method)
Hi,
Please find the below code according to the given requirements.
***************************************************************************
Legislator.java
***************************************************************************
package LegislatorFinder;
public class Legislator {
private String firstName;
private String lastName;
private String politicalParty;
/**
*
*/
public Legislator() {
firstName="";
lastName="";
politicalParty="";
}
/**
* @param firstName
* @param lastName
* @param politicalParty
*/
public Legislator(String firstName, String lastName,
String politicalParty) {
this.firstName = firstName;
this.lastName = lastName;
this.politicalParty =
politicalParty;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the politicalParty
*/
public String getPoliticalParty() {
return politicalParty;
}
/**
* @param politicalParty the politicalParty to
set
*/
public void setPoliticalParty(String politicalParty)
{
this.politicalParty =
politicalParty;
}
}
***************************************************************************
LegislatorFinder.java
***************************************************************************
package LegislatorFinder;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeMap;
public class LegislatorFinder {
TreeMap<String,Set<Legislator>>
legislators = new TreeMap<>();
//creating a dummy database for testing
public LegislatorFinder() {
Legislator l1 = new
Legislator("ajay", "kumar", "INC");
Legislator l2 = new
Legislator("Narendra", "Modi", "BJP");
Legislator l3 = new
Legislator("Rahul", "Gandhi", "INC");
Legislator l4 = new
Legislator("Sushma", "Swaraj", "BJP");
Legislator l5 = new
Legislator("Amit", "Shah", "BJP");
Legislator l6 = new
Legislator("Uttam", "Kumar", "INC");
Set<Legislator> set1 = new
HashSet<>();
set1.add(l1);
set1.add(l2);
set1.add(l5);
legislators.put("Delhi",
set1);
Set<Legislator> set2 = new
HashSet<>();
set1.add(l3);
set1.add(l4);
set1.add(l6);
legislators.put("Gujarat",
set2);
}
public void addLegislator(String state,Legislator l)
{
Set<String> keys =
legislators.keySet();
if(keys.contains(state)) {
legislators.get(state).add(l);
}
else {
Set<Legislator> lgSet = new HashSet<>();
lgSet.add(l);
legislators.put(state, lgSet);
}
}
public Set<Legislator> getLegislator(String
state){
Set<String> keys =
legislators.keySet();
for(String s : keys) {
if(state.equals(s)) {
return legislators.get(s);
}
}
return null;
}
public void substituteLegislators(String state,
Set<Legislator> newLegislators) {
Set<String> keys =
legislators.keySet();
if(keys.contains(state)) {
legislators.put(state, newLegislators);
}else {
legislators.put(state, newLegislators);
}
}
}
***************************************************************************
LegislatorTest.java
***************************************************************************
package LegislatorFinder;
import java.util.Set;
import java.util.TreeMap;
import org.junit.Test;
import static org.junit.Assert.*;
public class LegislatorTest {
@Test
public void testAddIfKeyExists() {
LegislatorFinder finder = new
LegislatorFinder();
TreeMap<String,Set<Legislator>> legislators =
finder.legislators;
Set<String> keys =
legislators.keySet();
String expected = "Delhi";
assertTrue(keys.contains(expected));
}
@Test
public void testAddIfKeyNotExists() {
LegislatorFinder finder = new
LegislatorFinder();
TreeMap<String,Set<Legislator>> legislators =
finder.legislators;
Set<String> keys =
legislators.keySet();
String expected =
"Telangana";
assertFalse(keys.contains(expected));
}
@Test
public void testGetExistingKey() {
LegislatorFinder finder = new
LegislatorFinder();
String expected = "Delhi";
assertNotNull((finder.getLegislator(expected)));
}
@Test
public void testGetNonExistingKey() {
LegislatorFinder finder = new
LegislatorFinder();
String expected = "MP";
assertNull((finder.getLegislator(expected)));
}
@Test
public void testSubstituteForUsedKey() {
LegislatorFinder finder = new
LegislatorFinder();
TreeMap<String,Set<Legislator>> legislators =
finder.legislators;
Set<String> keys =
legislators.keySet();
String expected = "Delhi";
assertTrue(keys.contains(expected));
}
@Test
public void testSubstituteForKeyNotUsed() {
LegislatorFinder finder = new
LegislatorFinder();
TreeMap<String,Set<Legislator>> legislators =
finder.legislators;
Set<String> keys =
legislators.keySet();
String expected =
"Telangana";
assertFalse(keys.contains(expected));
}
}
Output:
Hope this helps!!
Have a good day