Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore BCA127 CU-BCA-SEM-II-Object Oriented Programming Practical(Draft 2)(1) (1)-converted

BCA127 CU-BCA-SEM-II-Object Oriented Programming Practical(Draft 2)(1) (1)-converted

Published by Teamlease Edtech Ltd (Amita Chitroda), 2021-06-15 05:23:06

Description: BCA127 CU-BCA-SEM-II-Object Oriented Programming Practical(Draft 2)(1) (1)-converted

Search

Read the Text Version

CHANDIGARH UNIVERSITY Institute of Distance and Online Learning Course Development Committee Prof. (Dr.) R.S.Bawa Pro Chancellor, Chandigarh University, Gharuan, Punjab Advisors Prof. (Dr.) Bharat Bhushan, Director – IGNOU Prof. (Dr.) Majulika Srivastava, Director – CIQA, IGNOU Programme Coordinators & Editing Team Master of Business Administration (MBA) Bachelor of Business Administration (BBA) Coordinator – Dr. Rupali Arora Coordinator – Dr. Simran Jewandah Master of Computer Applications (MCA) Bachelor of Computer Applications (BCA) Coordinator – Dr. Raju Kumar Coordinator – Dr. Manisha Malhotra Master of Commerce (M.Com.) Bachelor of Commerce (B.Com.) Coordinator – Dr. Aman Jindal Coordinator – Dr. Minakshi Garg Master of Arts (Psychology) Bachelor of Science (Travel &Tourism Management) Coordinator – Dr. Samerjeet Kaur Coordinator – Dr. Shikha Sharma Master of Arts (English) Bachelor of Arts (General) Coordinator – Dr. Ashita Chadha Coordinator – Ms. Neeraj Gohlan Academic and Administrative Management Prof. (Dr.) R. M. Bhagat Prof. (Dr.) S.S. Sehgal Executive Director – Sciences Registrar Prof. (Dr.) Manaswini Acharya Prof. (Dr.) Gurpreet Singh Executive Director – Liberal Arts Director – IDOL © No part of this publication should be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording and/or otherwise without the prior written permission of the authors and the publisher. SLM SPECIALLY PREPARED FOR CU IDOL STUDENTS Printed and Published by: TeamLease Edtech Limited www.teamleaseedtech.com CONTACT NO:- 01133002345 For: CHANDIGARH UNIVERSITY Institute of Distance and Online Learning

Object Oriented Programming Practical Course Code: BCA127 Credit: 1 Course Objectives: • To inculcate the skills related to basics of Programming and use programming in day to day Applications. • To learn about the Basics of Functions and use of classes. • To develop skills for using Files and perform operations on files. PRACTICAL UNIT 1:DATA TYPES, OPERATORS, AND CONDITIONAL CONSTRUCTS 1. Program based on basics data types and operator in C++: 1 Assuming there are 7.481 gallons in a cubic foot, write a program that asks the user to enter a number of gallons, and then displays the equivalent in cubic feet. #include <iostream> using namespace std; double Convert(double); int main() { double dGallons; cout << \" -- Program to convert temperature in gallons to cubic feet -- \\n\\n\\n\"; cout << \"Enter value in Gallon : \"; cin >> dGallons; cout << \"Converted value in Cubic : \" << Convert(dGallons) << endl; return 0; } double Convert(double a) {

double dCubic = 7.481; double c = a / dCubic; return c; } Output : 2. You can convert temperature from degrees Celsius to degrees Fahrenheit by multiplying by 9/5 and adding 32. Write a program that allows the user to enter a floating-point number representing degrees Celsius, and then displays the corresponding degrees Fahrenheit. #include <iostream> using namespace std; float Convert(float); int main() { float dCelsius; cout << \" -- Program to convert temperature in Celsius to Fahrenheit -- \\n\\n\\n\"; cout << \"Enter degree in Celsius value : \"; cin >> dCelsius; cout << \"Corresponding degree in Fahrenheit value is : \" << Convert(dCelsius) << endl;; return 0; } float Convert(float dCelsius) { float dFahrenheit = 7.481; dFahrenheit = (dCelsius * 9.0) / 5.0 + 32; return dFahrenheit; } Output :

2. Program based on conditional constructs: 1. The normal speed of a vehicle is less than 65kmph. If entered speed is less than 65kmph you can print within speed limit otherwise over speed limit. #include <iostream> using namespace std; string SpeedChecker(int); int main() { int intSpeed; cout << \" -- Program to check speed of a vehicle -- \\n\\n\\n\"; cout << \"Enter the speed of a vehicle : \"; cin >> intSpeed; cout << endl <<SpeedChecker(intSpeed) << endl; return 0; } string SpeedChecker(int intSpeed) { int intNormalSpeed = 65; string strSpeedLimit; if (intSpeed > intNormalSpeed) { strSpeedLimit = \"Over speed limit\"; } else if (intSpeed <= intNormalSpeed) {

strSpeedLimit = \"Within speed limit\"; } return strSpeedLimit; } Output: 2. Create the equivalent of a four-function calculator. The program should request the user to enter a number, an operator, and another number. (Use floating point) It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use a switch statement to select the operation. Finally, display the result. When it finishes the calculation, the program should ask if the user wants to do another calculation. The response can be ‘y’ or ‘n’. #include <iostream> using namespace std; int main() { double INumber, IINumber, Answer; char oper, ch; do { cout << \"\\n-- Calculator --\"; cout << \"\\nEnter first number : \"; cin >> INumber ; cout << \"\\nEnter operator : \"; cin >> oper ; cout << \"\\nEnter second number : \"; cin >> IINumber; switch (oper) {

case '+': Answer = INumber + IINumber; break; case '-': Answer = INumber - IINumber; break; case '*': Answer = INumber * IINumber; break; case '/': Answer = INumber / IINumber; break; default: Answer = 0; } cout << \"Answer = \" << Answer; cout << \"\\n\\nDo another(Enter 'y' or 'n') ? \"; cin >> ch; } while (ch != 'n'); return 0; } Output: Additional programs 3. Demonstration of Bitwise Operators

#include <iostream> using namespace std; int main() { int x ; int y ; cout << \" -- Program to demonstrate Bitwise Operator-- \\n\\n\\n\"; cout << \"Enter two integer numbers for Bitwise operations \\n\"; cout << \"First number :- \" ; cin >> x; cout << endl << \"Second number :- \" ; cin >> y; cout << endl << \"Bitwise AND Operator :- \"<< ( x & y) << endl; cout << \"Bitwise OR Operator :- \" << (x | y) << endl; cout << \"Bitwise XOR Operator :- \" << (x ^ y) << endl; cout << \"Bitwise One's Complement Operator :- \" << ~x << endl; cout << \"Bitwise Left Shift Operator :- \" << (x << 2) << endl; cout << \"Bitwise Right Shift Operator :- \" << (x >> 2) << endl; return 0;

} Output : 4. Write additional program to demonstrate if else if ladder. #include <iostream> using namespace std; string NumberChecker(int); int main() { int intNum; cout << \" -- Program to check whether an integer is positive, negative or zero -- \\n\\n\\n\"; cout << \"Enter an integer number - \"; cin >> intNum; cout << endl << NumberChecker(intNum) << endl; return 0; } string NumberChecker(int intNum) { string strState; if (intNum > 0) { cout << \"You entered a positive integer: \" << intNum << endl; } else if (intNum < 0) {

cout << \"You entered a negative integer: \" << intNum << endl; } else { cout << \"You entered 0.\" << endl; } return strState; } Output: Output:

PRACTICAL UNIT 2:POLYMORPHISM, CONSTRUCTOR AND DESTRUCTOR 1. Program based on polymorphism: Create a class that imitates part of the functionality of the basic data type int. Call the class int(note different spelling). The only data in this class is an int variable. Include member functions to initialize an int to 0, to initialize it to an int value, to display it (it looks just like an int), and use operator overloading to add two int values. Write a program that exercises this class by creating two initialized and one uninitialized int values, adding these two initialized values and placing the response in the uninitialized value, and then displaying this result. #include <iostream> #include <conio.h> using namespace std; class Int { private: int intvar; public: Int() { intvar = 0; } Int(int x) { intvar = x; } void display() { cout << intvar << endl; } void add(Int x, Int y) { intvar = x.intvar + y.intvar;

} }; int main() { Int a(5), b(95); Int c; cout << \" -- Program based on polymorphism -- \\n\\n\\n\"; c.add(a, b); cout << \" Addition of two variables : \"; c.display(); return 0; } Output : 2. Program based on constructor and destructor Create a class called time that has separate int member data for hours, minutes, and seconds. One constructor should initialize this data to 0, and another should initialize it to fixed values. Another member function should display it, in 11:59:59 format. The final member function should add two objects of type time passed as arguments. A main () program should create two initialized time objects (should they be const?) and one that isn’t initialized. Then it should add the two initialized values together, leaving the result in the third time variable. Finally, it should display the value of this third variable. Make appropriate member functions const. #include <iostream> #include <conio.h> using namespace std; class time { private:

int hours, minutes, seconds; public: time() { hours = minutes = seconds = 0; } time(int h, int m, int s) { hours = h; minutes = m; seconds = s; } void showTime() const { cout << hours << ':' << minutes << ':' << seconds; } void addTime(time x, time y) { seconds = x.seconds + y.seconds; if (seconds > 59) { seconds -= 60; minutes++; } minutes += x.minutes + y.minutes; if (minutes > 59) { minutes -= 60; hours++; } hours += x.hours + y.hours; } }; int main() {

const time a(6, 06, 45), b(4, 03, 15); time c; cout << \" -- Program based on constructor and destructor -- \\n\\n\\n\"; c.addTime(a, b); cout << \" Time clock : \"; c.showTime(); } Output: Additional programs 3. Program based on polymorphism: #include <iostream> using namespace std; int sum(int x, int y) { return x + y; } double sum(double x, double y) { return x + y; } int sum(int x, int y, int z) { return x + y + z; } int main() { cout << \"-- Program based on Polymorphism, --\\n\\n\\n\"; cout << \"Sum of two integer numbers = \" << sum(11, 16) << endl; cout << \"Sum of two decimal numbers = \" << sum(11.5, 16.6) << endl;

cout << \"Sum of three integer numbers = \" << sum(11, 16, 17) << endl; return 0; } Output: 4. Constructor and Destructor #include<iostream> using namespace std; class cube { public: double side; double volume() { return(side * side * side); } cube(double dSide) { side = dSide; } cube() { cout << \"\\nA default constructor is called \\n\\n \" << endl; }

~cube() { cout << \"\\nDestructing \" << side << endl; } }; int main() { cube VCube; cout << \" -- Program to calculate Volume of Cube using constructor -- \\n\\n\"; cout << \"\\nEnter the length of the cube to find volume : \"; cin >> VCube.side; cout << \"\\nThe volume of second cube is : \" << VCube.volume() << endl; return(0); } Output:

PRACTICAL UNIT 3:POINTER, DMA AND FILE HANDLING 1. Program based on pointers and dynamic memory allocation. 1. Write a program that reads a group of numbers from the user and places them in an array of type float. Once the numbers are stored in the array, the program should average them and print the result. Use pointer notation wherever possible. #include <iostream> using namespace std; int main() { int numData, i; float numArray[50], Total = 0.0, average; cout << \"Enter the numbers of data between 1 to 50 : \"; cin >> numData; while (numData > 50 || numData <= 0) { cout << \"\\nNumbers should be in range of (1 to 50).\" << endl; cout << \"\\nEnter the number again: \"; cin >> numData; } for (i = 0; i < numData; ++i) { cout << endl; cout << i + 1 << \". Enter number: \"; cin >> numArray[i];

Total += numArray[i]; } average = Total / numData; cout << \"\\nAverage = \" << average; return 0; } Output: 2. Program to understand the concept of new and delete operator. #include <iostream> using namespace std; int main() { int numData,n; float fltData; cout << \" -- Program with new and delete operator -- \" ; int* ipointer = NULL; ipointer = new(nothrow) int;

cout << \"\\n\\nEnter the integer number : \"; cin >> numData; *ipointer = numData; cout << \"The value of the integer pointer is: \" << *ipointer << endl; cout << \"\\n\\nEnter the decimal number : \"; cin >> fltData; float* fpointer = new float(fltData); cout << \"The value of the float pointer is: \" << *fpointer << endl; cout << \"\\n\\nEnter the numbers of data between 1 to 50 : \"; cin >> n; while (n > 50 || n <= 0) { cout << \"\\nNumbers should be in range of (1 to 50).\" << endl; cout << \"\\nEnter the number again: \"; cin >> n; } int* pointer = new int[n]; for (int i = 0; i < n; i++) pointer[i] = i + 1; cout << \"\\n\\nThe values stored dynamically are: \" << endl; for (int i = 0; i < n; i++) cout << pointer[i] << endl; delete ipointer; delete fpointer;

delete[] pointer; return 0; } Output: 3. Program based on file handling: Write a program to store the information of about 5 students in a file and read the contents and print them on screen. #include<iostream> #include<fstream> using namespace std; class student { int rollno; char studentname[30]; float totalmarks; public: student() { } void getData();

void displayData(); }; void student::getData() { cout << \"\\nEnter Roll No. :- \"; cin >> rollno; cin.ignore(); cout << \"\\nEnter Name :- \"; cin.getline(studentname, 30); cout << \"\\nEnter Marks :- \"; cin >> totalmarks; } void student::displayData() { cout << \"\\nRoll No. :- \" << rollno << endl; cout << \"\\nName :- \" << studentname << endl; cout << \"\\nMarks :- \" << totalmarks << endl; } int main() { student s[5]; fstream file; int i; file.open(\"D:\\\\Student.txt\", ios::out); cout << \"-- File handling program -- \" << endl; cout << \"\\nWriting Student information to the file :- \" << endl; cout << \"\\nEnter 5 students Details to the File :- \" << endl;

for (i = 0; i < 5; i++) { s[i].getData(); file.write((char*)&s[i], sizeof(s[i])); } file.close(); file.open(\"D:\\\\Student.txt\", ios::in); cout << \"\\nReading Student information to the file :- \" << endl; for (i = 0; i < 5; i++) { file.read((char*)&s[i], sizeof(s[i])); s[i].displayData(); } file.close(); return 0; } Output :

Additional programs 4. Additional program on pointer to demonstrate how using pointer a function can return morethan one value #include<iostream> using namespace std; void num_root(float n, float* s, float* c) { *s = pow((double)n, (double)0.5); *c = (double)pow((double)n, (double)1 / (double)3); }

int main() { float fltNum,squareRoot,cubeRoot; cout << \" -- Program to calculate square root and cube root of a number using pointer -- \\n\\n\"; cout << \"\\nEnter number to find the root : \"; cin >> fltNum; num_root(fltNum, &squareRoot, &cubeRoot); cout << \"\\nSquare root of given number is : \" << squareRoot << endl; cout << \"\\Cube root of given number is : \" << cubeRoot << endl; return(0); } Output: 5. Pointer, DMA and File Handling 1. Open a file on runtime. 2. Read a file. 3. Count Number of line. 4. Count Number of words. 5. Size of text………. #include<iostream> #include<string.h> #include<fstream> #include<stdlib.h> using namespace std; int main() {

char s[100], fname[100]; cout << \"\\n\\n-- Program to \\n1.Open a file on runtime.\\n2.Read a file.\\n3.Count number of line.\\n4.Count number of words.\\n5.Size of text ....... \\n\\n\"; cout << \"Enter file name to read :: \"; cin >> fname; ifstream fin(fname); cout << \"\\nData in file : \"; while (fin.eof() == 0) { fin >> s; cout << s << \" \"; } int line = 1, word = 1, size; char ch; fin.seekg(0, ios::end); size = fin.tellg(); fin.seekg(0, ios::beg); while (fin) { fin.get(ch); if (ch == ' ' || ch == '\\n') word++; if (ch == '\\n') line++;

} cout << \"\\n\\nNumber of Lines = \" << line << \"\\n\\nNumber of Words = \" << word; cout << \"\\n\\nSize of Text File=\" << size << \"\\n\"; fin.close(); return 0; } Output:


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook