CSCI 132 Data Structures--Spring 2014

    Home | | Syllabus | | Assignments | | Lecture Notes

    Laboratory 1
    Due at the beginning of class, Wednesday 1/29


    Note: In this class, part of your lab and homework grade will reflect programming style. You must have consistent indentation, program prologues and program comments where appropriate. Please review the Style Guide on the notes page for guidelines on comments and indentation.


    In today's lab you will be practicing programming objects in C++. In lecture we developed the specification of the Student Class, wrote some client code that used an object of the Student class type and wrote the implementation of some of the functions. Today you will be filling in some of the function implementations and writing some new client code.

    The code that was presented in class is available in the directory:

      ~csci132/labs/lab1

    Start the lab as follows:

    • Log onto your radius account.
    • Create a new directory that you will use to keep your lab work. Name it with an appropriate name, such as "labs". You create a new directory in UNIX with the mkdir command:
        mkdir labs
      NOTE: You may already have a directoy named "labs" from csci131. You should change the name of that directory to something else before creating a new labs dirctory. Change the name by typing:
        mv labs csci131_labs
    • Protect your lab directory from other people that might want to snoop by typing:
        chmod 700 labs
    • Verify that your labs directory is protected by typing: ls -l
      The labs directory should be listed with the prefix:
        drwx------
      This means that you can read write and execute files in that directory, but no one else can.
    • Copy the code for lab1 into your labs folder by typing:
        cp -r ~csci132/labs/lab1 labs/.
    • Change directories into your new lab1 directory by typing:
        cd labs/lab1
    • Type ls to verify that the lab1 directory contains the following files:
      • student.h
      • student.cc
      • registerStudent.cc
    • Start up an emacs window.
    • Open the file, student.h, by typing: <CTRL>x <CTRL>f
      Fill in the Find file command so it reads: Find file: ~/labs/lab1/student.h
    • Use the student.h file to answer the following questions:

    Print out the lab 1 answer sheet. On this sheet, which is to be handed in, answer the following questions:

    1. The Student class had 6 member functions. List them. (include return types and parameter lists):

    2. The Student class has 4 data members. List them (include their type).

    3. In your own words, briefly describe what the key words public and private indicate.

    4. Open the student.cc file in your emacs window. Notice that several of the functions have no code in them. These are called "stubs" and are used to test programs that are in the development process. Which of the functions in the Student class are stubs and which have been implemented?

    5. Open the registerStudent.cc file in your emacs window. What is the name of the object of type Student that is created in this client code?

    6. What member functions are invoked for the Student object?

    Compiling multiple files.
    Type the following to compile the registerStudent program:

      <META>x-compile

    Delete the "Make -k" and fill in the command line to read:

      Compile command: g++ -g -Wall registerStudent.cc student.cc -o register

    From a command tool window, run the program by typing:

      ./register

    Follow the prompts in the program until it finishes, then answer the following question on your answer sheet:

    7. What does the program do when you first run it?

     

    Implementing the class functions
    Fill in the function stubs in the student.cc file so that they perform as specified in the pre- and post- conditions. Re-compile the code as above and run the new register program. Answer the following question on your answer sheet:

    8. What does the program do after you have implemented all the class functions?

     

    Modifying the client code
    Open the registerStudent.cc file. Make the following changes:

    • Modify the code to add a second student object named "greatStudent". When declaring this object, use the second constructor method (the one that takes 2 parameters) to specify the studentName as your own name and the student graduation year as your own graduation year.
    • Use the AddCourse function to add your current courses to the class list. (Don't add more than 6 courses and make sure the course labels are less than 10 characters. They should be designated in the CSCI132 form.
    • List out the student info and the course list for the greatStudent object. List these after the information for newStudent is listed.
    • Compile and run your program to make sure that it works correctly.
      Note: you may get some warnings on compilation, saying:
      	     warning: deprecated conversion from string constant to 'char*'
      	
      You may safely ignore those.

    Data Abstraction
    You have just been hired to work for the firm "Dilbert Industries" and your new boss knows just enough computer programming to make things really difficult for you. He insists that you should store all the names of courses in individual variables instead of in an array as is given above. Your first assignment is to rewrite the code for student.h and student.cc so that the course names are stored in separate variables. It is important that the implementation be such that the changes are invisible to the client code. That is, registerStudent.cc should run exactly the same way, with no changes. Here's what you need to do:

    • Make a copy of the current student.h file named student1.h by typing:
        cp student.h student1.h
    • Similarly, make a copy of the current student.cc file named student1.cc
    • Modify the student.h code so that instead of an array named "courses", there are 6 variables named course1, course2, course3, etc.
    • Modify the student.cc code so that all the member functions satisfy the same pre and post-conditions as before. You will only have to modify functions that deal with the list of courses.
    • Test your code by recompiling with the newly modified student.h and student.cc. The register program should run the same way it did before.

    What To Turn In.

    • The written answers to the 8 questions above.
    • A printed listing of each of the five files, student1.h, student.h, student1.cc, student.cc, registerStudent.cc. Be sure to label each file clearly!
    • Submit the soft copies of your files by typing:
        ~csci132/bin/submit csci132 lab1

    Be sure that the program prologue for each file contains your name, course, lecture section, date and purpose of the program or a description of the contents of the file (e.g. specification of the Student class).

    Reminder.
    Be sure to save a copy of each file in your labs directory. It is your responsibility to keep the copies of all programs written for this course until your graded assignment is returned.


    Home | | Syllabus | | Assignments | | Lecture Notes