Advertisement
Vladkoheca

Classroom.java

Dec 13th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | Source Code | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Classroom {
  4.  
  5.     public Student[] students;
  6.     public String className;
  7.  
  8.  
  9.     public Classroom(String className, int numStudents){
  10.         this.className = className;
  11.         this.students = new Student[numStudents];
  12.     }
  13.  
  14.     public void addStudent(){
  15.         Scanner sc = new Scanner(System.in);
  16.         for (int i = 0; i < students.length; i++) {
  17.             System.out.println("Enter a student name: " + (i + 1) + ": ");
  18.             String name = sc.nextLine();
  19.  
  20.             System.out.println("Enter the number of grades: ");
  21.             int numGrades = sc.nextInt();
  22.             int[] grades = new int[numGrades];
  23.  
  24.             for (int j = 0; j < numGrades; j++) {
  25.                 System.out.println("Enter grade: " + (j + 1) + ": ");
  26.                 grades[j] = sc.nextInt();
  27.             }
  28.             sc.nextLine();
  29.  
  30.             students[i] = new Student(name, grades);
  31.         }
  32.     }
  33.     public void displayStudents() {
  34.         System.out.println("Class: " + className);
  35.         for (Student student : students) {
  36.             student.displayInfo();
  37.             System.out.println("Average grade: " + student.calcAverage());
  38.         }
  39.     }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement