0% found this document useful (0 votes)
19 views

Programs

Uploaded by

vkptel7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Programs

Uploaded by

vkptel7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

PROGRAMMING in JAVA

Question

A class Number has been defined to perform operations on digits of an integer number.

Some of the members of the class are given below:


Class name : Number
Data members/instance variables :
num : integer to store a number.
s : integer to store sum of even digits.

Member functions/methods :
Number( ) : constructor to initialize the data members.
void Accept( ) : to accept an integer.
void sum_Even( ) : to find the sum of even digits the number in num and
store in data member ‘s’.
void show( ) : to display the original number and the sum of even
digits with appropriate messages.
Specify the class Number giving details of the constructor, functions void Accept( ), void
sum_Even( ) and void show( ). Define a main( ) function to create the object and call the
methods to enable the task.

Question
A class Palin has been defined to check whether an integer number is a Palindrome number or
not.
[If the original number and reverse of the original number are same/equal then the number is
a palindrome number]

Example 1: Let a number is 141, its reverse form is=141, so 141 is a Palindrome number.
Example 2: Let a number is 253, its reverse form is=352, so 253 is not a Palindrome number.
Some of the data members and methods are given below:
Class name : Palin
Data members/instance variables :
num : to store an integer.
rev : integer to store the reverse of the number.
Member functions/methods :
Palin( ) : constructor to initialize data members with legal initial
values.
Palin( int nx) : constructor to initialize data member num = nx.
int reverse_num( int n ) : stores the reverse of number ‘n’ in the data member
‘rev’ and returns it.
void Check( ) : by invoking the method int reverse(int) checks
whether the input number is Palindrome or not. Print
the result with an appropriate message.
Specify the class Palin giving details of the constructors and functions int reverse_num( ) and
void Check( ). Define a main( ) function to create an object and call the methods accordingly to
enable the task.

Question
A class Special has been defined to check whether an integer number is a special number or
not.
[If sum of factorials of each digit of a number is equal to the original number then the number
is a special number]

[The factorial of a number is the product of all the natural numbers from the given number to
1 or from 1 to the given number.
Example: Let a number is 4, the factorial of 4 (4 ! ) = 4x3x2x1 = 24 OR 1x2x3x4 = 24]
Some of the data members and methods are given below:
Class name : Special
Data members/instance variables :
num : to store an integer.
s : integer to store sum of factorial of each digit.
Member functions/methods :
Special( ) : constructor to initialize data members with legal initial
values.
Special( int nx) : constructor to initialize data member num = nx.
int fact( int y ) : returns the factorial of digit stored in argument ‘y’.
void check( ) : checks whether the number in ‘num’ is a special
number or not by invoking the method int fact(int) and
displays the result with appropriate message.
Specify the class Special giving details of the constructors and functions int fact(int) and
void check( ). Define a main( ) function to create an object and call the methods accordingly
to enable the task.
Question

A class telcall calculates the monthly phone bill of a consumer. Some of the members of the
class are given below:
Class name : telcall
Data members/ instance variables :
phno : phone number
name : name of consumer
n : number of calls made (integer)
amt : bill amount
Member functions/ methods :
telcall( int x, String y, int k ) : parameterized constructor to assign values to data
members.
void compute( ) : to calculate the bill amount based on the slabs given
below.
void display( ) : to display the details with suitable headings.
number of calls : rate
1 – 100 : Rs. 500/- as rental charge only
101 – 200 : Rs. 1.00/- per call + rental charge
201 – 300 : Rs. 1.20/- per call + rental charge
above 300 : Rs. 1.50/- per call + rental charge
Specify the class telcall giving the details of the constructor, void compute( ) and void
display( ). Define a main( ) function to create an object of type telcall and call the methods
accordingly.

Question
A class SortDigits has been defined to arrange the digits of a number in ascending order
without using any standard sorting technique.
For example: If a number is: 52962 then output will be: 22569

Some of the data members and methods are given below:


Class name : SortDigits
Data members/instance variables :
num : to store an integer.
Member functions/methods :
SortDigits( ) : constructor to initialize data member with legal initial
values.
void readNum( ) : to accept an integer.
void sortDisp( ) : sorts the digits of ‘num’ in ascending order without
using standard sorting technique and print the sorted
digits in one line without spaces.
Specify the class SortDigits giving details of the constructors and functions void readNum( ),
and void sortDisp( ). Define a main( ) function to create an object and call the methods
accordingly to enable the task.

Question

A class Digits performs extraction of digits from a number.


Some of the members of the class are given below:
Class name : Digits
Data members/ instance variables :
num : to store an integer number.
p : integer to store the product.
s : integer to store the sum.
Member functions/ methods :
Digits( ) : default constructor to assign values to data members.
Digits( int nx ) : parameterized constructor to assign data member num
= nx.
void extraction( ) : to calculate and store the product of first and last digit
of num into p and sum of remaining digits into s.
void display( ) : to display the number, product and sum of digits.
Specify the class Digits, giving the details of the constructors, functions void extraction( ) and
void display( ). Define a main( ) function to create an object and call the methods to enable the
task.

Question
A class arrange has been defined to arrange the digits of a number in descending order
without using any standard sorting technique. It is assumed that the integer does not contain
zero’s (0’s).
For example: If a number is: 52962 then output will be: 96522
Some of the data members and methods are given below:
Class name : arrange
Data members/instance variables :
num : to store an integer.
Srt : to store the integer with sorted digits.
Member functions/methods :
arrange( ) :
constructor to initialize data member with legal initial
values.
void readNum( ) : to accept an integer.
void sort( ) : sorts the digits of ‘num’ in descending order without
using standard sorting technique and store sorted
number in the data member ‘Srt’.
void Display( ) : displays the original and the sorted number with
appropriate messages.
Specify the class SortDigits giving details of the constructors and functions void readNum( ),
void sort( ) and void Display( ). Define a main( ) function to create an object and call the
methods accordingly to enable the task.

Question
A number is said to be sumproduct number, if the sum of its digits multiplied by product of its
digits is equal to the original number.
Example : Input : 144 = (1+4+4) * (1*4*4), = 9 * 16 = 144, so 144 is a sumproduct
number.
Design a class sum_product with the following details :
Class name : sum_product
Data members/instance variables :
N : integer to store a number.
Member functions/methods :
sum_product( ) : constructor to initialize the data members with initial
values.
void readNum( ) : to accept value of data member N from the input.
int Sum( int v ) : returns sum of all the digits of the number v.
int Product( int v ) : returns product of all the digits of the number v.
void Check( ) : decide and print whether the number N is a
sumproduct number or not by invoking functions
Sum(int) and Product(int).
Specify the class sum_product, giving details of the constructor, functions void readNum( ),
int Sum( ), int Product() and void Check( ). Define a a main( ) function to create the object
and call the methods accordingly to enable the task.

Question

A class Merger concatenates two positive integers that are greater than 0 and produces a new
merged number.

Example : If the 1st number is 68 and the 2nd is 543, then the concatenated number will be
68543.
Some of the members of the class are given below:
Class name : Merger
Data members/instance variables :
n1 : integer to store 1st integer.
n2 : integer to store 2nd integer.
mergeNum : integer to the store merged number.

Member functions/methods :
Merger( int nx, int ny) : parameterized constructor to initialize the data
members n1 = nx and n2 = ny.
void Join( ) : to concatenate the numbers n1 and n2 and store it in
mergeNum (do not use string operations).
void show( ) : to display the original number and the merged number
with appropriate messages.
Specify the class Merger, giving details of the constructor and functions void Join( ) and void
show( ). Define a main( ) function to create the object and call the methods to enable the task.

Question
A class Sorter has been defined to store integers in an array and sorts them in ascending
order.
Some of the data members and methods are given below:
Class name : Sorter
Data members/instance variables :
arr[ ] : stores integers.
n : integer to store size of the array.
Member functions/methods :
Sorter( int nx) : constructor to initialize n = nx and creates the array.
void Input( ) : input and store integers in the array of size n.
void Bubble( ) : sorts integers in ascending order using bubble sort
technique.
void Show( ) : displays the integer array.
Specify the class Sorter giving details of the constructor(.) and functions void Input( ), void
Bubble( ) and void Show( ). Define a main( ) function to create an object and call the methods
accordingly to print the input array and the sorted array.
Question
A class Sorter has been defined to store integers in an array and sorts them in ascending
order.
Some of the data members and methods are given below:
Class name : Sorter
Data members/instance variables :
arr[ ] : stores integers.
n : integer to store size of the array.
Member functions/methods :
Sorter( int nx) : constructor to initialize n = nx and creates the array.
void accept( ) : input and store integers in the array of size n.
void selectionSort( ) : sorts integers in ascending order using selection sort
technique.
void display( ) : displays the integer array.
Specify the class Sorter giving details of the constructor(.) and functions void accept( ), void
selectionSort( ) and void display( ). Define a main( ) function to create an object and call the
methods accordingly to print the input array and the sorted array.

Question
A class arrange has been defined to store integers in an array and sorts them in ascending
order.
Some of the data members and methods are given below:
Class name : arrange
Data members/instance variables :
ar[ ] : stores integers.
n : integer to store size of the array.
Member functions/methods :
arrange( int nx) : constructor to initialize n = nx and creates the array.
void readArr( ) : input and store integers in the array of size n.
void BubSort( ) : sorts integers in descending order using bubble sort
technique.
void Disp( ) : displays the integer array.
Specify the class Sorter giving details of the constructor(.) and functions void readArr( ), void
BubSort( ) and void Disp( ). Define a main( ) function to create an object and call the methods
accordingly to print the input array and the sorted array.
Question
A class NumName has been defined to print the number name of each digit stored in an array
of size 10.
Example : Input array: 3 0 7

Output: THREE ZERO SEVEN

Design a class NumName with the following details:


Class name : NumName
Data members/instance variables :
ar[ ] : integer array of size 10 to store digits.
Member functions/methods :
NumName( ) : constructor to initialize the data member with legal
initial values.
void fillArray( ) : to fill the array ar[ ] with single digits.
void Show_Name( ) : display the name of each digit present in the array ar[ ]
using the format shown in the example. The number
name should be printed in the same line with only one
space.
Specify the class NumName, giving details of the constructor, functions void fillArray( )
and void Show_Name( ). Define a main( ) function to create an object and call the methods
accordingly to enable the task.

Question
A class Merge has been defined to merge the elements of two single dimensional array of
sizes 5 and 8 respectively to another array.
Example : Input array 1: Input array 2:

9 1 2 41 6 3 7 8 12 14 4 6 9

Output array: 9 1 2 41 6 3 7 8 12 14 4 6 9

Design a class Merge with the following details:


Class name : Merge
Data members/instance variables :
Ar1[ ] : integer array of size 5.
Ar2[ ] : integer array of size 8.
margeArray[ ] : integer array.
Member functions/methods :
Marge( ) : constructor to initialize the data member with legal
initial values.
void readArray( ) : to fill the arrays Ar1[ ] and Ar2[ ] with integer
elements.
void Merge_Print( ) : to merge the elements of array Ar1[ ] followed by the
elements of array Ar2[ ] into the array mergeArray[ ]
and display elements of all the arrays.

Specify the class Merge giving details of the constructor and functions void readArray( ) and
void Merge_Print( ). Define a main( ) function to create an object and call the methods
accordingly to enable the task.

Question
A class MaxMin performs operations on numbers stored in a single dimensional array.
Some of the members of the class are given below:
Class name : MaxMin
Data members/instance variables :
num : to store an integer.
arr[ ] : integer array to store digits of the number.
g : integer to store the largest digit.
s : integer to store the smallest digit.
Member functions/methods :
MaxMin( int nx ) : constructor to initialize the data member num = nx and
other data members with legal initial values and create
the integer array.
void formArray( ) : to extract digits from the number in ‘num’ and store
them in the array ‘arr[ ]’.
void LargeSmall( ) : to find the largest and smallest digit from the array
‘arr[ ]’ without using any standard sorting technique
and store largest and smallest digits in ‘g’ and ‘s’
respectively.
void show( ) : to display the array elements, largest digit and smallest
digit with appropriate messages.
Specify the class MaxMin giving details of the constructor and functions void formArray( ),
void LargeSmall( ) and void show( ). Define a main( ) function to create an object and call the
methods accordingly to enable the task.
Question
A class SpyNum has been defined to print only spy numbers from the elements stored in the
array of n locations where n <= 20.
A number is called Spy number if the sum of digits of the number equals to the product of digits of the same
number.
Example: Input number: 1124, its sum of digits ( 1+1+2+4) = 8, product of digits ( 1*1*2*4) = 8, so 1124
is a Spy number.
Design a class SpyNum with the following details:
Class name : SpyNum
Data members/instance variables :
n : integer array to store the size of the array.
ar[ ] : integer array to store elements.
Member functions/methods :
SpyNum( int nn ) :
parameterized constructor to initialize the data member
n = nn and create the integer array of size n.
void readArray( ) : to fill the array ar[ ] with n integers.
int Sum( int nx ) : returns the sum of digits of the argument nx.
int product( int nx ) : returns the product of digits of the argument nx.
void show_Spy( ) : displays only spy numbers from the array by invoking
methods int Sum(int) and int product(int).
Specify the class SpyNum giving details of the constructor and functions void readArray( ),
int Sum(int), int product(int) and void show_Spy( ). Define a main( ) function to create an
object and call the methods accordingly to enable the task.

Question
A class matrix has been defined to print a matrix of 4 rows and 3 columns containing integer
elements and also compute sum of all the elements.
Design a class matrix with the following details:
Class name : matrix
Data members/instance variables :
ar[ ][ ] : integer array to store integers.
Member functions/methods :
matrix( ) : constructor to initialize the data member with legal
initial values.
void accept( ) : to enter integers in the array containing 4 rows and 3
columns.
void show( ) : compute the sum of integers in the array and display
the array in the form of matrix of 4 rows and 3
columns along with sum of integers with appropriate
messages.
Specify the class matrix giving details of the constructor and functions void accept( ) and void
show( ). Define a main( ) function to create an object and call the methods accordingly to enable
the task.

Question
A class Mat of order m has been defined to print the matrix and print sum of left diagonal and
sum of right diagonal elements from the matrix. Assume that the size m is >=2 and <=20.
Design a class Mat with the following details:
Class name : Mat
Data members/instance variables :
ar[ ][ ] : integer array to store integers.
m : integer to store the size of the array.
sl : integer to store sum of left diagonal elements.
sr : integer to store sum of right diagonal elements.
Member functions/methods :
Mat( int nn ) : constructor to initialize the data member m = nn and
other data members with legal initial values.
void readMat( ) : to fill the matrix with integers of order m x m.
void computeSum( ) : to find the sum of left diagonal elements and sum of
right diagonal elements and store them if sl and sr
respectively.
void show( ) : to print the matrix and sum of left diagonal elements
and sum of right diagonal elements with appropriate
messages.
Specify the class Mat giving details of the constructor and functions void readMat( ), void
computeSum( ) and void show( ). Define a main( ) function to create an object and call the
methods accordingly to enable the task.

Question
A class Matrix has been defined with m rows and n columns to print the matrix and sum of
elements of each row.
Design a class Matrix with the following details:
Class name : Matrix
Data members/instance variables :
mat[ ][ ] : array to store integers.
m : integer to store number of rows.
n : integer to store number of columns.
sum : integer to store sum of each row.
Member functions/methods :
Matrix( int mm, int nn ) : constructor to initialize the data member m = mm and
n = nn other data members with legal initial values.
void readMat( ) : to fill the matrix with integers of order m x n.
void show( ) : to print the matrix. Compute sum of each row and print
it.
Specify the class Matrix giving details of the constructor and functions void readMat( ) and
void show( ). Define a main( ) function to create an object and call the methods accordingly to
enable the task.

Question
A class trans has been defined with m rows and n columns to print the transpose of the
matrix.
Design a class trans with the following details:
Class name : trans
Data members/instance variables :
arr[ ][ ] : array to store integers.
trans_arr[ ][ ] : integer array to store transpose matrix.
m : integer to store number of rows.
n : integer to store number of columns.
Member functions/methods :
trans( int mm, int nn ) : parameterized constructor to initialize the data member
m = mm and n = nn other data members with legal
initial values and create the integer arrays.
void readMat( ) : to fill the matrix with integers of order m x n.
void transpose( ) : to store the transpose of the original matrix into
trans_arr[ ][ ].
void show( ) : to print the original and transpose matrix with
appropriate messages.
Specify the class trans giving details of the constructor and functions void readMat( ), void
transpose( ) and void show( ). Define a main( ) function to create an object and call the
methods accordingly to enable the task.

You might also like