CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
Bhavishya Samriya
Level 8
Jaipur
  • 07.08.2019
  • 1215views
  • 1comment

unable to pass the last condition

Question about the task Make a family
Java Syntax,  Level 8,  Lesson 11
Under discussion

1. Create a Human class with String name, boolean sex, int age, and ArrayList<Human> children fields.
2. Create and populate objects so that we end up with: two grandfathers, two grandmothers, one father, one mother, and three children.
3. Display all the Human objects (Hint: use the Human class's toString() method).

Requirements:
  • The program should display text on the screen.
  • The Human class must have four fields.
  • The Human class must have one method.
  • The Solution class must have one method.
  • The program should create objects and fill them with data to get two grandfathers, two grandmothers, one father, one mother, and three children. Then it should display all the Human objects on the screen.
package com.codegym.task.task08.task0824; import java.util.*; import java.util.ArrayList; /* Make a family */ public class Solution { public static void main(String[] args) { //write your code hereSystem.out.println(c1.toString()); ArrayList<Human> generation_1 = new ArrayList<Human>(); ArrayList<Human> generation_3 = new ArrayList<Human>(); ArrayList<Human> generation_2 = new ArrayList<Human>(); Human c1 = new Human("nvkvslk",23,true,generation_1); Human c2 = new Human("nvkvslk",23,true,generation_1); Human c3 = new Human("nvkvslk",23,true,generation_1); Human f = new Human("fwik",23,true,generation_2); Human m = new Human("fwik",23,false,generation_3); generation_1.add(f); generation_1.add(m); Human gm = new Human("jksdfn",23,false,generation_3); Human gm1 = new Human("jksdfn",23,false,generation_2); Human gf = new Human("jksdfn",23,true,generation_2); Human gf1 = new Human("jksdfn",23,true,generation_3); System.out.println(c1.toString()); System.out.println(c2.toString()); System.out.println(c3.toString()); System.out.println(f.toString()); System.out.println(m.toString()); System.out.println(gm.toString()); System.out.println(gm1.toString()); System.out.println(gf1.toString()); System.out.println(gf.toString()); } public static class Human { //write your code here String name; boolean sex; int age; ArrayList<Human> children = new ArrayList<>(); Human(String name , int age , boolean sex , ArrayList <Human> children) { this.name=name; this.age=age; this.sex=sex; this.children=children; } public String toString() { String text = ""; text += "Name: " + this.name; text += ", sex: " + (this.sex ? "male" : "female"); text += ", age: " + this.age; int childCount = this.children.size(); if (childCount > 0) { text += ", children: " + this.children.get(0).name; for (int i = 1; i < childCount; i++) { Human child = this.children.get(i); text += ", " + child.name; } } return text; } } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Guadalupe Gagnon
Level 37 , Tampa, United States
12 November 2019, 16:14
The problem in this code is that the children (the array inside person class) are not set properly on the Human objects created in main. You will need to rethink the relationship between the 4 grandparents, 2 parents, and 3 children.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.