Class, Constructor and Objects

Section B

2005

Question 4
Write a class with name employee and basic as its data member.
Memeber method: compute() - to find the net 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. Gross Pay = Basic Pay + Dearness Allowance + HRA.
Net Pay = Gross Pay - Provident Fund. [15]


import java.util.*;
public class employee
{
double basic; //data member
public void compute() //member method
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter basic pay : ");
basic=sc.nextDouble();
double da,hra,pf,gp,np; //local variables.
da=basic*25/100.0;
hra=basic*15/100.0;
pf=basic*8.33/100.0;
gp=basic+da+hra;
np=np-pf;
System.out.println("Basic Pay : "+basic);
System.out.println("Gross Pay : "+gp);
System.out.println("Net Pay : "+np);
}
public static void main()
{
employee ob=new employee();
ob.compute();
}
}

Variable Description

VariableTypeDescription
basicdoubleTo store basic pay
dadoubleTo store dearness allowance
hradoubleTo store house rent allowance
pfdoubleTo store provident fund
gpdoubleTo store gross pay
npdoubleTo store net pay

2007

Question 4
Define a class salary described as below:-
Data Members:
Name, Address, Phone, Subject Specialization, Monthly Salary, Income Tax.
Member methods:
(i) To accept the details of a teacher including the monthly salary.
(ii) To display the details of the teacher.
(iii) To compute annual Income Tax as 5% of annual salary above Rs. 1,75,000/-
Write main method to create object and call the above member methods. [15]

import java.util.*;
public class salary
{
String name;
String address;
long phone;
String subject;
double msalary;
double itax;
public void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name, address, phone, subject and monthly salary");
name=sc.nextLine();
address=sc.nextLine();
phone=sc.nextLong();
subject=sc.nextLine();
msalary=sc.nexDouble();
}
public void display()
{
System.out.println("Name : "+name);
System.out.println("Address : "+address);
System.out.println("Phone : "+phone);
System.out.println("Subject : "+subject);
System.out.println("Monthly salary : "+msalary);
}
public void compute()
{
double asalary=msalary*12;
if(asalary>175000)
itax=(asalary-175000)*5.0/100;
else
itax = 0.0;
System.out.println("Income Tax : "+itax);
}
public static void main()
{
salary ob=new salary();
ob.accept();
ob.display();
ob.compute();
}
}

2008

Question 4
Define a class employee having the following description:
Data members/Instance Variables:
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:
input() Store the pan number,name and taxable income
calc() Calculate tax for an employee
display() Output details of an employee
Write a program to compute tax according to the given conditions and display output as given format. [15] Total Taxable IncomeTax Rate
Upto Rs. 1,00,000No tax
From 1,00,001 to 1,50,00010% of the income exceeding Rs. 1,00,000
From 1,50,001 to 2,50,000Rs. 5,000+20% of the income exceeding 1,50,000
Above Rs.2,50,000Rs. 25,000+30% of income exceeding 2,50,000
Output:
Pan Name Tax
xxxx xxxx xx

import java.util.*;
public class employee
{
int pan;
String name;
double taxincome;
double tax;
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter pan number, name and taxincome");
pan=sc.nexInt();
name=sc.nextLine();
taxincome=sc.nexDouble();
}
public void calc()
{
if(taxincome<=100000)
tax=0.0;
else if(taxincome<=150000)
tax=(taxincome-100000)*10/100.0;
else if(taxincome<=250000)
tax=5000+(taxincome-150000)*20/100.0;
else
tax=25000+(taxincome-250000)*30/100.0;
}
public void display()
{
System.out.println(“Pan Number \t Name \t Tax-income \t Tax”);
System.out.println(pan+"\t"+name+"\t"+taxincome+"\t"+tax);
}
public static void main()
{
employee ob=new employee();
ob.input();
ob.calc();
ob.display();
}
}

2010

Question 5
Define a class student described as below:
Data members/Instance variables:
name, age, m1,m2,m3 (marks in 3 subjects), maximum, average.
Member methods:
(i) A parameterized constructor to initialize the data members.
(ii) To accept the details of a student.
(iii) To compute average and maximum out of three marks.
(iv) 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. [15]

import java.util.*;
public class student
{
String name;
int age;
double m1;
double m2;
double m3;
double maximum;
double average;
public student(String a,int b,double c,double d,double e)
{
name=a;
age=b;
m1=c;
m2=d;
m3=e;
}
public void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name, age and three marks ");
name=sc.nextLine();
age=sc.nexInt();
m1=sc.nexDouble();
m2=sc.nexDouble();
m3=sc.nexDouble();
}
public void compute()
{
average=(m1+m2+m3)/3.0;
if(m1>m2 &&m1>m3)
maximum=m1;
else if(m2>m1&& m2>m3)
maximum=m2;
else
maximum=m3;
}
public void display()
{
System.out.println("Name : "+name);
System.out.println("Age : "+age);
System.out.println("Marks : "+m1+", "+m2+", "+m3);
System.out.println("Average : "+average);
System.out.println("Maximum : "+maximum);
}
public static void main()
{
student ob=new student( “”,0,0.0,0.0,0.0);
ob.accept();
ob.compute();
ob.display();
}
}

2011

Question 4
Define a class called mobike with following description:
Instance variables/data members:
int bno : to store the bike’s 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 total charge
Member methods:
void input() : to input and store the detail of the customer
void compute() : to compute the rental charge
The rent is charged as follows:
First five daysRs. 500 per day
Next five daysRs. 400 per day
Rest of the daysRs. 200 per day
void display() : to display the details in the following format:
Bike No.Phone No.NameNo. of daysCharge
xxxxxxxxxxxxxxxxxxxxxx[15]

import java.util.*;
public class mobike
{
int bno;
int phno;
String name;
int days;
int charge;
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.println( “Enter bike number, phone number, name and number of days”);
bno=sc.nexInt();
phno=sc.nexInt();
name=sc.nextLine();
days=sc.nexInt();
}
public void compute()
{
if(days<=5)
charge=days*500;
else if(days<=10)
charge=5*500 + (days-5)*400;
else
charge=5*500 + 5*400 + (days-10)*200;
}
public void display()
{
System.out.println(“Bike No.\tPhone No.\tName\tNo. of days\tCharge”);
System.out.println(bno +“\t”+ phno +“\t”+ name +“\t”+ days +“\t”+ charge);
}
public static void main()
{
mobike ob=new mobike();
ob.input();
ob.compute();
ob.display();
}
}

2012

Question 4
Define a class Library with following description:
Instance Variables / Data Members:
int acc_num - stores the accession number of the book
String title - stores the title of the book
String author - stores the name of the author
Member methods:
1) void input() - To input and store the accession number, title and author
2) void compute() - To accept the number of days late, calculate and display the fine charged at the rate of Rs. 2 per day.
3) void display() - To display the details in the following format:
Accession NumberTitle Author
--------------------- ------
xxxxxxxxxxxxxxxxxxxxx xxxxxx
Write a main method to create an object of the class and call the above methods. [15]

import java.util.*;
public class Library
{
int acc_num;
String title;
String author;
Scanner sc=new Scanner(System.in);
public void input()
{
System.out.println(“Enter number, title and author”);
acc_num=sc.nextInt();
title = sc.nextLine();
author = sc.nextLine();
}
public void compute()
{
System.out.println(“Enter number of days late”);
int days=sc.nexInt();
double fine = days*2.0;
System.out.println(“Fine :”+fine);
}
public void display()
{
System.out.println(“Accession number \t Title \t Author”);
System.out.println(“-------------------------\t ----- \t --------”);
System.out.println(acc_num + “\t ”+ title + “\t” +author);
}
public static void main()
{
Library ob=new Library();
ob.input();
ob.compute();
ob.display();
}
}

2013

Question 4
Define a class named FruitJuice with the following description :
Instance variables/data members:
int product_code - stores the product code number
String flavour - stores the flavour of the juice(eg.orange,apple, etc)
String pack _type - stores the type of packaging(eg. Tetra pack, PET bottle, etc)
int pack_size - stores package size(eg.200ml,400ml,etc)
int product_price - stores the price of the product
Member methods:
(i) FruitJuice() - Default constructor to initialize integer data members to 0 and string data members to “”.
(ii) void input() - To input and store the product code, flavour, pack type, pack size and product price.
(iii) void discount() - To reduce the price by Rs. 10;
(iv) void display() - To display code, flavour, pack type, pack size and price [15]

import java.util.*;
public class FruitJuice
{
int product_ code;
String flavour;
String pack _type;
int pack_size;
int product_price;
public FruitJuice()
{
product_ code=0;
flavour="";
pack _type="";
pack_size=0;
product_price=0;
}
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter code, flavor, type, size and price of products");
product_ code=sc.nextInt();
flavour=sc.nextLine();
pack _type=sc.nextLine();
pack_size=sc.nextInt();
product_price=sc.nextInt();
}
public void discount()
{
product_price = product_price -10;
}
public void display()
{
System.out.println("Product code :"+product_code);
System.out.println("Flavour :"+flavour);
System.out.println("Pack type :"+pack _type);
System.out.println("Pack size :"+pack_size);
System.out.println("Product price :"+product_price);
}
public static void main()
{
FruitJuice ob=new FruitJuice();
ob.input();
ob.discount();
ob.display();
}
}

2014

Question 4
Define a class named movieMagic with the following description:
instance variables / data members:
int year - to store the year of release of a movie
String title - to store the title of the movie
float rating - to store the popularity rating of the movie
(minimum rating =0.0 and maximum rating =5.0)
Member methods:
(i) movieMagic() - Deafault constructor to initialize numeric data members to 0 and String data members to “”
(ii) void accept() - To input and store year, title and rating.
(iii) void display() - To display the title of a movie and a message based on the rating as per the table below:
0.0 to 2.0 Flop
2.1 to 3.4 Semi hit
3.5 to 4.5 Hit
4.6 to 5.0 Super hit
Wrtie a main method to create an object of the class and call the above member methods [15]

import java.util.*;
public class movieMagic
{
int year;
String title;
float rating;
public movieMagic()
{
year=0;
title=””;
rating=0.0f;
}
public void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter year, title and rating");
year= sc.nextInt();
title= sc.nextLine();
rating=sc.nextFloat();
}
public void display()
{
System.out.println("Title: "+title);
if(rating<=2.0)
System.out.println("Flop");
else if(rating<=3.4)
System.out.println("Semi hit");
else if(rating<=4.5)
System.out.println("Hit");
else if(rating<=5)
System.out.println("Super Hit");
else
System.out.println("Wrong Input");
}
public static void main()
{
movieMagic ob=new movieMagic();
ob.accept();
ob.display();
}
}

2015

Question 4
Define a class ParkingLot with the following description:
Instance variables/data memebers:
int vno – To store the vehicle number
int hours – To store the number of hours the vehicle parked in the parking lot
double bill – To store the bill amount
Member methods:
void input() – To input and store the vno and hours.
void calculate() – To compute the parking charge at the rate of Rs. 3 for the first hour
or part thereof, and Rs. 1.50 for each additional hour or part thereof.
void display() – To display the details
Write a main method to create an object of the class and call the above methods. [15]

import java.util.*;
public class ParkingLot
{
int vno;
int hours;
double bill;
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter vehicle number and parking hours");
vno= sc.nextInt();
hours= sc.nextInt();
}
public void calculate()
{
if(hours==1)
bill=3.0;
else
bill=3.0+(hours-1)*1.5;
}
public void display()
{
System.out.println("Vehicle Number: "+vno);
System.out.println("Hours: "+hours);
System.out.println("Charge: "+bill);
}
public static void main()
{
ParkingLot ob = new ParkingLot();
ob.input();
ob.calculate();
ob.display();
}
}

2016

Question 4
Define a class named BookFair with the following description:
Instance variables / Data members:
String Bname – stores the name of the book
double price – stores the price of the book
Member methods:
(i) BookFair() - Default constructor to initialize data members
(ii) void Input() - To input and store the name and the price of the book
(iii) void calculate() - To calculate the price after discount. Discount is calculated based on the following
PriceDiscount
Less than or equal to Rs. 10002% of price
More than Rs. 1000 and less than or equal to Rs. 300010% of price
More than Rs. 300015% of price
(iv) void display() - To display name and price of the book after discount
Write a main method to create an object of the class and call the above member methods. [15]

import java.util.*;
public class BookFair
{
String Bname;
double price;
public BookFair()
{
Bname=“”;
price=0.0;
}
public void Input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name and price");
Bname= sc.nextLine();
price= sc.nextDouble();
}
public void calculate()
{
double dis;
if(price<=1000)
dis=price*2/100.0;
else if(price<=3000)
dis=price*10/100.0;
else
dis=price*15/100.0;
price=price-dis;
}
public void display()
{
System.out.println("Name: "+Bname);
System.out.println("Price : "+price);
}
public static void main()
{
BookFair ob=new BookFair();
ob.Input();
ob.calculate();
ob.display();
}
}

2017

Question 4
Define a class ElectricBill with the following specification:
Instance variables / Data members:
String n – to store the name of the customer
int units – to store the number units consumed
double bill – stores the amount to be paid
Member methods:
(i) void accept() - To accept name of the customer and number of units consumed
(ii) void calculate() - To calculate the bill as per the following tariff:
Number of unitsRate per unit
First 100 unitsRs.2.00
Next 200 unitsRs.3.00
Above 300 unitsRs.5.00
A surcharge of 2.5% charged if the number of units is above 300.
(iii) void print() - To print the details as follows:
Name of the customer: …………………….
Number units consumed: …………
Bill amount: …………………
Write a main method to create an object of the class and call the above member methods. [15]

import java.util.*;
public class ElectricBill
{
String n;
int units;
double bill;
public void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name and units");
n= sc.nextLine();
units=sc.nexInt();
}
public void calculate()
{
if(units<=100)
bill=units*2.0;
else if(units<=300)
bill=100*2.0 + (units-100)*3.0;
else
{
bill=100*2.0 + 200*3.0 + (units-300)*5.0;
bill=bill+bill*2.5/100;
}
}
public void print()
{
System.out.println("Name of the customer: "+n);
System.out.println("Number units consumed: "+units);
System.out.println("Bill amount: "+bill);
}
public static void main()
{
ElectricBill ob = new ElectricBill();
ob.accept();
ob.calculate();
ob. print();
}
}

2018

Question 4
Design a class RailwayTicket with following description:
Instance variables/data members:
String name : To store the name of the customer
String coach : To store the type of coach customer wants to travel
long mobno : To store customer’s mobile number
int amt : To store basic amount of ticket
int totalamt : To store the amount to be paid after updating the original amount
Member methods:
void accept() - To take input for name, coach, mobile number and amount.
void update() - To update the amount as per the coach selected
(extra amount to be added in the amount as follows)
Types of CoachesAmount
First_AC700
Second_AC500
Third_AC200
SleeperNone
void display() - To display all details of a customer such as name, coach, total amount and mobile number
Write a main method to create an object of the class and call the above member methods. [15]

import java.util.*;
public class RailwayTicket
{
String name;
String coach;
long mobno;
int amt; int totalamt;
public void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name, coach, mobile number and basic amount");
name= sc.nextLine(); coach= sc.nextLine();
mobno=sc.nextLong();
amt=sc.nextInt();
}
public void update()
{
if(coach.equals(“First_AC”))
totalamt=amt+700;
else if(coach.equals(“Second_AC”))
totalamt=amt+500;
else if(coach.equals(“Third_AC”))
totalamt=amt+200;
else if(coach.equals(“Sleeper”))
totalamt=amt;
}
public void display()
{
System.out.println("Name: "+name);
System.out.println("Coach: "+coach);
System.out.println("Mobile Number: "+mobno);
System.out.println("Basic amount: "+amt);
System.out.println("Total amount: "+totalamt);
}
public static void main()
{
RailwayTicket ob=new RailwayTicket();
ob.accept();
ob.update(); ob.display();
}
}

2019

Question 4
Design a class ShowRoom with following description:
Instance variables/data members:
String name : To store the name of the customer
long mobno : To store customer’s mobile number
double cost : To store cost of the items purchased
double dis : To store discount
double amount : To store amount to be paid after discount
Member methods:
ShowRoom - default constructor to initialize data members
void input() - To input customer name, mobile number and cost.
void calculate() - To calculate dicount on cost of purchased items based on following citeria:
(extra amount to be added in the amount as follows)
CostDiscount rate
Less than equal to Rs. 100005%
More than 10000 and less than equal to 2000010%
More than 20000 and less than equal to 3500015%
More than 3500010%

void display() - To display customer name, mobile number, amount to be paid after discount
Write a main method also to create an object of the class and call the above member methods. [15]

import java.util.*;
public class ShowRoom
{
String name;
long mobno;
double cost;
double dis;
double amount;
public ShowRoom()
{
name=””;
mobno=0L;
cost=0.0;
dis=0.0;
amount=0.0;
}
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter name, mobile number and cost");
name= sc.nextLine();
mobno=sc.nextLong();
cost=sc.nextDouble();
}
public void calculate()
{
if(cost<=10000)
amount=cost-cost*5/100.0;
else if(cost<=20000)
amount=cost-cost*10/100.0;
else if(cost<=35000)
amount=cost-cost*15/100.0;
else
amount=cost-cost*20/100.0;
}
public void display()
{
System.out.println("Name: "+name);
System.out.println("Moile number: "+mobno);
System.out.println("Amount to be paid: "+amount);
}
public static void main()
{
ShowRoom ob=new ShowRoom();
ob.input();
ob.calculate();
ob.display();
}
}

2020

Question 4
A private Cab service company provides service within the city at the following rates:
AC CARNON AC CAR
UPTO 5 KM ₹ 150/-₹ 120/-
BEYOND 5 KM₹ 10/-PER KM₹ 08/- PER KM
Design a class CabService with the following description:
Member variables /data members:
String car_type - To store the type of car (AC or NON AC)
double km - To store the kilometer travelled
double bill - To calculate and store the bill amount
Member methods :
CabService() - Default constructor to initialize data members.
String data members to "" and
double data members to 0.0
void accept () - To accept car_type and km (using Scanner class only).
void calculate () - To calculate the bill as per the rules given above.
void display() - To display the bill as per the following format:
CAR TYPE:
KILOMETER TRAVELLED:
TOTAL BILL:
Create an object of the class in the main method and invoke the member methods. [15]

import java.util.*;
public class CabService
{
String car_type;
double km;
double bill;
public CabService()
{
car_type=””;
km=0.0;
bill=0.0;
}
public void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter car type [AC/NON-AC] and kilometers");
car_type= sc.nextLine();
km=sc.nextDouble();
}
public void calculate()
{
if(car_type.equals("AC"))
{
if(km<=5)
bill=150.0;
else
bill=150+(km-5)*10.0;
}
else if(car_type.equals("NON-AC"))
{
if(km<=5)
bill=120.0;
else
bill=120+(km-5)*8.0;
}
}
public void display()
{
System.out.println("CAR TYPE: "+car_type);
System.out.println("KILOMETER TRAVELLED: "+km);
System.out.println("TOTAL BILL: "+bill);
}
public static void main()
{
CabService ob=new CabService();
ob.accept();
ob.calculate();
ob.display();
}
}

Question 7
Design a class to overload a method Number( ) as follows:
(i) void Number (int num , int d) - To count and display the frequency of a digit in a number.
Example:
num = 2565685
d = 5
Frequency of digit 5 = 3
(ii) void Number (int n1) - To find and display the sum of even digits of a number.
Example:
n1 = 29865
Sum of even digits = 16
Write a main method to create an object and invoke the above methods. [15]

public class Overload
{
public void Number(int num,int d)
{
int i=num,ld,c=0;
while(i>0)
{
ld=i%10;
if(ld==d)
c++;
i/=10;
}
System.out.println("Frequency of digit "+d+" = "+c);
}
public void Number(int n1)
{
int i=n1,d,s=0;
while(i>0)
{
d=i%10;
if(d%2==0)
s+=d;
i/=10;
}
System.out.println(("Sum of even digits = "+s);
}
public static void main()
{
Overload ob=new Overload();
Scanner sc=new Scanner(System.in);
System.out.println("Enter number and digit to be counted");
int num=sc.nextInt();
int d=sc.nextInt();
ob.Number(num,d);
System.out.println("Enter number to find sum of even digits");
int n1=sc.nextInt();
ob.Number(n1);
}
}