Java query

CSDN Q & A 2022-02-13 09:12:07 阅读数:997

java query

4、 ( Students' course selection ) According to the class diagram below , Writing courses Course Classes and students Student class .
Course Class description is as follows :
(1)addStudent Method is used to add a student who chooses a course ;
(2)toString Method is used to return all the information of the course , Course name and course selection .
Student Class description is as follows :
(1)toString Method is used to return student information .
Be careful : In the figure, the modifiers and readers of all data fields of the two classes are omitted .

Writing test classes , Create a course object and two student objects , Add two elective students to this course .
A sample of :

img

img




Take the answer :
import java.util.ArrayList;import java.util.List;import java.util.Scanner;@Test public void test() { class Student { private int stuNo; private String name; Student() { } Student(int stuNo, String name) { this.stuNo = stuNo; this.name = name; } @Override public String toString() { return "Student{" + "stuNo=" + stuNo + ", name='" + name + '\'' + '}'; } } class Course { private String name; private List<Student> stuList; private int numberOfStudents; Course() { } Course(String name) { this.name = name; } Course(String name, List<Student> stuList, int numberOfStudents) { this.name = name; this.stuList = stuList; this.numberOfStudents = numberOfStudents; } public Course addStudent(Student stu) { if (CollectionUtil.isEmpty(stuList)) { stuList = new ArrayList<>(); } stuList.add(stu); return this; } @Override public String toString() { return "Course{" + "name='" + name + '\'' + ", stuList=" + stuList + ", numberOfStudents=" + numberOfStudents + '}'; } } Scanner scanner = new Scanner(System.in); System.out.println(" Please enter the course name "); String courseName = scanner.nextLine(); Course course = new Course(courseName); scanner = new Scanner(System.in); System.out.println(" Please enter the student id number "); int student1No = scanner.nextInt(); scanner = new Scanner(System.in); System.out.println(" Please enter the student's name "); String student1Name = scanner.nextLine(); Student student1 = new Student(student1No, student1Name); scanner = new Scanner(System.in); System.out.println(" Please enter the student id number "); int student2No = scanner.nextInt(); scanner = new Scanner(System.in); System.out.println(" Please enter the student's name "); String student2Name = scanner.nextLine(); Student student2 = new Student(student2No, student2Name); course.addStudent(student1); course.addStudent(student2); System.out.println(" Course name "+course.name); System.out.println(" List of selected students "); for(Student student : course.stuList){ System.out.print(" Student number "+student.stuNo); System.out.println(" full name "+student.name); } }


Other answers 2:

This is not hard to , The blogger wrote well .


Other answers 3:

img

copyright:author[CSDN Q & A],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130912050299.html