public class CoursePlanner
{
/** The list of enrolled courses, separated by a single space */
private String courseList; // To be initialized in part (a)
/** The total number of credit hours for the enrolled courses */
private int totalCredits; // To be initialized in part (a)
/**
* Processes course requests and builds the course list as described in part (a)
* Precondition: maxCredits > 0
*/
public CoursePlanner(int maxCredits)
{ /* to be implemented in part (a) */ }
/**
* Returns the next course name requested by the student
* or null if there are no more requests.
*/
public String getNextCourse()
{ /* implementation not shown */ }
/**
* Returns the number of credits for the specified course.
* Precondition: course is a valid course name.
*/
public int getCredits(String course)
{ /* implementation not shown */ }
/**
* Returns the number of courses in courseList that match the
* specified department prefix, as described in part (b)
* Precondition: prefix is not null.
*/
public int countCourses(String prefix)
{ /* to be implemented in part (b) */ }
/* There may be instance variables, constructors, and methods
that are not shown. */
}Example 1
| Method Call | Return Value | Action Taken |
|---|---|---|
| getNextCourse() | "CS101" | Check credits |
| getCredits("CS101") | 4 | Fits (4 <= 10). Add to list. |
| getNextCourse() | "MAT200" | Check credits |
| getCredits("MAT200") | 4 | Fits (4+4 <= 10). Add to list. |
| getNextCourse() | "ENG105" | Check credits |
| getCredits("ENG105") | 3 | Does not fit (8+3 > 10). Skip. |
| getNextCourse() | null | Stop processing. |
/** * Processes course requests and builds the course list as described in part (a) * Precondition: maxCredits > 0 */ public CoursePlanner(int maxCredits)
Example 1
| Method Call | Return Value |
|---|---|
| countCourses("CS") | 3 |
| countCourses("MAT") | 1 |
| countCourses("BIO") | 0 |
/** * Returns the number of courses in courseList that match the * specified department prefix, as described in part (b) * Precondition: prefix is not null. */ public int countCourses(String prefix)