package com.codegym.task.task06.task0621;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Cat relations
*/
public class Solution {
private static int i= 0;
private static int j = 0;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String grandFatherName = reader.readLine();
Cat catGrandFather = new Cat(grandFatherName);
String grandmotherName = reader.readLine();
Cat catGrandmother = new Cat(grandmotherName);
String fatherName = reader.readLine();
Cat catFather = new Cat(fatherName,catGrandFather);
String motherName = reader.readLine();
Cat catMother = new Cat(motherName, catGrandmother);
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(catGrandFather);
System.out.println(catGrandmother);
System.out.println(catFather);
System.out.println(catMother);
System.out.println(catSon);
System.out.println(catDaughter);
//System.out.println(catDaughter);
}
public static class Cat {
private String name;
private Cat parent,father,mother;
Cat(String name) {
this.name = name;
}
Cat(String name, Cat parent) {
this.name = name;
this.parent = parent;
}
Cat(String name, Cat father, Cat mother) {
this.name = name;
this.father = father;
this.mother = mother;
}
@Override
public String toString() {
if(parent == null && father == null && mother == null)
return "The cat's name is "+name+", no mother, no father";
else if(father == null && mother == null && parent != null)
{
if( i == 0)
{
i++;
return "The cat's name is "+name+", no mother, "+parent.name+" is the father";
}
else
return "The cat's name is "+name+", "+parent.name+" is the mother, no father";
}
else if(parent == null && father != null && mother != null)
{
if( j == 0)
{
j++;
return "The cat's name is "+name+", "+mother.name+
" is the mother, "+father.name+" is the father";
}
else
return "The cat's name is "+name+", "+mother.name+
" is the mother, "+father.name+" is the father";
}
else
return "";
}
}
}
Can anyone help me to find the mistake. Please help me, this is failing at
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
27 May 2019, 19:14
Next time you ask a question don't copy and paste code. Just use the slider:
.
![]()

0