Program construction
The completed programs to be submitted to the charon system by the date indicated.
C++ Familiarisation
You should expect to finish this work by the end of week 3 (Now week 4 due to non availability
of the Borland C++ compiler in the labs) at the latest. The completed programs to be submitted to
the charon system by this date.
A non extendable extension for this work is granted till the end of week 4 (Now week 5).
However, for your own personal satisfaction the charon system will still judge your work after this
date.
Exercise 1.1
Hello world
A program to write out the greeting message “hello world” to the terminal.
Input
Expected output
Hello world
Key points
A first C++ program
Using C++ Builder
Exercise 1.2
Times Table
A program to print out the 5 times table.
Input
Expected output
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50
11 * 5 = 55
12 * 5 = 60
Key points
Using loops
Output of numbers and text
Exercise 1.3
Times Table the general case
A program to print out any times table. The input to this program is an integer number specifying which time table is to be
The Term Paper on The UG And PG Time Table
INDIAN INSTITUTE OF TECHNOLOGY DELHI ACADEMIC & EXAMINATION-(UG, PG) Date: 2 July 2012 Sub.: UG and PG Time Table, 1st semester 2012-2013 1. The UG and PG Time Table for 1st semester 2012-2013 is shown in the following pages. Time-table for all the first year courses is given in the first four pages (M courses and P courses). 2. Starting time has been indicated by first letter of the day and ...
printed.
Hint:
int num; std::cin >> num;
would read a decimal number into the variable num.
Input
Expected output
2
1 * 2 = 2
2 * 2 = 4
3 * 2 = 6
4 * 2 = 8
5 * 2 = 10
6 * 2 = 12
7 * 2 = 14
8 * 2 = 16
9 * 2 = 18
10 * 2 = 20
11 * 2 = 22
12 * 2 = 24
Key points
Using loops
Output of numbers and text
Input of numbers in C++
Exercise 1.4
Order
A program to read in three integer numbers and print them out in ascending order. For example, if the numbers input were 10
7 8 then your output would be:
7 8 10
Input
Expected output
10 7 8
7 8 10
Key points
Selection constructs
Input/output of numbers in C++
To gain familiarity with nested selection statements.
Exercise 1.5
Calculator
A program to implement a desk calculator that has no operator precedence. For example, if the formula 2 + 3 * 4; where
input the result would be 20. Likewise if the formula 4 + 3 * 2; where input the result would be 14.
Input
Expected output
2 + 3 * 4;
Answer = 20
3 + 4 * 2 + 1;
Answer = 15
To gain familiarity with loops and selection statements.
Hint: A syntax diagram for the expression is shown below:
+———+ +——+
—————-| Number |————-| ; |—
/ +———+ +——+
| |
| +———+ |
——| + – * / |——/
+———+
This shows that a valid expression is a number followed by either a ‘;’ or the operator ‘+’, ‘-‘, ‘*’, ‘/’. If an operator is found
then the next item expected is a number. If the next item found is a ‘;’ then the expression is finished.
Key points
Revision of loops and selection constructs in C++
Adult Learning Skills Stress Life Work
Do you feel stress in your life? Does this affect the way you live and work? Many things currently going on in one's life, such as work, health, family and finances, can cause stress. It is how we individually identify the root cause and begin working on managing them effectively. As adult learners, there are various aspects of our work life that cause each of us some form of stress. We discovered ...
Input of characters and numbers
Syntax diagrams
Building a class
Exercise 2.1
You should expect to finish this work by the end of week 6 (Now week 7) at the latest. The
completed programs to be submitted to the charon system by this date.
A non extendable extension for this work is granted till the end of week 7 (Now week 8).
However, for your own personal satisfaction the charon system will still judge your work after this
date.
The exercise is to create the class Employee_pay that can be used as part of a larger program to manage the pay of
employees in a SME (Small to Medium Enterprise).
The responsibilities of the class Employee_Pay are as follows:
Method
Responsibility
set_hourly_rate
Sets the hourly rate.
add_hours_worked
Accumulates the number of hours worked so far.
pay
Delivers the pay for this week. Tax is deducted at 20% of the
total pay.
reset
Resets the hours worked back to zero.
hours_worked
Delivers the number of hours worked so far this week.
pay_rate
Delivers the hourly pay rate.
The partial C++ specification of this class is shown below:
class Employee_Pay
{
public:
Employee_Pay();
void add_hours_worked( int number_of_hours_worked );
void set_hourly_rate( float hourly_pay_rate );
void reset();
float pay();
int hours_worked();
float pay_rate();
private:
};
You may wish to use the following test program to help test the class that you have created.
#include
#include
void main()
{
std::cout << std::setiosflags( std::ios::fixed ); // Format x.y
std::cout << std::setiosflags( std::ios::showpoint ); // 0.10
std::cout << std::setprecision(2); // 2 dec places
Employee_Pay me;
me.set_hourly_rate( 5.00 );
std::cout << "Week 1" << "n";
me.add_hours_worked( 5 );
me.add_hours_worked( 5 );
std::cout << "Pay for week 1 : " << me.pay() << "n";
std::cout << "Week 2" << "n";
The Homework on Teen Workers Working Work Hours
Teen Workers Fifteen years old and working seems to be becoming a norm and in fact there are many teenagers younger than fifteen who are already working at paying jobs. Some of these students are as young as 12 years old. More than half of the secondary school students have paying jobs. This number grows each grade level the student goes up. The number of hours also rises along with the grade ...
me.reset();
me.add_hours_worked( 10 );
me.add_hours_worked( 10 );
std::cout << "Pay for week 2 : " << me.pay() << "n";
}
Key points
The class construct in C++ (Class, Object, Message, Method)
Access to components of a class (private, public)
Separating specification from implementation
Testing of a class
Submit only the class Employee_Pay (specification and implementation) to the charon system for testing. Do not submit your
test program.
Exercise 2.2
The exercise is to create the class Better_Employee_Pay that can be used as part of a larger program to manage the pay of
employees in a SME.
The responsibilities of the class Better_Employee_Pay are all the responsibilities of the class Employee_Pay plus the following
responsibilities:
Method
Responsibility
set_overtime_pay
Sets the overtime pay rate.
normal_pay_hours
Sets the number of hours in a week that have to be worked
before the overtime pay rate is applied.
pay
Delivers the pay for this week. This will consist of the hours
worked at the normal pay rate plus the hours worked at the
overtime rate. Tax is again deducted at 20%.
The partial C++ specification of this class is shown below:
class Better_Employee_Pay : public Employee_Pay
{
public:
Better_Employee_Pay();
void set_overtime_pay( float );
void set_normal_pay_hours( int );
float pay();
private:
};
You may wish to use the following test program to help test the class that you have created.
#include
#include
void main()
{
std::cout << std::setiosflags( std::ios::fixed ); // Format x.y
std::cout << std::setiosflags( std::ios::showpoint ); // 0.10
std::cout << std::setprecision(2); // 2 dec places
Better_Employee_Pay me;
me.set_hourly_rate( 5.00 );
me.set_normal_pay_hours( 40 );
me.set_overtime_pay( 10.00 );
std::cout << "Week 1" << "n";
me.add_hours_worked( 5 );
me.add_hours_worked( 5 );
std::cout << "Pay for week 1 : " << me.pay() << "n";
std::cout << "Week 2" << "n";
me.reset();
me.add_hours_worked( 10 );
me.add_hours_worked( 10 );
me.add_hours_worked( 10 );
me.add_hours_worked( 10 );
std::cout << "Pay for week 2 : " << me.pay() << "n";
std::cout << "Week 3" << "n";
me.reset();
me.add_hours_worked( 10 );
me.add_hours_worked( 10 );
me.add_hours_worked( 10 );
me.add_hours_worked( 10 );
me.add_hours_worked( 10 );
std::cout << "Pay for week 3 : " << me.pay() << "n";
std::cout << "Week 3 [if no overtime pay]" << "n";
std::cout << "Pay for week 3 : " << ((Employee_Pay)me).pay() << "n";
}
Key points
Base and derived class (Superclass and subclass)
Inheritance
Re-use of existing code
Overriding of a method
Access to components in a base class
Submit only the classes Employee_Pay and Better_Employee_Pay (specification and implementation for both) to the charon
system for testing. Do not submit your test program.
Using OO techniques in program construction
Exercise 3.1
You should expect to finish this work by the end of week 9 at the latest. The completed programs
to be submitted to the charon system by this date.
A non extendable extension for this work is granted till the end of week 10. However, for your
own personal satisfaction the charon system will still judge your work after this date.
The program that is to be created is the game of draughts that allows a game to be played between two human players.
The first part of the construction of this program is to build the class Move that allows a move to be recorded and individual
parts to be extracted. By building this class the construction of the rest of the game is simplified.
The specification of the class Move is as follows:
class Move
{
public:
enum Move_type { DRAW = -2, RESIGN = -1, END = 0 };
Move( Counter c = Counter(‘ ‘) );
void add( const int row, const int col );
bool special_move();
char colour();
int row();
int column();
bool to_be_made();
void start();
void next();
int part_of_move();
void from();
void to();
private:
};
To build this class you will also need to write the class Counter. However, an implementation of this class sufficient to allow
you to write the class Move is given below:
class Counter
{
public:
Counter( char c = ‘ ‘ )
{
}
char colour()
{
return ‘ ‘;
}
};
A simple test program for this class is shown below:
#include
int main()
{
Move move( Counter(‘x’) );
move.add( 8, 1 );
move.add( 6, 3 );
move.add( 4, 5 );
int part_no = 1;
move.start(); //Set start
while ( move.to_be_made() ) //While parts
{
std::cout << "Move part " << part_no++;// Part #
move.from(); // From
std::cout << " From " << move.row() << // extract
“, ” << move.column();
move.to(); // To
std::cout << " To " << move.row() << // extract
“, ” < std::cout << "n";
move.next(); // next
} //End while
}
When run the output from the test program is as follows:
Move part 1 From 8, 1 To 6, 3
Move part 2 From 6, 3 To 4, 5
The description of the game of draughts together with a break down into individual classes is contained here.
Key points
Using a UML style design
Building and testing a component of a program
Using arrays/collection classes
Test your class using the following test harness. After you have tested your program submit the class Move only to the charon
system.
Putting it all together
Exercise 4.1
This exercise is to be complete the whole game of draughts. The completed solution is to be demonstrated in the tutorial slot
of week 13.
Description of a solution is here
Partial solution of the draughts game is
Key points
From an OO design to implementation
Use of UML
Classes, inheritance, code re-use
You are to demonstrate your completed solution in the tutorial slots in week 13. The complete portfolio of work is to be
handed in for marking at the end of week 13 also.
Remember however, that you must have submitted solutions for exercises 1 – 3 to the charon system by the due dates given
above.
M.A.Smith University of Brighton. Created August 1999 Last modified August 1999 Version 1.0
Comments, suggestions, etc. [email protected] [Home page]
Printed / Displayed Sunday, April 30, 2000 12:44:55Resources