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
39118
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 '17, 00:15

Dmitry%20Sky's gravatar image

Dmitry Sky
113
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 '17, 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 '17, 15:28

Igor's gravatar image

Igor
112
accept rate: 0%

HELP. The following code doesnot pass the test

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

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

    String grandpaName = reader.readLine();
    Cat catGrandpa = new Cat(grandpaName);

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

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

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

    String sonName = reader.readLine();
    Cat catSon = new Cat(sonName, 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 mom;
    private Cat dad;

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

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

    @Override
    public String toString()
    {
        String parent="";
        if (mom == null)
            parent += ", no mother";
        else
            parent +=", mother is " + mom.name;

        if (dad == null)
            parent +=", no father";
        else
            parent +=", father is " + dad.name;

        return "Cat name is " + name + parent;
    }
}
}
link

answered 20 Nov '17, 13:11

arsalan's gravatar image

arsalan
11
accept rate: 0%

edited 20 Nov '17, 13:15

Hello everyone.

I don't get it: why doesn't it pass testing???

I get exactly same output as example...

package com.javarush.test.level06.lesson11.bonus02;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/* Add new functionality to the program.
Old task: Every cat has a name and a mom cat. Write a class to describe this situation. Create two objects: a daughter cat and a mom cat. Display them to the screen.
    New task: Every cat has a name, a dad cat and a mom cat. Write a class to describe this situation. Create 6 objects: mom, dad, son, daughter, grandma (mom’s mom) and grandpa (dad’s dad). Display them to the screen as follows: grandpa, grandma, dad, mom, son and daughter.

    Example input:
    grandpa Vinny
    grandma Maisy
    dad Kaos
    mom Velvet
    son Mario
    daughter Fluffy

    Example output:
    Cat name is grandpa Vinny, no mother, no father
    Cat name is grandma Maisy, no mother, no father
    Cat name is dad Kaos, no mother, father is grandpa Vinny
    Cat name is mom Velvet, mother is grandma Maisy, no father
    Cat name is son Mario, mother is mom Velvet, father is dad Kaos
    Cat name is daughter Fluffy, mother is mom Velvet, father is dad Kaos
*/

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

        String grandpaName = reader.readLine();
        Cat grandpa = new Cat(grandpaName, null, null);

        String grandmaName = reader.readLine();
        Cat grandma = new Cat(grandmaName, null, null);

        String dadName = reader.readLine();
        Cat dad = new Cat(dadName, grandpa, null);

        String momName = reader.readLine();
        Cat mom = new Cat(momName, null, grandma);

        String sonName = reader.readLine();
        Cat son = new Cat(sonName, dad, mom);

        String daughterName = reader.readLine();
        Cat daughter = new Cat(daughterName, dad, mom);

        System.out.println(grandpa);
        System.out.println(grandma);
        System.out.println(dad);
        System.out.println(mom);
        System.out.println(son);
        System.out.println(daughter);

    }

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

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

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

        }

    }

}
link

answered 26 Jun, 17:55

yslforeva's gravatar image

yslforeva
111
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:

×9
×8
×5
×4
×2

Asked: 05 Jun '15, 11:55

Seen: 15,021 times

Last updated: 26 Jun, 17:55