Use set with scanner and array

Asked

Viewed 377 times

1

I have the following question: I am using the following code that works smoothly

System.out.println("Digite a Primeira Nota: ");

Scanner inNota1 = new Scanner(System.in);

 estud.notas[0] = inNota1.nextDouble();

But when learning about the private and set method I don’t know how to build the code. It would look something like this??

System.out.println("Digite a Primeira Nota: ");
        Scanner inNota1 = new Scanner(System.in);
        estud.setNotas[0](inNota1.nextDouble());

As I specified, when I use the public method I can use the first code with Scanner quietly, now using the private method I don’t know how to assign the position of the array memory and how to insert the Scanner variable in the set syntax

  • 1

    No, I wouldn’t be. I wouldn’t go anywhere near that. In order to answer this question, explain what your classes are and how they relate. Your classes are Estudante and Nota? By the way, if notes is an array, probably using a Setter won’t be the best way.

1 answer

1

private and set are distinct concepts:


private

To keyword private is what we call Access Modifier.
There are basically three access modifiers: public, protected and private:

  • public: this is the most "open" access modifier that exists in Java. Any class can access the member that has this access modifier. You do not need to be a child class of the class that has members with this access modifier (also known as public members) not being in the same package;

  • protected: this access modifier restricts access to the member in which it is used so that it is accessed only by the classes that inherit from the class that has the members with this access modifier and the classes that are in the same package;

  • private: This access modifier is the most "restricted" modifier in Java. It restricts the access of members who have this modifier to only the class itself and the classes that are in the same package;


set

The term set (in that case) concerns the methods setters.

In Java there is the concept of encapsulation, which says (simply) that its attributes must be accessed exclusively by methods if they are to be accessed outside the class itself.

For example:

public class Pessoa {

    private String nome;

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) { //método "setter"
        this.nome = nome;
    }
}

What you (probably) want to do is a method Setter private:

private void setNota(double nota) {
    estud.notas[0] = nota;
}

Considering the two topics (the access modifier private and the method Setter), It’s up to you to decide if you really want to do this.
I particularly find it silly to access a member of your own class through a method Setter.

If you were to "settar" the value of estud.notas[0] through another class, then yes it would be advantage/correct to use a Setter for this and would also need to check the correct access modifier for the scenario.

  • As I specified, when I use the public method I can use the first code with Scanner quietly, now using the private method I don’t know how to assign the position of the array memory and how to insert the Scanner variable in the set syntax

  • Specified? The only method call present in the code is the nextDouble() ._. I didn’t find any Setter much less public. @crackeralphazer0

  • Man, you guys are tripping. Scanner receives the value typed by the user, in the first example using the public method I was able to assign the value of the array to the address [0] by Scanner normally. When we switched to private I don’t know the syntax that would indicate in the memory [0] of the notes array in which the inNotas1 scanner would assign the value. It has nothing to do with what exists in the rest of the code, I just want to know what would be the construction of the code in the last line being the private method

  • @crackeralphazer0 I believe the answer I posted has what you want. Just you include the method statement private void setNota(double nota) { estud.notas[0] = nota; } and call it so: setNota(inNota1.nextDouble());. Please tell me if that’s what you want.

  • It didn’t work but thanks anyway

  • But is that what you want? @crackeralphazer0 What didn’t work? Gave some error?

  • Yes, it was a mistake. I created a class with an array of 3 positions for notes, being private; I intend to receive the values of the notes by Scanner, as said above the method being public worked the code described first. Whereas when the method is private the syntax changes, I’m not finding a way to make it work in private mode

Show 2 more comments

Browser other questions tagged

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