[Java] Checking and comparing fields...

Nenad

~Choco Coronet~ Omnomnom
Reaction score
137
I have an assignment that i would like some help with, cause i have no idea what to place in this method.

The method:public boolean method isMotherOf "Create a method that tests whether this Person is the mother of the Person given as an argument to the method. "

Now, I'm still a little confused as we are using fields here (which i never used before).

What i have at the moment is fields that are used to store these objects:
Code:
public class Person {
	
	String name;
	Person mother;
	Person father;
	ArrayList<Person> children;

How am i supposed to check if a person is someones mother? I'm thinking it has something to do with comparing fields but i have no idea how to do that >.<
 

Vestras

Retired
Reaction score
248
I'm not quite sure what you mean but something like this?
Code:
bool isMotherOf(Person potentialChild) {
    return potentialChild.mother == this;
}
 

Nenad

~Choco Coronet~ Omnomnom
Reaction score
137
I'm not quite sure myself since i have never seen an assignment like this (nor is it in the book, as far as i can see) but i don't really understand what you did there xD

If it helps, here is the full assignment from the start:

The Person class will be used to, obviously, define a person.

String field name in public class Person in package x
A Person has a name. Create a String-field to hold the name.

Person field mother in public class Person in package x
A Person has a mother. Create a field for that, too.

Note that this is not a String field. Why? A Person's mother is also a Person! This gives you a taste of OOP. A class can actually hold fields of the same type as the class itself.
Person field father in public class Person in package x
Create a field for the father.

ArrayList<Person> field children in public class Person in package x
An ArrayList holding the Person's children.

The children are naturally also Persons. Since person can have more than one, we cannot use a simple field, but need an ArrayList.
public boolean method isMotherOf in public class Person in package x
Create a method that tests whether this Person is the mother of the Person given as an argument to the method.

This is all i have to go on and i would really like to actually learn what I'm doing here, instead of it just being done X)
 

UndeadDragon

Super Moderator
Reaction score
447
Something like: (maybe... I'm not quite sure on your assignment's wording).

Code:
public class Person {
	String name;
	Person mother;
	Person father;
	ArrayList<Person> children;

        private bool isMotherOf() {
             if(children.contains(Person("name")) {
                    return true;
             }
             else {
                    return false;
             }
        }
}

I'm not entirely sure on the assignment though.
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
This

Code:
import java.util.ArrayList;

public class Person {
    
    private String name;
    private Person mother;
    private Person father;
    private ArrayList<Person> children;
    
    public Person() {
        children = new ArrayList<Person>();
    }
    
    public boolean isMotherOf(Person child) {
        for (int i = 0; i < children.size(); i++) {
            if (children.get(i) == child)
                return true; // matched
        }
        return false; // never matched
    }

}
 

Nenad

~Choco Coronet~ Omnomnom
Reaction score
137
Can you explain the code a bit?

Edit: You changed it now, so i get it a lot better, but still:
1. What's the get(i) command? Does it call from the array list of children for that name and then if true returns true and if not returns false?
2. I'm also supposed to continue and test if or not the child's parent is the mother or father (the same thing i tested here but vice versa). I'm gonna try doing this now.
 

Vestras

Retired
Reaction score
248
I'm not quite sure myself since i have never seen an assignment like this (nor is it in the book, as far as i can see) but i don't really understand what you did there xD

If it helps, here is the full assignment from the start:



This is all i have to go on and i would really like to actually learn what I'm doing here, instead of it just being done X)

All the other replies are very inefficient compared to my solution. Mine is the correct one for the given assignment.
UndeadDragon's solution would not work because the list will never contain a new instance with the same name, as lists compare by reference and not by field values.
GetTriggerUnit-'s solution is way too inefficient.
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Alright here's something that should work
Code:
import java.util.ArrayList;

public class Person {
    
    public String name;
    public Person mother;
    public Person father;
    public ArrayList<Person> children;
    
    public Person(String name, Person mother, Person father) {
        this.name = name;
        this.mother = mother;
        this.father = father;
        children = new ArrayList<Person>();
    }
    
    public boolean isMotherOf(Person mother) {
        // Loop throught the mother's children
        for (int i = 0; i < mother.children.size(); i++) {
            // if children(x) == this (current instance of the class)
            if (mother.children.get(i) == this)
                // return yes it's his/her mother
                return true;
        }
        // it never returned yes, return no
        return false;
    }
    
    public boolean isFatherOf(Person father) {
        // same as for mother
        for (int i = 0; i < father.children.size(); i++) {
            if (father.children.get(i) == this) 
                return true;
        }
        return false;
    }

}
 

Nenad

~Choco Coronet~ Omnomnom
Reaction score
137
All the other replies are very inefficient compared to my solution. Mine is the correct one for the given assignment.

Is there any chance that you can explain you're solution to me? What does it do? If need be point me to a website or something, i can't just use it without knowing what it is and what it does.

Also, bool returns an error in eclipse, i need to use boolean, not that it really matters much.

Alright here's something that should work
Code:
import java.util.ArrayList;

public class Person {
    
    public String name;
    public Person mother;
    public Person father;
    public ArrayList<Person> children;
    
    public Person(String name, Person mother, Person father) {
        this.name = name;
        this.mother = mother;
        this.father = father;
        children = new ArrayList<Person>();
    }
    
    public boolean isMotherOf(Person mother) {
        // Loop throught the mother's children
        for (int i = 0; i < mother.children.size(); i++) {
            // if children(x) == this (current instance of the class)
            if (mother.children.get(i) == this)
                // return yes it's his/her mother
                return true;
        }
        // it never returned yes, return no
        return false;
    }
    
    public boolean isFatherOf(Person father) {
        // same as for mother
        for (int i = 0; i < father.children.size(); i++) {
            if (father.children.get(i) == this) 
                return true;
        }
        return false;
    }

}

Thank you, i use this with the other code no? ^^
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Mine's indeed inefficient compared to yours, but yours does not do what's asked.

ArrayList<Person> field children in public class Person in package x
An ArrayList holding the Person's children.

The children are naturally also Persons. Since person can have more than one, we cannot use a simple field, but need an ArrayList.
public boolean method isMotherOf in public class Person in package x
Create a method that tests whether this Person is the mother of the Person given as an argument to the method.
You have to loop throught a Person's children, as it says.

bool and boolean are the same thing.

>> no forget the other code.
now you could add method like addChildren(Person child) etc...
 

Vestras

Retired
Reaction score
248
Is there any chance that you can explain you're solution to me? What does it do? If need be point me to a website or something, i can't just use it without knowing what it is and what it does.

Also, bool returns an error in eclipse, i need to use boolean, not that it really matters much.

Okay, let's take this statement: "Create a method that tests whether this Person is the mother of the Person given as an argument to the method."
That means we need a method, isMotherOf, that should return a boolean value indicating whether this instance of the Person class is the mother of the other instance of Person passed through a method parameter.

Shortening this, we need to figure out of this instance of Person is equal to the mother of the instance of Person passed in the method parameter "potentialChild". How do we do this? Well, we certainly don't loop through all the children of the current instance of Person to see whether the list contains a Person with the same name as the given parameter (or the same instance, whatever)

We have a Person given to us. What fields does a Person have? name, mother, father and children. Through the isMotherOf we can access the mother field of the given Person, so what is the simplest way to figure out if this Person is equal to the mother of the given Person?

this == givenPerson.mother OR this == potentialChild.mother:
Code:
bool isMotherOf(Person potentialChild) {
    return potentialChild.mother == this;
}
No funky loop business. Just a simple one liner, as I am sure it was supposed to be.

EDIT:
" You have to loop throught a Person's children, as it says. "

You do in no way need to loop through a Person's children to achieve the desired effect. If I am understanding the assignment correctly, you do not, atleast.

If you really don't believe that my method returns the correct value, just test both. You can write the father equivalent yourself.

Oh, and if you really need to use loops, don't use for loops, use foreach loops:
for (Person person : children) (...)
I think that's the Java syntax, forgive me, I don't program Java, I use C# (which by the way is a much better language).
 

Nenad

~Choco Coronet~ Omnomnom
Reaction score
137
My mistake, i did not copy all of the freaking text to the bottom... i don't know why i didn't see it before. Here is the rest:

public boolean method isMotherOf in public class Person in package x
Create a method that tests whether this Person is the mother of the Person given as an argument to the method.

It is not enough to check whether the Person has this Person registered as his or her mother, you must check that the Person is in this Person's children list, too. Consistency is key here!

public boolean method isFatherOf in public class Person in package x
Create a method that tests whether this Person is the father of the Person given as an argument to the method.

public boolean method isSiblingOf in public class Person in package x
Create a method that tests whether this Person is the sibling of the Person given as an argument to the method.

This method is actually a lot more subtle than it might seem at a first glance. Ask yourself; When are two people formally siblings? Furthermore, Java doesn't care whether or not a Person can be his/her own sibling in the real world. In Java, that is completely possible unless you force it not to be!

Now tell me, what solution to use and if possible explain with comments briefly (as gettrigger did) about what parts of the code mean so i can use it in the rest of the tasks (4 similar ones).
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
Vestras, both of our ways work and return to the same in the end. But Nenad's teacher said that you had to use an ArrayList of person named children that contains the children of a mother. Mine loops throught that array and checks every single child of the mother to see if it's the child given. Yours checks if the given child's mother is equal to the mother of the class instance.

Acccording to me, this is what corresponds the most to what's asked, and both method could be used side by side to give even more choices to the user of the class.

Code:
import java.util.ArrayList;

public class Person {
    
    private String name;
    private Person mother;
    private Person father;
    private ArrayList<Person> children;
    
    public Person() {
        children = new ArrayList<Person>();
    }
    
    public boolean isMotherOf(Person child) {
        for (int i = 0; i < children.size(); i++) {
            if (children.get(i) == child)
                return true; // matched
        }
        return false; // never matched
    }
}
 

GetTriggerUnit-

DogEntrepreneur
Reaction score
129
That is what is would look like with the .isFatherOf and .isSiblingOf

Code:
import java.util.ArrayList;

public class Person {
    
    private String name;
    private Person mother;
    private Person father;
    private ArrayList<Person> children;
    
    public Person() {
        children = new ArrayList<Person>();
    }
    
    public boolean isMotherOf(Person child) {
        for (int i = 0; i < children.size(); i++) {
            if (children.get(i) == child)
                return true; // matched
        }
        return false; // never matched
    }
    
    public boolean isFatherOf(Person child) {
        for (int i = 0; i < children.size(); i++) {
            if (children.get(i) == child)
                return true;
        }
        return false;
    }
    
    public boolean isSiblingOf(Person mother, Person father) {
        return (isMotherOf(mother) && isFatherOf(father));
    }
}

Wow, that is fucking retarded. Double checking in programming ftl.
Sorry then.
Sorry, my english is not very good, are you saying im retarded or what? :eek:
 

Vestras

Retired
Reaction score
248
That is what is would look like with the .isFatherOf and .isSiblingOf

Code:
import java.util.ArrayList;

public class Person {
    
    private String name;
    private Person mother;
    private Person father;
    private ArrayList<Person> children;
    
    public Person() {
        children = new ArrayList<Person>();
    }
    
    public boolean isMotherOf(Person child) {
        for (int i = 0; i < children.size(); i++) {
            if (children.get(i) == child)
                return true; // matched
        }
        return false; // never matched
    }
    
    public boolean isFatherOf(Person child) {
        for (int i = 0; i < children.size(); i++) {
            if (children.get(i) == child)
                return true;
        }
        return false;
    }
    
    public boolean isSiblingOf(Person mother, Person father) {
        return (isMotherOf(mother) && isFatherOf(father));
    }
}


Sorry, my english is not very good, are you saying im retarded or what? :eek:

No no, not at all. I am saying that it is retarded to double check, alias the assignment is what is retarded.
 

UndeadDragon

Super Moderator
Reaction score
447
That assignment is not worded well at all :p

I had no idea what it was on about.
 

Nenad

~Choco Coronet~ Omnomnom
Reaction score
137
That assignment is not worded well at all :p

I had no idea what it was on about.

Almost none of the students here could get this or the previous assignments (me included). The teacher words them very poorly, but alas what can you do. Thanks for all the help guys, if i need more i'll ask x)
 

saw792

Is known to say things. That is all.
Reaction score
280
Yeah, you're all wrong (although I agree entirely with Vestras that double checking is retarded).

GetTriggerUnit's solution doesn't do the double check like what is asked in the specification, and also doesn't give the correct result. Both the isMotherOf and isFatherOf methods are identical, and are really isParentOf. The isSiblingOf method is taking the wrong arguments. Here is what you should have based on the specification (I took sibling to mean has same mother and father, not including half-brothers or half-sisters).

Code:
import java.util.ArrayList;

public class Person {
    
    private String name;
    private Person mother;
    private Person father;
    private ArrayList<Person> children;
    
    public Person() {
        children = new ArrayList<Person>();
    }
    
    public boolean isMotherOf(Person potentialChild) {
        if (potentialChild.mother != this)
            return false;
        
        for (Person child : children) {
            if (child == potentialChild)
                return true;
        }
        return false;
    }
    
    public boolean isFatherOf(Person potentialChild) {
        if (potentialChild.father != this)
            return false;
        
        for (Person child : children) {
            if (child == potentialChild)
                return true;
        }
        return false;
    }
    
    public boolean isSiblingOf(Person potentialSibling) {
        return potentialSibling != this &&
               mother.isMotherOf(potentialSibling) &&
               father.isFatherOf(potentialSibling);
    }
}
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top