在调试时遇到问题 - 空集

时间:2014-02-21 06:54:46

标签: java set

我最近在一次采访中被问到这个问题,我得到了一个班级和一个测试员课程,我必须跑去检查测试用例是否满意。我不得不编写addParent(),getParent(),addChild(),getChild(),setSpouse(),getSpouse,hasSpouse()和isRelated()方法。我被给了1小时,这是我能想到的。我一直试图调试3天,但我只是在添加父母和孩子后得到空集。

以下是我的代码:

    package kashyap;
    import java.util.Set;
    import java.util.HashSet;

    public class MyPerson implements Person {
//declarations
    private String name;
    private long ssn;
    private char gender;
    //private Person parent;
    private Set<Person> parents = new HashSet<Person>();
    private Set<Person> children = new HashSet<Person>();
    private Person spouse = null;
//methods   
    public String getName() {
        return name;
    }


    public void setName(String name) {
        this.name = name;
    }


    public long getSSN() {
        return ssn;
    }


    public void setSSN(long ssn) {
        this.ssn = ssn;
    }


    public char getGender() {
        return gender;
    }


    public void setGender(char gender) {
        this.gender = gender;
    }

    public MyPerson(String name, long ssn, char gender) {

        this.name = name;
        this.ssn = ssn;
        this.gender = gender;

    }


    public void addParent(Person parent) {
        // TODO Auto-generated method stub
        this.parents.add(parent);
    }

    public Set<Person> getParents() {
        // TODO Auto-generated method stub
        if(parents.isEmpty())
        {
            return null;
        }
        else
        {
            return parents;
        }
    }

    public void addChild(Person child) {
        // TODO Auto-generated method stub
        this.children.add(child);
    }

    public Set<Person> getChildren() {
        // TODO Auto-generated method stub
        if(children.isEmpty())
        {
            return null;
        }
        else
        {
            return children;
        }
    }

    public void setSpouse(Person spouse) {
        // TODO Auto-generated method stub
        this.spouse = spouse;
    }

    public Person getSpouse() {
        // TODO Auto-generated method stub
        if(spouse != null)
        {
            return spouse;
        }
        else
        {
            return null;
        }
    }

    public boolean hasSpouse() {
        // TODO Auto-generated method stub
        if(spouse != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean isRelated(Person person) {
        // TODO Auto-generated method stub

        boolean related = false;

        if (parents.contains(person) || children.contains(person) || person == spouse)
        {
            return true;
        }

        Set<Person> set_p =  person.getParents();
        Set<Person> set_c =  person.getChildren();
        Person s = person.getSpouse();

        Set<Person> relatives = new HashSet<Person>();

        if(set_p != null){
            for(Person p : set_p)
            {
                relatives.add(p);
            }
        } 
        if(set_c != null){
            for(Person c : set_c)
            {
                relatives.add(c);       
            }
        } 

            relatives.add(s);


        for( Person x : relatives){
            related = isRelated(x);
            if(related == true)
                return true;
        }
        return false;
    }
}

测试人员类:

package kashyap;

public class PersonTester {

    public static void main(String[] args) {

        PersonTester tester = new PersonTester();
        tester.testGrandChildToGreatGrandMotherRelationship(1);
        tester.testManToStrangerRelationship(3);
        tester.testGruncleRelationship(4);
        tester.testManToWifesCousinRelationship(5);
    }


    public void testManToWifesCousinRelationship(int testId) {
        // man
        Person jack = new MyPerson("Jack", 1, Person.MALE);     

        // wife
        Person  jill = new MyPerson("Jill", 2, Person.FEMALE);
        jack.setSpouse(jill);

        // wife's mother
        Person beth = new MyPerson("Beth", 3, Person.FEMALE);
        jill.addParent(beth);

        // wife's grandmother
        Person mary = new MyPerson("Mary", 4, Person.FEMALE);
        beth.addParent(mary);

        // wife's uncle
        Person dave = new MyPerson("Dave", 5, Person.MALE);
        mary.addChild(dave);

        //wife's aunt
        Person sally = new MyPerson("Sally", 6, Person.FEMALE);
        dave.setSpouse(sally);

        // wife's cousin
        Person andrew = new MyPerson("Andrew", 7, Person.MALE);
        sally.addChild(andrew);

        // wife's cousin's wife
        Person janet = new MyPerson("Janet", 8, Person.FEMALE);
        andrew.setSpouse(janet);

        // same person as janet...only related through SSN
        Person bigJ = new MyPerson("Janet", 8, Person.FEMALE);



        this.performTestAndPrintResults(testId, jack, janet, "Wife's Cousin", true);
        // test the reverse relationship
        this.performTestAndPrintResults(testId + 1, janet, jack, "Cousin's Husband", true);
        // test relationship through SSN
        this.performTestAndPrintResults(testId + 2, jack, bigJ, "With Janet being called by her nick name", true);

    }

    private void performTestAndPrintResults(int testCaseNumber, Person p1, Person p2, String relationship, boolean related) {

        String relatedOrNot = "not related";
        if(related) {
            relatedOrNot = "related";
        }

        System.out.print("TEST CASE #" + testCaseNumber);
        if(p1.isRelated(p2) == related) {
            System.out.print("(PASS): " + p1.getName() +  " and " + p2.getName() + "(" + relationship + ") are " + relatedOrNot + ". Your code agrees!\n");
        }
        else {
            System.out.print("(FAIL): " + p1.getName() + " and " + p2.getName() + "(" + relationship + ") are " + relatedOrNot + ". Your code disagrees!\n");
        }

    }


    public void testManToStrangerRelationship(int testId) {

        Person jack = new MyPerson("Jack" , 1, Person.MALE);
        Person stranger = new MyPerson("Perry" , 2, Person.MALE);

        this.performTestAndPrintResults(testId, jack, stranger, "Stranger", false);

    }


    public void testGrandChildToGreatGrandMotherRelationship(int testId) {

        Person man = new MyPerson("Jack", 1, Person.MALE);

        //mother
        Person  mother = new MyPerson("Jill", 2, Person.FEMALE);
        man.addParent(mother);

        // grandMother
        Person gm = new MyPerson("Beth", 3, Person.FEMALE);
        mother.addParent(gm);

        // great-grandMother
        Person ggm = new MyPerson("Mary", 4, Person.FEMALE);
        gm.addParent(ggm);

        //child
        Person  child = new MyPerson("Peter", 5, Person.MALE);
        man.addChild(child);

        //grandChild
        Person  grandChild = new MyPerson("Dave", 6, Person.MALE);
        child.addChild(grandChild);

        // is my grandChild related to my great-grandMother
        this.performTestAndPrintResults(testId, grandChild, ggm, "Great-Great-Great-GrandMa", true);
        this.performTestAndPrintResults(testId + 1, ggm, grandChild, "Great-Great-Great-GrandChild", true);
    }


    public void testGruncleRelationship(int testId) {

        // A gruncle is someone who is both a Grandfather & an Uncle to another person

        // child
        Person child = new MyPerson("Billy", 0, Person.MALE);

        Person father = new MyPerson("Jack", 1, Person.MALE);       
        Person  mother = new MyPerson("Jill", 2, Person.FEMALE);
        father.setSpouse(mother);
        father.addChild(child);

        // father's father (grand father)
        Person grandPa = new MyPerson("Dave", 3, Person.MALE);
        father.addParent(grandPa);

        // mother's sister (aunt)
        Person gm = new MyPerson("Beth", 4, Person.FEMALE);
        mother.addParent(gm);           
        Person aunt = new MyPerson("Shelly", 5, Person.FEMALE);
        gm.addChild(aunt);

        // father's father falls for mother's sister 
        //(code to test looping due to double relationships)
        grandPa.setSpouse(aunt);

        Person gruncle = grandPa;
        // am i related to wife's uncle's wife?
        this.performTestAndPrintResults(testId, child, gruncle, "Gruncle", true);   


    }

}

还有另一个接口实现了所有这些:

package kashyap;

import java.util.Set;

public interface Person {

        static final char MALE = 'M';
        static final char FEMALE = 'F';

        // name
        String getName();
        void setName(String name);

        //ssn
        long getSSN();
        void setSSN(long ssn);

        //gender
        char getGender();
        void setGender(char gender);

        // relationships
        void addParent(Person parent);
        Set<Person> getParents();

        void addChild(Person child);
        Set<Person> getChildren();

        void setSpouse(Person spouse);
        Person getSpouse();

        // returns true if the person is married
        boolean hasSpouse();        
        // returns true if the person is related, false otherwise
        boolean isRelated(Person person);
}

有人可以告诉我为什么所有套装都是空的吗?我只需要填充addParent(),getParent(),addChild(),getChild(),setSpouse(),getSpouse,hasSpouse()和isRelated()方法,所以我假设其余代码都没问题。< / p>

编辑:我被要求在这里使用套装,而不是选择。

1 个答案:

答案 0 :(得分:1)

按照我的说法,Set不是空的。我可以发现您的测试用例失败的一个原因,因为您没有在equals()

中覆盖hashcode()HashSet(对于MyPerson

例如,在方法isRelated(Person person)中,您正在检查parents.contains(person),其中包含所有元素的person 平等 parents