How to reference methods that use array as an object in the console?

Asked

Viewed 171 times

0

I have a class with the following attributes and constructor:

private Person[] persons;
private int personCount;

public PersonDatabase(int defaultPersonsCapacity){

    persons = new Person[defaultPersonsCapacity];
    personCount = 0;
}

Your methods are as follows::

public void addPerson(String name, String surname, int age){

    if(personCount == persons.length){

        System.out.println("Unable to add Person.");
        return;
    }

    persons[personCount] = new Person(name, surname, age);
    personCount++;

    System.out.println("Person added.");
}

public int getPersonCount() {
    return personCount;
}

Another is this:

public Person searchPerson (String name, String surname){
    for (Person p: persons) {
        if (p.getName().equals(name)&&p.getSurname().equals(surname)) return 
        p;
    }
    return  null;
}

The class, attributes, constructor and first method were already assembled (it’s an exercise), and I created the second method. The class Person which is in the attributes, has only setters and getters of attributes name and surname.

I created the following objects in the class Main:

Person []persons=new Person[3];
    persons[0] = new Person("Jorge", "X");
    persons[1] = new Person("João", "Y");
    persons[2] = new Person("Maria", "Z");

My questions are as follows::

  1. My third method (the "searchPerson") is correct?
  2. How can I reference the first and second method? I will need the "Scanner"?
  3. Regardless of the answer, could you give me details of how to do this?

I’ve done a lot of research on this, but unfortunately, I haven’t found anything satisfactory, and I’m having a hard time understanding the function of these methods. I got stuck on the reference methods.

  • 1

    It is not a case to use one ArrayList ? You’re complicating for no reason

2 answers

2

My second method is correct?

It is difficult to answer your question. She speaks in second method, but you can’t even tell which one she is talking about. Looking at your question the second method is the addPerson() , but the answer is searchPerson() which is the third method.

In fact it is not even known if everyone is in the same class, it is only probable.

As stated, you have problems not using a list and using a array should make a more sophisticated mechanism, but let’s go this way. Probably shouldn’t even be a list by the little that was seen in the question, another tree-based structure would probably be better. Any exercise that teaches you to do wrong is a bad exercise.

What do you call attribute is actually called field.

The second method is correct, but it probably shouldn’t be done that way, or at least it shouldn’t be the only option. It is generally more appropriate to create the person outside of it and pass the created object and not parameters to the database to create. It is linking the detail of creating the person with the database, this way it makes the code less flexible and may have problems in the future.

If you’re talking about the third method, it seems to be correct even though it’s badly formatted. At least it seems, do not have to state this without knowing exactly what he should do, without knowing all the requirements, which are not put in the question.

How can I initialize the first and second method? I will need "Scanner"?

There is no such thing as initializing method, it is a term you invented and only you know what it means.

Only you can say if you need one Scanner. Developing software is not following cake recipes, it is understanding the problem and putting the best solution to it. The question doesn’t quite define what the problem is, so it’s hard to say what the right thing to do is. Any response that tries to say this is wrong by definition, it makes assumptions to give an answer.

It is more complicated to answer when we do not even know the exact structure of the class. It would be much better to have put the whole class. When you want to know if the organization of the code is right it becomes complicated to answer without seeing all of it.

Regardless of the answer, could you give me details of how to do this?

This is considered too broad for us to be able to answer, actually the right thing to do is to close the question because of this.

What I can say is that there are several conceptual errors in this code, but I don’t even know which ones can be fixed because it has artificial requirements, another reason not to respond properly. Whatever happens you will learn wrong with this exercise.

Besides class Person that I can’t talk much (and just because it wasn’t posted I don’t show my code working), you should have your current class, I’ll play it completely here because the accepted answer made it no longer a useful class and mix things up. in addition to having basic coding errors:

class Program {
    public static void main(String[] args) {
        PersonDatabase db = new PersonDatabase(3);
        //se quiser aqui colcoa um Scanner para pegar os dados e usá-los no lugar de literais
        db.addPerson("Jorge", "X", 20);
        db.addPerson("João", "Y", 20);
        db.addPerson("Maria", "Z", 20);
        Person pessoa = searchPerson("João", "Y");
        if (pessoa == null) return;
        System.out.println(pessoa.getName() + " | " + pessoa.getSurname());
        pessoa = searchPerson("Jorge", "X");
        if (pessoa == null) return;
        System.out.println(pessoa.getName() + " | " + pessoa.getSurname());
    }
}

class PersonDatabase {
    private static Person[] persons;
    private static int personCount;
    public PersonDatabase(int defaultPersonsCapacity) {
        persons = new Person[defaultPersonsCapacity];
        personCount = 0;
    }
    public void addPerson(String name, String surname, int age) {
        if (personCount == persons.length) {
            System.out.println("Unable to add Person.");
            return;
        }
        persons[personCount] = new Person(name, surname, age);
        personCount++;
        System.out.println("Person added.");
    }
    public int getPersonCount() {
        return personCount;
    }
    public Person searchPerson(String name, String surname) {
        for (Person p : persons) {
            if (p.getName().equals(name) && p.getSurname().equals(surname))
                return p;
        }
        return null;
    }
}

I put in the Github for future reference.

I know, you didn’t like the answer, but the question isn’t good either. I only answered because the current answer is not adequate either, and between being alone giving the wrong impression and another answer that does not say much but indicates the right way, I thought it was better to answer. I know most people won’t like this answer because today people want cake recipes and don’t learn to develop software the right way, but it’s the right thing to do since the question hasn’t been closed before.

1


Yes, your second method is ok, it will return to the person who match the nameand surname. Regarding how you can initialize the first and the second method, I don’t quite understand your point, but if I’m thinking, you want to know how to feed the Addperson, is that it? If yes, you can make calling him in his class Main, for example, instead of doing this:

Person []persons=new Person[3];
persons[0] = new Person("Jorge", "X");
persons[1] = new Person("João", "Y");
persons[2] = new Person("Maria", "Z");

You do that:

addPerson("Jorge", "X", 20);
addPerson("João", "Y", 20);
addPerson("Maria", "Z", 20);

Don’t forget to call before the PersonDatabase(3); to set the size of your array.

Then you can use the searchPerson as follows:

Person pessoa = searchPerson("João", "Y");

I believe that based on your methods, your class would look something like this:

public class Exemplo {
    private static Person[] persons;
    private static int personCount;

    public static void PersonDatabase(int defaultPersonsCapacity) {

        persons = new Person[defaultPersonsCapacity];
        personCount = 0;
    }

    public static void main(String[] args) {
        PersonDatabase(3);

        addPerson("Jorge", "X", 20);
        addPerson("João", "Y", 20);
        addPerson("Maria", "Z", 20);

        Person pessoa = searchPerson("João", "Y");
        System.out.println(pessoa.getName() + " | " + pessoa.getSurname());
        pessoa = searchPerson("Jorge", "X");
        System.out.println(pessoa.getName() + " | " + pessoa.getSurname());
    }

    public static void addPerson(String name, String surname, int age) {

        if (personCount == persons.length) {

            System.out.println("Unable to add Person.");
            return;
        }

        persons[personCount] = new Person(name, surname, age);
        personCount++;

        System.out.println("Person added.");
    }

    public int getPersonCount() {
        return personCount;
    }

    public static Person searchPerson(String name, String surname) {
        for (Person p : persons) {
            if (p.getName().equals(name) && p.getSurname().equals(surname))
                return p;
        }
        return null;
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.