Labs 1-8


DEPARTMENT OF APPLIED SCIENCE AND GRADUATE STUDIES
OBJECT ORIENTED PROGRAMMING

Lab Number: One

Lab Date:
Task One
Write a function called hms_to_sec() that takes three int values – for hours, minutes,
and seconds – as arguments and returns the equivalent time in seconds (type long).
Test the function by getting the input in the main(), calling the function and
displaying the value of seconds it return.
Task Two
Write a void function named AddTime() that has three reference parameters: hours,
minutes, and elapsedTime. elapsedTime is an integer number of minutes to be added
to the starting time passed in through hours and minutes. The resulting new time is
returned through hours and minutes. Test the function by getting input in the main()
and calling the function.
For example:
Before Call to AddTime() hours=12 minutes=44 elapsedTime=198
After Call to AddTime() hours=16 minutes=2

Lab Number: Two

Task One
Create a structure of type date that contains three members: the day, the month and
the year, all of type int. Test the structure by creating a variable in the main(), get the
input from user and display the date in DD/MM/YY format.
In addition, ask the user to input the days passed since the given input date, update
the date and display the new date. Assume all months to have 30 days.
Interaction with the program would like:
Enter day, month, and year: 23 6 11
The current date is: 23/6/11
Enter the number of days passed: 10
The new date is: 3/7/11
Task Two
Create a structure of type fraction which has two members, the fraction’s numerator
and the fraction’s denominator. Test the structure by creating two variables in the
main() and get the input from the user.
Write down two separate functions AddFract(), (that displays the sum of the two
fraction variables) and SubFract() (that displays the difference of the first fraction
from the second fraction variable)
Test the function by calling them using the same variables.

Lab Number: Three

Lab Date:
Task One
Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 cent
toll. Mostly they do, but some times a car goes by without paying. The tollbooth keeps
track of the number of cars that have gone by, and of the total amount of money
collected. Model this tollbooth with a class called tollbooth. The two data items are: a
type int to hold the total number of cars and a type double to hold the total amount of
money collected. Initialize both of these variables to 0. A member function called
payingCar() increments the car total and adds 0.50 to the cash total .Another function,
called nopayCar() , increments the car total but adds nothing to the cash total. Finally, a
member function called display() displays the two totals.
This program should keep on asking the user for choice; if the user press 1,
payingCar() function should be called and if the user press 2, nopayCar() function is
called. Pushing the 3 should call the function display () to print out the total cars and
total cash and the program exits.
Task Two
Construct a class named Date that has three data members for day, month and year.
The class should have a member function named getdata() to get the date as input from
the user, a member function showdata() that displays the date in the DD/MM/YYYY
format. Also include a function nextday() that increments a date by one day. Ensure
that the function correctly increments days into a new month and into a new year.
Test the class by creating an object of type date in the main().
Lab Number: Four
Create a class called Account that a bank might use to represent customers’ bank accounts. The class should include one data member of type int to represent the account balance. The class should provide a constructor that receives an initial balance and uses it to initialize the data member.
The class should provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the account and should ensure that the debit amount does not exceed the account’s balance. If it does, the balance should be left unchanged and the function should print a message indicating that “Not enough balance in the Account”. Member function getBalance should return the current balance.
Write a program that creates an object of type Account and tests the member functions.
Task Two
Write a class description for complex numbers. A complex number in mathematics is defined as x + y, where x defines the real part and y defines the imaginary part. Provide the get Data and displayData functionalities to input and display a complex number.
Also write methods for addition, subtraction and multiplication of two complex
numbers.
Write a program that creates three complex numbers and initialize them to zero.
Provide options to either input, display, add, subtract or multiply the two numbers.
Add: x3 = x1 + x2, y3 = y1 + y2
Subtract: x3 = x1 – x2, y3 = y1 – y2
Multiply: x3 = x1 * x2 – y1 * y2,
y3 = x1 * y2 + y1 * x2

Lab Number: Five

Lab Date:
Task One
Create a class student that contains student number, student name and marks of
three subjects; english, mathematics and computers. Include a member function to
get data from the user, and another function to display the data.
In main() function, create an array of type student of size 100 and ask user to input
data for as many students as he wishes. Student number should be assigned serially.
Write three functions in the main(); maxEnglish(), maxMath() and maxComputer()
each of them go through the array, looking for the highest marks in the respective
subject. The functions should take the array and number of students as an argument
and return the index number of the highest marks.
The program then displays the details of the student with highest marks in the
individual subject

Lab Number: Six

Lab Date:
Task One
Create a class package having package id and package weight (in kg.) as its data
members and functionality of getdata() and displaydata().
Create an array container of 25 empty packages. Ask the user to start filling in the
packages (getting the input) till the net weight of the container does not exceed 50
kg. Package id is to be assigned serially and implicitly and put the check that the
package weight should not be less than 2 and greater than 8 kg.
After filling in the packages, calculate the average weight of the package in the
container. Use static data members where necessary.
Lab Number: Seven
Lab Date:
Task One
A 12 - Hour clock displays the time in the format HH:MM:SS A/P; where ‘A’ stands for
AM and ‘P’ stands for PM. Model this clock with a class PerfectClock.
The clock should have the initial value of 00:00:00 A. It should have the functionality
of receiving the input from the user, displaying the current time and increasing and
decreasing the time by a second. The class should use the overloaded increment (++)
and decrement (--) operators to achieve the task.
Write a menu driven program in the main() which first creates an object of type
PerfectClock and then performs the tasks depending on the user input.
Pressing the key “R” first resets the clock and then display the time
Pressing the key “S” asks the user to set the clock to a particular time
Pressing the key “U” first increases the time by one second and then display it
Pressing the key “D” first decrease the time by one second and then display it
Pressing the key “E” terminates the program
Lab Number: Eight
Lab Date: Tuesday, November 1, 2011
Task One
Create a class called Time that includes three data members for hours, minutes and
seconds. The class should have a member function named getdata() to get the time
as input from the user and a member function display() to display the time in
hr:mm:sec format.
The class should have the functionality of adding two time objects, finding the
difference of two time objects and comparing two time values. Overload the addition
operator (+) to add two time objects, subtraction operator (-) to subtract two time
objects and equals to operator (==) to compare two time objects.
Write a program that first creates three time objects and initialize them to zero.
Provide following menu to perform different options.
Pressing “I”: Get the input for two objects
Pressing “A”: Add the two objects of class time, assign the result in third object and
display the resultant time
Pressing “S”: Finds the time difference between two objects, assign the result in
third object and display the resultant time.
Pressing “C”: Checks if the two objects are equal or not
Pressing “E”: Terminates the program
Post A Comment
  • Blogger Comment using Blogger
  • Facebook Comment using Facebook
  • Disqus Comment using Disqus

No comments :