Task:

  1. Create a Student class with the name and course parameters;

  2. in the Main method create 5 objects (students).

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).

Help to cope with the second method, it does not work to create it.

Main class:

 import java.util.Scanner; public class Main { public static void main(String[] args) { Student student1 = new Student(); student1.name = "Sasha"; student1.course = 1; student1.show(); Student student2 = new Student(); student2.name = "Lesha"; student2.course = 2; student2.show(); Student student3 = new Student(); student3.name = "Anya"; student3.course = 3; student3.show(); Student student4 = new Student(); student4.name = "Tanya"; student4.course = 4; student4.show(); Student student5 = new Student(); student5.name = "Ilya"; student5.course = 5; student5.show(); } student1.countCourse (); 

Student class:

 import java.util.Scanner; public class Student { public String name; public int course; public void show() { System.out.println(name + " " + course); } public void countCourse() { int i = course; String m = name; System.out.println("Введите курс: "); Scanner sc = new Scanner(System.in); if (sc.hasNextInt() && sc.nextInt() > 0 && sc.nextInt() <= 5) { i = sc.nextInt(); System.out.println(m); } else { System.out.println("Введите курс от 1 до 5"); } } } 

    2 answers 2

    I would redo a little:

    1. I would put all students on the list so that in the show and countCourse one could pass this list, and then run countCourse inside the cycle and display the necessary information.
    2. Added a constructor that accepts the student's name and course as input. That will simplify, at least, the creation of an object and adding it to the collection.
    3. Since viewing all the information about students and viewing all those who are on a certain course, in fact, in this case, it seems to me, does not apply to a particular student, methods can be made static.
    4. In the countCourse method, countCourse organize an infinite loop in which we request a digit (there are not enough error checks but this will be% on your conscience)). As soon as we get the required number of the course, we stop the cycle and organize another cycle, which runs on the transferred collection by the student and displays the name + course of the student whose number coincided with the entered one.

    Something like this.

    class Student

     package test; import java.util.List; import java.util.Scanner; /** * Created by iprogrammer on 13.11.2016. */ public class Student { private String name; private int course; public Student(String name, int course) { this.name = name; this.course = course; } public static void show(List<Student> students) { for (Student student : students) { System.out.println(student.name + " " + student.course); } } public static void countCourse(List<Student> students) { Scanner sc = new Scanner(System.in); int course = 1; while (true) { System.out.println("Введите курс (1-5): "); course = sc.nextInt(); // Integer.parseInt(sc.nextLine()); if (course >= 1 && course <= 5) break; } System.out.println("Студенты с курса номер " + course + ": "); for (Student student : students) { if (student.course == course) System.out.println(student.name + " " + student.course); } } } 

    main method

     List<Student> students = new ArrayList<>(); students.add(new Student("Sasha", 1)); students.add(new Student("Lesha", 2)); students.add(new Student("Anya", 3)); students.add(new Student("Tanya", 4)); students.add(new Student("Ilya", 5)); Student.show(students); Student.countCourse(students); 

      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:

      1. 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.

      2. 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.

      3. 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(); } } } }