0% found this document useful (0 votes)
76 views

Lab 10

Uploaded by

S CHOWDHURY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Lab 10

Uploaded by

S CHOWDHURY
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Lab 10 - Exercise and Lab Quiz 4 Practice

There is no Lab Assignment 10 to submit. Yay!


Just our Lab Quiz 4 to practise!

Enjoy! 
Lab 10 - Learning Outcomes
At the end of this Lab 10, we will be able to …
 Solve a problem by developing a C++ program along with some classes,
following the four steps of the software development process.
 Use the class construct to develop a Stack data collection class and a
StackNode class.
 Differentiate between private and public access modifiers and select the most
appropriate access modifiers when developing classes.
 Write constructors, destructor, getters, setters and class-specific methods
(push(), pop(), peek()) for a Stack data collection class.

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 2


In order to get ready for this Lab 10
 Have a read through our lecture notes for Lecture 31 - Stack -
Designing/Implementing a Stack in C++ and listen to its recording.
 Have a look at the Demo code posted: for example, Stack.h.
 Feel free to make use of them in this lab, but keep in mind that all of these
posted code files may not be fully implemented.

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 3


Setup instructions
 Within our sfuhome/cmpt-125 directory …
 Create a Lab10 folder.
 cd into sfuhome/cmpt-125/Lab10.
 Download the given file posted on our course web site into this current
folder. We are now ready to go!
 Make sure we save all the files needed for our Lab 10 in this folder:
sfuhome/cmpt-125/Lab10.
 Enjoy!

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 4


Also, have a look at the GPS link on the
menu of our course web site! We must
satisfy these requirements as well.

Step 1 - General Requirements


 For all programs we shall write this semester, here is a list of general requirements which must
always be satisfied, unless there is a requirement in a particular Lab Exercise that explicitly states
otherwise.
1. If we need to use a literal value (i.e., “hard code” a literal value), we must declare it as a constant.
2. We must descriptively name
 our program (implementation file, i.e., the file with a .cpp extension) and executable file
 our constants, local variables, parameters and data members (x, y, a, b are usually not descriptive variables names)
 our functions/methods
3. Our user interface must clearly describe the expected input to the user (which input we are expecting the use to
enter, its format and range of value, if possible) and the resulting output.
4. We must comment our programs.
5. We must indent the statements in our programs.
6. We must include a header comment block at the top of each of our source files listing the name of the source file, a
description of the program found in the source file, the name of the author of the program (this should be you)
and the date of creation of the source file/program.
7. We must give our source files a .cpp extension and our header files a .h extension (when writing C++ code).
8. We must not use goto statements.
9. We must construct proper conditions for our conditional and iteration statements, unlike: while(1).

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 5


Let’s start!

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 6


Lab Exercise
Step 1 - Problem Statement and Requirements:
Write a C++ program that determines when a given word is a palindrome.

Sample Input 1:
Please, enter a word: rotator
Sample Output 1:
“rotator” is a palindrome.
Sample Input 2:
Please, enter a word: Yay! It works!
Sample Output 2:
“Yay! It works!” is not a palindrome.
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 7
Lab Exercise
Step 1 - Problem Statement and Requirements:
 There are many ways of solving this problem. In this Lab 10 Exercise, your
solution must make use of a Stack data collection class.
 This Stack data collection class must use a linked list as its data structure.
 This linked list must be made of Node (or StackNode) objects instantiated from
a Node (or StackNode) class. Therefore we cannot use typedef nor struct.
 The data members of our classes must be private, expect for the Node (or
StackNode) class where public data members are acceptable.
 The method members of our classes must be public. We can also have a few
private method members if necessary.

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 8


Lab Exercise
Step 1 - Problem Statement and Requirements:

 In solving this problem, we must create the following files:


 a Stack.h file and a Stack.cpp file (a Stack class)
We can either create a Node class or a StackNode class (Node.h or

StackNode.h file and Node.cpp or StackNode.cpp file) or create a
Node class or StackNode class inside the private section of the Stack
class
 a testDriver.cpp to test our Stack class
 an application file containing the main function and named
palindromeChecker.cpp
 a makefile
Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 9
Step 2 - Design
Before jumping into Step 3 – Implementation, let’s take the time to think how we
will be solving this problem, i.e., what is the algorithm (the steps) our main
function (in the application program) will perform when solving this problem.

Algorithm:

Note that the steps in our algorithm can be used as comments in our program!

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 10


Step 2 – Design
Feel free to use the design we saw in our Lecture 31:

Attributes of a Stack:
 Data structure (in this Lab 10: linked list)
Operations of a Stack:
 push: Insert an element onto the top of the stack
 pop: Remove the top most element of the stack
 peek: Retrieve the top most element of the stack (but do not remove the
element)
 isEmpty: Is the stack empty?

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 11


Reminder about Stack’s class
invariant
 It is the implementation of the methods of our Stack data collection class that
ensures that the class invariant (LIFO or FILO) holds true!
 We write our code such that …
 push method only pushes an element at the top of the Stack

 pop method only pops the element on the top of the Stack

 peek method only peeks at the element on the top of the Stack

 Therefore it is important to clearly define and indicate where the top of the
Stack is located.

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 12


Step 3 – Implementation
 Linked list-based implementation of a Stack data collection class
head

3 7 5 8
top? top?

Is the top of the Stack at the front


or at the end of the linked list.
How to decide?
Hint: Let’s recycle as
Answer: Select the location of top
much code as we can
such that the operations of our
from our Lab 8! Lots of
Stack data collection are as time
it is useful in this Lab 10.
efficient as possible -> O(1).

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 13


Finally …
 Remember, this Lab 10 is a practice lab for Lab Quiz 4 . So there is no
submission.
 However, we are strongly encourage to show our code to the instructor
and/or TAs if we wish to receive feedback or have questions.

 Enjoy!

Copyright © Anne Lavergne, School of Computing Science, Simon Fraser University 14

You might also like