Professional Documents
Culture Documents
void main ( ) - to create an object named obj and call all the above functions appropriately.
SOLUTION:
Number(long x)
num=(int)x;
int sumOfDigits()
int t=num;
int s=0;
int r=0;
while(t>0)
r=t%10;
s+=r;
t/=10;
I - TECH 1
CLASS BASED PROGRAM WITH SOLUTION
return s;
long factorial()
int f=1;
for(int i=1;i<=num;i++)
f*=i;
return (long)f;
void main()
int sum=obj.sumOfDigits();
long fac=obj.factorial();
I - TECH 2
CLASS BASED PROGRAM WITH SOLUTION
Member Methods:
void accept( ) – to accept the string and a character from the user
void change( ) – to replace the first alphabet of every word with ‘ch’
void next( ) – to change the original string by converting each character to its successive character
(‘z’ changes to ‘a’).
Example: Sample Input: BlueJ is fun Sample Output: CmvfK jt gvo void display( ) – to display
the changed string after calling change( ) and after calling next( )
SOLUTION:
import java.util.*;
String x;
char ch;
void accept()
I - TECH 3
CLASS BASED PROGRAM WITH SOLUTION
x=sc.nextLine();
ch=sc.next().charAt(0);
void change()
x=x+" ";
char b=0;
String str="";
String a="";
for(int i=0;i<x.length();i++)
b=x.charAt(i);
if(b!=' ')
str=str+b;
else
str=str.replace(str.charAt(0),ch);
str=str+" ";
a=a+str;
str="";
System.out.println(a);
void next()
I - TECH 4
CLASS BASED PROGRAM WITH SOLUTION
char b=0;
String str="";
for(int i=0;i<x.length();i++)
b=x.charAt(i);
if(b=='z')
b='a';
str=str+b;
str=str+(char)(b+1);
else
str=str+" ";
System.out.println(str);
void display()
change();
next();
void main()
I - TECH 5
CLASS BASED PROGRAM WITH SOLUTION
ob.accept();
ob.display();
Member Methods void input( ) – to accept the values for the data members
void sumOfDistance( ) – to find the sum of distances and print the same with a suitable message
static void main( ) – to create an object, provide the distances and call respective functions.
SOLUTION:
import java.util.*;
int f1,f2;
int n1,n2;
void input()
I - TECH 6
CLASS BASED PROGRAM WITH SOLUTION
f1=sc.nextInt();
n1=sc.nextInt();
f2=sc.nextInt();
n2=sc.nextInt();
void showDistance()
void sumOfDistance()
int s=(f1*12)+n1+(f2*12)+n2;
int fs=s/12;
int ns=s%12;
ob.input();
ob.showDistance();
I - TECH 7
CLASS BASED PROGRAM WITH SOLUTION
ob.sumOfDistance();
Data members
Member methods
int max(int x, int y) - to find and return the maximum value of the corresponding elements of the arrays
void main( ) - to display both the arrays as well as maximum of the corresponding elements of the
arrays
SOLUTION:
import java.util.*;
Array_Operators()
for(int i=0;i<10;i++)
a[i]=0;
I - TECH 8
CLASS BASED PROGRAM WITH SOLUTION
b[i]=0;
void getlist()
System.out.println("ENTER ARRAYS'ELEMENTS");
for(int i=0;i<10;i++)
a[i]=sc.nextInt();
for(int i=0;i<10;i++)
b[i]=sc.nextInt();
if(x>y)
return x;
return y;
void main()
ob.getlist();
int k=0;
for(int i=0;i<10;i++)
I - TECH 9
CLASS BASED PROGRAM WITH SOLUTION
System.out.println(a[i]);
for(int i=0;i<10;i++)
System.out.println(b[i]);
for(int i=0;i<10;i++)
k=ob.max(a[i],b[i]);
Data member :
Int km , bill ;
String name ;
I - TECH 10
CLASS BASED PROGRAM WITH SOLUTION
SOLUTION:
import java.util.*;
int km,bill;
String name;
carbill(String n,int k)
name=n;
km=k;
void input()
name=sc.nextLine();
km=sc.nextInt();
void calculate()
if(km>0&&km<=1)
bill=km*25;
I - TECH 11
CLASS BASED PROGRAM WITH SOLUTION
else if(km>1&&km<=5)
bill=25+(km-1)*20;
else if(km>5&&km<=15)
bill=25+4*20+(km-5)*15;
else if(km>15)
bill=25+4*20+10*15+(km-15)*10;
void display()
void main()
ob.input();
ob.calculate();
ob.display();
6. A class Parallelogram has been defined to calculate the perimeter and area of a parallelogram.
I - TECH 12
CLASS BASED PROGRAM WITH SOLUTION
Member function/methods
void accept() : to accept values of data members from the user.
void accept( ), double calculatePeri( ), double calculateArea( ) and void show( ). Define main( ) to create
an object of Parallelogram and call the member methods accordingly to calculate and display perimeter
and area of it.
SOLUTION:
import java.util.*;
double base;
double side;
double height;
void accept()
base=sc.nextDouble();
side=sc.nextDouble();
height=sc.nextDouble();
I - TECH 13
CLASS BASED PROGRAM WITH SOLUTION
double calculatePeri()
double perimeter=2*(base+side);
return perimeter;
double calculateArea()
double area=base*height;
return area;
void show()
double peri=calculatePeri();
double area=calculateArea();
void main()
ob.accept();
ob.show();
I - TECH 14
CLASS BASED PROGRAM WITH SOLUTION
Member Methods:
FriuitJuice() – default constructor to initialize integer data members to zero and string data members
to “”.
void input() – to input and store the product code, flavor, pack type, pack size and product price.
void display() – to display the product code, flavor, pack type, pack size and product price.
SOLUTION:
import java.util.*;
int product_code;
String flavour;
String pack_type;
int pack_size;
int product_price;
FruitJuice()
product_code=0;
flavour="";
pack_type="";
pack_size=0;
I - TECH 15
CLASS BASED PROGRAM WITH SOLUTION
product_price=0;
void input()
product_code=sc.nextInt();
flavour=sc.nextLine();
pack_type=sc.nextLine();
sc.next();
pack_size=sc.nextInt();
product_price=sc.nextInt();
void discount()
product_price=product_price-10;
void display()
System.out.println(product_code+"\t"+flavour+"\t"+pack_type+"\t"+pack_size+"\t"+product_price);
void main()
I - TECH 16
CLASS BASED PROGRAM WITH SOLUTION
ob.input();
ob.discount();
ob.display();
float rating – to store the popularity rating of the movie. (minimum rating = 0.0 and maximum rating =
5.0)
Member Methods:
(i) movieMagic() :Default constructor to initialize numeric data members to 0 and String data member
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.
SOLUTION:
import java.util.*;
I - TECH 17
CLASS BASED PROGRAM WITH SOLUTION
int year;
String title;
float rating;
movieMagic()
year=0;
title="";
rating=0.0f;
void accept()
title=sc.nextLine();
rating=sc.nextFloat();
year=sc.nextInt();
void display()
if(rating<0.0&&rating>5.0)
System.exit(0);
else
if(rating>=0.0&&rating<=2.0)
I - TECH 18
CLASS BASED PROGRAM WITH SOLUTION
System.out.println("Flop");
else if(rating>2.0&&rating<=3.4)
System.out.println("Semi-hit");
else if(rating>3.4&&rating<=4.5)
System.out.println("Hit");
else if(rating>4.5)
System.out.println("Super Hit");
void main()
ob.accept();
ob.display();
int hours – To store the number of hours the vehicle is parked in the parking lot
Member methods:
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.
I - TECH 19
CLASS BASED PROGRAM WITH SOLUTION
Write a main method to create an object of the class and call the above methods
SOLUTION:
import java.util.*;
int vno;
int hours;
double bill;
void input()
System.out.println("ENTER VEHICLE NUMBER AND NO. OF HOURS FOR WHICH IT WAS PARKED IN
THE PARKING LOT");
vno=sc.nextInt();
hours=sc.nextInt();
void calculate()
if(hours>0&&hours<=1)
bill=3.0*hours;
else
bill=3.0*1+(hours-1)*1.50;
void display()
I - TECH 20
CLASS BASED PROGRAM WITH SOLUTION
System.out.println(vno+"\t"+hours+"\t"+"RS."+bill+"0");
void main()
ob.input();
ob.calculate();
ob.display();
Member Methods:
Price Discount
More than Rs 1000 and less than or equal to Rs 3000 10% of price
(iv) void display() To display the 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.
I - TECH 21
CLASS BASED PROGRAM WITH SOLUTION
SOLUTION:
import java.util.*;
String bname;
double price;
BookFair()
bname="";
price=0.0d;
void input()
bname=sc.nextLine();
price=sc.nextDouble();
void calculate()
double p=0.0d;
if(price>0.0&&price<=1000.0)
p=price-(0.02*price);
else if(price>1000.0&&price<=3000.0)
p=price-(0.1*price);
I - TECH 22
CLASS BASED PROGRAM WITH SOLUTION
else if(price>3000.0)
p=price-(0.15*price);
System.out.println("RS. "+p+"0");
void display()
System.out.print(bname +"\t");
calculate();
void main()
ob.input();
ob.calculate();
ob.display();
Methods:
void accept() : to accept the name of the customer and number of units consumed.
I - TECH 23
CLASS BASED PROGRAM WITH SOLUTION
Write a main() method to create an object of the class and call the above member functions.
SOLUTION:
import java.util.*;
String n;
int units;
double bill;
void accept()
n=sc.nextLine();
units=sc.nextInt();
I - TECH 24
CLASS BASED PROGRAM WITH SOLUTION
void calculate()
if(units>0&&units<=100)
bill=units*2.0;
else if(units>100&&units<=300)
bill=100*2.0+(units-100)*3.0;
else if(units>300)
{bill=100*2.0+200*3.0+(units-300)*5.0;
bill=bill+0.025*bill;
void print()
void main()
ob.accept();
ob.calculate();
ob.print();
I - TECH 25
CLASS BASED PROGRAM WITH SOLUTION
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)
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.
SOLUTION:
import java.util.*;
String name;
I - TECH 26
CLASS BASED PROGRAM WITH SOLUTION
String coach;
long mobno;
int amt;
int totalamt;
void accept()
name=sc.nextLine();
coach=sc.nextLine();
mobno=sc.nextLong();
amt=sc.nextInt();
void update()
if(coach.equalsIgnoreCase("First_Ac"))
totalamt=amt+700;
else if(coach.equalsIgnoreCase("Second_Ac"))
totalamt=amt+500;
else if(coach.equalsIgnoreCase("Third_Ac"))
totalamt=amt+250;
else if(coach.equalsIgnoreCase("sleeper"))
totalamt=amt;
void display()
I - TECH 27
CLASS BASED PROGRAM WITH SOLUTION
System.out.println(name+"\t"+coach+"\t"+mobno+"\t"+totalamt);
void main()
ob.accept();
ob.update();
ob.display();
12. Design a class named ShowRoom with the following description: [2019]
Instance variables/data members:
Member methods:
void calculate() : to calculate discount on the cost of purchased items, based on the following criteria:
More than Rs. 10000 and less than or equal to Rs. 20000 10%
I - TECH 28
CLASS BASED PROGRAM WITH SOLUTION
More than Rs. 20000 and less than or equal to Rs. 35000 15%
void display() : to display customer name, mobile number, amount to be paid after discount.
Write a main() :method to create an object of the class and call the above member methods.
SOLUTION:
import java.util.*;
String name;
long mobno;
double cost;
double dis;
double amount;
ShowRoom()
name="";
mobno=0L;
cost=0.0d;
dis=0.0d;
amount=0.0D;
void input()
I - TECH 29
CLASS BASED PROGRAM WITH SOLUTION
name=sc.nextLine();
mobno=sc.nextLong();
cost=sc.nextDouble();
void calculate()
if(cost>0.0&&cost<=10000.0)
dis=0.05*cost;
else if(cost>10000.0&&cost<=20000.0)
dis=0.1*cost;
else if(cost>20000.0&&cost<=35000.0)
dis=0.15*cost;
else if(cost>35000.0)
dis=0.2*cost;
amount=cost-dis;
void display()
System.out.println(name+"\t"+mobno+"\t"+"RS."+amount+"0");
void main()
I - TECH 30
CLASS BASED PROGRAM WITH SOLUTION
ob.input();
ob.calculate();
ob.display();
I - TECH 31