Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. package com.javarush.test.level07.lesson12.home06;
  2.  
  3. /* Семья
  4. Создай класс Human с полями имя(String), пол(boolean),возраст(int), отец(Human), мать(Human). Создай объекты и заполни их так, чтобы получилось: Два дедушки, две бабушки, отец, мать, трое детей. Вывести объекты на экран.
  5. Примечание:
  6. Если написать свой метод String toString() в классе Human, то именно он будет использоваться при выводе объекта на экран.
  7. Пример вывода:
  8. Имя: Аня, пол: женский, возраст: 21, отец: Павел, мать: Катя
  9. Имя: Катя, пол: женский, возраст: 55
  10. Имя: Игорь, пол: мужской, возраст: 2, отец: Михаил, мать: Аня
  11. */
  12.  
  13. public class Solution
  14. {
  15. public static void main(String[] args)
  16. {
  17. //напишите тут ваш код
  18. Human ded1 = new Human("Petya", true, 55);
  19. Human ded2 = new Human("Kolya", true, 60);
  20. Human bab1 = new Human("Masha", false, 55);
  21. Human bab2 = new Human("Rita", false, 55);
  22. Human fat1 = new Human("Sasha", true, 30, ded1, bab1);
  23. Human mam1 = new Human("Olya", false, 35, ded2, bab2);
  24. Human kid1 = new Human("Anna", false, 10, fat1, mam1);
  25. Human kid2 = new Human("Nadja", false, 12, fat1, mam1);
  26. Human kid3 = new Human("Vasja", false, 13, fat1, mam1);
  27.  
  28. System.out.println(ded1);
  29. System.out.println(ded2);
  30. System.out.println(bab1);
  31. System.out.println(bab2);
  32. System.out.println(fat1);
  33. System.out.println(mam1);
  34. System.out.println(kid1);
  35. System.out.println(kid2);
  36. System.out.println(kid3);
  37. }
  38.  
  39. public static class Human
  40. {
  41. //напишите тут ваш код
  42. String name;
  43. Boolean sex;
  44. int age;
  45. String father;
  46. String mother;
  47.  
  48.  
  49. public Human(String name, Boolean sex, int age ){
  50. this.name = name;
  51. this.sex = sex;
  52. this.age = age;
  53.  
  54. }
  55.  
  56. public Human(String name, Boolean sex, int age, Human father, Human mother )
  57. {
  58. this.name = name;
  59. this.sex = sex;
  60. this.age = age;
  61. this.father = father.name;
  62. this.mother = mother.name;
  63. }
  64.  
  65. public String toString()
  66. {
  67. String text = "";
  68. text += "Имя: " + this.name;
  69. text += ", пол: " + (this.sex ? "мужской" : "женский");
  70. text += ", возраст: " + this.age;
  71.  
  72. if (this.father != null)
  73. text += ", отец: " + this.father;
  74.  
  75. if (this.mother != null)
  76. text += ", мать: " + this.mother;
  77.  
  78. return text;
  79. }
  80. }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement