The following code doesnot pass the test ...please help

public class Solution
{
    public static String grandpaName;
    public static String grandmaName;
    public static void main(String[] args) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        grandpaName=reader.readLine();
        Cat catGrandpa=new Cat(grandpaName);

     grandmaName=reader.readLine();
    Cat catGrandma= new Cat(grandmaName);

    String fatherName=reader.readLine();
    Cat catFather=new Cat(fatherName, catGrandpa);

    String motherName = reader.readLine();
    Cat catMother = new Cat(motherName, catGrandma);

    String sonName=reader.readLine();
    Cat catSon = new Cat(sonName,catMother,catFather);

    String daughterName = reader.readLine();
    Cat catDaughter = new Cat(daughterName, catMother, catFather);

    System.out.println(catGrandpa);
    System.out.println(catGrandma);
    System.out.println(catFather);
    System.out.println(catMother);
    System.out.println(catSon);
    System.out.println(catDaughter);
}

public static class Cat
{
    private String name;
    private Cat parent1;
    private Cat parent2;

    Cat(String name)
    {
        this.name = name;
    }

    Cat(String name, Cat parent)
    {
        this.name = name;
        this.parent1 = parent;
    }
    Cat (String name, Cat parent1, Cat parent2)
    {
        this.name=name;
        this.parent1=parent1;
        this.parent2=parent2;
    }

    @Override
    public String toString()
    {
        if (parent1 == null && parent2==null)
            return "Cat name is " + name + ", no mother"+", no father";
        else if (parent2==null)
            if(parent1.name.equals(grandpaName))
            return "Cat name is " + name + ", no mother,"+" father is " + parent1.name;
             if(parent1.name.equals(grandmaName))
                return "Cat name is " + name + ", mother is " + parent1.name + ", no father";
        else
            return "Cat name is " + name + ", mother is " +parent1.name+ ", father is "+parent2.name;
    }
}

}

asked 05 Jun '15, 11:55

Reem's gravatar image

Reem
39116
accept rate: 0%


I also need help to pass the test. in my opinion the program is fine....

public static String granpaName;

public static void main(String[] args) throws IOException
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    granpaName = reader.readLine();
    Cat catGrandpa = new Cat(granpaName);

    String granmaName = reader.readLine();
    Cat catGrandma = new Cat(granmaName);

    String dadName = reader.readLine();
    Cat catDad = new Cat(dadName, catGrandpa);

    String motherName = reader.readLine();
    Cat catMother = new Cat(motherName, catGrandma);

    String sonName = reader.readLine();
    Cat catSon = new Cat(sonName, catMother, catDad);

    String daughterName = reader.readLine();
    Cat catDaughter = new Cat(daughterName, catMother, catDad);

    System.out.println(catGrandpa);
    System.out.println(catGrandma);
    System.out.println(catDad);
    System.out.println(catMother);
    System.out.println(catSon);
    System.out.println(catDaughter);
}

public static class Cat
{
    private String name;
    private Cat mom;
    private Cat dad;

    public Cat(String name)
    {
        this.name = name;
    }

    public Cat(String name, Cat grandparent)
     {
        this.name = name;

        if(grandparent.name.equals(granpaName))
            this.dad = grandparent;
        else
            this.mom = grandparent;

     }

    public Cat(String name, Cat mom, Cat dad)
     {
        this.name = name;
        this.mom = mom;
        this.dad = dad;
    }

    @Override
    public String toString()
    {
        if (mom == null && dad == null)
            return "Cat name is " + name + ", no mother, no father";
        else if (mom == null)
            return "Cat name is " + name + ", no mother, father is " + dad.name;
        else if (dad == null)
            return "Cat name is " + name + ", mother is " + mom.name + ", no father";
        else
            return "Cat name is " + name + ", mother is " + mom.name + ", father is " + dad.name;
    }
}
link

answered 07 Mar, 00:15

Dmitry%20Sky's gravatar image

Dmitry Sky
112
accept rate: 0%

Apparently in constructor with mother & father you have to apply specific order:

Cat(String name, Cat father, Cat mother);

, instead of

Cat(String name, Cat mother, Cat father);

PS. If I helped you, please upvote - don't post another answer.

link

answered 13 Apr, 08:23

noelo's gravatar image

noelo
102
accept rate: 0%

in 1st code we don't know who is parent father or mother so it will be wrong to say that this.parent1 = parent;

Cat(String name, Cat parent)
{
    this.name = name;
    this.parent1 = parent;
}

in 2nd code looks like it won't work if grandpa and grandma will get same names.

Works, passed testing.

public class Solution
{
    public static void main(String[] args) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        String granpaName = reader.readLine();
        Cat granpa = new Cat(granpaName);

        String granmaName = reader.readLine();
        Cat granma = new Cat(granmaName);

        String fatherName = reader.readLine();
        Cat catFather = new Cat(fatherName,true, granpa);

        String motherName = reader.readLine();
        Cat catMother = new Cat(motherName,false, granma);

        String sonName = reader.readLine();
        Cat catSon = new Cat(sonName, catFather, catMother);

        String daughterName = reader.readLine();
        Cat catDaughter = new Cat(daughterName, catFather, catMother);

        System.out.println(granpa);
        System.out.println(granma);
        System.out.println(catFather);
        System.out.println(catMother);
        System.out.println(catSon);
        System.out.println(catDaughter);
    }

    public static class Cat
    {
        private String name;
        private Cat parentM;
        private Cat parentF;
        private boolean sex;

        Cat(String name)
        {
            this.name = name;
        }

        Cat(String name, boolean sex, Cat parent)
        {
            this.name = name;
            if(sex)
                this.parentF = parent;
            else
                this.parentM = parent;
            this.sex = sex;
        }

        Cat(String name, Cat father, Cat mother){
            this.name = name;
            this.parentF = father;
            this.parentM = mother;
        }

        @Override
        public String toString()
        {
            if (parentF == null && parentM == null)
                return "Cat name is " + name + ", no mother, no father ";

            if (parentM == null && parentF != null)
                return "Cat name is " + name + ",  no mother, father is " + parentF.name;

            if (parentF == null && parentM != null)
                return "Cat name is " + name + ", mother is " + parentM.name + ", no father ";

            if (parentF != null && parentM != null)
                return "Cat name is " + name + ", mother is " + parentM.name + ", father is " + parentF.name;

            return "";
        }
    }

}
link

answered 23 May, 15:28

Igor's gravatar image

Igor
112
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×8
×5
×3
×2
×2

Asked: 05 Jun '15, 11:55

Seen: 13,407 times

Last updated: 23 May, 15:28