THINK FIRST·CODE LATER

← All labs

Composition and aggregation: course roster

Problem

Model courses and students:

  • Student (name, number of enrolled courses) — students exist independently of courses;
  • MeetingTime (day, start hour) — created by the course and never shared;
  • Course (code, capacity, a MeetingTime it creates in its constructor — composition — and an ArrayList<Student> of students passed in — aggregation), with enroll(Student) (refuse when full or already enrolled), cancel() and toString() → CPS2231 (Tue 10:00) 2/3: Lina Omar.

CourseRoster.main reads course CODE DAY HOUR CAPACITY, enroll NAME CODE and cancel CODE until the end of input (cannot enroll Mei in CPS1231 when refused). It prints the remaining courses and, for each student, Lina takes 1 course(s). A cancelled course disappears, but its students do not.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l14-3-data-1.txt

course CPS2231 Tue 10 3
course CPS1231 Mon 8 1
enroll Lina CPS2231
enroll Omar CPS2231
enroll Lina CPS1231
enroll Mei CPS1231
What your program must print for l14-3-data-1.txt
cannot enroll Mei in CPS1231
CPS2231 (Tue 10:00) 2/3: Lina Omar
CPS1231 (Mon 8:00) 1/1: Lina
Lina takes 2 course(s)
Omar takes 1 course(s)
Mei takes 0 course(s)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l14-3-data-1.txt

Windows PowerShell does not understand <:

Get-Content l14-3-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.