As I understand it, in this task, under:
1 method (show) displays all information about students. 2 method: you need to transfer all 5 students and bring to the console the names of students who study in a particular course (the course number must be transferred to the method with a scanner).
the creation of methods in the Main class, and not in the Student class is understood.
And now in order:
Task: create a class Student with parameters name and course
This class is better defined in this way:
public class Student { private String mName; private int mCourse; public Student(String name, int course) { mName = name; mCourse = course; } public String getName() { return mName; } public int getCourse() { return mCourse; } public void setName(String name) { mName = name; } public void setCourse(int course) { mCourse = course; } public void print() { System.out.println("Студент " + mName + " учится на " + mCourse + " курсе."); } }
Pay attention to:
The access modifier is private for class fields. With this modifier, class fields are not directly accessible from the outside. You can get / set the values of these fields using the corresponding getters / setters. Always declare class fields as private unless explicitly required otherwise.
Prefix m for class fields - this prefix is usually added for all non-static class fields (for static fields, the prefix will be s ). From it, you can immediately understand that we are dealing with a regular class field.
The Student(String name, int course) constructor Student(String name, int course) , with which we can immediately create an object with the necessary parameters.
I agree with you that in the Student class it is advisable to define some method that will display a textual representation of the object on the screen.
Further:
in the Main method create 5 objects (students). 1 method (show) displays all information about students.
If the task is an entry-level and academic task, then we will use a simple array to store the list of students:
public class Main { public static void main(String[] args) { Student[] students = new Student[5]; students[0] = new Student("Вася", 5); students[1] = new Student("Петя", 1); students[2] = new Student("Аня", 3); students[3] = new Student("Федор", 5); students[4] = new Student("Маша", 5); show(students); } public static void show(Student[] students) { for (int i=0; i<students.length; i++) { students[i].print(); } } }
2 method: you need to transfer all 5 students and bring to the console the names of students who study in a particular course (the course number must be transferred to the method with a scanner).
public class Main { public static void main(String[] args) { Student[] students = new Student[5]; students[0] = new Student("Вася", 5); students[1] = new Student("Петя", 1); students[2] = new Student("Аня", 3); students[3] = new Student("Федор", 5); students[4] = new Student("Маша", 5); System.out.print("Введите курс: "); Scanner scanner = new Scanner(System.in); if (scanner.hasNextInt()) { show(students, scanner.nextInt()); } else { System.out.print("Введены некорректные данные!"); } } public static void show(Student[] students, int course) { for (int i=0; i<students.length; i++) { if (students[i].getCourse() == course) { students[i].print(); } } } }