Computer Project by Aditi Singh of Class 12-b
Computer Project by Aditi Singh of Class 12-b
Computer Project by Aditi Singh of Class 12-b
- ADITI SINGH
- XII-B
INDEX
Topic Page Number Signature
1|Page
PROGRAMS ON STRINGS
Write a program to check 5-7
if the two given string are
anagram or not.
Write a Java program
which accepts string from 7-9
user and displays vowel
characters, digits and
blank space separately.
Write a program to find
duplicate character in a 9-11
string and count the
number of occurrences.
Write a program to count
the number of words in 11-13
given string.
PROGRAMS ON RECURSION
Write a recursive function 13-15
to calculate and return
GCD of two numbers.
Write a recursive to count
and return number of 15-17
digits of a number given
as argument.
Create a program to
display the Fibonacci 17-19
series using recursion.
Write a program in java to
accept a number and 19-21
count its even digits using
recursive functions.
PROGRAMS ON NUMBERS
Write a program to check 21-24
whether a number is a
Niven number or not. Any
positive integer which is
divisible by the sum of its
digits is a Niven number
or Harshad number Niven
number.
Write a program to accept
a number and check 24-26
whether the number is a
Palindrome or not.
Write a program to check
whether a number is a 26-29
perfect number or not
using a do-while loop.
Write a program to accept
a long integer and check if 29-32
it is an Armstrong
number.
2|Page
PROGRAMS ON ARRAYS
Write a program to create 32-34
a jagged array in java
Jagged Array in Java-If we
are creating odd number
of columns in a 2D array,
it is known as a jagged
array. In other words, it is
an array of arrays with
different number of
columns.
Java Program to Display
Transpose Matrix 34-37
Write a program to
display upper triangular 37-40
matrix
Write a program to rotate
a matrix anticlockwise in 40-42
java.
PROGRAMS ON DATA
STRUCTURE 42-47
Queue is an entity which
can hold a maximum of
100 integers. The queue
enables the user to add
integers from the rear and
remove integers from the
front.
WordPile is an entity
which can hold maximum 47-50
of 20 characters. The
restriction is that a
character can be added or
removed from one end
only.
A double-ended queue is a
linear data structure 50-54
which enables the user to
add and remove integer
from either ends, i.e., from
front or rear.
NIC institute’s resource
manager has decided to 54-59
network the computer
resources like printer,
storage media, etc. so that
minimum resources and
maximum sharing could
be availed. Accordingly
printers are linked to a
centralized system and the
printing jobs are done on
a ‘first cum first served’
3|Page
basis only. This is like the
first person’s printing job
will get done first and the
next person’s job will be
done as the next job in the
list and so on.
PROGRAMS ON
OPERATION ON FILES 59-61
Read names of 5 students
through keyword and
store them in file
names.txt.
Read rollno and marks of
5 students and write them 61-63
on a file namely “stu.dat”.
Input a string and
separate words in it. 63-65
Process the string as a
character array.
Read data from text file
“names.txt” and display it 65-67
on monitor.
MISCELLANEOUS
PROGRAMS(DATE 67-69
PROGRAMS)
Write a program to input
day number and year and
display date.
Write a program to input
a date in dd/mm/yyyy 69-72
format and print next
date. Assume that entered
date is valid.
PROGRAMS ON
INHERITANCE 72-77
In the below program of
inheritance, class Bicycle
is a base class, class
MountainBike is a derived
class that extends Bicycle
class and class Test is a
driver class to run
program.
Create a class named
Animal and use it to 77-80
categorize the names of
various animals using
inheritance.
Create a class calculator
with the methods 80-83
addition(int a, b) ,
multiplication(int a , b),
etc. and use it by
4|Page
inheritance for calculation
of two numbers.
Create a class Player and
inherit class SoccerPlayer 83-86
use this to name and
categorize the player if he
plays soccer.
Programs on Strings
QUESTION:1
Write a program to check if the two given string are anagram or not.
Anagram:
An anagram is a rearrangement of the letters of one word or phrase to another
word or phrase, using all the original letters exactly once.
ALGORITHM
IF(str1.length() != str2.length)
status=true
ELSE
INITIALIZE ARRAY
5|Page
PRINT s1+" and "+s2+" are anagrams"
ELSE
PRINT s1+" and "+s2+" are not anagrams"
END METHOD
CREATE main(String[] args)
CALL isAnagram("keEp", "peeK");
CALL isAnagram("Java", "Vaja");
CALL isAnagram("Lock", "Clock");
END MAIN
END CLASS
PROGRAM
import java.util.*;
public class AnagramTest
{
static void isAnagram(String s1, String s2)
{
//Removing white spaces from
String str1 = s1.replaceAll("\\s", "");
String str2 = s2.replaceAll("\\s", "");
boolean status = true;
if(str1.length() != str2.length())
{
status = false;
}
else
{
char[] s1Array = str1.toLowerCase().toCharArray();
char[] s2Array = str2.toLowerCase().toCharArray();
Arrays.sort(s1Array);
Arrays.sort(s2Array);
status = Arrays.equals(s1Array, s2Array);
}
if(status)
{
6|Page
System.out.println(s1+" and "+s2+" are anagrams");
}
else
{
System.out.println(s1+" and "+s2+" are not anagrams");
}
}
public static void main(String[] args)
{
isAnagram("keEp", "peeK");
isAnagram("Java", "Vaja");
isAnagram("Lock", "Clock");
}
VARIABLE DECRIPTION
Name Data type Use
QUESTION:2
Write a Java program which accepts string from user and displays vowel characters, digits
and blank space separately.
ALGORITHM
CREATE CLASS Alphabets
7|Page
INITIALIZE String str;
INITIALIZE int vowels = 0, digits = 0, blanks = 0;
INITIALIZE char ch;
PROGRAM
import java.util.*;
class Alphabets
{
public static void main(String args[])
{
String str;
int vowels = 0, digits = 0, blanks = 0;
char ch;
8|Page
if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' ||
ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U')
vowels ++;
else if(Character.isDigit(ch))
digits ++;
else if(Character.isWhitespace(ch))
blanks ++;
}
System.out.println("Vowels : " + vowels);
System.out.println("Digits : " + digits);
System.out.println("Blanks : " + blanks);
System.out.println("\n\n\n ");
}
}
VARIABLE DECRIPTION
Name Data type Use
QUESTION:3
Write a program to find duplicate character in a string and count the number of occurrences.
ALGORITHM:
CREATE CLASS CountChar
9|Page
INITIALIZE count=0, size= 0;
DO
INITIALIZE char name[] = str.toCharArray()
size = name.length
count = 0
FOR(int j=0; j < size; j++)
IF((name[0] == name[j]) && ((name[0] >= 65 && name[0] <= 91) || (name[0] >=
97 && name[0] <= 123)))
INCREMENT count
IF(count!=0)
PRINT(name[0]+" "+count+" Times");
str = str.replace(""+name[0],"");
WHILE(size != 1);
CLOSE METHOD
CLOSE CLASS
PROGRAM
import java.io.*;
public class CountChar
{
public static void main(String[] args)
{
String str;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the String:");
str = br.readLine();
int count=0, size= 0;
do
{
char name[] = str.toCharArray();
size = name.length;
count = 0;
for(int j=0; j < size; j++)
{
if((name[0] == name[j]) && ((name[0] >= 65 && name[0] <= 91) || (name[0] >=
97 && name[0] <= 123)))
count++;
10 | P a g e
}
if(count!=0)
System.out.println(name[0]+" "+count+" Times");
str = str.replace(""+name[0],"");
}
while(size != 1);
}
}
VARIABLE DECRIPTION
Name Data type Use
QUESTION:4
Write a program to count the number of words in given string.
ALGORITHM
11 | P a g e
CREATE METHOD main (String args[])
PROGRAM
class WordCount
{
static int i,count=0,result;
static int wordcount(String str)
{
char ch[]= new char[str.length()];
for(i=0; i<str.length(); i++)
{
ch[i] = str.charAt(i);
if( ((i>0)&&(ch[i]!=' ')&&(ch[i-1]==' ')) || ((ch[0]!=' ')&&(i==0)) )
count++;
}
return count;
}
public static void main (String args[])
{
result = WordCount.wordcount("Java is a pure Object oriented programming language
");
System.out.println("The number of words in the String are : "+result);
}
}
VARIABLE DECRIPTION
Name Data type Use
12 | P a g e
i,j int loop control variable
PROGRAM ON RECURSION
QUESTION:1
Write a recursive function to calculate and return GCD of two numbers.
ALGORITHM
CREATE CLASS Recusion_1
IF(s==0)
RETURN b
ELSE
RETURN gcd(s,b%s)
13 | P a g e
PRINT("Enter second number")
CALL gcd=gcd(n1,n2)
PRINT("GCD = "+gcd)
CLOSE METHOD
CLOSE CLASS
PROGRAM
import java.util.Scanner;
class Recusion_1
if(s==0)
return b;
else
return gcd(s,b%s);
14 | P a g e
int n1=sc.nextInt();
int n2=sc.nextInt();
int gcd=gcd(n1,n2);
System.out.println("GCD = "+gcd);
VARIABLE DESCRIPTION
Name Datatype Use
gcd int To calculate gcd of two
numbers
s int To store small number
b int To store big number
n1 int To store first number
n2 int To store second number
QUESTION:2
Write a recursive to count and return number of digits of a number given as argument.
ALGORITHM
CREATE Recursion_2
IF(n/10==0)
RETURN 1
ELSE
15 | P a g e
RETURN countDigits(n/10) + 1
CLOSE METHOD
CLOSE METHOD
CLOSE CLASS
PROGRAM
import java.util.Scanner;
class Recursion_2
if(n/10==0)
return 1;
else
return countDigits(n/10) + 1;
16 | P a g e
}
int n=sc.nextInt();
int c=countDigits(n);
VARIABLE DESCRIPTION
Name Datatype Use
countDigits int To store number of digits
that are counted
n int To store number
c int To store number of digits
that are counted
QUESTION:3
ALGORITHM
CREATE CLASS Fibonacci
CREATE METHOD staticintfibonacci(intn)
IF(n <= 1) {
returnn;
returnfibonacci(n-1) + fibonacci(n-2)
CREATE METHODmain(String[] args)
17 | P a g e
INITIALIZE intnumber = 10
PRINT ("Fibonacci Series: First 10 numbers:");
FOR(inti = 1; i<= number; i++)
PRINT (fibonacci(i) + " ")
CLOSE FOR
CLOSE METHOD
CLOSE CLASS
PROGRAM
public classMain
{
//method to calculate fibonacci series
staticintfibonacci(intn) {
if(n <= 1) {
returnn;
}
returnfibonacci(n-1) + fibonacci(n-2);
}
publicstaticvoidmain(String[] args) {
intnumber = 10;
//print first 10 numbers of fibonacci series
System.out.println ("Fibonacci Series: First 10 numbers:");
for(inti = 1; i<= number; i++)
{
System.out.print(fibonacci(i) + " ");
}
}
}
VARIABLE DECRIPTION
Name Data type Use
n int formal variable used for
calculating Fibonacci
number int actual vvariable that passes
the value to n
i int loop control variable
j int loop control variable
18 | P a g e
QUESTION:4
Write a program in java to accept a number and count its even digits using recursive
functions.
ALGORITHM
CREATE CLASSEvenDigits
IF(n/10==0)
RETURN n%2==0?1:0
ELSE
RETURNcountEvenDigits(n/10)+countEvenDigits(n%10)
CLOSE METHOD
CLOSE METHOD
CLOSE CLASS
PROGRAM
import java.util.Scanner;
class EvenDigits
if(n/10==0)
19 | P a g e
{
return n%2==0?1:0;
else
return countEvenDigits(n/10)+countEvenDigits(n%10);
int n=sc.nextInt();
int c=countEvenDigits(n);
VARIABLE DECRIPTION
Name Data type Use
n int formal variable used for input
c int accept the return value from
fuction
PROGRAMS ON NUMBERS
20 | P a g e
QUESTION:1
Write a program to check whether a number is a Niven number or not. Any positive integer
which is divisible by the sum of its digits is a Niven number or Harshad number Niven number.
ALGORITHM
CREATE CLASS NivenNumber
WHILE(temp != 0)
RETURN(num % sumOfDigits == 0)
WHILE (number != 0)
IF (isNiven (number))
21 | P a g e
PRINT(number + is a Niven number")
ELSE
PRINT("Exiting...")
CLOSE METHOD
CLOSE CLASS
PROGRAM
/*NivenNumber.java*/
import java.util.Scanner;
int sumofDigits=0;
while(temp != 0)
sumofDigitssumofDigits + digit;
22 | P a g e
}
while (number != 0)
if (isNiven (number))
else
number = keyboard.nextInt();
System.out.println("Exiting...");
keyboard.close();
VARIABLE DESCRIPTION
Name Data Type Use
SumOfDigits int used to calculate the sum of
digits
temp int used to store the value of
num temporarily
23 | P a g e
num int used to input number
digit int used to calculate digit
number boolean formal variable used to call
the value of num
QUESTION:2
Write a program to accept a number and check whether the number is a Palindrome or not.
ALGORITHM
CREATE CLASS PalindromeNumber
WHILE (num> 0)
24 | P a g e
COMPUTE num = num / 10
IF (numReverse == numOrginal)
ELSE
CLOSE METHOD
CLOSE CLASS
PROGRAM
/* PalindromeNumber.java* /
import java.util.Scanner;
int numOrginal,numReverse = 0;
numOrginal = num;
while (num> 0)
25 | P a g e
num = num / 10;
if (numReverse == numOrginal)
else
keyboard.close();
VARIABLE DESCRIPTION
Name Data Type Use
numOriginal int used to store the original
number
numReverse int used to calculate the
reverse of the number
num int used to input number from
user
digit int used to extract digits of
num
QUESTION:3
Write a program to check whether a number is a perfect number or not using a do-while loop.
ALGORITHM
CREATE CLASS PerfectNumber
INITIALIZE int i = 1
PRINT(“Enter an integer”)
26 | P a g e
INPUT int num = keyboard.nextInt()
DO
IF(num % i == 0)
COMPUTE sum += i
INCREMENT i++
CLOSE DO WHILE
IF (sum == num)
ELSE
METHOD CLOSE
CLASS CLOSE
PROGRAM
/*Perfect number.java*/
import java.util.Scanner;
int sum = 0;
27 | P a g e
int i = 1;
System.out.println(“Enter an integer”);
do
If(num % i == 0)
Sum += i;
i++;
if (sum == num)
else
keyboard.close();
VARIABLE DESCRIPTION
Name Data Type Use
sum int used to calculate sum of
digits
28 | P a g e
i int loop control variable
num int used to store number from
user
QUESTION:4
Write a program to accept a long integer and check if it is an Armstrong number.
ALGORITHM
CREATE CLASS ArmstrongNumber
WHILE (num ! = 0)
INCREMENT totalDigits++
CLOSE WHILE
29 | P a g e
INITIALIZE num = numOrginal
WHILE(num> 0)
CLOSE WHILE
IF (numOrginal == sumOfPowers)
ELSE
CLOSE METHOD
CLOSE CLASS
PROGRAM
/* ArmstrongNumber.java */
import java.util.Scanner;
int totalDigits= 0;
30 | P a g e
System.out.print("Enter a number: ");
long numkeyboard.nextInt();
numOrginal= num;
while (num ! = 0)
totalDigits++;
num = num/10;
num = numOrginal;
while (num> 0)
If (numOrginal== sumOfPowers)
else
keyboard.close();
31 | P a g e
}
VARIABLE DESCRIPTION
Name Data Type Use
numOriginal long used to store the value of
num temporarily
sumOfPowers long used to calculate sum of
digits raised to the power of
number of digits
num long used to input number from
user
totalDigits int used to calculate total
number of digits.
PROGRAMS ON ARRAYS
QUESTION:1
Jagged Array in Java-If we are creating odd number of columns in a 2D array, it is known as a
jagged array. In other words, it is an array of arrays with different number of columns.
ALGORITHM
CLASSTestJaggedArray
32 | P a g e
INITIALIZEarr[i][j] = count++
PRINT(arr[i][j]+" ")
PRINTLN()
CLOSE METHOD
CLOSE CLASS
PROGRAM
class TestJaggedArray{
int count = 0;
arr[i][j] = count++;
33 | P a g e
System.out.print(arr[i][j]+" ");
System.out.println();//new line
VARIABLE DECRIPTION
Name Data type Use
arr int input the array elements and
store
count int Counting the number of
elements
i int outer loop control variable
j char inner loop control variable
QUESTION:2
Java Program to Display Transpose Matrix
ALGORITHM
CREATE CLASS Transpose
34 | P a g e
CLOSE FOR (INNER)
CLOSE FOR
PRINT()
CLOSE FOR
PRINT()
CLOSE METHOD
CLOSE CLASS
PROGRAM
import java.util.*;
35 | P a g e
public static void main(String args[])
arr[i][j] = sc.nextInt();
System.out.println();
36 | P a g e
// transpose and print matrix
System.out.println();
VARIABLE DECRIPTION
Name Data type Use
arr[][] int Input array and compute
transpose
i int outer loop control variable
j int inner loop control variable
QUESTION:3
Write a program to display upper triangular matrix
ALGORITHM
CREATE class UpperTriangular
{1, 2, 3},
{8, 6, 4},
{4, 5, 6}
37 | P a g e
};
IF(rows != cols)
ELSE
IF(i > j)
PRINT("0 ")
ELSE
PRINT()
CLOSE METHOD
CLOSE CLASS
PROGRAM
public class UpperTriangular
//Initialize matrix a
38 | P a g e
int a[][] = {
{1, 2, 3},
{8, 6, 4},
{4, 5, 6}
};
rows = a.length;
cols = a[0].length;
if(rows != cols){
else {
//Performs required operation to convert given matrix into upper triangular matrix
if(i > j)
System.out.print("0 ");
else
System.out.println();
39 | P a g e
}
VARIABLE DECRIPTION
Name Data type Use
rows int Input string from user
col int Counting the number of
letters
i int store size of array
j int loop control variable
QUESTION:4
Write a program to rotate a matrix anticlockwise in java.
ALGORITHM
CREATE CLASSRotateMatrixAniclockwise
FOR(int i=0;i<3;i++)
FOR(int j=0;j<3;j++)
PRINT(" "+a[i][j]+"\t")
40 | P a g e
PRINT("\n")
FOR(int i=2;i>=0;i--)
FOR(int j=0;j<=2;j++)
PRINT(""+a[j][i]+"\t")
PRINT("\n");
CLOSE METHOD
CLOSE CLASS
PROGRAM
public class RotateMatrixAniclockwise
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
System.out.print(" "+a[i][j]+"\t");
System.out.println("\n");
41 | P a g e
for(int i=2;i>=0;i--)
for(int j=0;j<=2;j++)
System.out.print(""+a[j][i]+"\t");
System.out.println("\n");
VARIABLE DECRIPTION
Name Data type Use
ALGORITHM
CREATE CLASS Queue
42 | P a g e
CREATE METHOD Queue( int mm)
INITIALIZE size=mm
INITIALIZE front=-1
INITIALIZE rear=-1
CLOSE METHOD
RETURN front==-1
IF (rear==-1)
INITIALIZE front=0
INITIALIZE rear = 0
INITIALIZE Que[rear]= v
INITIALIZE Que[++rear] =v
ELSE
PRINT ("OVERFLOW!!")
METHOD CLOSE
IF(isEmpty())
RETURN 9999
43 | P a g e
ELSE
INITIALIZE elemnt=Que[front]
IF(front==rear)
INITIALIZE front=rear=-1
ELSE
front++
RETURN elemnt
CLOSE METHOD
FOR(int i=front;i<=rear;i++)
System.out.println(Que[i])
CLOSE FOR
CLOSE METHOD
CLOSE CLASS
PROGRAM
public class Queue
size=mm;
44 | P a g e
front=-1;
rear=-1;
Que=new int[size];
booleanisEmpty()
return front==-1;
if (rear==-1)
front=0;
rear = 0;
Que[rear]= v;
Que[++rear] =v;
else
System.out.println("OVERFLOW!!");
int elemnt;
if(isEmpty())
return 9999;
45 | P a g e
else
elemnt=Que[front];
if(front==rear)
front=rear=-1;
else
front++;
return elemnt;
for(int i=front;i<=rear;i++)
{System.out.println(Que[i]);
VARIABLE DESCRIPTION
Name Datatype Use
Que[] int Array to input and store
elements
front int To point the index of the
front
rear int To point the index of the
rear
size int Stores the size of the array
mm int Inputs size
v int Initializes que
elemnt int Element to be deleted
46 | P a g e
QUESTION:2
WordPile is an entity which can hold maximum of 20 characters. The restriction is that a
character can be added or removed from one end only.
ALGORITHM
CREATE Class WordPile
DECLARE char[] ch
INITIALIZE top = -1
CLOSE METHOD
ELSE
INCREMENT top++
INITIALIZE ch[top] = v
CLOSE METHOD
47 | P a g e
IF (top == -1)
ELSE
DECREMENT top--
RETURN c
CLOSE METHOD
CLOSE CLASS
PROGRAM
public class WordPile
char[] ch;
int capacity;
int top;
capacity = cap;
top = -1;
ch=new char[cap];
void pushChar(char v)
System.out.println("WordPile is full.");
48 | P a g e
else
top++;
ch[top] = v;
char popChar()
if (top == -1)
return '\\';
else
char c = ch[top];
top--;
return c;
VARIABLE DESCRIPTION
Name Datatype Use
ch[] char Character array to store the
character elements
capacity int Integer variable to store the
maximum capacity
top int To point to the index of the
49 | P a g e
topmost element
cap int Initialize data member
capacity
v char Used as a formal variable
QUESTION:3
A double-ended queue is a linear data structure which enables the user to add and remove
integer from either ends, i.e., from front or rear.
ALGORITHM:
CREATE Class Dequeue
INITIALISE lim = l
COSE METHOD
IF ( front> 0)
DECREMENT front--
Else
50 | P a g e
PRINT ("Overflow from front. ")
CLOSE METHOD
INCREMENTN ++rear
ELSE
CLOSE METHOD
IF (front != rear)
INCREMENT ++front
RETURN val
ELSE
RETURN -9999
CLOSE METHOD
CLOSE CLASS
PROGRAM
51 | P a g e
public class Dequeue
int arr[];
public Dequeue(int l)
lim = l;
front = rear = 0;
if ( front> 0)
arr[front] = val;
front--;
else
++rear;
52 | P a g e
arr[rear] = val;
else
int popfront()
if (front != rear)
++front;
return val;
else
return -9999;
VARIABLE DESCRIPTION
Name Datatype Use
arr[] int Array to store up to 100
integer elements
lim int Stores the limit of the
dequeue
front int To point to the index of the
front end
rear int To point to the index of the
rear end
l int Formal variable that
initializes data members
val int Used to add integer from
the rear
53 | P a g e
QUESTION:4
NIC institute’s resource manager has decided to network the computer resources like printer,
storage media, etc. so that minimum resources and maximum sharing could be availed.
Accordingly printers are linked to a centralized system and the printing jobs are done on a
‘first cum first served’ basis only. This is like the first person’s printing job will get done first
and the next person’s job will be done as the next job in the list and so on.
ALOGORTITHM
CREATE Class Printjob
INITIALZE Capacity = 20
INITIALIZE rear =1
CLOSE METHOD
CLOSE METHOD
RETURN front = = -1
CLOSE METHOD
54 | P a g e
CREATE METHOD addJob()
INITIALIZE int id = 0
TRY
INPUT id=sc.nextInt()
CATCH(Exception e)
PRINT(e)
INITIALIZE Newjob = id
IF ( rear == -1)
INITIALIZE job[rear] = id
INITIALIZE job[++rear] = id
METHOD CLOSE
IF ( isEmpty() )
ELSE
IF ( front == rear)
ELSE
55 | P a g e
INCREMENT front ++
CLOSE METHOD
CLOSE CLASS
PROGRAM
import java.util.Scanner;
public Printjob()
Capacity = 20;
front -1;
rear =1;
createJob();
booleanisEmpty()
56 | P a g e
}
int id = = 0;
try
id = in.nextInt ();
catch(Exception e)
System.out.println(e);
Newjob = id;
if ( rear == -1)
front rear = 0;
job[rear] = id;
job[++rear] = id;
int elem;
57 | P a g e
if( isEmpty() )
else
elem job[front] ;
if ( front == rear)
else
front ++
VARIABLE DESCRIPTION
Name Datatype Use
Job[] int Array of integers to store
the printing jobs
Newjob int To add a new printing job
into the array
Capacity int The maximum capacity of
the integer array
front int To point to the index of the
front
rear char To point to the index of the
rear
id int To initialize
58 | P a g e
ALGORITHM
CREATE Class I0
TRY
outFile.println(name)
outFile.close()
CATCH(IOException e)
PRINT (e)
CLOSE CLASS
PROGRAM
import java.io.* ;
public class I0
59 | P a g e
static InputStreamReaderisr = new InputStreamReader(System.in) ;
try
outFile.println(name);
outFile.close();
catch(IOException e)
System.err.println(e);
VARIABLE DESCRIPTION
Name Datatype Use
name String to input name of file
60 | P a g e
i int loop control variable
QUESTION:2
Read rollno and marks of 5 students and write them on a file namely “stu.dat”.
ALGORITHM
CREATE Class BinaryOutput
TRY
INPUT rno=sc.nextInt()
INPUT marks=sc.nextInt()
dw.writeInt(rno)
dw.writeFloat(marks)
CLOSE dw
CLOSE fw
61 | P a g e
CATCH(IOException e)
PRINT(e)
CLOSE CLASS
PROGRAM
import java.io.* ;
try
rno=Integer.parseInt(stdin.readLine());
marks=Float.parseFloat(stdin.readLine());
dw.writeInt(rno);
dw.writeFloat(marks);
dw.close();
62 | P a g e
fw.close();
catch(IOException e)
System.err.println(e);
VARIABLE DESCRIPTION
Name Datatype Use
marks float to input marks of student
rno int to input roll number
i int loop control variable
QUESTION:3
Input a string and separate words in it. Process the string as a character array.
ALGORITHM
CREATE Class StringProcessing
63 | P a g e
START FOR LOOP
IF(character = = ')
ELSE
PRINT (character)
PRINT(“\n”)
CLOSE CLASS
PROGRAM
import java.io.*;
import java.util.*;
class StringProcessing
System.out.printin("Input a string") ;
int numberCharactersdata.length( );
if(character = = ')
64 | P a g e
System.out.printIn() ;
else
System.out.print(character);
System.out.println(“\n”);
VARIABLE DESCRIPTION
Name Datatype Use
data String Used to input a string
counter Int loop control variable
QUESTION:4
Read data from text file “names.txt” and display it on monitor.
ALGORITHM
CREATE Class FileIO
INCREMENT i++
PRINT(“Name “ + i + “ : “)
PRINT(text)
CLOSE FILE
65 | P a g e
CLOSE CLASS
PROGRAM
import java.io.*;
class FileIO
String text;
Int i=0;
while((text = fileInput.readLine())!=null)
i++;
System.out.print(“Name “ +i + “ : “);
System.out.println(text);
fileInput.close();
VARIABLE DESCRIPTION
Name Datatype Use
text String used to input text and store
it in file
i Int loop control variable
ALGORITHM
CREATE Class DayNumberToDate
WHILE (ar[i]<n)
INITIALIZE n-=ar[i]
INCREMENT i++
PRINT (d+"/"+m+"/"+y)
CLOSE CLASS
PROGRAM
import java.util.*;
class DayNumberToDate
67 | P a g e
public static void main(String args[])
int n=sc.nextInt();
System.out.println("Enter year");
int y=sc.nextInt();
int ar[]={31,y%4==0?29:28,31,30,31,30,31,31,30,31,30,31};
int i=0;
while(ar[i]<n)
n-=ar[i];
i++;
int d=n;
int m=i+1;
System.out.println(d+"/"+m+"/"+y);
VARIABLE DESCRIPTION
Name Datatype Use
n int To store the number of days
y int To store the year which is
entered by the user
ar[] int To check the month
i int To initialize
d int To store the number of days
m int To store the month
68 | P a g e
QUESTION:2
Write a program to input a date in dd/mm/yyyy format and print next date. Assume that
entered date is valid.
ALGORITHM
CREATE Class NextDate
sc2.useDelimiter("/")
IF (d<ar[m-1])
START OUTER IF
INCREMENT d++
CLOSE OUTER IF
ELSE
IF (m<12)
START INNER IF
69 | P a g e
INITIALIZE d=1
INCREMENT m++
END INNER IF
ELSE
INITIALIZE d=1
INITIALIZE m=1
INCREMENT y++
PRINT (d+"/"+m+"/"+y)
CLOSE CLASS
PROGRAM
import java.util.*;
class NextDate
String date=sc.next();
sc2.useDelimiter("/");
int d=sc2.nextInt();
int m=sc2.nextInt();
int y=sc2.nextInt();
70 | P a g e
int ar[]={31,(y%4==0?29:28),31,30,31,30,31,31,30,31,30,31};
if(d<ar[m-1])
d++;
else
if(m<12)
d=1;
m++;
else
d=1;
m=1;
y++;
System.out.println(d+"/"+m+"/"+y);
VARIABLE DESCRIPTION
Name Datatype Use
date String To store date
d int To store date which is
entered by the user
71 | P a g e
m int To store month which is
entered by the user
y int To store year which is
entered by the user
ar[] int To check the month
PROGRAMS ON INHERITANCE
QUESTION:1
In the below program of inheritance, class Bicycle is a base class, class MountainBike is a
derived class that extends Bicycle class and class Test is a driver class to run program.
ALGORITHM
CREATE CLASS Bicycle
CLOSE METHOD
CLOSE METHOD
72 | P a g e
CLOSE METHOD
CLOSE METHOD
CLOSE CLASS
int startHeight)
super(gear, speed);
seatHeight = startHeight;
CLOSE METHOD
CLOSE METHOD
73 | P a g e
return (super.toString() + "\nseat height is "
+ seatHeight);
CLOSE METHOD
CLOSE CLASS
PRINT(mb.toString())
CLOSE METHOD
CLOSE CLASS
PROGRAM
class Bicycle
this.gear = gear;
this.speed = speed;
74 | P a g e
}
speed -= decrement;
speed += increment;
75 | P a g e
public MountainBike(int gear, int speed,
int startHeight)
super(gear, speed);
seatHeight = startHeight;
seatHeight = newValue;
+ seatHeight);
76 | P a g e
public static void main(String args[])
System.out.println(mb.toString());
VARIABLE DESCRIPTION
Name Datatype Use
gear int used to input gear
QUESTION:2
Create a class named Animal and use it to categorize the names of various animals using
inheritance.
ALGORITHM
CREATE CLASS Animal
CLOSE METHOD
77 | P a g e
CREATE METHOD public void display()
labrador.display()
labrador.eat()
CLOSE METHOD
CLOSE CLASS
PROGRAM
class Animal
String name;
78 | P a g e
}
class Main
labrador.name = "Rohu";
labrador.display();
labrador.eat();
79 | P a g e
}
VARIABLE DESCRIPTION
Name Datatype Use
name int to input name of animal
QUESTION:3
Create a class calculator with the methods addition(int a, b) , multiplication(int a , b), etc.
and use it by inheritance for calculation of two numbers.
ALGORITHM
CREATE CLASS Calculation
DECLARE int z
COMPUTE z = x + y
CLOSE METHOD
COMPUTE z = x - y
CLOSE METHOD
CLOSE CLASS
80 | P a g e
COMPUTE z = x * y
CLOSE METHOD
demo.addition(a, b)
demo.Subtraction(a, b)
demo.multiplication(a, b)
CLOSE METHOD
CLOSE CLASS
PROGRAM
class Calculation
int z;
z = x + y;
z = x - y;
81 | P a g e
System.out.println("The difference between the given numbers:"+z);
z = x * y;
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
VARIABLE DESCRIPTION
Name Datatype Use
a int actual variable used to send
inputs to calling class
b int actual variable used to send
inputs to calling class
x,y,z int used as formal variables
82 | P a g e
while calculation
QUESTION:4
Create a class Player and inherit class SoccerPlayer use this to name and categorize the
player if he plays soccer.
ALGORITHM
CREATE CLASS Player
CLOSE METHOD
CLOSE METHOD
CLOSE NAME
CALL super(name)
CLOSE METHOD
CLOSE METHOD
CLOSE CLASS
PROGRAM
public class InheritanceExample
83 | P a g e
{
soccerPlayer.run();
soccerPlayer.kick();
}
class Player
String name;
Player(String name)
this.name = name;
}
}
84 | P a g e
SoccerPlayer(String name)
super(name);
}
}
soccerPlayer.run();
soccerPlayer.kick();
}
VARIABLE DESCRIPTION
Name Datatype Use
name String used to input and pass the
name of the player
85 | P a g e