Method Programs
1) Design a class to overload a function area( ) as follows :
(i) double area(double a, double b, double c) with three double arguments, returns the area
of a scalene triangle using the formula :
area=s(s−a)(s−b)(s−c)area=s(s−a)(s−b)(s−c)
where s=a+b+c2s=2a+b+c
(ii) double area(int a, int b, int height) with three integer arguments, returns the area of a
trapezium using the formula :
area = 1221 height(a + b)
(iii) double area(double diagonal1, double diagonal2) with two double arguments, returns
the area of a rhombus using the formula :
area = 1221 (diagonal1 x diagonal2)
2) Design a class to represent a bank account. Include the following members:
Data members
1. Name of the depositor
2. Account number
3. Type of account
4. Balance amount in the account
Methods
1. To assign initial values
2. To deposit an amount
3. To withdraw an amount after checking balance
4. To display the name and balance
5. Do write proper constructor functions
3) Write a program using a function called area() to compute area of the following:
(a) Area of circle = (22/7) * r * r
(b) Area of square= side * side
(c) Area of rectangle = length * breadth
Display the menu to display the area as per the user's choice.
4) Write a program using method name Glcm(int,int) to find the Lowest Common Multiple
(LCM) of two numbers by GCD (Greatest Common Divisor) of the numbers. GCD of
two integers is calculated by continued division method. Divide the larger number by the
smaller, the remainder then divides the previous divisor. The process is repeated till the
remainder is zero. The divisor then results in the GCD.
LCM = product of two numbers / GCD
5) Design a program in Java to calculate the tax for the people living in Mango city. Specify
a class taxpayer whose class description is given below:
Class name:
TaxCalculator
Data members: int PAN
String name
double taxableIncome
double tax
Member methods: inputData()
displayData()
computeData()
The tax is calculated according to the following rules:
Total Annual Taxable Income Rate of Taxation
Up to 60000 0%
Above 60000 but up to 150000 5%
Above 150000 but up to 500000 10%
Above 500000 15%
6) A cloth showroom has announced the following festival discounts on the purchase of
items based on the total cost of the items purchased:
Total cost Discount (in Percentage)
Less than Rs. 2000 5%
Rs. 2001 to Rs. 5000 25%
Rs. 5001 to Rs. 10000 35%
Above Rs. 10000 50%
Write a program to input the total cost and to compute and display the amount to be paid by
the customer after availing the discount. Three methods also their input(), calculate() and
display().
7) You are to print the telephone bill of a subscriber. Create a class having the following data
members:
Phone Number of long data type (for storing the phone number).
Name of String type (for storing the name of a the subscriber).
Hire Charge a symbolic constant of int type (to store monthly hire charge say `. 200).
Units Consumed of int type (to store the monthly units consumed by the subscriber).
Bill Amount of float type (to store the bill amount that is payable).
Create member functions for the following:
i. Constructor to initialise all data members except Hire Charge and Bill Amount.
ii. Calculate the bill amount payable which is Hire Charge+(`. 1 per unit for the first
100 units, `. 1.50 per unit for the next 100 units and `. 2.00 per unit there after.
iii. To display the Bill for the subscriber.
8) Define a class Taximeter having the following description:
Data members/instance variables:
int taxino - to store taxi number
String name - to store passenger’s name
int km - to store number of kilometers travelled
Member functions:
Taximeter() - constructor to initialise taxino to 0, name to “” and km to 0.
input() - to store taxino,name,km
calculate() - to calculate bill for a customer according to given conditions
Kilometers travelled(km) Rate/km
1 km ` 25
1 < km ≤ 6 ` 10
6 < km ≤ 12 ` 15
12 < km ≤ 18 ` 20
>18 km ` 25
display() - To display the details in the following format
Taxino Name Kilometres travelled Bill amount
9) Create a class SalaryCalculation that is described as below:
Class Name : SalaryCalculation
Data members :
name (String type data) basicPay,specialAlw, conveyanceAlw, gross, pf, netSalary,
AnnualSal (All double type data)
Member methods :
SalaryCalculation( ) -A constructor to assign name of employee (name),
basic salary (basicPay) of your choice and conveyance
allowance (conveyanceAlw) As ` 1000.00.
void SalaryCal( ) - to calculate other allowances and salaries as given:
specialAlw = 25% of basic salary.
gross = basicPay + specialAlw + conveyanceAlw.
netSalary = gross - pf.
AnnualSal = 12 months netSalary.
void display( ) - to print the name and other calculations with
suitable headings. Write a program in Java to calculate all the
details mentioned above and print them all.
10) Create a class called PrimeDigits to find the sum of the prime digits in an integer.
The class should have the following members.
Data Members:
n of int data type.
Member Functions:
Parameterised constructor to initialise the value of n
Method called isPrime() which accepts an integer as a parameter and return true if
it is a prime number otherwise return false.
Method called sumOfPrimeDigits() which accepts an integer as a parameter and
find the sum of prime digits only.
11) Create a class called Series which will contain the following members:
Data Members:
x of double type n of int type
Constructors/Member Functions:
i. Parameterised constructor to initialise the data members.
ii. To calculate and print the sum of the following series:
x+x/2!+x/3!+x/4!+...+x/n!
iii. To calculate and print the sum of the following series:
x/2!+x2/3!+x3/4!+x4/5!+…+xn/(n+1)!
iv. To calculate and print the sum of the following series:
v. x/2! - x2/3!+x3/4! - x4/5!+…±xn/(n+1)!
where the symbol ! stands for factorial eg. 5!=5*4*3*2*1, 3!=3*2*1
12)