Before me is the task: to write a class "Students" in which they will choose their elder. But for starters, I would like to give students the names that I shoved into an array. There will be 10 students (objects) in a class (((or it’s better to create 1 object. This has not yet been decided)) with 3 qualities each ("znanie", "trydolubie", "ysidchivoct"). These will be variables that will take a value from 0 to 10. Here is what I have at the moment:

public class Students { String Starosta; String [] name={"Петя","Витя","Маша","Гриша","Света","Коля","Никита","Вика","Артур","Валера","Дима"}; public void getName(){ } public static void main(String[] args) { Students student1= new Students(); Students student2= new Students(); Students student3= new Students(); Students student4= new Students(); Students student5= new Students(); Students student6= new Students(); Students student7= new Students(); Students student8= new Students(); Students student9= new Students(); Students student10= new Students(); student1.getName(); System.out.println(student1.nextInt(10)); 

Since I am a green Padawan and Uncle Google does not give answers, I decided to turn to the sages for guidance. Here are the questions I have:

  1. How to make so that from this array randomly chosen name and assigned to the student?

  2. Is it possible for a student (who already has a name) to assign random qualities?

    2 answers 2

    You need the Random class, which can generate random numbers. It works like this:

     //в метод nextInt передаётся максимальное число. Но оно никогда не выпадет int randomIntFromZeroToNine = new Random.nextInt(10); 

    With an array, you can use it along with its length like this:

     String randomName = namesArr[new Random.nextInt(namesArr.length)]; 

    By code (everything is bad, not very good, everything must be rewritten):

    • The class describing the student should not have a plural in the title.
    • Warden should also be of type Student
    • The name must be stored in the field. The getName method getName as the name implies, should return a reference to this field.
    • Create 10 variables of the same type is not worth it - push them immediately into an array / list. Better to list ( List->ArrayList ).
    • The names of variables, classes, etc. should be in the bourgeois language. Translit hurts the eye and a non-Russian-speaking coder cannot understand what this variable contains. And if the burzhuinsky call - the coder of any country will understand.

      My suggestions / recommendations:

      • The “Headman” field should be made of type boolean, create a getter and a setter for it. This type is most suitable for the class "Student". If you additionally had the class "Group", then in it the appropriate type for the "Head" field was just "Student".
      • Make several constructors: by default, setting random fields for the name, knowledge, diligence and perseverance; accepting a name as a parameter and setting random values ​​for the remaining fields; full, taking as parameters the values ​​of all fields.