Hi, I really need help with this code. I have the comments written out as to wha
ID: 3786307 • Letter: H
Question
Hi, I really need help with this code. I have the comments written out as to what steps need to be taken to complete the program but I am having a hard time understanding the program. I need help with the steps/comments in BOLD.This program basically selects a child for babysitting, either son or daughter and if the child belongs to the mother, then the mother gets payment and if the child doesnt belong to the mother then the mothers get the revenue. I have created the mother object, child, daughter and son and the main method.
public class Exercise3 {
public static void main(String[] args) {
//create a mother object
Mother N = new Mother();
//get a random number between 0 and 5 for the number of children of Mother object(N)
int n = (int)(Math.random() * 6 + 0);
//print the random number, which is the number of children of mother object, number of children mother has
System.out.println(n);
//create an array of 20 child objects, which can be son or daughter objects, decided randomly
Child [ ] childArray;
childArray = new Child[20];
Child newChild;
for (int i = 0; i < 20; i++)
{
int num = (int)Math.round(Math.random());
if (num == 0)
{
newChild = new Son();
//newChild.setName(name);
}
else
{
newChild = new Daughter();
}
childArray[i] = newChild;
}
//print the total list of children
System.out.println(childArray);
//randomly choose N child objects to be children of the Mother object
Random rand = new Random();
int num = rand.nextInt(20);
//display the names of the children of the mother object
//then for the first 10 child objects in the array, you are going to add up the balance for the mother object, mother.lookAfter(childobject)
//print the mother objects balance after each iteration, print 10 statements
//print out the final balance of the mother
}
}
import java.util.ArrayList;
public class Mother
{
private ArrayList children;
private int balance;
public Mother()
{
balance = 0;
}
public void addChild(Child child)
{
children.add(child);
}
public void lookAfter(Child child)
{
//if child belongs to the mother then balance += .getPayment();
//else balance += .getRevenue();
}
}
public int getBalance()
{
//write the getter method for the balance instance variable;;;
return .balance;
}
public void printChildren()
{
///iterate the children array list and print the names;
///printing name, pass the object that you are currently itterating,System.out.println(c)
}
}
public class Child {
public Child()
{
name = "unknown";
}
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getPayment()
{
return 0;
}
public int getRevenue()
{
return 0;
}
public String toString()
{
return getName();
}
}
public class Son extends Child
{
public int getPayment()
{
return -25;
}
public int getRevenue()
{
return 5;
}
}
public class Daughter extends Child
{
public int getPayment()
{
return -20;
}
/*
@return an integer denoting the earning of the mother of the daughter
*/
public int getRevenue()
{
return 7;
}
}
Explanation / Answer
Hi I am Giving you Complete Code which is running fine I was added some extra code as per your requirement
I did't removed you'r previous comments
just copy this code in to your editor:
import java.util.ArrayList;
public class Mother {
/*
* I am Storing all Child Objects In This ArrayList
*/
private ArrayList<Child> children = new ArrayList<Child>();
private int balance;
public ArrayList getChildren() {
return children;
}
public void setChildren(ArrayList children) {
this.children = children;
}
public void setBalance(int balance) {
this.balance = balance;
}
public Mother() {
balance = 0;
}
public void addChild(Child child) {
children.add(child);
}
public void lookAfter(Child child) {
if (children.contains(child)) {
balance = child.getPayment();
} else {
balance = child.getRevenue();
}
// if child belongs to the mother then balance += .getPayment();
// else balance += .getRevenue();
}
public int getBalance() {
// write the getter method for the balance instance variable;;;
return balance;
}
public void printChildren() {
/// iterate the children array list and print the names;
/// printing name, pass the object that you are currently
/// itterating,System.out.println(c)
for (int i = 0; i < children.size(); i++) {
System.out.println("Children OF Mother:" + children.get(i).getName());
}
}
}
-------------------------------------------------Child.java---------------------------------
public class Child {
private String name;
public Child() {
name = "unknown";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPayment() {
return 0;
}
public int getRevenue() {
return 0;
}
public String toString() {
return getName();
}
}
--------------------------Daughter.java--------------------------------------------
public class Daughter extends Child {
public int getPayment() {
return -20;
}
/*
* @return an integer denoting the earning of the mother of the daughter
*/
public int getRevenue() {
return 7;
}
}
---------------------------------Son.java----------------------------------
public class Son extends Child {
public int getPayment() {
return -25;
}
public int getRevenue() {
return 5;
}
}
--------------------------------Excercise3.java---------------------------------
import java.util.Random;
public class Excercise3 {
public static void main(String[] args) {
// create a mother object
Mother mother = new Mother();
// get a random number between 0 and 5 for the number of children of
// Mother object(mother)
Random random = new Random();
int n = random.nextInt(5);
// print the random number, which is the number of children of mother
// object, number of children mother has
System.out.println("No of children :" + n);
for (int i = 0; i < n; i++) {
mother.addChild(new Child());
}
// create an array of 20 child objects, which can be son or daughter
// objects, decided randomly
Child[] childArray;
childArray = new Child[20];
Child newChild;
for (int i = 0; i < 20; i++) {
int num = (int) Math.round(Math.random());
if (num == 0) {
newChild = new Son();
newChild.setName("Son "+(i+1));
} else {
newChild = new Daughter();
newChild.setName("Daughter "+(i+1));
}
childArray[i] = newChild;
}
// print the total list of children
for (int i = 0; i < childArray.length; i++) {
System.out.println(childArray[i]);
}
// randomly choose N child objects to be children of the Mother object
Random rand = new Random();
int num = rand.nextInt(20);
for (int i = 0; i < num; i++) {
mother.addChild(childArray[i]);
}
// display the names of the children of the mother object
mother.printChildren();
// then for the first 10 child objects in the array, you are going to
// add up the balance for the mother object,
// mother.lookAfter(childobject)
for (int i = 0; i < 10; i++) {
mother.lookAfter(childArray[i]);
System.out.println("Mother Balance:" + mother.getBalance());
}
// print the mother objects balance after each iteration, print 10
// statements
// print out the final balance of the mother
System.out.println("Final Balance Of An Mother:" + mother.getBalance());
}
}
-------------------------------------------------Out Put-------------------------
No of children :4
Daughter 1
Daughter 2
Son 3
Son 4
Son 5
Son 6
Daughter 7
Son 8
Daughter 9
Daughter 10
Daughter 11
Daughter 12
Son 13
Son 14
Son 15
Daughter 16
Son 17
Daughter 18
Daughter 19
Son 20
Children OF Mother:unknown
Children OF Mother:unknown
Children OF Mother:unknown
Children OF Mother:unknown
Children OF Mother:Daughter 1
Children OF Mother:Daughter 2
Children OF Mother:Son 3
Children OF Mother:Son 4
Children OF Mother:Son 5
Children OF Mother:Son 6
Children OF Mother:Daughter 7
Children OF Mother:Son 8
Children OF Mother:Daughter 9
Children OF Mother:Daughter 10
Children OF Mother:Daughter 11
Children OF Mother:Daughter 12
Children OF Mother:Son 13
Mother Balance:-20
Mother Balance:-20
Mother Balance:-25
Mother Balance:-25
Mother Balance:-25
Mother Balance:-25
Mother Balance:-20
Mother Balance:-25
Mother Balance:-20
Mother Balance:-20
Final Balance Of An Mother:-20
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.