UMBC CMSC 202, Computer Science II, Spring 1999,
Sections 0101, 0102, 0103, 0104
Project 1: Simulating a Print Queue
Due: Monday February 22, 1999
See Project Notes
Objective
The objectives of this project are 1) to practice coding in C++,
2) to practice using the documentation and naming standards for the
course, 3) to review pointer manipulations and linked lists, and
4) to write a program using an object-oriented approach.
Background
In this project you will simulate adding jobs to a print queue and
dispatching the jobs to one of three printers based on printer
workload. Some simple rules and assumptions will govern the operation
of the queue:
- The size of each print job will be specified in number of pages.
- Every page takes the same amount of time to print regardless of
content.
- All of the printers take the same amount of time to print one page.
- The time that it takes to remove a job from the queue and send it
to any of the printers is equal to the time that it takes to print
one page.
The following programming specifications apply to the print queue:
- Use a Queue (FIFO) data structure.
- Implement the Queue as a linked list.
- There is no restriction on the number of jobs that may be added
to the queue.
We will use C++ classes to simulate the print jobs, the print queue,
and the printers. The class declarations are given below:
class PrintJob {
private:
long _id;
int _num_pages;
PrintJob* _next_job;
public:
// Constructor
// Param id_num: Id Number for job
// Param page_count: # pages in job
PrintJob (long id_num, int page_count);
// Gets the print job's id number
// Returns: id number
long GetId();
// Gets number of pages in print job
// Returns: number of pages
int GetNumPages();
// Gets the job that follows this job
// in the print queue
// Returns: Pointer to next job or NULL
// if last job in queue
PrintJob* GetNextJob();
// Sets the next job pointer for this job
// Param next: Pointer to next job in queue
void SetNextJob (PrintJob* next);
};
class PrintQueue {
private:
char _name[21];
PrintJob* _head;
PrintJob* _tail;
public:
// Default constructor. Name is "Unnamed";
PrintQueue();
// Construct PrintQueue with specified name
// Param queueName: Name of the queue
// (20 characters max.)
PrintQueue (char* queueName);
// Add a job to the queue
// Param jobPtr: Pointer to the PrintJob
// to be added
void Enqueue (PrintJob* job);
// Remove a job from the queue
// Returns: Pointer to first job in queue
// Pre-condition: The queue must not be empty
PrintJob* Dequeue ();
// Check to see if queue is empty
// Returns: True if empty, false if not
int IsEmpty();
};
class Printer {
private:
char _name[21];
long _current_job_id;
int _total_pages;
int _pages_remaining;
public:
// Default constructor
// Sets printer name to "Unnamed"
Printer();
// Constructs printer with specified name
// Param printer_name: Name of printer (20 chars. max)
Printer (char* printer_name);
// Accept job from the print queue
// Parm job_ptr: Pointer to the job to be accepted
void AcceptJob (PrintJob* job_ptr);
// Print one page and decrement count of
// pages remaining to be printed
void PrintPage();
// Determine if printer is idle
// Returns: true if pages remaining is zero,
// otherwise false
int IsIdle();
};
Assignment
For this project, you will implement all the member functions of the
PrintJob, PrintQueue, and Printer classes.
Your main() should begin with the
statements shown below. You will need to write the necessary additional
statements that will produce the required output (also shown below).
There are to be no print statements in main(). All of the
output should be produced by print statements in appropriate member
functions.
PrintJob myJobs[10] = { PrintJob (100, 43),
PrintJob (101, 17),
PrintJob (102, 22),
PrintJob (103, 54),
PrintJob (104, 11),
PrintJob (105, 10),
PrintJob (106, 3),
PrintJob (107, 18),
PrintJob (108, 12),
PrintJob (109, 27) };
PrintQueue myQueue ("CMSC 202 Print Queue");
for (int i = 0; i < 10; ++i) {
myQueue.Enqueue (&myJobs[i]);
}
Printer myPrinters[3] = { Printer ("Printer #1"),
Printer ("Printer #2"),
Printer ("Printer #3") };
Expected Output:
Print Job #100 added to CMSC 202 Print Queue
Print Job #101 added to CMSC 202 Print Queue
Print Job #102 added to CMSC 202 Print Queue
Print Job #103 added to CMSC 202 Print Queue
Print Job #104 added to CMSC 202 Print Queue
Print Job #105 added to CMSC 202 Print Queue
Print Job #106 added to CMSC 202 Print Queue
Print Job #107 added to CMSC 202 Print Queue
Print Job #108 added to CMSC 202 Print Queue
Print Job #109 added to CMSC 202 Print Queue
Print Job #100 accepted by Printer #1
Print Job #101 accepted by Printer #2
Print Job #102 accepted by Printer #3
Print Job #101 finished (17 pages)
Print Job #103 accepted by Printer #2
Print Job #102 finished (22 pages)
Print Job #104 accepted by Printer #3
Print Job #104 finished (11 pages)
Print Job #105 accepted by Printer #3
Print Job #100 finished (43 pages)
Print Job #106 accepted by Printer #1
Print Job #105 finished (10 pages)
Print Job #107 accepted by Printer #3
Print Job #106 finished (3 pages)
Print Job #108 accepted by Printer #1
Print Job #108 finished (12 pages)
Print Job #109 accepted by Printer #1
Print Job #107 finished (18 pages)
Print Job #103 finished (54 pages)
Print Job #109 finished (27 pages)
Implementation Issues
You must use C++ for this project. You must not change the class
declarations given above in any way. Your print queue must be
implemented as a linked list. Be sure that you have read the
Project Organization page.
For this project, you should submit a makefile and seven source files
named as follows:
- Proj1.C (This source file contains main().)
- PrintJob.H
- PrintJob.C
- PrintQueue.H
- PrintQueue.C
- Printer.H
- Printer.C