Programming Assignment 1 Read Assignment Understand the overall

  • Slides: 17
Download presentation
Programming Assignment 1 • Read Assignment • Understand the overall problem • Identify requirements:

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 1

st Identify Requirements (1 Query) • Input file of messages is arranged chronologically (first-come

st Identify Requirements (1 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 2

rd Identify Requirements (3 query) • 3 rd query DELETE-MOST-RECENT-MESSAGE • This query can

rd Identify Requirements (3 query) • 3 rd query DELETE-MOST-RECENT-MESSAGE • This query can be done by the pop() operation on stacks. • Clearly, the 3 rd query is another clue that a stack of messages is a wise choice of a structure CS 103 3

th Identify Requirements (4 query) • 4 th query DISPLAY k. • It suggests

th Identify Requirements (4 query) • 4 th 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 print. Body( ) which prints out the body of the message. CS 103 4

nd Identify Requirements (2 query) • 2 nd query LIST-MESSAGES-FROM address • This suggests

nd Identify Requirements (2 query) • 2 nd 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 * get. Sender( ) which returns the “From” value of the message. CS 103 5

Identify Requirements 1 st Query Revisited • 1 st Query: LIST-MESSAGES-BY-DATE • The listing

Identify Requirements 1 st Query Revisited • 1 st 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 6

Identify Requirements 1 st Query Revisited (contd. ) • To be able to print

Identify Requirements 1 st 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 print. Header( ) which prints out the header of the message in the prescribed format: sender. Address##subject##date CS 103 7

The Message Class • Any stack must be a stack of elements of some

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 8

What Goes into Defining a New Class • When you define a new class,

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 9

The Message Class (Members) • Our earlier requirement analysis led us to conclude that

The Message Class (Members) • Our earlier requirement analysis led us to conclude that the Message class must have the following public methods/operations: – void print. Body( ) – char * get. Sender( ) – void print. Header( ) • The methods require the class to have 4 data private fields: sender, subject, date, and body. CS 103 10

The Message Class (Data type of the member variables) • The sender, being a

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 11

Data type of the member variables (Contd. ) • The body, being a string

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 12

Implementation of the 3 methods of the Message Class • It should be now

Implementation of the 3 methods of the Message Class • It should be now straightforward to implement – void print. Body( ) – char * get. Sender( ) – void print. Header( ) • print. Body( ) and print. Header( ) should use cout • get. Sender( ) returns the sender variable. CS 103 13

The Message Stack • The Message stack is really a modification of the stack

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 14

The IO Needed for this Project • You need to be able to read

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 15

The IO Needed for this Project (Contd. ) • To read the line FROM:

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 16

Reading the query file • Declare ifstream in(“queriesfile. txt”); • Declare char * command[30],

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 17