You are on page 1of 31

CLASS BASED PROGRAM WITH SOLUTION

1. Write a program in Java using a class with the following specifications.

Class name : Number Data members : private num of integer type.

Member functions: Number (long x) - constructor to initialize num to x;

int sumOfDigits ( ) - to calculate the sum of the digits of num.

long factorial ( ) - to display the factorial of a number

void main ( ) - to create an object named obj and call all the above functions appropriately.

SOLUTION:

public class Number//Q1

private int num;

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()

Number obj=new Number((long)10);

int sum=obj.sumOfDigits();

System.out.println("SUM OF DIGITS OF "+num+" = "+sum);

long fac=obj.factorial();

System.out.println("FACTORIAL OF "+num+" = "+fac);

I - TECH 2
CLASS BASED PROGRAM WITH SOLUTION

2. Design a class ModString with the following specifications

Class name – ModString

Data Members: String x – to store the string

char ch – to store the character

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’

Example : Sample Input: BlueJ is fun

ch = ‘C’ Sample Output: ClueJ Cs Cun

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( )

void main( ) – to create an object and call the respective functions

SOLUTION:

import java.util.*;

public class ModString

String x;

char ch;

void accept()

Scanner sc=new Scanner(System.in);

System.out.println("ENTER A STRING AND A CHARACTER RESPECTIVELY");

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;

else if(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

ModString ob=new ModString();

ob.accept();

ob.display();

3. The sum of two distances is calculated as:

Distance 1 = 10 feet 24 inches

Distance 2 = 5 feet 16 inches

Sum of distances = 18 feet 4 inches (1 feet = 12 inches)

Design a class Distance with the following specifications

Class Name : Distance

Data members int f1, f2 – to store the distance in feet

int n1, n2 – to store the distance in inches

Member Methods void input( ) – to accept the values for the data members

void showDistance( ) – to display the distances with suitable message

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.*;

public class Distance

int f1,f2;

int n1,n2;

void input()

I - TECH 6
CLASS BASED PROGRAM WITH SOLUTION

Scanner sc=new Scanner(System.in);

System.out.println("ENTER VALUES OF 2 DISTANCES IN TERMS OF FEET AND INCHES


RESPECTIVELY");

f1=sc.nextInt();

n1=sc.nextInt();

f2=sc.nextInt();

n2=sc.nextInt();

void showDistance()

System.out.println("Distance 1 = "+f1+" feet "+n1+" inches");

System.out.println("Distance 2 = "+f2+" feet "+n2+" inches");

void sumOfDistance()

int s=(f1*12)+n1+(f2*12)+n2;

int fs=s/12;

int ns=s%12;

System.out.println("Sum of distances = "+fs+" feet "+ns+" inches");

static void main()

Distance ob=new Distance();

ob.input();

ob.showDistance();

I - TECH 7
CLASS BASED PROGRAM WITH SOLUTION

ob.sumOfDistance();

4. Define a class with the following specifications

Class name - Array Operators

Data members

int a[ ] - private data member to store 10 integers

int b[ ] - private data member to store 10 integers

Member methods

Array Operations( ) - default constructor to initialize a[ ] and b[ ] to zero

void getlist( ) - to accept arrays from the user

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.*;

public class Array_Operators

int a[]=new int[10];

int b[]=new int[10];

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()

Scanner sc=new Scanner(System.in);

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();

int max(int x,int y)

if(x>y)

return x;

return y;

void main()

Array_Operators ob=new Array_Operators();

ob.getlist();

int k=0;

System.out.println("ARRAY a[] = ");

for(int i=0;i<10;i++)

I - TECH 9
CLASS BASED PROGRAM WITH SOLUTION

System.out.println(a[i]);

System.out.println("ARRAY b[] = ");

for(int i=0;i<10;i++)

System.out.println(b[i]);

System.out.println("MAXIMUM OF CORRESPONDING ELEMENTS");

for(int i=0;i<10;i++)

k=ob.max(a[i],b[i]);

System.out.println("MAXIMUM OF "+a[i]+" and "+b[i]+" = "+k);

5. A cab service has following rates for their customs.

No. of KM. Rate

First 1 km Rs/- 25 per km

Next 4 km Rs/- 20 per km

Next 10 km Rs/- 15 per km

Above 15 km Rs 10/- per km

Define a class carbill.

Data member :

Int km , bill ;

String name ;

Parametric constructer : (name ,km) initialize the data member

Member method : - void input ( ) : - To input the km and name.

void calculate ( ) : - To calculate bill.

I - TECH 10
CLASS BASED PROGRAM WITH SOLUTION

void display ( ) : - to display the details name, km and total bill

void main ( ) :- to call all the method

SOLUTION:

import java.util.*;

public class carbill

int km,bill;

String name;

carbill(String n,int k)

name=n;

km=k;

void input()

Scanner sc=new Scanner(System.in);

System.out.println("ENTER NAME AND KM TRAVELLED");

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()

System.out.println("NAME \t KM TRAVELLED \t TOTAL BILL");

System.out.println(name+"\t"+km+"\t"+" RS. "+bill);

void main()

carbill ob=new carbill(null,0);

ob.input();

ob.calculate();

ob.display();

6. A class Parallelogram has been defined to calculate the perimeter and area of a parallelogram.

Class name : Parallelogram

Data members /instance variables :

double base : to store base of a parallelogram


double side : to store side of a parallelogram

double height : to store height 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.

double calculatePeri () : calculate and return the area of the parallelogram.

double calculateArea () : to calculate and return the area of the parallelogram.

void show() : to display the perimeter and area of the parallelogram.

Specify the class Parallelogram giving details of the functions

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.

Area of Parallelogram = base * height Perimeter of parallelogram = 2(base + side)

SOLUTION:

import java.util.*;

public class Parallelogram

double base;

double side;

double height;

void accept()

Scanner sc=new Scanner(System.in);

System.out.println("ENTER VALUES OF BASE,SIDE AND HEIGHT OF PARALLELOGRAM IN cm ONLY");

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();

System.out.println("PERIMETER OF THE PARALLELOGRAM ="+peri+" cm");

System.out.println("AREA OF THE PARALLELOGRAM ="+area+" sq.cm");

void main()

Parallelogram ob=new Parallelogram();

ob.accept();

ob.show();

7. Define a class called FruitJuice with the following description: [2013]


Instance variables/data members:

I - TECH 14
CLASS BASED PROGRAM WITH SOLUTION

int product_code – stores the product code number

String flavour – stores the flavor of the juice.(orange, apple, etc)

String pack_type – stores the type of packaging (tetra-pack, bottle etc)

int pack_size – stores package size (200ml, 400ml etc)

int product_price – stores the price of the product

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 discount() – to reduce the product price by 10.

void display() – to display the product code, flavor, pack type, pack size and product price.

SOLUTION:

import java.util.*;

public class FruitJuice

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()

Scanner sc=new Scanner(System.in);

System.out.println("ENTER PRODUCT CODE,FLAVOUR,PACK TYPE,PACK SIZE AND PRODUCT PRICE");

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");

System.out.println(product_code+"\t"+flavour+"\t"+pack_type+"\t"+pack_size+"\t"+product_price);

void main()

FruitJuice ob=new FruitJuice();

I - TECH 16
CLASS BASED PROGRAM WITH SOLUTION

ob.input();

ob.discount();

ob.display();

8. Define a class named movieMagic with the following description: [2014]


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() :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.

Rating Message to be displayed

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

SOLUTION:

import java.util.*;

public class movieMagic

I - TECH 17
CLASS BASED PROGRAM WITH SOLUTION

int year;

String title;

float rating;

movieMagic()

year=0;

title="";

rating=0.0f;

void accept()

Scanner sc=new Scanner(System.in);

System.out.println("ENTER YEAR OF RELEASE,TITLE AND RATING OF MOVIE");

title=sc.nextLine();

rating=sc.nextFloat();

year=sc.nextInt();

void display()

if(rating<0.0&&rating>5.0)

System.exit(0);

else

System.out.println("TITLE OF THE MOVIE : "+title);

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()

movieMagic ob=new movieMagic();

ob.accept();

ob.display();

8. Define a class Parking Lot with the following description: [2015]


Instance variables/data members:

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 methods:

void input() – To input and store 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 detail

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.*;

public class Parking_Lot

int vno;

int hours;

double bill;

void input()

Scanner sc=new Scanner(System.in);

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()

System.out.println("VEHICLE NO. \t NO. OF HOURS \t TOTAL BILL");

I - TECH 20
CLASS BASED PROGRAM WITH SOLUTION

System.out.println(vno+"\t"+hours+"\t"+"RS."+bill+"0");

void main()

Parking_Lot ob=new Parking_Lot();

ob.input();

ob.calculate();

ob.display();

9. Define a class named BookFair with the following description: [2016]


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 criteria.

Price Discount

Less than or equal to Rs 1000 2% of price

More than Rs 1000 and less than or equal to Rs 3000 10% of price

More than Rs 3000 15% 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.*;

public class BookFair

String bname;

double price;

BookFair()

bname="";

price=0.0d;

void input()

Scanner sc=new Scanner(System.in);

System.out.println("ENTER NAME AND PRICE OF THE BOOK");

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.println("NAME \t PRICE OF BOOK AFTER DISCOUNT");

System.out.print(bname +"\t");

calculate();

void main()

BookFair ob=new BookFair();

ob.input();

ob.calculate();

ob.display();

10. Define a class ElectricBill with the following specifications: [2017]


class : ElectricBill

Instance variables/data members:

String n : to store the name of the customer.

int units : to store the number of units consumed.

double bill : to store the amount to be paid.

Methods:

void accept() : to accept the name of the customer and number of units consumed.

I - TECH 23
CLASS BASED PROGRAM WITH SOLUTION

void calculate() : to calculate the bill as per the following tariff:

Number of units – Rate per unit

First 100 units – Rs. 2.00


Next 200 units – Rs. 3.00
Above 300 units – Rs. 5.00
A surcharge of 2.5% charged if the number of units consumed is above 300 units.

void print() : To print the details as follows:

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 functions.

SOLUTION:

import java.util.*;

public class ElectricBill

String n;

int units;

double bill;

void accept()

Scanner sc=new Scanner(System.in);

System.out.println("ENTER NAME AND NO. OF ELECTRIC UNITS CONSUMED");

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()

System.out.println("Name of the customer: "+n);

System.out.println("Number of units consumed: "+units);

System.out.println("Bill amount: RS. "+bill+"0");

void main()

ElectricBill ob=new ElectricBill();

ob.accept();

ob.calculate();

ob.print();

I - TECH 25
CLASS BASED PROGRAM WITH SOLUTION

11. Design a class RailwayTicket with following description: [2018]


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)

Type of Coaches Amount


First_AC 700
Second_AC 500
Third_AC 250
sleeper None

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.*;

public class RailwayTicket

String name;

I - TECH 26
CLASS BASED PROGRAM WITH SOLUTION

String coach;

long mobno;

int amt;

int totalamt;

void accept()

Scanner sc=new Scanner(System.in);

System.out.println("PLEASE ENTER YOUR NAME,COACH TYPE,MOBILE NO. AND BASIC AMOUNT OF


TICKET");

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 TYPE \t MOBILE NO. \t TOTAL AMOUNT");

System.out.println(name+"\t"+coach+"\t"+mobno+"\t"+totalamt);

void main()

RailwayTicket ob=new RailwayTicket();

ob.accept();

ob.update();

ob.display();

12. Design a class named ShowRoom with the following description: [2019]
Instance variables/data members:

String name : to store the name of the customer.

long mobno : to store the mobile number of the customer.

double cost : to store the cost of the items purchased.

double dis : to store the discount amount.

double amount : to store the amount to be paid after discount.

Member methods:

ShowRoom() : default constructor to initialize data members.

void input() : to input customer name, mobile number, cost

void calculate() : to calculate discount on the cost of purchased items, based on the following criteria:

Cost Discount (in percentage)

Less than or equal to Rs. 10000 5%

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%

More than Rs. 35000 20%

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.*;

public class ShowRoom

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()

Scanner sc=new Scanner(System.in);

I - TECH 29
CLASS BASED PROGRAM WITH SOLUTION

System.out.println("ENTER CUSTOMER NAME,MOBILE NUMBER AND COST");

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("CUSTOMER NAME \t MOBILE NUMBER \t AMOUNT TO BE PAID AFTER


DISCOUNT");

System.out.println(name+"\t"+mobno+"\t"+"RS."+amount+"0");

void main()

ShowRoom ob=new ShowRoom();

I - TECH 30
CLASS BASED PROGRAM WITH SOLUTION

ob.input();

ob.calculate();

ob.display();

I - TECH 31

You might also like