Graderpractice for programmers

273387 MAD 63 - Mini Test 2

Awaiting submission
Write a class named Student with 2 fields (class variables) and a constructor to set the 2 fields.
- name: String
- graduate: Bool

Test code:

let s1 = Student(name: "Harry Potter", graduate: false)
print(s1.name)  // Harry Potter

Submit the class file only. Do not submit the test code.

Awaiting submission
Write a class named University with 2 fields (class variables) and a constructor to set the name field. By default, the students is an empty list. Add a function/method for numberOfStudents that returns an Int.
- name: String
- students: array of Student
- numberOfStudents(): Int

Test code:

let u1 = University(name: "Hogwarts")
print(u1.name)  // Hogwarts
print(u1.numberOfStudents())  // 0

Submit the Student AND University classes in a single file only. Do not submit the test code.

Awaiting submission
Add a function/method to University that adds a student to the array of students.

Test code:

let u1 = University(name: "Hogwarts")
u1.add(student: Student(name: "Harry Potter", graduate: true))
print(u1.numberOfStudents())  // 1
u1.add(student: Student(name: "Cho Chang", graduate: false))
print(u1.numberOfStudents())  // 2

Each time a new student is added, the number of students should increase by 1.

Awaiting submission
Add a function/method to the University class named numberOfGraduates which returns the number of students who are graduates (graduate=true).

Test code:

let u1 = University(name: "Hogwarts")
u1.add(student: Student(name: "Harry Potter", graduate: true))
u1.add(student: Student(name: "Ron Weasley", graduate: true))
u1.add(student: Student(name: "Cho Chang", graduate: false))
print(u1.numberOfGraduates()) // 2

Submit the class file only. Do not submit the test code.

Awaiting submission
Add a function/method to the University class named currentStudentNames which returns the names (String) of the current students (graduate=false).

Test code:

let u1 = University(name: "Hogwarts")
u1.add(student: Student(name: "Harry Potter", graduate: true))
u1.add(student: Student(name: "Ron Weasley", graduate: true))
u1.add(student: Student(name: "Luna Lovegood", graduate: false))
u1.add(student: Student(name: "Ginny Weasley", graduate: false))
for name in u1.currentStudentNames() {
    print(name)
}
// Prints:
// Luna Lovegood
// Ginny Weasley

Submit the class file only. Do not submit the test code.