CSCI 132 Data Structures--Spring 2014

    Home | | Syllabus | | Assignments | | Lecture Notes

    Laboratory 1 Solutions

    Solution to student.cc code:

    /*******************************************************************
    * Name: Brenda Student
    * Date: 1/21/14
    * Course: CSCI 132
    * Assignment: Lab1
    * Instructor: Royden
    * Implementation: student.cc
    * Implementation File: This file contains the implementation for
    * all the member functions for the Student class, storing each course
    * in a separate variable.
    ********************************************************************/
    
    #include "student.h"
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    Student::Student( ) {
      //Precondition:
      //	None.
      //Postcondition:
      //	Initializes the Student's number of courses to 0,
      //	Initializes the Student's graduation date to 0,
      //	Initializes the Student's name to "John Doe"
    
      numCourses = 0;
      gradDate = 0;
      strcpy(studentName, "John Doe");
    } //end Student()
    
    Student::Student(/*in*/ char name[], /*in*/ int year) {
      //Precondition:
      //	name[ ] is an array of characters, less than 30 long, 
      //	containing the student's name.
      //	year is an integer representing the student's graduation date.
      //Postcondition:
      //	Initializes the Student's number of courses to 0,
      //	Initializes the Student's graduation date to year,
      //	Initializes the Student's name to the string contained in name.
      numCourses = 0;
      gradDate = year;
      strcpy(studentName, name);
    
    } //end Student(char, int)
    
    void Student::AddCourse( /*in*/ string9 courseName){
      //Precondition:
      //	courseName is a string of less than 10 characters
      //	There are less than 6 courses in the course list already.
      //Postcondition:
      //	The string, courseName, is added to the courselist.
    
      switch(numCourses) {
        case 0:
          strcpy(course1, courseName);
          break;
        case 1:
          strcpy(course2, courseName);
          break;
        case 2:
          strcpy(course3, courseName);
          break;
        case 3:
          strcpy(course4, courseName);
          break;
        case 4:
          strcpy(course5, courseName);
          break;
        case 5:
          strcpy(course6, courseName);
          break;
      } //end switch
          
      numCourses += 1;
    } //end AddCourse()
    
    void Student::ListInfo(void) const {
      //Precondition:
      //	A Student object has been initialized.
      //Postcondition:
      //    The function prints out the student's name and
      //	   graduation date, one each line.
      //    The student's name is printed in the form: 
      //      "Name: Jane Doe"
      //    The gradduation date is printed in the form:
      //      "Graduation Date: 2006"
      //    One blank line is printed after the information list.
    
      cout << "Name: " << studentName << endl;
      cout << "Graduation date: " << gradDate << endl;
      cout << endl;
    
    } //end ListInfo()
    
    void Student::ListCourses(void) const {
      //Precondition:
      //	A Student object has been initialized.
      //Postcondition:
      //	The courses in the course list are printed to the monitor.
      //    Courses are printed in the form: "Course 1: csci262"
      //	One course is on each line.
      //    One blank line is printed after the list of courses.
    
      for (int i = 0; i < numCourses; i++) {
        cout << "Course " << i+1 << ": ";
        switch(i){
          case 0:
            cout << course1;
    	    break;
          case 1:
            cout << course2;
    	    break;
          case 2:
            cout << course3;
    	    break;
          case 3:
            cout << course4;
    	    break;
          case 4:
            cout << course5;
    	    break;
          case 5:
            cout << course6;
    	    break;
        } //end switch statement
        cout << endl;
      } //end for loop
      cout << endl;
    
    } //end ListCourses()
    
    void Student::SetGradDate(/*in*/ int year) {
      //Precondition:
      //	year is an integer representing a date.
      //Postcondition:
      //	The graduation date for the student object is set to year.
    
      gradDate = year;
    } //end SetGradDate()
    
    
    
    

    Home | | Syllabus | | Assignments | | Lecture Notes