Shamanix
Shamanix

Reputation: 81

Method taking reference to an array of references?

I missed one of my lectures in Java, and the subject was classes, methods, constructors etc. The homework is one task:

Create a class Person, objects of which describe persons, and which contains only two felds: name (String) and year of birth (int). In this class, define

  • constructor taking name and year of birth;

  • constructor taking only name and setting the year of birth to default value 1990;

  • method isFemale returning true if this person is a woman (we assume, not very sensibly, that only women and all women have names ending with the letter 'a'); otherwise the method returns false;

  • static function getOlder taking two references to objects of class Person and returning the reference to the older of these two persons;

  • static function getOldest taking the reference to an array of references to objects of class Person and returning the reference to the oldest person represented in the array;

  • static function getYoungestFemale taking the reference to an array of refe- rences to objects of class Person and returning the reference to the youngest woman represented in the array, or null if there is no woman in the array.

In a separate class, write a main function in which the whole functionality of the class Person is tested.

I checked some tutorials and explanations, I didn't go straight here asking for help but after 2 hours of ripping my hair out I've been only able to come up with this:

public class Person {
    String name;
    int yob; //year of birth

    public Person() {
        Person jan = new Person("Jan", 1995); //the names are polish
        Person joanna = new Person("Joanna", 1993);
        Person michal = new Person("Michal", 1980);
        Person beata = new Person("Beata", 1979);
        Person kazimierz = new Person("Kazimierz", 1998);
        Person magdalena = new Person("Magdalena", 1999);
    }

    public Person(String name, int yob) {
        this.name = name;
        this.yob = yob;
    }

    public Person(String name) {
        this.name = name;
        this.yob = 1990;
    }

    public static boolean isFemale(String name) {
        if(name.equals("Joanna")) {
            return true;
        } else {
            return false;
        }
    }

    public static String getOlder(Person x?, Person y?) { // if I understand the task correctly, I should reference any two names?
        if(x?.yob>y?.yob) {
            return x?.name;
        } else {
            return y?.name;
        }

    //getOldest and getYoungestFemale methods go here

    }
}

However, I can't wrap my head around the last three steps. My brain is literally boiling. It would really help if anyone could explain the last three bullet points (getOlder reference to any 2 people and getOldest/getYoungestFemale)

If you don't have time to explain, some example of a "method taking a reference to an array" should be enough for me to get a basic understanding.

Thanks in advance.

Upvotes: 2

Views: 802

Answers (3)

EkcenierK
EkcenierK

Reputation: 1439

Here are some hints which should help you work out the answer yourself without me giving away the solution ;)

1)

 public static String getOlder(Person x?, Person y?) {
      // if I understand the task correctly, I should reference any two names?     
     if(x?.yob>y?.yob) { 
            return x?.name;       
     } else {         
            return y?.name;       
     }
}

This code is almost correct! Just remove the question marks! Also remember that the older person will have an earlier yob. EDIT, also you need to return the reference to the person, not their name, so return either x or y.

2) getOldest and getYoungestWoman

Person[]

is an array of references to Person objects. You should be able to read up on how to loop through the elements of an array and compare values.

3) an extra: if you declare those 6 Person objects inside the constructor, you won't be able to access them in other methods of the class. it is ok to create the Person objects there, but you must declare them outside the constructor. Declare them in the class.

Upvotes: 2

hsnkhrmn
hsnkhrmn

Reputation: 1000

Firstly create another class which will have main method. Within main create an array:

Person[] parr = new Person[6];
//Then fill all your person to this array:
parr[0] = new Person("name", year);
parr[1] = ....

Then pass this array handler to your methods:

Person p1 = Person.findSomething(parr);

In Person class:

public static Person findSomething(Person[] parr){
  for (Person p : parr){
     if (p.name.endsWith("a")) return p;
  }
  return null;
}

Upvotes: 2

zapl
zapl

Reputation: 63955

Usually.. you don't call it "reference to an array of references of something" You just say "array of something". Even though arrays of objects are arrays of references to objects. Just like a variable for an object is just a reference to an object.

Type heyImAReference = new ImTheObject();

So when you write

Person person = new Person();

You'll have the class Person as type, person as a reference to an instance (or object) of that class and the resulting entity of new Person() as the actual thing that is being referenced. Usually called "instance" or in your case "object".

When it comes to arrays of persons and you do

Person[] persons = new Person[5];

You create via new Person[5] an array instance that has 5 slots, in each slot can go a Person instance figuratively, actually though you have 5 references. Person[0] being the first, Person[1] being the second and so on. So that is an "array of references to objects of class Person".

And persons is a reference to that. So it is a "reference to an array of references to objects of class Person"

  • static function getOldest taking the reference to an array of references to objects of class Person and returning the reference to the oldest person represented in the array

means nothing more than

static Person getOldest(Person[] persons) {
    ...
}

I would call that a method that takes an array of Persons and returns a Person. Though technically, it's all just references that go in and come out. The Person objects don't "move"

Upvotes: 2

Related Questions