Computer Project Class 12th
Computer Project Class 12th
UNITY COLLEGE
Acknowledgement
I would like to express my sincere gratitude to the following individuals who have significantly
contributed to my academic journey and success.
Firstly, I am profoundly grateful to our esteemed Principal, Mr. Deepak Mathews, whose
leadership, guidance, and unwavering support have been a source of inspiration throughout my
academic years. His commitment to excellence and encouragement has greatly motivated me to
achieve my goals.
I also extend my heartfelt thanks to my teacher, Mr. Monis Naqvi. His dedication, insightful
teaching, and encouragement have played a crucial role in my understanding of the subject
matter and my overall academic development. His passion for teaching and personal support
have been invaluable.
Feel free to adjust it if needed to better fit your personal style or specific contributions of your
principal and teacher.
1
INDEX
(1) A class Employee contains employee details and another class Salary calculates the
employee’s net salary. The details of the two classes are given below: [10,2008] Class name : Employee
Data members : empNo : stores the employee number empName : stores the employee name empDesig
: stores the employee’s designation Member functions/methods Employee( ) : default constructor
Employee( . . . ) : parameterized constructor to assign values to employee number, name and
designation void display( ) : display the employee details Class name : Salary Data members : basic : float
variable to store the basic pay Member functions : Salary( . . . ) : parameterized constructor to assign
values to data members void calculate( ) : calculate the employee’s net salary according to the following
rules: DA = 10% of basic HRA = 15% of basic Salary = basic + DA + HRA PF = 8% of Salary Net salary =
Salary – PF Display the employee details and the Net salary Specify the class Employee giving details of
the constructors and member function void display( ).Using the concept of Inheritance specify the class
Salary giving details of constructor and the member function void calculate( ). The main function needs
to be written.
Import java.util.Scanner;
class Employee
{
int empno;String empname,empdesig;
Employee()
{
3
empno=0;
empname=null;
empdesig=null;
}
Employee(int a,String b,String c)
{
empno=a;
empname=b;
empdesig=c;
}
void display()
{
System.out.println("Employee name:"+empname);
System.out.println("Employee number:"+empno);
System.out.println("Employee designation:"+empdesig);
}
}
Variable Data
Scope Description
Name Type
empno int Instance Stores the employee number.
empname String Instance Stores the employee name.
empdesig String Instance Stores the employee designation.
basic float Instance Stores the basic pay of the employee.
Method Stores the Dearness Allowance which is 10% of the basic
da double
(calculate) pay.
Method Stores the House Rent Allowance which is 15% of the
hra double
(calculate) basic pay.
Method Stores the Gross Pay which is the sum of basic pay, DA,
gross double
(calculate) and HRA.
Method
pf double Stores the Provident Fund which is 8% of the gross pay.
(calculate)
Method Stores the Net Pay which is the Gross Pay minus the
netpay double
(calculate) Provident Fund.
name String Main method Temporary variable to take user input for employee name.
Temporary variable to take user input for employee
n int Main method
number.
d String Main method Temporary variable to take user input for employee
5
Variable Data
Scope Description
Name Type
designation.
b float Main method Temporary variable to take user input for basic pay.
Object of the Scanner class used for taking input from the
ob Scanner Main method
user.
Object of the Salary class used to call the calculate method
call Salary Main method
and display the employee and payment details.
This table provides an overview of each variable used in the program, including its data type,
scope, and purpose.
(2) A super class Worker has been defined to store the details of a worker. Define a sub class Wages to
compute the monthly wages for the worker. The details of both the classes are given below: Class name
Data members : : Worker Name : to store the name of the worker [ 10,2011] Basic Member functions :
to store the basic pay in decimal Worker(….) : parameterized constructor to assign values to the instance
variables void display() : display worker details class name Data members : Wages hrs : stores the hours
worked rate : stores rate per hour wage Member functions : stores the overall wage of the worker
Wages(….) : parameterized constructor to assign values to the instance variables of both classes double
overtime( ) : calculates and returns the overtime amount as (hours * rate ) void display() : calculates the
wage using the formula wage=overtime amount +basic pay and displays it along with other details
Specify the class Worker giving details of the constructor() and void display(). Using the concept of
inheritance, specify the class Wages giving details of the constructor(), double overtime() and void
display(). The main function needs to be wriiten
String Name;
double Basic;
6
Name=n;
Basic=b;
int hrs;
double rate,wage;
super(a,b);
hrs=c;
rate=d;
wage=0.0;
}
7
return(rate*hrs);
double x=overtime();
wage=x+super.Basic;
super.display();
System.out.println("Wage="+wage);
Wages Class
(3) A super class Record has been defined to store the names and ranks of 50 students. Define a sub
class Rank to find the highest rank along with the name. The details of both classes are given below:
[ 10,2012] Class name Record Data Mambers: name[] : to store the names of students rnk[] Member
functions: : to store the ranks of students Record() : default constructor void readvalues() : to store
name and ranks void display() Class name Rank Data Mambers: : displays the names and the
corresponding ranks index Member functions: : integer to store the index of the topmost rank Rank() :
constructor to invoke the base class constructor and to initialize index to 0. void highest() : finds the
index location of the topmost rank and stores it in index without sorting the array displays the name and
ranks along with the name having the topmost rank. Specify the class Record giving details of the
constructor(), void readvalues(), void display(). Using the concept of inheritance, specify the class Rank
giving details of constructor(), void highest() and void display(). The main function needs to be wriiten.
import java.io.*;
class Record {
public String name[];
public int rank[];
9
Record() {
name = new String[50];
rank = new int[50];
}
void display() {
System.out.println("The names and corresponding ranks
are: ");
for (int i = 0; i < 50; i++) {
System.out.println(name[i] + " " + rank[i]);
}
}
}
Rank() {
super();
index = 0;
}
void highest() {
for (int i = 0; i < 50; i++) {
for (int j = 0; j < 50; j++) {
if (rank[i] < rank[j]) {
index = i;
}
}
}
}
10
void display() {
super.display();
System.out.println("The Topmost rank achiever is " +
name[index] + " " + rank[index]);
}
}
Method Return
Scope Description
Name Type
Record
Record Constructor Initializes the name and rank arrays.
class
readvalues void
Record Reads the names and ranks from the user using
class BufferedReader.
display void
Record Displays the names and corresponding ranks of all the
class students.
11
Method Return
Scope Description
Name Type
Calls the parent class constructor and initializes the
Rank Constructor Rank class
index to 0.
highest void Rank class Finds the index of the student with the highest rank.
Displays the names and ranks of all the students and the
display void Rank class
topmost rank achiever.
(4) A super class Detail has been defined to store the details of a customer. Define a sub class Bill to
compute the monthly telephone charge of the customer as per the chart given below: [ 10,2013]
NUMBER OF CALLS RATE 1- 100 Only rental charge 101-200 60 paisa per call + rental charge 201-300 80
paisa per call + rental charge Above 300 1 rupee per call + rental charge The details of both the classes
are given below: Class Name Data members : Detail name : to store the name of the customer. address :
to store the address of the customer. telno : to store the phone number of the customer. rent Member
functions: : to store the monthly rental charge Detail(..) : parameterized constructor to assign values to
data members. void show() Class Name : Bill Data members : to display the detail of the customer. n : to
store the number of calls. amt Member functions: : to store the amount to be paid by the customer.
Bill(..) : parameterized constructor to assign values to data Members of both classes and to initialize amt
= 0.0. void cal() : calculates the monthly telephone charge as per the charge given above. void show() :
to display the detail of the customer and amount to be paid. Specify the class Detail giving details of the
constructor( ) and void show(). Uisng the concept of inheritance, specify the class Bill giving details of
the constructor( ), void cal() and void show(). THE MAIN FUNCTION AND
String name,address;
12
long telno;
double rent;
name=n;
address=a;
telno=t;
rent=r;
void show()
System.out.println("Name:"+name);
System.out.println("Address:"+address);
System.out.println("Telephone Number:"+telno);
System.out.println("MonthlyRent:"+rent);
int n;
double amt;
{
13
super(a,b,c,d);
n=e;
amt=0.0;
void cal()
amt=rent;
amt=n*0.6+rent;
amt=n*0.8+rent;
else if(n>300)
amt=n+rent;
void show()
{
14
super.show();
System.out.println("Number of calls="+n);
System.out.println("Amount to pay="+amt);
rent double Stores the monthly rent for the telephone service.
Method Description
Detail Class
Method Description
Bill Class
Method Description
Bill(String a, String b, long c, Constructor to initialize the Bill class variables and call the
double d, int e) Detail class constructor.
(5) A super class Perimeter has been defined to calculate the perimeter of a parallelogram. Define a sub
class Area to compute the area of the parallelogram by using the required data members of the super
class. The details are given below: [ 10,2014] Class name Data Members : Perimeter a : to store the
length in decimal b Member functions: : to store the breadth in decimal Perimeter(…) : parameterized
constructor to assign values to data Members double Calculate() : calculate and return the perimeter of
a parallelogram as 2 *(length +breadth) void show() : to display the data members along with the
perimeter of the parallelogram. Class name Data Members : Area h : to store the height in decimal b
Member functions: : to store the area of the parallelogram Area(…) : parameterized constructor to
assign values to data Members of both classes. void doarea() : computes the area( breadth * height).7
void show() : to display the data members of both the classes along with the area and perimeter of the
parallelogram. Specify the class Perimeter giving details of the constructor(…), double Calculate() and
void show(). Using the concept of inheritance, specify the class Area giving details of the constructor(…),
void doarea() and void show(). The main function and algorithm need not be written.
double a,b;
{
17
a=aa;b=bb;
double Calculate()
return (2*(a+b));
void show()
System.out.println("Length="+a);
System.out.println("Breadth= "+b);
System.out.println("Perimeter="+Calculate());
double h;
double area;
super(aa,bb);
h=cc;
void doarea()
18
area=b*h;
void show()
super.show();
System.out.println("Height= "+h);
System.out.println("Area= "+area);
Class Area
Methods in Perimeter
show void Displays the length, breadth, and perimeter of the rectangle.
Methods in Area
doarea void Calculates the area of the rectangle using breadth b and height h.
show void Displays the length, breadth, perimeter, height, and area of the rectangle.
20
String programs
(1) Write a program to enter any sentence and check if it is a snowball string or not.
The words in the sentence may be separated by one or more spaces and
terminated by ‘.’ or ‘?’ only. The program will generate appropriate error message
for any other terminating character.
Example 1
INPUT: He may give bonus.
OUTOUT: IT IS A SNOWBALL STRING
Example 2
INPUT: Is the cold water frozen?
OUTPUT: IT IS A SNOWBALL STRING
import java.util.Scanner;
class Snowball{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String s = in.nextLine();
char last = s.charAt(s.length() - 1);
if(last != '.' && last != '?'){
System.out.println("INCORRECT TERMINATING CHARACTER.
INVALID INPUT");
return;
}
int len = 0;
String word = "";
boolean status = true;
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
21
if(Character.isLetterOrDigit(ch))
word += ch;
else{
if(len == 0)
len = word.length();
else if(len + 1 != word.length()){
status = false;
break;
}
else
len = word.length();
word = "";
}
}
if(status)
System.out.println("IT IS A SNOWBALL STRING");
else
System.out.println("IT IS NOT A SNOWBALL STRING");
}
}
22
Variable
Data Type Description
Name
in Scanner An instance of the Scanner class used to take input from the user.
s String A string variable to store the input sentence entered by the user.
last char A character variable to store the last character of the input sentence s.
len int
An integer variable to store the length of the previous word.
Initialized to 0.
word String
A string variable to store the current word being processed from the
sentence.
status boolean
A boolean variable to keep track of whether the sentence is a
snowball string.
ch char
A character variable to store the current character being processed
from s.
23
int i=0,count=1,len=0,pos=0;
String sen="",newsen="",wd="",wd1="";
24
sen =sc.nextLine();
len = sen.length();
System.out.println("INVALID INPUT.");
return;
sen = sen.toUpperCase();
wd = sc.next();
wd = wd.toUpperCase();
pos = sc.nextInt();
for(i=0;i< len;i++)
ch=sen.charAt(i);
if(ch==' '||ch=='.'||ch=='?'||ch=='!')
/*do nothing*/
25
else
newsen=newsen+wd1+ch;
wd1="";
count++;
else
wd1=wd1+ch;
System.out.println("NEW SENTENCE:"+newsen);
}
26
(3) A class Mix has been defined to mix two words, character by character, in the
following manner:
The first character of the first word is followed by the first character of the second
word and so on. If the words are of different length, the remaining characters of
the longer word are put at the end.
Example: If the First word is “JUMP” and the second word is “STROLL”, then the
required word will be “JSUTMRPOLL”
Some of the members of the class are given below:
Classname Mix
Data member/instance
variable:
wrd to store a word
len to store the length of the word
Member functions/methods:
Mix( ) default constructor to initialize the data members with legal initial values
void feedword( ) to accept the word in UPPER case
void mix_word( Mix P, Mix Q ) mixes the words of objects P and Q as stated above and stores the resultant
current object
void display( ) displays the word
import java.util.*;
class Mix
{
String wrd;
int len;
static Scanner x=new Scanner(System.in);
Mix()
{
wrd="";
len=0;
}
28
void feedword()
{
System.out.println( "Enter a word in UPPER CASE");
wrd=x.next();
len=wrd.length();
}
void mix_word(Mix P,Mix Q)
{
int s=(P.len <Q.len)? P.len:Q.len;
for(int i=0;i<s;i++)
wrd += P.wrd.charAt(i)+""+Q.wrd.charAt(i);
if (P.len > Q.len)
wrd +=P.wrd.substring(Q.len);
else
wrd +=Q.wrd.substring(P.len);
}
void display()
{
System.out.println("WORD = " + wrd);
}
static void main()
{
Mix obj=new Mix();
Mix obj1=new Mix();
Mix obj2= new Mix();
obj.feedword();
obj1.feedword();
obj2.mix_word (obj,obj1);
obj2.display();
}
}
29
Variable Data
Scope Description
Name Type
Stores the word input by the user in upper case and the
wrd String Instance
resultant mixed word.
len int Instance Stores the length of the input word.
x Scanner Class (static) A static Scanner object used for taking user input.
obj Mix
Local (main An instance of Mix used to call methods and hold the
method) first word.
obj1 Mix
Local (main An instance of Mix used to call methods and hold the
method) second word.
obj2 Mix
Local (main An instance of Mix used to call methods and hold the
method) mixed word result.
30
(4) A class SwapSort has been defined to perform string related operations on a
word input.
Some of the members of the class are as follows:
Class name SwapSort
Data members/instance
variables:
wrd to store a word
len integer to store length of the word
swapwrd to store the swapped word
sortwrd to store the sorted word
Member
functions/methods:
SwapSort( ) default constructor to initialize data members with legal initial values
void readword( ) to accept a word in UPPER CASE
void swapchar( ) to interchange/swap the first and last characters of the word in ‘wrd’ and stores t
‘swapwrd’
void sortword( ) sorts the characters of the original word in alphabetical order and stores it in ‘so
void display( ) displays the original word, swapped word and the sorted word
import java.util.*;
String wrd,swapwrd,sortwrd;
int len;
SwapSort()
{
31
swapwrd="";
sortwrd="";
void readword()
wrd=x.next();
len=wrd.length();
void swapchar()
void sortword()
char c;
for(int i=65;i<=90;i++)
for(int j=0;j<len;j++)
c=wrd.charAt(j);
if(c==i)
sortwrd += c;
}
32
void display()
x.readword();
x.swapchar();
x.sortword();
x.display();
}
33
(5) A class ConsChange has been defined with the following details: [10]
Class name: ConsChange
Data members/instance variables:
word: stores the word len: stores the length of the word
Member functions/methods:
ConsChange(): default constructor
void readword(): accepts the word in lowercase
void shiftcons(): shifts all the consonants of the word at the beginning followed by the
vowels (e.g. spoon becomes spnoo)
void changeword(): changes the case of all occurring consonants of the shifted word
to uppercase, for e.g. (spnoo becomes SPNoo)
void show(): displays the original word, shifted word and the changed word
Specify the class ConsChange giving the details of the constructor ), void readword (
), void shiftcons (), void changeword() and void show(). Define the main() function to
create an object and call the functions accordingly to enable the task.
import java.io.*;
class ConsChange {
String word;
int len; public ConsChange() {
word = new String ();
len = word, length ();
}
public void readword () throws IOException {
InputStreamReader isr = new InputStreamReader (System.in);
BufferedReader br = new BufferedReader (isr);
System.out.println ("Enter the word"):
word = br.readLine();
word = word, trim().toLowerCase();
if (word.indexOf (") > 0)
word = word.substring (0, word.indexOf ("));
len = word, length();
}
35
Variable
Type Description
Name
word String Stores the input word from the user after processing.
len int Stores the length of the processed word.
cons String Temporary variable to store consonants extracted from word.
vowel String Temporary variable to store vowels extracted from word.
ch char
Temporary variable used to store individual characters while processing
word.
pos int Stores the position of the last consonant found in word.
37
Array Programs
import java.util.*;
class BinSearch
int arr[];
int n;
BinSearch(int nn)
n=nn;
}
38
void fillarray()
arr=new int[n];
for(int i =0;i<n;i++)
arr[i]=x.nextInt();
void sort()
int t;
for(int i=0;i<n-1;i++)
for(int j =0;j<n-1-i;j++)
if (arr[j]>arr[j+1])
t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
int m=(l+u)/2;
39
if(arr[m]==v)
return m;
else if(l>u)
return -1;
else if (arr[m]>v)
return bin_search(l,m-1,v);
else
return bin_search(m+1,u,v);
obj.fillarray();
obj.sort();
}
40
Variable
Data Type Scope Description
Name
Instance
arr int[] Array to hold the elements entered by the user.
variable
Instance
n int The number of elements in the array.
variable
x Scanner Static variable Scanner object to read user input.
Temporary variable used for swapping elements
t int Local variable
during sorting.
Loop variable used for iterating through the array
i int Local variable
during sorting and filling.
Loop variable used for iterating through the array
j int Local variable
during sorting.
Method
l int Lower bound index for the binary search.
parameter
Method
u int Upper bound index for the binary search.
parameter
Method
v int The value to be searched for in the array.
parameter
m int Local variable Middle index calculated during the binary search.
obj BinSearch Local variable Object of the BinSearch class used to call its methods.
41
import java.io.*;
import java.util.*;
class MatRev{
private int m;
private int n;
m=mm;
n = nn;
arr=newint[m][n];
arr[i][j] = sc.nextInt();
int rev = 0;
for(int i = x; i != 0; i /= 10)
return rev;
}
43
System.out.println();
int x = sc.nextInt();
int y = sc.nextInt();
obj1.fillArray();
obj2.revMat(obj 1);
obj1.show();
obj2.show();
44
Variable
Data Type Scope Description
Name
Method local
rev int Used to store the reversed number in the reverse method
variable
Method local
sc Scanner Scanner object to read input from the user
variable
Method local
x int Number of rows input by the user
variable
Method local
y int Number of columns input by the user
variable
Method local First instance of the MatRev class used to store the original
obj1 MatRev
variable matrix
Method local Second instance of the MatRev class used to store the
obj2 MatRev
variable reversed matrix
45
(3) Two matrices are said to be equal if they have the same dimension and their
corresponding elements are equal.
For example, the two matrices A and B given below are equal:
Design a class EqMat to check if two matrices are equal or not. Assume that the two
matrices have the same dimension.
Some of the members of the class are given below:
import java.util.*;
class EqMat
int a[][];
int m;
int n;
46
m=mm;
n=nn;
a=new int[m][n];
void readarray()
a[i][j]=sc.nextInt();
if (P.a[i][j]!=Q.a[i][j])
return 0;
return 1;
}
47
void print()
System.out.println();
System.out.print(a[i][j]+"\t");
ob1.readarray();
ob2.readarray();
if (ob1.check(ob1,ob2)==1)
System.out.println("Equal Matrix");
ob1.print();
ob2.print();
else
48
System.out.println("not equal");
(4) A class Trans is defined to find the transpose of a square matrix. A transpose of a matrix
is obtained by interchanging
ORIGINAL TRANSPOSE
11 5 7 11 8 1
8 13 9 5 13 6
1 6 20 7 9 20
Methods/Member functions:
m = mm
void display() : displays the original matrix and the transport matrix by
Specify the class Trans giving details of the constructor(), void fillarray(), void transpose() and
void display().
50
Define a main() function to create an object and call the functions accordingly to enable the task.
. import java.util.Scanner;
class Trans
int arr[][];
int m;
m=mm;
arr=new int[m][m];
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
arr[i][j]=sc.nextInt();
}
51
System.out.println(“TRANSPOSE”);
for(int i=0;i<m;i++)
for(int j=0;j<m;j++)
System.out.print(arr[j][i]+”\t”);
System.out.println();
(5) A class Admission contains the admission numbers of 100 students. Some of the data
members/member functions are given below: [10]
Member functions/methods:
void fill Array(): to accept the elements of the array in ascending order
int binSearch(int 1, int u, int v): to search for a particular admission number (v) using binary
search and recursive technique and returns 1 if found otherwise returns -1
Specify the class Admission giving details of the constructor, void fill Array() and int
binSearch(int, int, int). Define the main() function to create an object and call the functions
accordingly to enable the task.
Import java.util.Scanner;
class Admission {
int adno[];
public Admission()
adno=newint[100];
for(int i=0;i<adno.length;i++)
adno[i]=sc.nextInt();
int m=(1+u)/2;
if(1>u)
return-1;
return 1;
elseif(v>adno[m])
else
obj.fillArray();
if(result == 1)
System.out.println(key+"available.");
else
i int Loop counter for iterating through the array adno when
filling it with admission numbers.
m int The middle index of the current search range in the binary
search.
key int The key value entered by the user to search for in the array
adno.
55
result int The result of the binary search, indicating if the key was
found
(1) A class Mixer has been defined to merge two sorted integer arrays in ascending order.
Some of the members of the class are given below: [10]
Member functions:
void accept(): to accept the elements of the array in ascending order without any duplicates
Mixer mix (Mixer A): to merge the current object array elements with the parameterized array
elements and return the resultant object
Specify the class Mixer, giving details of the constructor(int), void accept(), Mixer mix(Mixer)
and void display(). Define the main( ) function to create an object and call the function
accordingly to enable the task.
import java.util.*;
class Mixer
intarr[];
56
int n;
Mixer(int nn)
n=nn;
arr=new int[n];
void accept()
arr[i]=sc.nextInt();
Mixer mix(Mixer A)
while(xA.arr[y])
B.arr[z]=A.arr[y];
y++;
else
{
57
B.arr[y]=arr[x];
x++;
z++;
while(x<n)
B.arr[z++]=arr[x++];
while(y<A.n)
B.arr[z++]=A.arr[y++];
return B;
void display()
for(int i=0;i<n;i++)
System.out.println(arr[i]);
R=P.mix(Q);
R. display();
}
58
x int Loop counter for iterating through the arr array of the
current Mixer object.
y int Loop counter for iterating through the arr array of the
Mixer object A.
z int Loop counter for iterating through the arr array of the
Mixer object B.
(2) A class Shift contains a two-dimensional integer array of order (m×n) where the
maximum values of both m and n are 5. Design the class Shift to shuffle the matrix (i.e.
the first row becomes the last, the second row becomes the first and so on). The details of
the members of the class are given below: [10]
Member functions/methods:
Shift(int mm, int nn): parameterized constructor to initialize the data members m=mm and n=nn
void cyclic(Shift p): enables the matrix of the object (P) to shift each row upwards in a cyclic
manner and store the resultant matrix in the current object
Specify the class Shift giving details of the constructor(), void input(), void cyclic(Shift) and
void display(). Define the main() function to create an object and call the methods accordingly to
enable the task of shifting the array elements.
Import java.io.*;
class Shift
{
60
intmat[][];
int m;
int n;
m = mm;
n = nn;
m = 5;
n = 5;
if(i == 0)
this.mat[m-1][j] = p.mat[i][j];
61
else
this.mat[i-1][j] = p.mat[i][j];
System.out.println();
System.out.print("M = ");
System.out.print("N = ");
int nn = Integer.parseInt(br.readLine());
obj 1.input();
obj1.display();
obj2.cyclic(obj1);
62
obj2.display();
Shift(int mm, int nn) Constructor Instance Constructor initializing the matrix
dimensions to 5x5 regardless of input parameters mm and nn.
input() void Instance Method to take input from the user to fill the
matrix mat.
main(String[] args) void Static The main method to execute the program, create
Shift objects, and perform matrix input, display, and shift operations.
(3) A class Adder has been defined to add any two accepted time.
Member
functions/methods:
void addtime( Adder X, Adder Y) adds the time of the two parameterized objects X and Y and
stores the sum in the current calling object
void disptime( ) displays the array elements with an appropriate message (i.e.
hours = and minutes = )
import java.util.*;
{
64
Adder()
{ a[0]=0;a[1]=0;
void readtime()
a[0]=x.nextInt();
a[1]=x.nextInt();
void disptime()
System.out.println("Hours=" + a[0]);
System.out.println("Minutes=" + a[1]);
a[1]=X.a[1] + Y.a[1];
a[0]=a[1]/60;
a[1]=a[1]%60;
a.readtime();
b.readtime();
c.addtime(a,b);
c.disptime();
x=x1+x22,y=y1+y22
Member functions:
Point midpoint (Point A, Point B): calculates and returns the midpoint of the two points A and B
Specify the class Point giving details of the constructor (), member functions void readpoint ( ),
Point midpoint (Point, Point) and void displaypoint () along with the main () function to create
an object and call the functions accordingly to calculate the midpoint between any two given
points.
import java.io.*;
class Point
67
int x;
inty;
public Point ()
x = 0;
y = 0;
x = (A.x+B.x)/2;
y = (A.y + B.y)/2;
System.out.println(x);
68
System. out.println(y);
obj1.readpoint();
obj2.readpoint();
obj4.displayPoint();
(5) A class Combine contains an array of integers which combines two arrays into a single
array including the duplicate elements, if any, and sorts the combined array. Some of the
members of the class are given below: [10]
Member functions/methods:
void sort(): sorts the elements of the combined array in ascending order using the selection sort
technique.
void mix(Combine A, Combine B): combines the parameterized object arrays and stores the
result in the current object array along with duplicate elements, if any.
Specify the class Combine giving details of the constructor (int), void inputarray(), void sort(),
void mix (Combine, Combine) and void display (). Also, define the main() function to create an
object and call the methods accordingly to enable the task.
rt java. io.*;
class Combine
{
70
size = nn;
int i;
void sort ()
int i, j, t;
{
71
t = com [i];
com [j] = t;
int i, j;
com[j] = A.com[i];
void display ()
int i;
{
72
System.out.println(com[i]);
c1.inputarray();
c2.inputarray();
c3.sort();
c3.display();
}
73
Other Programs
(1) A class Palin has been defined to check whether a positive number is a Palindrome
number or not.
The number ‘N’ is palindrome if the original number and its reverse are same. Some of the
members of the class are given below:
Data members/instance
variables:
Methods/Member functions:
int reverse(int y) reverses the parameterized argument ‘y’ and stores it in ‘revnum’
using recursive technique
import java.util.*;
int num,revnum;
Palin()
num=0;revnum=0;
void accept()
num=x.nextInt();
int reverse(int y)
if(y>0)
return reverse(y/10);
else
return revnum;
}
76
void check()
int p=num;
if( num==reverse(p))
System.out.println("palindrome");
else
System.out.println("not a palindrome");
obj.accept();
obj.check();
(2) Design a class ArmNum to check if a given number is an Armstrong number or not. [A
number is said to be Armstrong if sum of its digits raised to the power of length of the
number is equal to the number]
Example:
371 = 33 + 73 + 13
1634 = 14 + 64 + 34 + 44
54748 = 55 + 45 + 75 + 45 + 85
Thus, 371, 1634 and 54748 are all examples of Armstrong numbers.
Methods/Member functions:
member n=nn
int sum_pow(int i) returns the sum of each digit raised to the power of the length of the
number using
78
recursive technique
number is 2)
import java.io.*;
import javautil.*;
class ArmNum
private int n;
private int 1;
n = num;
i = 0;
i++;
if(n == sumPow(n))
else
System.out.print("N =");
obj.isArmstrong();
num Stores the number read from user input int Local variable
in the main method
(3) Design a class Perfect to check if a given number is a perfect number or not. [ A number
is said to be perfect if sum of the factors of the number excluding itself is equal to the
original number]
Methods/Member functions:
Perfect (int nn) parameterized constructor to initialize the data member num=nn
int sum_of_factors(int i) returns the sum of the factors of the number(num), excluding
itself, using recursive technique
void check() checks whether the given number is perfect by invoking the
function sum_of_factors() and displays the result with an appropriate message
import java.util.*;
class Perfect
int num;
81
Perfect(int nn)
num=nn;
int sum_of_factors(int i)
if (i == num)
return 0;
else if (num%i==0)
return i + sum_of_factors(i+1);
else
return sum_of_factors(i+1);
void check()
int s=sum_of_factors(1);
if (s==num)
else
System.out.println("Enter a number");
int no=sc.nextInt();
ob.check(); // ob.check();
(4) Design a class Convert to find the date and the month from a given day number for a
particular year.
Example: If day number is 64 and the year is 2020, then the corresponding date would be:
Classname Convert
(date)
Methods/Member functions:
void day_to_date( ) converts the day number to its corresponding date for a
particular year and stores the date in ‘d’ and the month in ‘m’
import java.util.*;
class Convert
int n,d,m,y;
Convert( )
n=0;
y=0;
void accept()
n=x.nextInt() ;
y=x.nextInt() ;
void day_to_date()
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
85
if(y%4==0)
a[2]=29;
while(s<n)
s=s+a[c++];
s=s-a[--c];
d=n-s;
m=c;
void display()
String x[]={"","JANUARY","FEBRUARY","MARCH","APRIL","MAY",
"JUNE","JULY","AUGUST","SEPTEMBER","OCTOBER","NOVEMBER","DECEMBER"};
obj.accept( ) ;
obj.day_to_date();
obj.display();
}
86
(5) A class Mix has been defined to mix two words, character by character, in the following
manner:
The first character of the first word is followed by the first character of the second word and so
on. If the words are of different length, the remaining characters of the longer word are put at the
end.
Example: If the First word is “JUMP” and the second word is “STROLL”, then the required
word will be “JSUTMRPOLL”
Classname Mix
Member functions/methods:
void mix_word( Mix P, Mix Q ) mixes the words of objects P and Q as stated above and
stores the resultant word in the current object
import java.util.*;
class Mix
String wrd;
int len;
Mix()
wrd="";
len=0;
void feedword()
wrd=x.next();
len=wrd.length();
for(int i=0;i<s;i++)
wrd += P.wrd.charAt(i)+""+Q.wrd.charAt(i);
wrd +=P.wrd.substring(Q.len);
89
else
wrd +=Q.wrd.substring(P.len);
void display()
obj.feedword();
obj1.feedword();
obj2.mix_word (obj,obj1);
obj2.display();
len Stores the length of the word wrd int Instance variable of
the Mix class
90
s Stores the length of the shorter word between two Mix objects
int Local variable in the mix_word method