Download presentation
Presentation is loading. Please wait.
1
Programming Assignment 1
Read Assignment Understand the overall problem Identify requirements: Focus on the queries to Determine what kind of actions are needed on what data Identify the objects/structures and which of the action to be done by which of the objects CS 103
2
Identify Requirements (1st Query)
Input file of messages is arranged chronologically (first-come first-added) First query requires that the messages be read in the reverse order (last-come first-out) This suggests that the messages should be stored in a stack of messages Each message should be object (more about it later) CS 103
3
Identify Requirements (3rd query)
3rd query DELETE-MOST-RECENT-MESSAGE This query can be done by the pop() operation on stacks. Clearly, the 3rd query is another clue that a stack of messages is a wise choice of a structure CS 103
4
Identify Requirements (4th query)
4th query DISPLAY k. It suggests that we need an additional operation for our stack: Message get(int k); which returns the kth message from the top. The Message class must provide a method void printBody( ) which prints out the body of the message. CS 103
5
Identify Requirements (2nd query)
2nd query LIST-MESSAGES-FROM address This suggests the stack should provide a method void report( char[] sender) which reports all the message from the specified sender To tell who the sender of a message is, the Message class must provide a public method char * getSender( ) which returns the “From” value of the message. CS 103
6
Identify Requirements 1st Query Revisited
1st Query: LIST-MESSAGES-BY-DATE The listing requires that we get all the messages in the stack in stack order, but not pop the messages off the stack This suggests another new method to add to your stack of messages: void report( ) which prints out the headers of messages in the stack from top to bottom. CS 103
7
Identify Requirements 1st Query Revisited (contd.)
To be able to print out the “header” of each message on the stack, the Message class should provide a method like: void printHeader( ) which prints out the header of the message in the prescribed format: senderAddress##subject##date CS 103
8
The Message Class Any stack must be a stack of elements of some type.
The element type can be built-in (such as int, char, bool, long, float, double) or user-defined In this project, as we have seen, it makes perfect sense to have each element be of type “Message” that the programmer has to define Nearly all new types you need to define are classes Therefore, we need to define a Message class CS 103
9
What Goes into Defining a New Class
When you define a new class, you need to determine: What operations (methods) the class should do for you, by examining the problem requirements What data should be packaged in the class so that the aforementioned operations can be implemented. CS 103
10
The Message Class (Members)
Our earlier requirement analysis led us to conclude that the Message class must have the following public methods/operations: void printBody( ) char * getSender( ) void printHeader( ) The methods require the class to have 4 data private fields: sender, subject, date, and body. CS 103
11
The Message Class (Data type of the member variables)
The sender, being a string of at most 50 characters, can be char sender[50]; The date is a string of 10 characters, so it can be char date[10]; Note that because we do not need to do anything with the dates other than printing them, this type is adequate The subject, being a string of at most 60 characters, can be char subject[60]; CS 103
12
Data type of the member variables (Contd.)
The body, being a string of unspecified maximum length, should be declared as a char pointer: char *bodyptr; When a message is read from the message input file, a dynamic character array can be created bodyptr = new char[length]; of the appropriate length that will be determined from the number of characters present in the body of the message CS 103
13
Implementation of the 3 methods of the Message Class
It should be now straightforward to implement void printBody( ) char * getSender( ) void printHeader( ) printBody( ) and printHeader( ) should use cout getSender( ) returns the sender variable. CS 103
14
The Message Stack The Message stack is really a modification of the stack of integers that we developed in Lecture 3. Change the data type from int to Message. Add the methods that we mentioned earlier: Message get(int k); void report( ) // reports all the messages void report(char[] sender) // reports all messages from the specified sender CS 103
15
The IO Needed for this Project
You need to be able to read the files messagesfile.txt and queriesfile.txt For file IO, add #include <fstream> to your program file (at the top) Declare ifstream in(“messagesfile.txt”); CS 103
16
The IO Needed for this Project (Contd.)
To read the line FROM: sender char FROM[4], sender[50]; in>>FROM; in.getline(sender,50); To read the line DATE: date char DATE[4], date[10]; in>>DATE; in.getline(date,10); To read the line SUBJECT: subject char SUBJECT[4], subject[60]; in>> SUBJECT; in.getline(subject,60); CS 103
17
ifstream in(“queriesfile.txt”);
Reading the query file Declare ifstream in(“queriesfile.txt”); Declare char * command[30], sender[50]; int k; in>> command; if (strcmp(command,”LIST-MESSAGES-FROM”) in>>sender; if (strcmp(command,” DISPLAY”) in>>k; CS 103
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.