Constructors and Class&Objects
Constructors and Class&Objects
2. Enter any two variables through constructor parameters and write a program to swap and
print the values.
13. Create a class with one integer instance variable. Initialize the variable using
a. default constructor
b. parameterized constructor.
14. Diifferentiate between public and private modifiers for members of a class.
15. In the program given below, state the name and the value of the
a. method argument or argument variable
b. class variable
c. local variable
d. instance variable
class myClass
{
static int x=7;
int y=2;
public static void main(String args[])
{
myClass obj = new myClass();
System.out.println(x);
obj.sampleMethod(5);
int a=6;
System.out.println(a);
}
void sampleMethod(int n)
{
System.out.println(n);
System.out.println(y);
}
}
16. Name the Java keyword that Stores the address of the currently calling object
1. Write a class with name employee and basic as its data member, to find the gross pay
of an employee for the following allowances and deduction. Use meaningful variables.
Dearness Allowance = 25% of Basic Pay
House Rent Allowance = 15% of Basic Pay
Provident Fund = 8.33% of Basic Pay
Net Pay = Basic Pay + Dearness Allowance + House Rent Allowance
Gross Pay = Net Pay - Provident Fund.
2. 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.
Member functions:
input() Store the pan number,name and taxable income
calc() Calculate tax for an employee
display() Output details of an employee
5. Write a program to compute the tax according to the given conditions and display the
output as per given format.
Total Annual Taxable Income Tax Rate
Upto Rs. 1,00,000 No tax
From 1,00,001 to 1,50,000 10% of the income exceeding 100000
From 1,50,001 to 2,50,000 5000+20% of the income exceeding 150000
Above Rs. 2,50,000 25000+30%of the income exceeding 150000
Output:
Pan Number Name Tax-income Tax
- - - -
6. An electronics shop has announced the following discounts on the purchase of certain
items.
Purchase amount in Rs. Discount on Laptop Discount on Desktop PC
0 – 25000 0.0% 5.0%
25001 – 57000 5.0% 7.5%
57001 – 100000 7.5% 10.0%
More than 100000 10.0% 15.0%
7. Write a program based on the above criteria, to input name, address, amount of
purchase and type of purchase (L for Laptop and D for Desktop) by a customer.
Compute and print the discount and net amount to be paid by a customer along with
his name and address.
(Hint: discount = (discount rate/100)*amount of purchase
Net amount = amount of purchase - discount)
Member methods:
A parameterized constructor to initialize the data members.
To accept the details of a student.
To compute average and maximum out of three marks.
To display the name, age, marks in three subjects, maximum and
average.
Write main () to create an object of the class and call the above member
methods.
Member methods:
void input() to input and store the detail of the customer
void compute() to compute the rental charge
Member methods:
void input() - To input and store the accession number, title and
author
void compute() - To accept the number of days late, calculate and
display the fine charged at the rate of Rs. 2 per day.
void display() - To display the details in the following format:
Accession Number Title Author
Write a main method to create an object of the class and call the above
methods.
14.