0% found this document useful (0 votes)
82 views18 pages

Java Slips Solutions PDF

The document contains two C++ programs. The first program uses inline functions to find the maximum and minimum of two integer numbers. The second program creates an Employee class with data members like employee number, name, designation and salary. It defines different constructors and a function to calculate income tax of 2% of salary. Objects are initialized using default, parameterized and copy constructors and their details are displayed.

Uploaded by

dkpatil2929
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)
82 views18 pages

Java Slips Solutions PDF

The document contains two C++ programs. The first program uses inline functions to find the maximum and minimum of two integer numbers. The second program creates an Employee class with data members like employee number, name, designation and salary. It defines different constructors and a function to calculate income tax of 2% of salary. Objects are initialized using default, parameterized and copy constructors and their details are displayed.

Uploaded by

dkpatil2929
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/ 18

// Q1.Write a C++ program to accept length and width of a rectangle.

Calculate and
// as well as area of a rectangle by using inline function. SLIP NO 12
#include <iostream>
using namespace std;

inline void calculate (double length, double width, double& area, double& perimeter)
area = length * width;
perimeter = 2 * (length + width);

int main()

double length, width, area, perimeter;


cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;

calculate(length, width, area, perimeter) ;


cout << "Area of the rectangle is: " << area << endl;
cout << "Perimeter of the rectangle is: " <<
perimeter << endl;
return 0;
V/ 02.Write a C++ program to create a class which
contains single dimensional integer ar
Upiven size. Define member function to display median
I/ Constructor to allocate and Destructor to of a given array. (Use Dynamic
free memory of an object).
#include <iostream>
#include <algorithm>
using namespace std;
class Array {
private:
int* arr;
int size;
public:
Array(int sz) {
size = SZ}
arr = neW
int[size];

~Array() { I
delete[] arrs
void displayMedian() {
sort(arr, arr + size);
if (size % 2 == 0) {
int mid = size / 2j
cout <« "Median of the array is:"<< (arr[midT + arr[mid - 1]) /
2.0 << endl;
else {
int mid = size / 2;
cout << "Median of the array is:"<< arr[mid] << endl;

}
void readArray() {
cout << "Enter << size <<elements of the array:<< endl;
for (int i = 0; issizes i+)
cin >> arr[i];

int main() {
int nj
cout << "Enter the size of the array:
cin >> n
Array a(n)
a.readArray() ;
a.displayMedian()
retun O;
File Edit View

I| Q1.Write a C++ program to implement a class student to overload following functions as follows:
1/ a. int maximum(int, int) - returns the maximum score of two students
// b. int maximum(int *a, int arraylength) - returns the maximum score from an array a' SLIP NO 13
#include <iostream>
using namespace std;

class student {
public :
int maximum(int a, int b) {
return a > b ?a : b;
}
int maximum (int *a, int arraylength) {
int maxscore = a[0];
for(int i-1; i<arraylength; i++) {
if(a[i] > maxscore) {
maxscore = a[i];

}
return maxscore;

}B
int main() {
student s;
int scorel = 80, score2 90;
int scores [] = {85, 92, 88, 90, 87};
int num_scores = sizeof (scores) /sizeof (scores [0]);
cout << "Maximum score of two students: " << s.maximum(scorel, score2) << endl;
cout << "Maximum score from an array: << S.maximum(scores, num_scores) << endl;
return O;
class Distance which contains dat
// Q2.Write a Ct+ program to create a
#include <iostream> SLIP NO 13
using namespace std;

class Distance {
private:
int kilometer;
int meter;
public:
Distance() {
kilometer = 0;
meter = 0;

void accept distance() {


cout << "Enter distance in kilometers: ";
cin >> kilometer;
cout << "Enter distance in meters: ";
cin >> meter;
normalize distance();

void display distance() { m" << er


cout <« "Distance: <« kilometer << "" km << Meter <<

}
bool operator>(Distance d2) {
int total meter1 = kilometer * 1000 + meter;
int total meter2 = d2.kilometer * 1000 + d2. meter;
return total meter1 > total meter2;
}
private:
void normalize distance() {
if(meter = 1000) {
int extra kilometer = meter / 1000;
meter %= 100;
kilometer += extra kilometer;
}

int main() {
Distance d1 d25
cout << "Enter distance 1:" << endl;
d1.accept distance()s
cout << "Enter dístance 2:" << endl;
d2.accept distance()5
cout << endl;
cout << "Distance 1:" << end;
d1.display_distance() ;
cout < "Distance 2:" << endl;
d2.display_distance();
cout <« endl;
if (d1 > d2)
cout << "Distance 1 is greater than distance 2" << endl;
} else {
cout << "Distance 2 is greater than distance 1" << endl
return 0;
File tdit View

// Q1. Write a C++program to


#include <iostream> SLIP NO 14interchange values of two integer nu
using namespace std;
void swap(int &a, int &b)
int temp = aj
a = b;
b = temp;
}

int main() {
int x = 10, y = 20;
cout << "Before swap: X = <<<x X << , y = << y << endl;
Swap(x, y);
cout << "After swap: x = << X << " << y << endl;
return 0;
}
I/ or 02. Create a class Fraction that contains two data members as
numerator and denominator
I/Write a C+t program to overload following operators
1/a. Unary (pre and post both)
I/b. <« and >> verload as friend functions//not understand SLIP NO..14
#include <iostream>
using namespace std;

class Fraction {
private:
int numerator, denominator;
public:
Fraction(int n = 0, int d = 1) : numerator (n), denominator(d) }
friend istream& operator>> (istream& in, Fraction& f);
friend ostream& operator<<(ostream& out, const Fraction& f);
Fraction operator++(); I/ pre-increment
Fraction operator++(int); // post-increment
}
istream& operator>>(istream& in, Fraction& f) {
in >> f.numerator >> f.denominator:
return in;

ostream& operator<<(ostream& out, const Fraction& f) {


out << f.numerator <<'/ <« f.denominator;
return out;

Fraction Fraction: :operatort+() {


numerator += denominator;
return *this;

Fraction Fraction: :operatort+(int) {


Fraction temp = *this;
numerator += denominator;
return temp;

int main() {
Fraction fi(3, 4), f2;
cout << "Enter fraction
cin >> f2; (numerator/denominator): ";
cout << "f1 = <« f1 << endl;
cOut << "f2 = << f2 << endl;
+fl;
cout << "After pre-increment, f1 = " << f1 << endl;
f2++;
cout << "After post-increment, f2 " << f2 << endl;
return 0;
que_n que_nO_5 que_ que_noO_| que_n( que_nO_2 que_n que_iO
File Edit View

1/ Q1.Write a C++ program to check minimum and maximum of two integer number (use inline
1/ function and conditional operator) SLIP NO 15
#include <iostream>
using namespace std;
inline int max(int a, int b) {
return (a > b) ?a: b;

inline int min(int a, int b) {


return (a < b) ?a : b;

int main() {
int num1, num2;
cout << "Enter two integer numbers: ";:
cin >> num1 >> num2;
cout << "Maximum of " << num1 << " and " < num2 <« " is << max(num1, num2) << endl;
cout <« "Minimum of << num1 << " and " << num2 << is << min(num1, num2) << endl;
return 0;
}
or 02. Write C++ progran to create a
I/ Designation and Salary. class Empluyee containing data menbers Emp
ro, Emp Nane.
Il Constructor. Also Nite Create and initialize the objects using default, paraneterized and CoN
meber function to calculate Income tax of the
I/ of salary. eployee which is 2%
#include <iostream)
#include (string>
Using anespace std;
class Enployee {
private:
int Emp no;
string Errp_Name ;
string Designation;
double Salary;
public:
I/ Default Constructor
Employee() {
Emp no= 0;
Erp_Name =
Designation =
Salary = ;

I/ Paraneterized Constructor
Eployee(int enpNo, string empame, string designation, double salary) (
Brp no =empNo;
Ep Name = erpName;
Designation = designation;
Salary = salaryS
}
// copy Constructor
Employee(const Enployee& e) {
Enp_no = e.Brp_ no;
Enp Name = e.Emp Name
Designation = e.Designation;
Salary = e.Salary;

1/ Member function to calculate Incane tax


double calculateIncameTax() {
return .2 * Salary;

I/ Merber function to display Employee details


void displayEmployee() {
cout << "Employee No: " << Enp no << endl;
cout << "Enployee Name : " << Bnp Name <« endl;
cout << "Designation: <« Designation << endl;
cout << "Salary: " << Salary << endl;

int main() {
1/ Initializing objects using different constructors
Employee elj
Enployee e2(191, "John Doe", "Aarnager", 5O000);
Enployee e3 = e2;
I/ Displaying Enployee details
cout << "Enployee 1 details:" <% endl;
el,displayEsployee();
cout <K endi;

cout << "Enployee 2 details:" << endl;


e2.displayEmployee();
cout << "Incane Tax:" <<
e2.calculateIncaIeTax() << endl «« end
cout << "Erployee 3 details" <<
endl;
e3.displayEmployee();
cout < "Incame Tax: " <<
e3.calculateIncomeTax() << endl <« endl;
return Ø;
Te

I| Q1. Write a C++ program to create a class Number which contains two integer data members. Create
I/ and initialize the object by using default constructor, parameterized constructor. Write a
I| member function to display maximum from given two numbers for all objects.
#include<iostream>
using namespace std; SLIP NO 16

class Number {
private:
int num1, num2;

public:
Number() {
numl = 0;
num2 : 0;

Number (int nl, int n2) {


numi = n1;
num2 = n2;

void displayMax() {
cout << "The maximum of " << num1 << " and " << num2 << " is " << ((num1 > num2)? num1 : num2) <« endl;

};

int main() {
Number objl, obj2 (8, 5), obj3(7, 2);
obj1. displayMax (0;
obj2.displayMax();
obj3.displayMax ();
return e;
}
I| or Q2. Create a class Time containing members as: 16 SLIP
1| - hours
|| - mínutes
1|- seconds
1/ Write a C+t program for
overloading
I/ also write a member function operators >> and << to accept and display a Time
to display tine in total seconds.//not understand logic
#include <iostream>

using namespace std;


class Time {
private:
int hours, minutes, seconds;
public:
Time() { hours = minutes seconds 0 )
friend istream& operstor> >(istre
friend ostream& operator< (ostre an, Ti ne&):
ank, const Tine&)
int total_seconds() const;

istream& operator> >(istream& in, Times t) {


cout <« "Enter time
in >> t.hours; (hh:m:ss):
in. ignore();
in >> t.minutes;
in. ignore();
in >> t.seconds;
return in;

ostream& operator<<(ostream& out, const Tine& t) {


out <« t.hours << ":" <«
return out; t.minutes << ": <«
t.seconds;
int Time:
:total_seconds()
return (hours* 3600) +const {
(minutes * 60) +
seconds;
int main() {
Time t;
cin >> t;
cout << "Time
cout <« "Total entered: << t << endl;
seconds: <<
return : t.total_seconds () <« endl;
// Q1. Write a Ct+ program to check if number is prime or rn
#include <iostream>
using namespace std; SLIP 17
bool isPrime(int num) {
if (num <= 1) {
return false;

for (int i = 2; i <= num / 2; i++) {


if (num % i == e) {
return false;
}
}
return true;
}
int main() {
int num;
cOut << "Enter a number: ":
cin >> num;
if (isPrime (num)) {
cout << num << " is a prime number" << endl;
} else {
cout << num << " is not a prime number" << endl;
}
return 0;
W02.. Create a class Fraction containing data members as Numerator and Denor
SLIP NO 17
#include <iostream>
using namespace std;
class Fraction {
private:
int numerator;
int denominator;

public:
Fraction(int n, int d) {
numerator = n;
denominator = d;

Fraction operator++() {
numerator += denominator;
return *this;

Fraction operator--() {
numerator -= denominator;
return *this;

Fraction operator*(const Fraction& f) {


int n = numerator * f.numerator;
int d = denominator * f.denominator;
return Fraction(n, d);

void print() {
cout << numerator <<" << denominator << endl;
}

int main() {
Fraction f1(1, 2)
Fraction f2(3, 4);
Fraction f3 ++fl;
Fraction f4 =--f2;
Fraction f5 = f1 * f2;
cout << "f1 after
increment:"
f1.print();
cout << "f2 after decrement: ";
f2.print()
cout K "f1*F2
f5-print();
return 0;
// Q1.Write a C++ program to calculate following series:
I/ (1) + (1+2) + (1+2+3) + (1+2+3+4) +
1/ +(1+2+3+4+...+n)
#include <iostream> SLIP NO 18
using namespace std;
int main() {
int n
cout <« "Enter the value of n: ";
cin >> n;

int sum = Ø;
for (int i = 1; i <= n; i++) {
int inner sum = 0;
for (int j = 1; j <= i; j++) {
inner sum += j;
}
sum += inner_ sum;
}

cout << "Sum of the series is: " << sum << endl3

return 0;
and
I/ 02. Write a C++ program to read student information such as rollno, name
I/ students. Write the student information using file handling
#include <iostream> SLIP NON 18
#include <fstream>
using namespace std;

struct Student {
int roll_no;
string name;
float percentage;

int main() {
int n;
cout << "Enter the number of students:
cin >> n

Student students[n];
for (int i = 0; i < n; it+) {
cout << "Enter the information for student" << i+1 << ":\n";
cout << "Roll No: ";
cin >> students [i] . roll_no;
cout << "Name:
cin.ignore () ;
getline(cin, students[i] .name);
cout << "Percentage : ";
cin >> students [i].percentage;

ofstream file ("students . txt");

for (int i = 0; i < n; i++) {


file << "Roll No: " << students [i].roll_no << endl;
file << "Name: << students [i].name << endl;
file << "Percentage : << students[i].percentage << "%" << endl << endl;

file.close() ;
cout << "Student information written to file successfully. \n"

return 0;
que_n que_no_2 que_n que_n que_no_2 que_no_3 que_n que_no_2 que
File Edit View

// Q1. Write a C++ program to display factors of a number.//n


#include <iostream> SLIP NO 19
using namespace std;

int main() {
int num;
cout << "Enter a number:
cin >> num;

cout << "Factors of << num << are: ";


for (int i = 1; i <= num; it+) {
if (num % i == 0) {
cOut << i << "<<endl;

return e;
}
1/ Q2. Design a two base classes Employee (Name, Designation) and Project (Pr
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
SLIP NO 19
class Employee {
protected:
string name;
string designation;
public:
Employee() )
Employee (string n, string d) {
name = n;
designation = d;

void input() {
cout << "Enter Employee Name:
cin >> names
cout << "Enter Employee Designation: ";
cin >> designation;
void display() {
cout << "Employee Name: << name << endl;
cout << "Employee Designation: << designation << endl;

class Project {
protected:
int project_id;
string title;
public:
Project() (}
Project (int id, string t) {
project_id = id;
title = t;

void input () {
cout << "Enter Project ID: ";
cin >> project_id;
cout << "Enter Project Title : ";
cin >> title;
u1.Write a C++ progra to sort integer and
float array elaents in ascending oner by usir
I| fuxtion overloating SLIP ND 29
#include <iostreaD
using namespace std;

void sortArray(int arr[], int n) {


for (int i 0; i<n - 1; i+) {
for (int j =0; j <n - i - 1; j+) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr(j] = arr[j+1];
arr(j+1] =temp;

void sortArray(float arr[U, int n) {


for (int i = 0; i<n - 1; i+){
for (int j = 0; j <n -i- 1; j+) {
if (arr[i] > arr[j+1]) {
float temp = arrj];
arr[j] = arr(j+1];
arr[j+1] =temp;
}

void printArray (int arrU, int n) {


for (it i = 0; i< n; itt) {
cout <K arr(i] <« " ";
cout <K endl;
}

void printArray(float arr[], int n) {


for (it i = 0; i< n; i+) {
cout <K arr[i] <<" ";
}
cout < endl;

int nain() {
int arr1[] = {5, 2, 7, 1, 8};
float arr2[] = {3.5, 2.0, 7.8, 1.2, 6.4};
int n1 = sizeof (arr1) /
int n2 = sizeof(arr2) / sizeof(arr1[0]) ;
sizeof(arr2[0]);
sortArray(arr1, n1);
sortArray(arr2, n2);
cout << "Sorted Integer
pritArray(arr1, n1); Array:"
cout << "Sorted Float
printArray(arr2, n2); Array:M;
return 0;
"Sample.txt" file. Store a l t
I| or 02. Write a C++ program to read the contents of a
#include <iostream> SLIP NO 20
#include <fstream>
#include <ctype.h>
using namespace std;

int main() {
ifstream fin;
fin.open("Sample.txt") ;

ofstream fout_ upper;


fout_upper.open("Upper.txt");

ofstream fout lower;


fout _lower.open ("Lower.txt");
ofstream fout_digit;
fout_digit.open("Digit.txt");
ofstream fout convert;
fout_convert.open("Convert.txt");
char ch;
while (fin >> ch) {
if (isupper(ch) ) {
fout upper << ch;
else if (islower(ch) ) {
fout lower <K chi

else if (isdigit(ch)) {
fout digit << chË

if (isalpha(ch)) {
if (islower (ch)) {
ch = toupper(ch);

else {
ch = tolower(ch);

fout convert «« ch;

fin.close();
fout_ upper.close()5
fout lower.close();
fout digit.close();
fout convert.close()

return 0;
}

You might also like