Object Oriented Programs with Conditional Statement
Question 1
Write a class program to accept two numbers as instance variables. Use the following functions for the given
purposes:
Class Name — Calculate
void inputdata() — to input both the values
void calculate() — to find sum and difference
void outputdata() — to print sum and difference of both the numbers
Use a main method to call the functions.
Question 2
Write a class program with the following specifications:
Class Name — Triplet
Data Members — int a, b, c
Member Methods:
void getdata() — to accept three numbers
void findprint() — to check and display whether the numbers are Pythagorean Triplets or not.
Question 3
Define a class Employee having the following description:
Data Members Purpose
int pan To store personal account number
String name To store name
double taxincome To store annual taxable income
double tax To store tax that is calculated
Member functions Purpose
void input() Store the pan number, name, taxable income
void cal() Calculate tax on taxable income
void display() Output details of an employee
Calculate tax based on the given conditions and display the output as per the given format.
Total Annual Taxable Income Tax Rate
Up to ₹2,50,000 No tax
From ₹2,50,001 to ₹5,00,000 10% of the income exceeding ₹2,50,000
From ₹5,00,001 to ₹10,00,000 ₹30,000 + 20% of the income exceeding ₹5,00,000
Above ₹10,00,000 ₹50,000 + 30% of the income exceeding ₹10,00,000
Output:
Pan Number Name Tax-Income Tax
.......... .... .......... ...
.......... .... .......... ...
Question 4
Define a class Discount having the following description:
Data Members Purpose
int cost to store the price of an article
String name to store the customer's name
double dc to store the discount
double amt to store the amount to be paid
Member functions Purpose
void input() Stores the cost of the article and name of the customer
void cal() Calculates the discount and amount to be paid
void display() Displays the name of the customer, cost, discount and amount to
be paid
Write a program to compute the discount according to the given conditions and display the output as per the
given format.
List Price Rate of discount
Up to ₹5,000 No discount
From ₹5,001 to ₹10,000 10% on the list price
From ₹10,001 to ₹15,000 15% on the list price
Above ₹15,000 20% on the list price
Output:
Name of the customer Discount Amount to be paid
.................... ........ .................
.................... ........ .................
Question 5
Define a class Telephone having the following description:
Data Members Purpose
int prv, pre to store the previous and present meter readings
int call to store the calls made (i.e. pre - prv)
String name to store name of the consumer
double amt to store the amount
double total to store the total amount to be paid
Member functions Purpose
void input() Stores the previous reading, present reading and name of the
consumer
void cal() Calculates the amount and total amount to be paid
void display() Displays the name of the consumer, calls made, amount and
total amount to be paid
Write a program to compute the monthly bill to be paid according to the given conditions and display the
output as per the given format.
Calls made Rate
Up to 100 calls No charge
For the next 100 calls 90 paise per call
For the next 200 calls 80 paise per call
More than 400 calls 70 paise per call
However, every consumer has to pay ₹180 per month as monthly rent for availing the service.
Output:
Name of the customer Calls made Amount to be paid
.................... .......... .................
.................... .......... .................
Question 6
Define a class Interest having the following description:
Data Members Purpose
int p to store principal (sum)
int r to store rate
int t to store time
double interest to store the interest to be paid
double amt to store the amount to be paid
Member functions Purpose
void input() Stores the principal, rate, time
void cal() Calculates the interest and amount to be paid
void display() Displays the principal, interest and amount to be paid
Write a program to compute the interest according to the given conditions and display the output.
Time Rate of interest
For 1 year 6.5%
For 2 years 7.5%
For 3 years 8.5%
For 4 years or more 9.5%
(Note: Time to be taken only in whole years)
Question 7
Define a class Library having the following description:
Data Members Purpose
String name to store name of the book
int price to store the printed price of the book
int day to store the number of days for which fine is to be paid
double fine to store the fine to be paid
Member functions Purpose
void input() To accept the name of the book and printed price of the book
void cal() Calculates the fine to be paid
void display() Displays the name of the book and fine to be paid
Write a program to compute the fine according to the given conditions and display the fine to be paid.
Days Fine
First seven days 25 paise per day
Eight to fifteen days 40 paise per day
Sixteen to thirty days 60 paise per day
More than thirty days 80 paise per day
Question 8
Bank charges interest for the vehicle loan as given below:
Number of years Rate of interest
Up to 5 years 15%
More than 5 and up to 10 years 12%
Above 10 years 10%
Write a program to model a class with the specifications given below:
Class name: Loan
Data Members Purpose
int time Time for which loan is sanctioned
double principal Amount sanctioned
double rate Rate of interest
double interest To store the interest
double amt Amount to pay after given time
Member Methods Purpose
void getdata() To accept principal and time
void calculate() To find interest and amount.
Interest = (Principal*Rate*Time)/100
Amount = Principal + Interest
void display() To display interest and amount
Question 9
Hero Honda has increased the cost of its vehicles as per the type of the engine using the following criteria:
Type of Engine Rate of increment
2 stroke 10% of the cost
4 stroke 12% of the cost
Write a program by using a class to find the new cost as per the given specifications:
Class name: Honda
Data Members Purpose
int type To accept type of engine 2 stroke or 4 stroke
int cost To accept previous cost
Member Methods Purpose
void gettype() To accept the type of engine and previous cost
void find() To find the new cost as per the criteria given above
void printcost() To print the type and new cost of the vehicle
Question 10
Define a class called 'Mobike' with the following specifications:
Data Members Purpose
int bno To store the bike number
int phno To store the phone number of the customer
String name To store the name of the customer
int days To store the number of days the bike is taken on rent
int charge To calculate and store the rental charge
Member Methods Purpose
void input() To input and store the details of the customer
void compute() To compute the rental charge
void display() To display the details in the given format
The rent for a mobike is charged on the following basis:
Days Charge
For first five days ₹500 per day
For next five days ₹400 per day
Rest of the days ₹200 per day
Output:
Bike No. Phone No. Name No. of days Charge
xxxxxxx xxxxxxxx xxxx xxx xxxxxx
Question 11
Write a program using a class with the following specifications:
Class name: Caseconvert
Data Members Purpose
String str To store the string
Member Methods Purpose
void getstr() to accept a string
void convert() to obtain a string after converting each upper case letter into lower
case and vice versa
void display() to print the converted string
Question 12
Write a program by using a class with the following specifications:
Class name: Vowel
Data Members Purpose
String s To store the string
int c To count vowels
Member Methods Purpose
void getstr() to accept a string
void getvowel() to count the number of vowels
void display() to print the number of vowels
Question 13
Define a class Student with the following specifications:
Data Members Purpose
String name To store the name of the student
int eng To store marks in English
int hn To store marks in Hindi
int mts To store marks in Maths
double total To store total marks
double avg To store average marks
Member Methods Purpose
void accept() To input marks in English, Hindi and Maths
void compute() To calculate total marks and average of 3 subjects
void display() To show all the details viz. name, marks, total and average
Write a program to create an object and invoke the above methods.
Question 14
Define a class called ParkingLot with the following description:
Data Members Purpose
int vno To store the vehicle number
int hours To store the number of hours the vehicle is parked in the parking lot
double bill To store the bill amount
Member Purpose
Methods
void input( ) To input the vno and hours
void calculate( ) To compute the parking charge at the rate ₹3 for the first hour or the part
thereof and ₹1.50 for each additional hour or part thereof.
void display() To display the detail
Write a main method to create an object of the class and call the above methods.
Question 15
Define a class ElectricityBill with the following specifications:
Data Members Purpose
String n stores the name of the customer
int units stores the number of units consumed
double bill stores the amount to be paid
Member Methods Purpose
void accept() to store the name of the customer and number of units consumed
void calculate() to calculate the bill as per the tariff table given below
void print() to prints the details in the format given below
Tariff Table
Number of units Rate per unit
First 100 units ₹2.00
Next 200 units ₹3.00
Above 300 units ₹5.00
A surcharge of 2.5% charged if the number of units consumed is above 300 units.
Output:
Name of the customer: _________________________
Number of units consumed: _____________________
Bill amount: ________________
Write a main method to create an object of the class and call the above member methods.