Professional Documents
Culture Documents
- ADITI SINGH
- XII-B
INDEX
TOPIC PAGE NUMBER SIGNATURE
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.
1|Page
ALGORITHM
IF(str1.length() != str2.length)
status=true
ELSE
INITIALIZE ARRAY
END CLASS
PROGRAM
import java.util.*;
public class AnagramTest
2|Page
{
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)
{
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
3|Page
s2 String Input string 2
QUESTION:2
Write a Java program which accepts string from user and displays vowel characters, digits
and blank space separately.
ALGORITHM
CREATE CLASS Alphabets
4|Page
CLOSE METHOD
CLOSE CLASS
PROGRAM
import java.util.*;
class Alphabets
{
public static void main(String args[])
{
String str;
int vowels = 0, digits = 0, blanks = 0;
char ch;
VARIABLE DECRIPTION
Name Data type Use
5|Page
blanks int Count number of blanks
QUESTION:3
Write a program to find duplicate character in a string and count the number of occurrences.
ALGORITHM:
CREATE CLASS CountChar
PROGRAM
import java.io.*;
public class CountChar
{
public static void main(String[] args)
{
6|Page
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++;
}
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
7|Page
CREATE CLASS WordCount
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);
8|Page
}
}
VARIABLE DECRIPTION
Name Data type Use
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)
9|Page
CREATE METHOD main(String args[])
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);
10 | P a g e
public static void main(String args[])
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
Write a recursive to count and return number of digits of a number given as argument.
ALGORITHM
CREATE Recursion_2
IF(n/10==0)
11 | P a g e
RETURN 1
ELSE
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
12 | P a g e
{
return countDigits(n/10) + 1;
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 static int fibonacci(int n)
IF (n <= 1) {
return n;
return fibonacci(n-1) + fibonacci(n-2)
13 | P a g e
CREATE METHOD main(String[] args)
INITIALIZE int number = 10
PRINT ("Fibonacci Series: First 10 numbers:");
FOR (int i = 1; i <= number; i++)
PRINT (fibonacci(i) + " ")
CLOSE FOR
CLOSE METHOD
CLOSE CLASS
PROGRAM
QUESTION:4
14 | P a g e
Write a program in java to accept a number and count its even digits using recursive
functions
ALGORITHM
CREATE CLASS EvenDigits
IF(n/10==0)
RETURN n%2==0?1:0
ELSE
RETURN countEvenDigits(n/10)+countEvenDigits(n%10)
CLOSE METHOD
CLOSE METHOD
CLOSE CLASS
PROGRAM
import java.util.Scanner;
class EvenDigits
if(n/10==0)
return n%2==0?1:0;
15 | P a g e
}
else
return countEvenDigits(n/10)+countEvenDigits(n%10);
int n=sc.nextInt();
int c=countEvenDigits(n);
VARIABLE DECRIPTION
Name Data type Use
PROGRAMS ON NUMBERS
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.
16 | P a g e
ALGORITHM
CREATE CLASS NivenNumber
WHILE(temp != 0)
RETURN(num % sumOfDigits == 0)
WHILE (number != 0)
IF (isNiven (number))
ELSE
17 | P a g e
INPUT number = keyboard.nextInt()
PRINT("Exiting...")
CLOSE METHOD
CLOSE CLASS
PROGRAM
/*NivenNumber.java*/
import java.util.Scanner;
int sumofDigits=0;
while(temp != 0)
18 | P a g e
System.out.println("Enter an integer (0 to exit)");
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
num int used to input number
digit int used to calculate digit
number boolean formal variable used to call
the value of num
19 | P a g e
QUESTION:2
Write a program to accept a number and check whether the number is a Palindrome or not.
ALGORITHM
CREATE CLASS PalindromeNumber
IF (numReverse == numOrginal)
ELSE
20 | P a g e
CLOSE METHOD
CLOSE CLASS
PROGRAM
/* PalindromeNumber.java* /
import java.util.Scanner;
numOrginal = num;
if (numReverse == numOrginal)
else
21 | P a g e
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”)
22 | 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;
int i = 1;
23 | P a g e
Scanner keyboard = new scanner(System.in);
System.out.println(“Enter an integer”);
do
If(num % i == 0)
Sum += i;
i++;
if (sum == num)
else
keyboard.close();
24 | P a g e
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
25 | 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;
26 | P a g e
System.out.print("Enter a number: ");
numOrginal = num;
while (num ! = 0)
totalDigits++;
num = num/10;
num = numOrginal;
If (numOrginal == sumOfPowers)
else
keyboard.close();
27 | 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
Write a program to create 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.
ALGORITHM
CLASS TestJaggedArray
28 | P a g e
FOR (int i=0; i<arr.length; i++)
PRINT(arr[i][j]+" ")
PRINTLN()
CLOSE METHOD
CLOSE CLASS
PROGRAM
class TestJaggedArray{
int count = 0;
arr[i][j] = count++;
System.out.print(arr[i][j]+" ");
29 | P a g e
System.out.println();//new line
VARIABLE DECRIPTION
Name Data type Use
QUESTION:2
Java Program to Display Transpose Matrix
ALGORITHM
CREATE CLASS Transpose
30 | P a g e
PRINT("Matrix before Transpose ")
CLOSE FOR
PRINT()
CLOSE FOR
PRINT()
CLOSE METHOD
CLOSE CLASS
PROGRAM
import java.util.*;
31 | P a g e
System.out.println("enter the elements of matrix");
arr[i][j] = sc.nextInt();
System.out.println();
System.out.println();
32 | P a g e
}
VARIABLE DECRIPTION
Name Data type Use
QUESTION:3
Write a program to display upper triangular matrix
ALGORITHM
CREATE class UpperTriangular
{1, 2, 3},
{8, 6, 4},
{4, 5, 6}
};
IF(rows != cols)
ELSE
33 | P a g e
PRINT("Upper triangular matrix: ")
IF(i > j)
PRINT("0 ")
ELSE
PRINT()
CLOSE METHOD
CLOSE CLASS
PROGRAM
public class UpperTriangular
//Initialize matrix a
int a[][] = {
{1, 2, 3},
{8, 6, 4},
{4, 5, 6}
};
rows = a.length;
34 | P a g e
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();
VARIABLE DECRIPTION
Name Data type Use
35 | P a g e
rows int Input string from user
col int Counting the number of letters
QUESTION:4
Write a program to rotate a matrix anticlockwise in java.
ALGORITHM
CREATE CLASS RotateMatrixAniclockwise
FOR(int i=0;i<3;i++)
FOR(int j=0;j<3;j++)
PRINT(" "+a[i][j]+"\t")
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
36 | P a g e
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");
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
37 | P a g e
Name Data type Use
ALGORITHM
CREATE CLASS Queue
INITIALIZE size=mm
INITIALIZE front=-1
INITIALIZE rear=-1
CLOSE METHOD
RETURN front==-1
IF (rear==-1)
INITIALIZE front=0
38 | P a g e
INITIALIZE rear = 0
INITIALIZE Que[rear]= v
INITIALIZE Que[++rear] =v
ELSE
PRINT ("OVERFLOW!!")
METHOD CLOSE
IF(isEmpty())
RETURN 9999
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
39 | P a g e
CLOSE METHOD
CLOSE CLASS
PROGRAM
public class Queue
size=mm;
front=-1;
rear=-1;
Que=new int[size];
boolean isEmpty()
return front==-1;
if (rear==-1)
front=0;
rear = 0;
Que[rear]= v;
40 | P a g e
}
Que[++rear] =v;
else
System.out.println("OVERFLOW!!");
int elemnt;
if(isEmpty())
return 9999;
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]);
41 | P a g e
}
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
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
42 | P a g e
ELSE
INCREMENT top++
INITIALIZE ch[top] = v
CLOSE METHOD
IF (top == -1)
ELSE
DECREMENT top--
RETURN c
CLOSE METHOD
CLOSE CLASS
PROGRAM
public class WordPile
char[] ch;
int capacity;
int top;
capacity = cap;
43 | P a g e
top = -1;
ch=new char[cap];
void pushChar(char v)
System.out.println("WordPile is full.");
else
top++;
ch[top] = v;
char popChar()
if (top == -1)
return '\\';
else
char c = ch[top];
top--;
return c;
44 | P a g e
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
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)
45 | P a g e
DECREMENT front--
Else
CLOSE METHOD
INCREMENTN ++rear
ELSE
CLOSE METHOD
IF (front != rear)
INCREMENT ++front
RETURN val
ELSE
RETURN -9999
CLOSE METHOD
46 | P a g e
CLOSE CLASS
PROGRAM
public class Dequeue
int arr[];
public Dequeue(int l)
lim = l;
front = rear = 0;
if ( front > 0)
arr[front] = val;
front--;
else
47 | P a g e
{
++rear;
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
48 | 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
49 | P a g e
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
INCREMENT front ++
50 | P a g e
CLOSE METHOD
CLOSE CLASS
PROGRAM
import java.util.Scanner;
public Printjob()
Capacity = 20;
front -1;
rear =1;
createJob();
boolean isEmpty()
51 | 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;
if( isEmpty() )
else
52 | P a g e
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
ALGORITHM
CREATE Class I0
53 | P a g e
TRY
outFile.println(name)
outFile.close()
CATCH(IOException e)
PRINT (e)
CLOSE CLASS
PROGRAM
import java.io.* ;
public class I0
try
54 | P a g e
BufferedWriter bW = new Bufferedwriter (fw);
outFile.println(name);
outFile.close();
catch(IOException e)
System.err.println(e);
VARIABLE DESCRIPTION
Name Datatype Use
name String to input name of file
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
55 | P a g e
CREATE BufferedReader OBJECT
TRY
INPUT rno=sc.nextInt()
INPUT marks=sc.nextInt()
dw.writeInt(rno)
dw.writeFloat(marks)
CLOSE dw
CLOSE fw
CATCH(IOException e)
PRINT(e)
CLOSE CLASS
PROGRAM
import java.io.* ;
56 | P a g e
public static void main(String[ ] args)
try
rno=Integer.parseInt(stdin.readLine());
marks=Float.parseFloat(stdin.readLine());
dw.writeInt(rno);
dw.writeFloat(marks);
dw.close();
fw.close();
catch(IOException e)
System.err.println(e);
VARIABLE DESCRIPTION
Name Datatype Use
57 | P a g e
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
IF(character = = ')
ELSE
PRINT (character)
PRINT(“\n”)
CLOSE CLASS
PROGRAM
58 | P a g e
import java.io.*;
import java.util.*;
class StringProcessing
System.out.printin("Input a string") ;
if(character = = ')
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
59 | P a g e
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
CLOSE CLASS
PROGRAM
import java.io.*;
class FileIO
String text;
Int i=0;
while((text = fileInput.readLine())!=null)
60 | P a g e
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)
61 | P a g e
INITIALIZE n-=ar[i]
INCREMENT i++
PRINT (d+"/"+m+"/"+y)
CLOSE CLASS
PROGRAM
import java.util.*;
class DayNumberToDate
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;
62 | P a g e
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
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("/")
63 | P a g e
IF (d<ar[m-1])
START OUTER IF
INCREMENT d++
CLOSE OUTER IF
ELSE
IF (m<12)
START INNER IF
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();
64 | P a g e
Scanner sc2=new Scanner(date);
sc2.useDelimiter("/");
int d=sc2.nextInt();
int m=sc2.nextInt();
int y=sc2.nextInt();
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);
65 | P a g e
}
VARIABLE DESCRIPTION
Name Datatype Use
date String To store date
d int To store date which is entered
by the user
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
66 | P a g e
CLOSE METHOD
CLOSE METHOD
CLOSE METHOD
CLOSE CLASS
int startHeight)
super(gear, speed);
seatHeight = startHeight;
CLOSE METHOD
CLOSE METHOD
67 | P a g e
CREATE METHOD @Override public String toString()
+ seatHeight);
CLOSE METHOD
CLOSE CLASS
PRINT(mb.toString())
CLOSE METHOD
CLOSE CLASS
PROGRAM
class Bicycle
68 | P a g e
this.gear = gear;
this.speed = speed;
speed -= decrement;
speed += increment;
69 | P a g e
public MountainBike(int gear, int speed,
int startHeight)
super(gear, speed);
seatHeight = startHeight;
seatHeight = newValue;
+ seatHeight);
70 | P a g e
MountainBike mb = new MountainBike(3, 100, 25);
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
71 | P a g e
CREATE METHOD main(String[] args)
labrador.display()
labrador.eat()
CLOSE METHOD
CLOSE CLASS
PROGRAM
class Animal {
String name;
72 | P a g e
System.out.println("My name is " + name);
class Main {
labrador.name = "Rohu";
labrador.display();
labrador.eat();
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
73 | P a g e
CREATE METHOD public void addition(int x, int y)
COMPUTE z = x + y
CLOSE METHOD
COMPUTE z = x - y
CLOSE METHOD
CLOSE CLASS
COMPUTE z = x * y
CLOSE METHOD
demo.addition(a, b)
demo.Subtraction(a, b)
demo.multiplication(a, b)
CLOSE METHOD
CLOSE CLASS
PROGRAM
74 | P a g e
class Calculation {
int z;
z = x + y;
z = x - y;
z = x * y;
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
75 | P a g e
}
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 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
76 | P a g e
CLOSE METHOD
CLOSE CLASS
PROGRAM
public class InheritanceExample
soccerPlayer.run();
soccerPlayer.kick();
}
class Player
String name;
Player(String name)
this.name = name;
}
}
77 | P a g e
// SoccerPlayer class inherits features from Player
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
78 | P a g e