CodeGym
促销活动
CodeGym University
学习
课程
任务
调查和小测验
游戏
帮助
提醒时间表
社区
用户
论坛
聊天
文章
成功故事
活动
评论
订阅服务
浅色主题
问题
  • 评论
  • 关于我们
开始
开始学习
立即开始学习
  • 全部问题
PeaceLovePang
第 14 级
Warrington
  • 01.06.2020
  • 552次阅读
  • 5个评论

I don't know what the problem is, please help me.

有关 任务的问题 构建家庭
Java 语法,  第 8 级,  课程 11
已解决


1.创建包含 String name、boolean sex、int age 和 ArrayList<Human> children 字段的 Human 类。
2.创建并填充对象,以便我们最终得到:两个祖父、两个祖母、一个父亲、一个母亲和三个孩子。
3.显示所有 Human 对象(提示:使用 Human 类的 toString() 方法)。

要求:
  • 程序应在屏幕上显示文本。
  • Human 类必须包含四个字段。
  • Human 类必须包含一个方法。
  • Solution 类必须包含一个方法。
  • 程序应创建对象并用数据填充它们,以得到两个祖父、两个祖母、一个父亲、一个母亲和三个孩子。然后,程序应在屏幕上显示所有 Human 对象。
package zh.codegym.task.task08.task0824; /* 构建家庭 */ import java.util.ArrayList; public class Solution { public static void main(String[] args) { //在此编写你的代码 ArrayList<Human> childrens=new ArrayList<Human>(); Human child1 = new Human("childnumberone",false,1,childrens); System.out.println( child1.toString()); Human child2 = new Human("childnumbertwo",false,1,childrens); System.out.println( child2.toString()); Human child3 = new Human("childnumberthree",false,1,childrens); System.out.println( child3.toString()); childrens.add(child1); childrens.add(child3); childrens.add(child2); Human grandfather= new Human("汤姆",false,80,childrens); System.out.println( grandfather.toString()); Human grandfather2= new Human("汤姆2",false,81,childrens); System.out.println( grandfather2.toString()); Human grandmother= new Human("珍妮",false,82,childrens); System.out.println( grandmother.toString()); Human grandmother2= new Human("珍妮2",false,83,childrens); System.out.println( grandmother2.toString()); Human father = new Human("father",true,20,childrens); System.out.println( father.toString()); Human mother = new Human("mother",false,25,childrens); System.out.println(mother.toString()); } public static class Human { //在此编写你的代码 public String name; public boolean sex; public int age; public ArrayList<Human> children; public Human(String name, boolean sex, int age, ArrayList<Human> children) { this.name = name; this.sex = sex; this.age = age; this.children = children; } public String toString() { String text = ""; text += "名字:" + this.name; text += ",性别:" + (this.sex ? "男" : "女"); text += ",年龄:" + this.age; int childCount = this.children.size(); if (childCount > 0) { text += ",孩子:" + this.children.get(0).name; for (int i = 1; i < childCount; i++) { Human child = this.children.get(i); text += "," + child.name; } } return text; } } }
0
评论 (5)
  • 受欢迎
  • 新
  • 旧
你必须先登录才能发表评论
Harry
级别 15 ,Singapore,Singapore
1 六月 2020, 13:33
You may use the children instance variable to represent the parent-children relations required by the problem
0
PeaceLovePang
级别 14 ,Warrington,United Kingdom
1 六月 2020, 14:36
Sorry, I still don't understand. Could you be more specific
0
Harry
级别 15 ,Singapore,Singapore
1 六月 2020, 16:15解决方法
Sure, let me explain it in a more detailed way. Now the issue is that values of all children instance variables are initialized using the same ArrayList childrens, meaning that child1, child2 and child3 are the children of all Human objects. However, this does not reflect the relationships given in this problem. Suppose a couple has 2 children, Tom and Sam. In the meanwhile, Tom and Sam's grandparents are fathers and mothers of the couple. To represent these relationships, we can first create two Human objects for Tom and Sam respectively. For Tom and Sam, in the constructor we just need to initialize their children instance variables using empty ArrayList. In the second step, we create two Human objects to be the father and the mother. Since they are the parent of Tom and Sam, when initializing their instance variables children, we need to create ArrayList and add Tom and Sam to it. Only in this way, other objects can know that the father and the mother has two children, Tom and Sam. In the third step, we just need to follow the same way to create grandparents' objects. I don't know whether my explanation can make the solution a little bit more clearer.
+2
PeaceLovePang
级别 14 ,Warrington,United Kingdom
2 六月 2020, 01:11
Thank you very much, I understand, and I passed through your explanation, you are a dedicated person
0
Harry
级别 15 ,Singapore,Singapore
2 六月 2020, 01:59
I am glad that it helped:)
0
了解
  • 注册
  • Java 课程
  • 任务帮助
  • 定价
  • 游戏项目
  • Java 语法
社区
  • 用户
  • 文章
  • 论坛
  • 聊天
  • 成功故事
  • 活动
  • Affiliate Program
公司
  • 关于我们
  • 联系方式
  • 评论
  • 新闻发布室
  • CodeGym 教育
  • 常见问题
  • 支持
CodeGym CodeGym 是一个从零开始学习 Java 语言编程的在线课程。本课程是初学者掌握 Java 语言的绝佳方式。它包含 1200 多个可即时验证的任务,以及基本范围内的 Java 基础理论。为了帮助你在教育上取得成功,我们实现了一组激励功能:小测验、编码项目以及有关高效学习和 Java 语言开发人员职业方面的内容。
关注我们
界面语言
程序员不是天生的 © 2023 CodeGym
MastercardVisa
程序员不是天生的 © 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.