Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The output for this mapping problem is not what it should be. Here is the intend

ID: 3599553 • Letter: T

Question

The output for this mapping problem is not what it should be. Here is the intended output:

* INPUT OUTPUT
*
* p sa 8
* p li 7
* p he 9
* p tu 5
* f li 7 tu 5
* f li 7 he 9
* f tu 5 sa 8
* l tu 5 li 7 sa 8
* l sa 8 tu 5
* u li 7 tu 5
* l tu 5 sa 8
* q li 7 he 9 yes
* q he 9 li 7 yes
* q he 9 li 23 no
* q he 9 he 9 no

My output is:

P sa 8
L sa 8
sa 8
P li 7
P he 9
P tu 5
F li 7 tu 5
F li 7 he 9
F tu 5 sa 8
L tu 5 (Output: li 7 sa 8 tu 5) XXX
L sa 8 (Output: sa 8 tu 5) XXX
U li 7 tu 5
L tu 5 (No Output) XXX

Q li 7 he 9
No XXX
Q he 9 li 7
No XXX
Q he 9 li 23
No XXX
Q he 9 he 9
Yes XXX

Every wrong line of output is marked with XXX to highlight the issues. Here are the instructions followed by my code. Debug is just a tester to get the output you see:

* Complete the methods of MyFB so that it works.

* Each method must work in the worst case time given below (assuming uniform hashing and ignoring string lengths):

*

* addPerson must take constant time

*

* addFriendship must take constant time

*

* queryFriendship must take constant time

*

* removeFriendship should take constant time

*

* lookupFriends should take time proportional to the number of

* friends that the named person has (or less)

*

* Your solution should use a single field of type

*

* HashMap<Person,HashSet<Person>>

*

* You will need to add hashCode and equals methods to the Person class.

* Don't change the first line of any method given below.

* The names and types of the methods should not change.

* You can add more classes too. Just put them all in this file, inside MyFB.

* Any classes you add should be "static" and not "public".

* You can import/use any file in the java APIs.

* You do not need to implement a hash table for this assignment.

* You can use java.util.HashMap, java.util.HashSet, or others.

*

* You do not need to implement a iterable object.

* Also note that all of the Map implementations we have looked at create an iterable

* object via the keys() method.

*

*

* Here is an example session:

*

* INPUT OUTPUT

*

* p sa 8

* p li 7

* p he 9

* p tu 5

* f li 7 tu 5

* f li 7 he 9

* f tu 5 sa 8

* l tu 5 li 7 sa 8

* l sa 8 tu 5

* u li 7 tu 5

* l tu 5 sa 8

* q li 7 he 9 yes

* q he 9 li 7 yes

* q he 9 li 23 no

* q he 9 he 9 no

*

* Note that friendship is symmetric and irreflexive.

* So if a/b are friends, then so are b/a.

* And no one is their own friend.

*

* Put the following in a file "input.txt" to run this test:

p sa 8

p li 7

p he 9

p tu 5

f li 7 tu 5

f li 7 he 9

f tu 5 sa 8

l tu 5

l sa 8

u li 7 tu 5

l tu 5

q li 7 he 9

q he 9 li 7

q he 9 li 23

q he 9 he 9

*/

MY CODE:

public class MyFB {

public static boolean DEBUG = true;

public static Person makePerson(In in) {

try {

String name = in.readString();

int age = in.readInt();

return makePerson(name, age);

} catch (java.util.InputMismatchException e) {

StdOut.println("Input format error");

return null;

}

}

Person k;

HashMap<Person, HashSet<Person>> map;

public MyFB() {

map = new HashMap<Person, HashSet<Person>>();

}

public MyFB(HashMap<Person, HashSet<Person>> map) {

this.map = map;

}

public HashMap<Person, HashSet<Person>> getMap() {

return map;

}

public static Person makePerson(String name, int age) {

return new Person(name, age);

}

static class Person {

String name;

int age;

public Person(String name, int age) {

this.name = name;

this.age = age;

}

public String toString() {

return name + " " + age;

}

public int hashCode()

{

int ha = 31;

ha = 31 * ha + ((Integer) age).hashCode();

ha = 31 * ha + (null == name ? 0 : name.hashCode());

return ha;

}

public boolean equals(Object o) {

if (this == o)

return true;

if ((o == null) || (o.getClass() !=

this.getClass()))

return false;

Person t = (Person) o;

return age == t.age && (name == t.name || (name != null && name.equals(t.name)));

}

}

// a person "exists" after they are added using addPerson

// addPerson does nothing if p already exists

public void addPerson(Person p) {

if (DEBUG) {

StdOut.format("P %s ", p);

}

if (map.containsValue(p)) {

return;

}

if (!map.containsValue(p)) {

HashSet<Person> set1 = new HashSet<Person>();

set1.add(p);

map.put(p, set1);

}

}

// addFriendship does nothing if p1 and p2 are already friends or if one

// does not exist

public void addFriendship(Person p1, Person p2) {

if (DEBUG) {

StdOut.format("F %s %s ", p1, p2);

}

if (p1.equals(p2)) {

return;

}

if (!map.containsKey(p1) || !map.containsKey(p2)) {

return;

}

if (map.containsKey(p1) && map.containsKey(p2)) {

HashSet<Person> s1 = map.get(p1);

HashSet<Person> s2 = map.get(p2);

s1.add(p2);

s2.add(p1);

map.put(p1, s1);

map.put(p2, s2);

}

}

// removeFriendship does nothing if p1 and p2 are not friends or if one does

// not exist

public void removeFriendship(Person p1, Person p2) {

if (DEBUG) {

StdOut.format("U %s %s ", p1, p2);

}

if (p1.equals(p2)) {

return;

}

if (!map.containsKey(p1) || !map.containsKey(p2)) {

return;

}

if (map.containsKey(p1) && map.containsKey(p2)) {

HashSet<Person> s1 = map.get(p1);

HashSet<Person> s2 = map.get(p2);

s1.remove(p2);

s2.remove(p1);

map.remove(p2, s2);

map.remove(p1, s1);

map.remove(p1);

}

}

// queryFriendship returns false if p1 and p2 are not friends or if one does

// not exist

public boolean queryFriendship(Person p1, Person p2) {

if (DEBUG) {

StdOut.format("Q %s %s ", p1, p2);

}

if (p1 == p2) {

return false;

}

if (!map.containsKey(p1) || !map.containsKey(p2)) {

return false;

} else {

return true;

}

}

// lookupFriends returns null or empty iterable if p does not exists

public Iterable<Person> lookupFriends(Person p) {

if (DEBUG) {

StdOut.format("L %s ", p);

}

if (!map.containsKey(p))

return null;

else {

// System.out.println(p);

return map.get(p);

}

}

Explanation / Answer

I have fixed the code . The main program which processes the input.txt is not given in the question. But I am sure, the answer will work to give you the correct output. I will be glad to help you in case of any issues. Just post a comment and I shall help you. In case the program did not work as intended, please give me the code for processing the input file as well. If the code worked well for you, request you to rate it. Thank you.

To indent the code in eclipse, please select code using Ctrl+A then press Ctrl+i


import java.util.HashMap;
import java.util.HashSet;
public class MyFB {
public static boolean DEBUG = true;
public static Person makePerson(In in) {
try {
String name = in.readString();
int age = in.readInt();
return makePerson(name, age);
} catch (java.util.InputMismatchException e) {
StdOut.println("Input format error");
return null;
}
}
Person k;
HashMap<Person, HashSet<Person>> map;
public MyFB() {
map = new HashMap<Person, HashSet<Person>>();
}
public MyFB(HashMap<Person, HashSet<Person>> map) {
this.map = map;
}
public HashMap<Person, HashSet<Person>> getMap() {
return map;
}
public static Person makePerson(String name, int age) {
return new Person(name, age);
}
static class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return name + " " + age;
}
public int hashCode()
{
int ha = 31;
ha = 31 * ha + ((Integer) age).hashCode();
ha = 31 * ha + (null == name ? 0 : name.hashCode());
return ha;
}
public boolean equals(Object o) {
if (this == o)
return true;
if ((o == null) || (o.getClass() !=
this.getClass()))
return false;
Person t = (Person) o;
return age == t.age && (name == t.name || (name != null && name.equals(t.name)));
}
}
// a person "exists" after they are added using addPerson
// addPerson does nothing if p already exists
public void addPerson(Person p) {
if (DEBUG) {
StdOut.format("P %s ", p);
}
if (map.containsKey(p)) {
return;
}

map.put(p, new HashSet<Person>());
}
// addFriendship does nothing if p1 and p2 are already friends or if one
// does not exist
public void addFriendship(Person p1, Person p2) {
if (DEBUG) {
StdOut.format("F %s %s ", p1, p2);
}
if (p1.equals(p2)) {
return;
}
if (!map.containsKey(p1) || !map.containsKey(p2)) {
return;
}

HashSet<Person> s1 = map.get(p1);
HashSet<Person> s2 = map.get(p2);
if(!s1.contains(p2))
{
s1.add(p2);
s2.add(p1);
}
}
// removeFriendship does nothing if p1 and p2 are not friends or if one does
// not exist
public void removeFriendship(Person p1, Person p2) {
if (DEBUG) {
StdOut.format("U %s %s ", p1, p2);
}
if (p1.equals(p2)) {
return;
}
if (!map.containsKey(p1) || !map.containsKey(p2)) {
return;
}
HashSet<Person> s1 = map.get(p1);
HashSet<Person> s2 = map.get(p2);
if(s1.contains(p2))
{
s1.remove(p2);
s2.remove(p1);
}
}
// queryFriendship returns false if p1 and p2 are not friends or if one does
// not exist
public boolean queryFriendship(Person p1, Person p2) {
if (DEBUG) {
StdOut.format("Q %s %s ", p1, p2);
}
if (p1 == p2) {
return false;
}
if (!map.containsKey(p1) || !map.containsKey(p2)) {
return false;
} else {
HashSet<Person> s1 = map.get(p1);
return s1.contains(p2);
}
}
// lookupFriends returns null or empty iterable if p does not exists
public Iterable<Person> lookupFriends(Person p) {
if (DEBUG) {
StdOut.format("L %s ", p);
}
if (!map.containsKey(p))
return null;
else {
// System.out.println(p);
return map.get(p);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote