In this milestone we will create a Course class to represent a course and display its information on the screen. We will create two global functions that will validate the courses in a schedule file and store it in an array of Course objects, and display information from an array of Course objects.

Engineering · College · Mon Jan 18 2021

Answered on

To complete this milestone, you need to follow these steps:

1. Define the `Course` class. 2. Create global functions `validateCourses` and `displayCourses`.

Here's a basic outline of how you might write the `Course` class and its methods in a programming language like C++ or Java:

**Step 1: Define the Course class.** ```cpp class Course { private: string courseName; string courseCode; int credits; // Other attributes can be added here, like instructor, schedule, etc.

public: Course(string name, string code, int creds) { courseName = name; courseCode = code; credits = creds; }

// Getter and setter methods for the attributes. void displayCourseInfo() { cout << "Course Name: " << courseName << endl; cout << "Course Code: " << courseCode << endl; cout << "Credits: " << credits << endl; } }; ```

**Step 2: Create global functions.** For this part, let's first consider what your "schedule file" looks like. It may be a CSV or a plain text file where each line represents a course. We'll assume it's a plain text file for simplicity.

```cpp bool validateCourses(const string& scheduleFilename, Course courses[], int& courseCount) { ifstream file(scheduleFilename);

if (!file.is_open()) { return false; }

string line; while (getline(file, line)) { // Parse each line to construct Course object // For simplicity, assume each line contains courseName, courseCode, credits separated by commas stringstream ss(line); string name, code, creditStr; getline(ss, name, ','); getline(ss, code, ','); getline(ss, creditStr, ','); int credits = stoi(creditStr); // Construct a Course object with this data. courses[courseCount++] = Course(name, code, credits); }

file.close(); return true; }

void displayCourses(Course courses[], int courseCount) { for (int i = 0; i < courseCount; ++i) { courses[i].displayCourseInfo(); cout << endl; // Adds an empty line between courses. } } ```

**Using these functions:** You'd use `validateCourses` to read from your schedule file and populate an array of `Course` objects. After validation, call `displayCourses` to iterate over the array and show each course's information.

Note: This is a simplistic implementation. Error checking, memory management (if using dynamic arrays or vectors), more complex parsing for courses with various attributes, file format validation, etc., would need to be considered for a more robust program.

Extra: In object-oriented programming (OOP), classes are used as blueprints for creating objects that encapsulate data and functionality. The `Course` class, in this context, represents the concept of an academic course.

Global functions, although less encapsulated, are useful when you need to perform operations that don't necessarily belong to a single object or when dealing with procedural programming.

Validation is a critical aspect of programming. It ensures that the data with which your program operates is correct and sensible. It typically involves checking user inputs, file formats, and contents against expected formats and constraints.

An array is a collection of items stored at contiguous memory locations. In the context of this project, an array of `Course` objects is used to store multiple courses. Remember that in many modern programming languages, you would rather use a dynamic array or a list type like `std::vector` in C++ or `ArrayList` in Java, as they provide more flexibility and safety.

Related Questions