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

Project Programe 1 To 15

The PrintJob class models a printing queue. It contains an array to store job IDs, and variables to track the front and rear of the queue. The constructor initializes these variables. The addJob method adds new jobs to the rear if space is available, otherwise displays a message. The removeJob method removes jobs from the front if the queue is not empty, otherwise displays a message. The displayJob method prints the current jobs.
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)
117 views

Project Programe 1 To 15

The PrintJob class models a printing queue. It contains an array to store job IDs, and variables to track the front and rear of the queue. The constructor initializes these variables. The addJob method adds new jobs to the rear if space is available, otherwise displays a message. The removeJob method removes jobs from the front if the queue is not empty, otherwise displays a message. The displayJob method prints the current jobs.
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/ 94

Question 1:

A class that has been defined to keep the books from bottom to top one by

one till the space is available. The details of the class Rack are given below:

Class name: Rack

Data data members/instance variables:

rack[ ] : string to hold a maximum of 100 books.

bkNam : string to store book name

size : story the capacity of the array

point: to print the current TOP Index in the rack

Member functions/methods:

Rack(int cc): constructor to initialise data members size=cc , point=-1 and to

create the string array.

voidkeepBook(): to input the name of a book in bkNam and keep it in the rack

at the current point index if space is available, otherwise display the message

rack is full.

voidtakeBook():to remove a book from the rack and print if rack is not empty

otherwise print a message rack is empty

Void printBooks():display the book names present in the rack, if rack is empty

then print "rack is empty".


Algorithm:

class Rack

Step 1: start

Step 2: an array rack of String type of size 10is declared

Step 3: instance variable bkNam of String type is declared

Step 4: instance variables i, size , point is declared

public static void main (String args [])

Step 1: start

Step 2: size of the rack is taken input and store in s

Step 3: object of the class is created

Step 4: methods are called

Step 5: end

Rack (int cc)

Step 1: start

Step 2: initialise size=cc , point=-1, and create a string array

Step 3: end

voidkeepBook ()

Step 1: start

Step 2: input name of the book in bkNam and keep in rack if space is available

otherwise display a

message "rack is full"

Step 3: end

voidtakeBook ()

Step 1: start

Step 2: local variable b of string type is declared


Step 3: remove a book from the rack and print if rack is not empty otherwise print a

message rack is empty

Step 4: end

voidprintBooks ()

Step 1: Start

Step:2Display the book names present in the rack,if rack is empty then print “rack s

empty”

Class Ended
import java.util.*;

class Rack

String rack[]=new String [10];

String bkNam;

inti, size,point;

Rack(int cc)

size=cc;

point =-1;

for(i=0;i<size;i++)

rack[i]="";

voidkeepBook()

Scanner sc=new Scanner(System.in);

System.out.println("enter the name of the book");

bkNam=sc.nextLine();

if(point==(size-1))

System.out.println("Rack is full");

else
{

point++;

rack[point]=bkNam;

voidtakeBook()

String b;

if(point==-1)

System.out.println("Rack is empty")

else

b=rack[point];

System.out.println("Removed Book="+b);

point--;

voidprintBook()

if(point==-1)

System.out.println("Rack is empty");

}
else

System.out.println("Book Name:");

for(i=(point-1);i>=0;i--)

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

public static void main()

Scanner sc=new Scanner(System.in);

System.out.println("Enter the size of rack");

int s=sc.nextInt();

Rack ob=new Rack(s);

ob.keepBook();

ob.keepBook();

ob.keepBook();

ob.takeBook();

ob.printBook();

}
OUTPUT:

Enter the size of rack

Enter the name of book

computer

Enter the name of book

economics

Enter the name of book

language

Removed Book = language

Book Name: computer


Variable Description Table:

Variable Data Type Description

rack[] String String to hold maximum of 100


book

bkname String String to store book name

size int Integers to store the capacity of


the array

point int Integers to point the Top index of


the rack

b String String to Store element


Question 3:

A class point is declared as follows:

Class name: point

Data member/instance variable:

arr[][]: dimensional array of integers of maximum size 10x10

m,n: to store actual size of rows and columns

Members functions/methods:

point(): are constructor to store 0 to each location of arr[ ] [ ].

point (int mm, int nn): constructor to initialise mm to m and nn into n as actual

size of rows and

columns.

void get_mat(): to input the matrix arr[][] of m rows and n columns.

void display_mat(): print the matrix arr[][] of m xn

void print_saddle_point():to find and print the saddle point from the matrix

arr[][]. A saddle point

from any matrix is determined by finding smallest element in a row which

should be greatest in its

corresponding column
ALGORITHM:class point

Step 1: start

Step 2: a 2D array of int type of size 10x10 is declared

Step 3: instance variables m, n are declared

public static void main (String args[])

Step 1: start

Step 2: instance variables mm, nn of int type are declared

Step 3: input of the rows and columns are taken

Step 4: object of the class is created

Step 5: methods are called

Step 6: end

point ()

Step 1: start

Step 2: local variables i and j of int type are declared

Step 3: initialise the array to 0

Step 4: end

point (int mm, int nn)

Step 1: start

Step 2: initialise m=mm and n=nn

Step 3: end

void get_mat ()

Step 1: start

Step 2: local variable i and j of int type are declared


Step 3: elements of the array are taken input

Step 4: end

void display_matt ()

Step 1: start

Step 2: local variable i and j int type are declared

Step 3: display the matrix after saddling

Step 4: end

void print_saddle_point ()

Step 1: start

Step 2: local variables c, k, f=0,i,j, min, max are initialised and declared

Step 3: find and print saddle point from the matrix

Step 4: end

class ended
import java.util.*;

class point

int arr[][]=new int[10][10];

int m,n;

point()

int i,j;

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

for(j=0;j<10;j++)

arr[i][j]=0;

point(int mm,int nn)

m=mm;

n=nn;

void get_mat()

int i,j;

for(i=0;i<m;i++)
{

for(j=0;j<n;j++)

Scanner sc=new Scanner(System.in);

System.out.println(“enter the lament in the array”);

ar[i][j]=sc.nextInt();

void display_matt()

int i,j;

for(i=0;i<m;i++)

for(j=0;j<n;j++)

System.out.print("MATRIX AFTER SADDLING"+arr[i][j]+"");

System.out.println();

void print_saddle_point()

int c,k,f=0,i,j,min,max;

for(i=0;i<m;i++)
{

min=arr[i][0];

c=0;

for(j=0;j<n;j++)

min=arr[i][j,c=j;

max=arr[k][c];

for(k=0;k<n;k++)

if(arr[k][c]>max)

if(max==min)

System.out.println("SADDLE POINT NUMBER :"+max);

f=1;

if(f==0)

System.out.println("SADDLE POINT NUMBER IS NOT IN MATRIX");

}
public static void main()

Scanner sc = new Scanner(System.in);

int mm,nn;

System.out.print("ENTER THE NUMBER OF ROWS AND COLUMNS");

mm=sc.nextInt();

nn=sc.nextInt();

point ob=new point(mm,nn);

ob.get_mat();

ob.display_matt();

ob.print_saddle_point();

}
OUTPUT:

ENTER THE NUMBER OF ROWS AND COLUMNS

Enter the elements in an array

3574

1649

2475

MATRIX AFTER SADDLING

3574

1649

2475

SADDLE POINT NUMBER :-3


Question 4:-

Class printjob with the following details:

Class name:PrintJob

Instance variable/data members:

Job[]: array of integers to hold the printing jobs.

Newjob: to add a new printing job into the array

Capacity: the maximum capacity of the integer array.

Front: to point index of the front.

Rear: point index of the rear.

Members function/methods:

Printjob(): to initialise data members capacity=20, Front Rear= -1.

void addJob-adds the new printing job at the end point of the last printing job,

if possible otherwise

display a message "friend job is ful cannot add anymore".

Void removeJob() :remove the printing job from the front of the printing job is

not empty, otherwise

display a message" print job is empty".

Specify the class print job given the details of the constructor and function.
Algorithm:-

public static void main()

Step 1: start

Step 2: Initialise the size of an array.

Step 3: a local variable size int data type is declared.

Step 4: object of the class is created

Step 5: methods are called

Step 6: end

PrintJob()

Step 1:start

Step 2: initialise capacity, front and rear.

Step 3: end

void addJob()

Step 1: start

Step 2: input through scanner

Step 3: condition to check whether rear equals to capacity-1,if rear

equals to capacity-1 than print "job is

full can't add anymore.

Step 4:else, increase rear with 1.

Step 5: end

void removeJob()

Step 1:start
Step 2: condition to check whether front and rear is equals to -1, if yes

then print,"job is empty".

Step 3: else, initialise variable int data type

Step 4: check front equals to rear or not

Step 5: if equals then initialise front and rear with -1

Step 6: else, increase the value of front by 1.

Step 7: end

void displayJob()

Step 1: start

Step 2: initialise a variable 'i ' for loop

Step 3: execute the loop to print the array

Step 4: end
import java.util.*;

class PrintJob

int job[]=new int[20];

int Newjob,Capacity, Front, Rear;

PrintJob()

Capacity =20;

Front=-1;

Rear=-1;

void addJob()

Scanner sc=new Scanner(System.in);

System.out.println("ENTER NEW JOB");

Newjob-sc.nextInt();

if(Rear==(Capacity-1))

System.out.println(" PRINT JOB IS FULL CANNOT ADD ANYMORE");

else

if(Front==-1&&Rear==-1)

Front =0;

Rear=0;

else

Rear=Rear+1;
job[Rear]=newJob;

void removeJob()

if(Front=-1&&Rear ==-1)

System.out.println("JOB IS EMPTY");

else

int val=job[Front];

if(Front==Rear)

else

Front=-1;

Rear=-1;

else

Front=Front+1;

void displayJob()

{
int i;

System.out.println("JOBS:");

for(i=Front;i<=Rear;i++)

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

public static void main()

PrintJob ob=new PrintJob();

ob.addJob();

ob.addJob();

ob.addJob();

ob.addJob();

ob.removeJob();

ob.displayJob();

}
OUTPUT:-
Enter new job

Enter new job

Enter new job

Enter new job

Jobs:

4
Variable Description Table:-

Variable Data type Description

job[] int Array of integers to hold the


printing jobs

newjob int To add a new printing jobs into


the array

capacity int The maximum capacity of the


integers array

front int To point index of the front

rear int To point index of the rear

val int To store element

i int Loop variable


Question 7:-

A super class record has been defined to store the names and ranks of 50 students.

Define a

subclass Rank to find the highest rank along with the name.

The details of both the classes are given below:

Class name: record

Data members/instance variables:

name[] :to store the names of the students.

rnk[]: to store the ranks of the students.

Members functions

record(): constructor to initialise data members members.

void readvalue():to store the names and ranks and ranks.

void display(): display the names and the corresponding rank.

Class Name: Rank

Data members/instance variable:

Index: integer to store the index of top most rank.

Member functions/methods:

rank():constructor to invoke the base class.

constructor to initialise index

void highest():find the index/location of the topmost rank and store it in index without

sorting the array.

void display (): display the names and ranks along with the name having the topmost

rank.
ALGORITHM:-

class record

Step 1: start

Step 2: a single dimension array name and rank of size 50 are initialised

Record ()

Step 1: start

Step 2: a local variable i of int type is initialise

Step 3: initialise the data members with their initials

Step 4: end

void readValues ()

Step 1: start

Step 2: accept the name and rank of the students from the user

Step 3: end

void display ()

Step 1: start

Step 2: print the name of the students and ranks with suitable headings

Step 3: end

class ended

class Rank extends Record

Step 1: start

Step 2: an instance variable index is initialised

Rank ()

Step 1: start

Step 2: initialise the data member with its initial

Step 3: end
void highest ()

Step 1: start

Step 2: find the index of the topmost rank and store it in index without sorting

Step 3: end

void display ()

Step 1: start

Step 2: print the names and ranks of the students with the name having the topmost

rank

Step 3:

end
import java.util.*;

class Record

String name[] = new String[50];

int rnk[]=new int[50];

Record()

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

name[i]="";

rnk[i]=0;

void readValues()

Scanner sc=new Scanner(System.in);

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

System.out.println("ENTER NAME OF THE STUDENT");

name[i]=sc.next();

System.out.println("ENTER RANK OF THE STUDENT");

rnk[i]=sc.nextInt();

void display()

System.out.println("NAME\t\t"+"RANK");

for(int i=0;i<50;i++)
System.out.println(name[i]+"\t\t"+rnk[i]);

class Rank extends Record

int index;

Rank()

index=0;

void highest()

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

if(rnk[i]<rnk[index])

index=i;

void display()

super.display();

System.out.println(“THE TOP MOST RANK:\t”+rnk[index]);

System.out.println(“NAME WITH TOP MOST RANK:\t”+name[index]);

}
OUTPUT:-
ENTER NAME OF THE STUDENT:

SUMAN

ENTER RANK OF THE STUDENT:

11

ENTER NAME OF THE STUDENT:

AMAN

ENTER RANK OF THE STUDENT:

ENTER NAME OF THE STUDENT:

ASHOK

ENTER RANK OF THE STUDENT:

ENTER NAME OF THE STUDENT:

NITIN

ENTER RANK OF THE STUDENT:

ENTER NAME OF THE STUDENT:

MOHIT

ENTER RANK OF THE STUDENT:

NAME
SUMAN
AMAN
ASHOK
NITIN
MOHIT
RANK
11
4
7
2
9
THE TOPMOST RANK: 2

NAME WITH TOPMOST RANK: NITIN


Variable Description Table:-

Variable Data Type Description

name[ ] String String to store name of the


student

rnk[ ] int Integers to store the rank of the


student

index int Integers to store a number


Question 5:-

A disarium number is a number in which the sum of digit to the power of their respective

position is

equal to the number itself.

example 135 =113253

Hence,135 is a disarium number.

design a class Disarium to check if a given number is a disarium number or not some of the

members

of the class are given below:

class name: disarium

data members:

int num: store the number

int size :store the size of the number

Members methods

Disarium (int nn): parameterized constructor to initialise the data members n=nn end and

size=0

void countDigit(): count the total number of digit and assign it to size

int sumofDigit(int n, int p): return the sum of the digits of the number (n)to the power of their

respective position(p) using recursive technique.

void check(): check whether the number is a disarium number and display result with an

appropriate

message.

specific the class Disarium giving the details of the constructor(), void countDigit (), white

count digits in

sum of digit(int,int),void check(). define the main function to create an object and call the

function of

according to enable the task.


ALGORITHM:-

class disarium

instance variables num and size of int type are initialised

public static void main (String args[])

Step 1: start

Step 2: a no. to be checked is taken input

Step 3: object of the class is created

Step 4: methods are called

Step 5: end

Disarium (int nn)

Step 1: start
Step 2: initialise the data members n=nn and size=0
Step 3: end
void countDigit ()

Step 1: start

Step 2: local variable p of int type is initialised

Step 3: count the total no. of digits and assign it to size

Step 4: end

int sumofDigit (int n, int p)

Step 1: start

Step 2: local variables i, d and sum of int type are initialised and declared

Step 3: return the sum of the digits of the number (n) to the power of their respective position (p) using

recursive technique

Step 4: end

void check ()

Step 1: start
Step 2: method sum of digits is called
Step 3: if the sum of digit to the power of their respective position is equal to the number itself, print
"Disarium Number" else print " Not a Disarium Number"
"
Step 4: end

class ended
import java.util.*;
class Disarium
{
int num, size;
Disarium(int nn)
{
num=nn, size=0;
}
void countdigit()
{
int p=num;
while(p>0)
{
p=p/10, size++;
}
}
int sumofdigits(int n,int p)
{
int d,sum=0;
for(int i=(p-1);i>=0;i--)
{
d=n/(int)(Math.pow(10,i));
sum sum+(int)(Math.pow(d,(p-i)));
n=n%(int)(Math.pow(10,i));
}
return sum;
}
void check()
{
if(sumofdigits(num,size)==num)
System.out.println("Disarium Number");
else
System.out.println("Not a Disarium Number");
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Number");
int no-sc.nextInt();
Disarium ob=new Disarium (no);
ob.countdigit();
ob.check();
}
}
OUTPUT:-
Enter a number

135

Disarium number

Enter a number

122

Not a disarium number


Variable Description Table:-

Variable Data Type Description

num int Integer to store the number

size int Integer to store size of number

nn int Integer to assign num = nn

p int Integer to assign p=num

n int Function variable

p int Function variable

i int Loop variable

no int Integers to take input from the


user
Question 6:-

A class name SwapSort has been define to perform string related operation on a word input

some of the

members of the class are as below:

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 initialise data members with legal initial values

void readword (): to accept a word in uppercase

void swapchar():to interchange the first and last character of the word in 'wrd' and store the

new word

in swapwrd.

void display (): display the original words swapped word and the sorted word

Specify the class SwapSort given the details of the constructor (),void readword (),void

swapchar(), void

display(). Define the main () function to create an object and class the functions accordingly

to enable

the task.
ALGORITHM:-

class SwapSort

Step 1: start

Step 2: instance variables wrd, swapwrd, sortwrd of String and len of int type are initialised

public SwapSort ()

Step 1: start

Step 2: initialize data members with their default values

Step 3: end

void readword ()

Step 1: start

Step 2: input of the word in upper case is taken

Step 3: find the length of the word

Step 4: end

void swapchar ()

Step 1: start

Step 2: local variables ch1, ch2 of char type are initialised

Step 3: swap the first and last characters of the word in 'wrd' and store the new word in '

'swapwrd'

Step 4: end

void sortword ()

Step 1: start

Step 2: local variables i and j of int type are initialised

Step 3: sort the characters of the original word in alphabetical order and store it in 'sortwrd'
import java.util.*;

class SwapSort

String wrd,swapwrd,sortwrd;

int len;

public SwapSort()

wrd="";

len=0;

swapwrd="";

sortwrd="";

void readword()

Scanner sc=new Scanner(System.in);

System.out.println("Enter a word in upper case");

wrd=sc.next();

len=wrd.length();

void swapchar()

char ch1,ch2;

ch1=wrd.charAt(0);

ch2=wrd.charAt(len-1);

swapwrd=ch2+wrd.substring(1,len-1)+ch1;

void sortword()
{

int i,j;

for(i=65;i<=90;i++)

for(j=0;j<len;j++)

if(wrd.charAt(j)==i)

sortwrd=sortwrd+wrd.charAt(j);

void display()

System.out.println("Original word\t"+wrd);

System.out.println("Swapped word\t"+swapwrd);

System.out.println("Sorted word\t"+sortwrd);

public static void main (String args[])

SwapSort ob=new SwapSort();

ob.readword();

ob.swapchar();

ob.sortword();

ob.display();

}
OUTPUT :-
Enter a word in upper case

COMPUTER

Original word COMPUTER

Swapped word ROMPUTEC

SORTED word CEMOPRTU


Variable Description Table:-
Variable Data Type Description

wrd String String to store a word

swapwrd String String to store the swapped word

sortwrd String String to store the sorted word

len int Integer to store the length of the


word

ch1 char Character to return the value

ch1 char Character to return the value

i int Integer to run the loop

j int Integer to run the loop


Question 7:-

A class mixer has been define to merge two sorted integer arrays in ascending order. some

of the

members of the class are given below

Class name: Mixer

Data members/instance variables:

int arr[] :to store the element of an array

int n to store the size of the array

Member functions:

Mixer(int nn): constructor to assign n=nn

void accept():to accept the element of the array in ascending order without any duplicates.

Mixer mix(Mixer A):tomorrow is the current objects are element with the parameterized array

element

and return the resultant object

void display (): to display the elements of the array

specify the class mixer, given details of the constructor void accept(), Mixermix (Mixer) and

void

display(), define the main () function to create an object and call the function accordingly to
enable the
task.
ALGORITHM:-
Step 1: Start

Step 2: Object of the class is created

Step 3: Methods are called

Step 4: End

Mixer (int nn)

Step 1: Start

Step 2: assigning n=nn

Step 3: end

void accept ()

Step 1: Start

Step 2: local variable i of int type are

declared and accepting the elements of the array in ascending order

Step 3: End

Mixer mix(Mixer A)

Step 1: Start

Step 2: local variables i and j of int type are declared

Step 3: merging the current object's array elements with the parameterized array elements

and

returning the resultant object

Step 4: end

void display ()

Step 1: start

Step 2: local variable i of int type is declared

Step 3: the merged elements of the array is displayed

Step 4: end
import java.util.*;

class Mixer

int arr[];

int n;

Scanner sc=new Scanner(System.in);

Mixer(int nn)

n=nn;

arr=new int[n];

void accept()

System.out.println("Enter elements of both the arrays one by one in ascending order:");

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

arr[i]=sc.nextInt();

Mixer mix(Mixer A)

Mixer t=new Mixer(n+A.n);

for(int i=0;i<A.n;i++)

t.arr[i]=A.arr[i];

for(int j=0;j<n;j++)

t.arr[A.n+j]=arr[j];

return(t);

void display()

{
System.out.println("Merged elements:");

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

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

public static void main(String args[])

Mixer ob1=new Mixer(3);

Mixer ob2=new Mixer(4);

Mixer ob3=new Mixer(7);

ob1.accept();

ob2.accept();

ob3=ob2.mix(ob1);

ob3.display();

}
OUTPUT:-
Enter elements of both the arrays one by one is ascending order:

43

54

65

Enter element of both the array one by one in ascending order:

76

87

98

99

Merged elements:

43

54

65

76

87

98

99
Variable Description Table:-

Variable Data Type Description

arr[ ] int Integer to store the element of


the array

n int Integer to store the size of the


array

nn int Integer to assign n=nn

i int Loop variable

j int Loop variable


Question 8:-

Design a class called change to convert a decimal number into its equivalent number in base

16 and vice

versa

For example, (i) the decimal number 35 is 23 in base 16.

(ii) the decimal number 107 is 6B in base 16.

Some of the members of the class change are as follows:

Class name: Change

Data members/ instance variable:

a[]: an integer array

n: integer to be converted to base 16

Member functions/methods:

Change (): constructor to initialise 0 to instance variables.

void input (): to accept an integer to be converted to base 16

void hexadeci(String str): to convert hexadecimal number back to decimal form.

void decihexa(): to convert decimal integer 'n' to hexadecimal form.

Specify the class change given the details of the constructor and functions void input (), void

hexadeci

(String str) and void decihexa (). The main function need not to be written.
ALGORITHM:-

Class: Change

Step 1: start

Step 2: an int type array of size 50 is declared

Step 3: instance variables a and i of int type are declared

Change ()

Step 1: start

Step 2: constructor to initialise 0 to instance variables

Step 3: end

void input ()

Step 1: start

Step 2: accepting an integer to be converted to base 16

Step 3: end

void decihexa(String str)

Step 1: start

Step 2: local variables a, len, p, k, d, s, str1, ch are declared and initialised

Step 3: converting hexadecimal number back to decimal form

Step 4: print the number in decimal form

Step 5: end

void hexadeci ()

Step 1: start

Step 2: local variables i, c, r, t, str2 are declared and initialised

class ended
import java.util.*;
class Change
{
int a[]=new int[50];
int n,i;
Change()
{
for(i=0;i<50;i++)
{
a[i]=0;
n=0;
}
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a decimal number");
n=sc.nextInt();
}
void decihexa(String str)//to convert hexadecimal number back to decimal form
{
int a=0,len,p=0,k=0;
double d=0,s=0;
String str1="";
char ch;
len=str.length();
p=len;
for(i=0;i<len;i++)
{
ch=str.charAt(i);
if(ch>='A')
k=(int)ch-55;
else
{
str1=str1+ch;
k=Integer.parseInt(str1);
str1="";
}
d=Math.pow(16,(p-1))*k;
s=s+d;
p--;
}
System.out.print("The decimal equivalent of Hexadecimal Number"+str+"is:");
System.out.print((int)s);
}
void hexadeci()//to convert decimal integer to hexadecimal form
{
int i=0,c=0,r,t;
String str2="";
System.out.print("The Hexadecimal equivalent of Decimal Number"+n+"is:");
while(n>0)
{
r=n%16;
a[i]=r;
n=n/16;
i++;
c++;
}
for(i=(c-1);i>=0;i--)
{
if(a[i]>=10)
{
t=a[i]-10;
t=65+t;
str2=str2+(char)t;
}
else
{
str2=str2+(char) (48+a[i]);
}
System.out.print(str2);
System.out.println();
decihexa(str2);
}
}
Output:-
Description
Enter a decimal number
35
The Hexadecimal equivalent of Decimal Number35 is: 23
The decimal equivalent of Hexadecimal Number23 is: 35
Enter a decimal number
107
The Hexadecimal equivalent of Decimal Number 107 is: 6B
The decimal equivalent of Hexadecimal Number 6B is: 107
Variable Description Table:-
Variable Data Type Description

a[ ] int Integer to store the element of the


array

n int Integer to be converted to base


16

i int Integer to run the loop

str String String to convert hexadecimal


back to decimal form

a int Integer to store value

len int Integer to store length of str

p int Integer to store len’s value

k int Integer to store value

d double Double type to store value

s double Double type to store value

ch char Character to return value

str1 String String to store space

i int Integer to store value

c int Integer to store value

r int Integer to store value

t int Integer to store value

str2 String String to store sentence


Question 9:-

A class "ISC_Scores" define the scores of a candidate in 6 subject and another class

"Bestfour" define the

source in best four subject.

The details of both the classes are given below

Class name:ISC_Scores

Data members

int number [6][2]: to contain marks for 6 subject and subject code.

Member function/methods:

iscscores(): constructor to accept the marks

int point(int): to return the point in each subject according to the following conditions.

Marks>=90: 1 point

80-90. 2 point

70-79. 3 point

60-69. 4 point

50-59.5 point

40-49. 6. point

: accordingly

Class name: Bestfour

Members functions/methods

void bestsubjects():display the total points and best four subject codes using the concept of

inheritance

specify the details of both the classes using the concept of inheritance. specify details of the

constructor

iscscores(),methods int point(int) and void bestsubject(). Assume that other methods are

written for

you you need not write the main method.


ALGORITHM:-
class ISC_Scores

Step 1: a 2D array of int type of 6 rows and 2 column is declared

void getiscscores ()

Step 1: start

Step 2: local variable i of int type is declared

Step 3: subject codes and marks in 6 different subjects are taken input

Step 4: end

int point (int sub_score)

Step 1: start

Step 2: local variable i and p of int type are initialised and declared

Step 3: to return the point in each subject according to the conditions

Step 4: end

class ended

class Bestfour extends ISC_Scores

Step 1: instance variables i, j, k, t=0, temp, c=2

void bestsubject()

Step 1: start

Step 2: display the total points and best four subject codes

Step 3: end

class ended
import java.util.*;
class ISC_Scores
{
protected int number[][]=new int[6][2];
void getiscscores()
{
Scanner sc=new Scanner(System.in);
int i;
System.out.println("Enter subject codes and marks in 6 different subjects:");
for(i=0;i<6;i++)
{
number[i][0]=sc.nextInt();
number[i][1]=sc.nextInt();
}
}
int point(int sub_score)
{
int i,pt=0;
for(i=1;i<=6;i++)
{
if(sub_score>=(100-i*10))
{
pt=i;
break;
}
}
return(pt);
}
} //end of base class
class Bestfour extends ISC_Scores
{
int i,j,k,t=0,temp,c=2;
void bestsubject()
{
for(i=0;i<6;i++)
t=t+point(number[i][1]);
System.out.println("Total points obtained:");
System.out.println(t);
for(i=0;i<6-1;i++)
{
for(j=0;j<(6-1-i);j++)
{
if(number[j][1]<number[j+1][1])
{
for(k=0;k<c;k++)
{
temp-number[j][k];
number[j][k]=number[j+1][k];
number[j+1][k]=temp;
}
}
}
}
System.out.println("Subject code with 4 best scores:");
for(i=0;i<4;i++)
System.out.println(number[i][0]+"\t\t"+number[i][1]);
}
} //end of derived class
OUTPUT:-
Enter subject codes and marks in 6 different subjects:
00
88
12
87
14
90
15
94
16
91
18
85
Total points obtained:
9
Subjects codes with best scores:
15 94
16 91
14 90
11 88
Variable Description Table:-

Variable Data Type Description

number[ ] int Integer to store marks for six


subject and subject code

i int Integer to run the loop

pt int Integer to store value

i int Integer to run the loop

j int Integer to run the loop

k int Integer to run the loop

temp int Integer to store value

c int Integer to store k loop’s value


Question 10:-
A library issue with books on rental basis at 2% charge on the cost price of the book per day.
As per rules
of the library a book can be written for 7 days without any fine. If the book is written after 7
days of fine
will also be charged for the access day as per the chart given below:
| Numbers of excess | fine per day |
| 1 to 5 | No tax. |
| 6 to 10.| Rs. 2.00 I
|More than 10 days I rs. 3.00. I
| Above rs 10,00,001 rs.5.00. I
design of class and another class computer to perform the task that their details of the
classes are given
below:
Class name: Library
Data members/instance variable:
name: name of the book
author: Author name of the book
p price of the book in decimal
Member functions:
Library (): parameterize constructor to assign value to the data members
void show(): display the book details
Data members/ instant variables:
d: numbers of days taken in returning the books
f: to store the fine
Member functions
Compute (): parameterized constructor to assign the value to the data members of both the
classes
void fines(): calculate the fine for the excess days.
void display ():display the book details along with the number of days find and total amount
to be paid.
Total amount can be calculate as:( 2 %of the price*total numbers of days)+fine.
Specify the class library given the details of the constructor and void show ().Using the
concept of
inheritance, specify the class compute given the details of the of the constructor void fine()
and the void
display () function. You need not write the main function.
ALGORITHM:-
class Library
Step 1: start
Step 2: instance variable name and author of string type and p of double type is initialised
Library (String nm, String au, double pr)
Step 1: start
Step 2: assign the data members name=nm, author=au, p=pr
Step 3: end
void display ()
Step 1: start
Step 2: print the name, author, price in rupees
Step 3: end
class ended
class Compute extends Library
Step 1: start
Step 2: instance variables d of int type and f of double type is initialised
Compute (String n, String a, double prc, int dy)
Step 1: start
Step 2: super class is called
Step 3: assign data members d=dy and f=0.0 D
Step 4: end
void fine ()
Step 1: start
Step 2: calculate the fine for excess days
Step 5: end

Void display ()
Step 1: start
Step 2: super class is called
Step 3: local variable total of double type is initialised to store the total amount to pay
Step 4: print the no. of days, fine and total amount to be paid
Class endedz
import java.util.*;
class Library
{
String name;
String author;
double p;
Library(String nm,String au,double pr)
{
name=nm;
author=au;
p=pr;
}
void show()
{
System.out.println("Title"+"\t"+author+"\t"+"Price in Rs.");
System.out.println(name+"\t"+author+"\t"+p);
}
} //end of base class
class Compute extends Library
{
int d;
double f;
Compute(String n,String a,double prc,int dy)
{
super(n,a,prc);
d=dy;
f=0.0D;
}
void fine()
{
if(d>7)
{
if((d-7)<=5)
f=2*(d-7);
elseif((d-7)>5&&(d-7)<=10)
f=3*(d-7);
else
f=5*(d-7);
}
}
void display()
{
super.display();
double total;
System.out.println(“No of days taken to return the book:\t”+d);
System.out.println(“Fine charged:\t”+f);
total=(2.0/100.0*p)+f;
System.out.println(“Total amount to be paid :\t”+total):
}
}//end of derived class
OUTPUT:-
Title PANDEY & DEY Price in Rs.

ISC COMPUTER PANDEY & DEY 540.0

No of days taken to return the book: 20

Fine charged: Rs.65.0

Total amount to be paid: 75.8

Title M.L.AGGARWAL Price in Rs.

ISC MATHEMATICS M.L.AGGARWAL 600.0

No of days taken to return the book: 30

Fine charged: Rs.65.0

Total amount to be paid: 75.8


Variable Description Table:-

Variable Data Type Description

name String String to store name of the book

author String String to store author name of the


book

p double To store price of the book

nm String String to store name’s value

au String String to store author’s value

pr double To store p’s value

d int Integer to store number of the


days taken in returning of the
book

f double To store fine

n String String to take input from the user


through super class

a String String to take input from the user


through super class

prc double To take input from the user


through super class

dy int Integer to store d’s value

total double To store total amount to be paid


Question 11:-

A class matrix_operation is declared whose details are given below:

Data members/instance variables:

arr[][] :double dimensional array of integer of side 10x10

m,n: integers to store actual size of rows and columns

x: integer to store a number

Members functions/methods:

void getrowcolumn() :two input values of m and n as rows and columns

void getmatrix(): to enter a matrix ar[][] of mxn.

void print_mat_and_sum(): to print the matrix of mxn and also print sum of greatest element

from each

row.

void change_diagonal(): enter the size of x. Change the value of both the diagonals of matrix

ar[][] by x,

if m=n and print the new matrix,otherwise print the message "it is not a square Matrix so

change is not

possible".
ALGORITHM:-
class matrix_operation
public static void main (String args[])
Step 1: start
Step 2: object of the class is created
Step 3: methods are called
Step 4: end
void getrowcolumn ()
Step 1: start
Step 2: accept the value of rows and columns
Step 3: end
void getmatrix ()
Step 1: start
Step 2: A local variable i and j of int type is initialised
Step 3: elements for the array are taken input
Step 4: end
void print_mat_and_sum ()
Step 1: start
Step 2: a local variables i, j, p=0, s=0 are initialised and declared
Step 3: the array is displayed
Step 4: print the sum of greatest element from each row
Step 5: end
void change_diagonal ()
Step 1: start
Step 2: a local variable i and j of int type are initialised
Step 3: accept the value of x
Step 4: if m = n print the new matrix otherwise print the message "it is not a square Matrix,so
change is
is not possible"
Step 5: end
import java.util.*;
class matrix_operation
{
int ar[][]=new int[10][10];
int m,n,x;
void getrowcolumn()
{
Scanner sc =new Scanner(System.in);
System.out.println("ENTER THE VALUES OF m AND n");
m=sc.nextInt();
n=sc.nextInt();
}
void getmatrix()
{
Scanner sc=new Scanner(System.in);
int i,j;
System.out.println("ENTER NUMBERS");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
ar[i][j]=sc.nextInt();
}
}
}
void print_mat_and_sum()
{
int i,j,s=0,p=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(ar[i][j]+"");
}
System.out.println();
}
for(i=0;i<m;i++)
{
p=0;
for(j=0;j<n;j++)
{
if(ar[i][j]>p)
{
p=ar[i][j];
}
}
s=s+p;
}
System.out.println("SUM OF GREATEST ELEMENT OF EACH ROW="+s);
}
void change_diagonal()
{
Scanner sc =new Scanner (System.in);
int i,j;
System.out.println("ENTER THE VALUE OF x");
x=sc.nextInt();
if(m==n)
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if((i==j)||((i+j)==2))
{
ar[i][j]=x;
System.out.print(ar[i][j]+"");
}
}
System.out.println();
}
}
else
{
System.out.println("IT IS NOT A SQUARE MATRIX SO CHANGE IS NOT POSSIBLE");
}
}
public static void main()
{
matrix_operation ob=new matrix_operation();
ob.getrowcolumn();
ob.getmatrix();
ob.print_mat_and_sum();
ob.change_diagonal();
}
}
OUTPUT:-
ENTER THE VALUES OF m AND n
4
4
ENTER NUMBERS
6
4
3
6
2
7
4
6
3
76
3
5
4
6
4
3
6436
2746
37635
4643
SUM OF GREATEST ELEMENT OF EACH ROW=95
ENTER THE VALUE OF x
9
99
9
99
9
Variable Description Table:-

Variable Data Type Description

arr[ ] int A double dimensional array of


integer of size 10x10

m int Integer to store actual size of


rows

n int Integer to store actual size of


columns

x int Integer to store a number

i int Integer to run the loop

j int Integer to run the loop

s int Integer to store a number

p int Integer to store a number


Question 12:-

Class name: Search

Data members/ instance variables:

arr[]:integer array of maximum size 150.

limit: integer variable to store actual limit of arr[].

num: integer variable to store the number to search.

start,last :integers to store start and last index of arr[].

Members functions/methods:

Search():constructor to initialise 0 to arr[] and also to data members.

Search (int n):constructor to assign n to limit.

void readarray(): to read array arr[] up to the limit.

void disparray():to print array upto the given limit.

void sort():to sort arr[] in ascending order to using insertion sort method.

void inputsearchnum():to read value of num which is to be searched.

int binasearch(int start,int last): to search and return index of num from the ascending order

are if (num)

is found otherwise return -1 use binary technique.

void printresult():by invoking function in binasearch () print the index and the element to be

searched

else output "searched unsuccessful".

Specify the class and call the above function


Algorithm:-
public static void main()
Step 1: start
Step 2: local variable 'n' is initialise
Step 3: input of the limit is taken
Step 4: object of the class is created
Step 5: methods are called
Step 6: end
Search ()
Step 1: start
Step 2: Initialising a variable 'i' to execute the loop
Step 3:
Search (int)
Step 1: Start
Step 2: initialise a local variable limit
Step 3:end
void readarray ()
Step 1: input through scanner
Step 2: a local variable 'i' is taken of int data type
Step 3: input of the array
Step 4: end
void disparray()
Step 1: start
Step 2: initialise a local variable
Step 3: execute a loop
Step 4: print a array
Step 5: end
void sort()
Step 1: start
Step 2: local variable of int datatype is declared
Step 3: for loop execute
Step 4: while loop execute
Step 5: sort the array
Step 6: end
void inputsearchnum ()
Step 1: start
Step 2: input the value which is to be searched
Step 3: end
int binasearch ()
Step 1:start
Step 2: local variable of int Data type is declared
Step 3: search" num"
Step 4: if, found then return the number with it position
Step 5: else, return-1
Step 6: end
void printresult ()
Step 1: start
Step 2: a local variable of int data type is declared
Step 3: call the method binasearch (int) to search a number
Step 4: if the number is returned then print it with the index of that number
Step 5: or else print the message “searched unsuccessful”
Step 6: end
import java.util.*;
class Search
{
int arr[]=new int[150];
int limit,num,start,last,x;
Search()
{
int i;
for(i=0;i<=150;i++)
{
arr[i]=0;
}
limit=0;
num=0;
start=0;
last=0;
}
Search(int n)
{
limit=n;
}
void readarray()
{
Scanner sc=new Scanner(System.in);
int i;
for(i=0;i<=limit;i++)
{
System.out.println("ENTER THE NUMBERS");
arr[i]=sc.nextInt();
}
}
void disparray()
{
int i;
for(i=1;i<=limit;i++)
{
System.out.print(arr[i]+"\t");
}
}
void sort()
{
int i,ptr,temp=0;
for(i=1;i<=limit;i++)
{
ptr=i-1;
pub while(arr[ptr]>arr[temp])
{
arr[ptr+1]=arr[ptr];
ptr--;
}
arr[ptr+1]=arr[temp];
}
}
void inputsearchnum()
{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER NUMBER TO BE SEARCH");
num=sc.nextInt();
}
int binsearch(int start,int last)
{
int p;
int mid;
mid=(start+last)/2;
if(start>last)
return 0;
else
{
if(num= arr[mid])
{
x=mid;
p=1;
}
if(num<arr[mid])
start-mid-1;
if(num>arr[mid])
{
start=mid+1;
}
}
return binsearch(start,last);
}
void printresult()
{
int p=binsearch(start,last);
if(p==1)
{
System.out.println("INDEX="+x);
System.out.println(num);
}
else
System.out.println("SEARCH UNSUCCESSFUL");
}
public static void main()
{
Scanner sc=new Scanner(System.in);•
int n;
System.out.println("ENTER THE LIMIT");
n=sc.nextlnt();
Search ob=new Search(n);
ob.readarray();
ob.sort();
ob.inputsearchnum();
ob.printresult();
ob.disparray();
}
}
OUTPUT:-

ENTER THE LIMIT


5
ENTER THE NUMBERS
1
ENTER THE NUMBERS
2
ENTER THE NUMBERS
3
ENTER THE NUMBERS
4
ENTER THE NUMBERS
5
ENTER THE NUMBERS
6
ENTER NUMBER TO BE SEARCH
2
INDEX=1
2
Variable Description Table:-

Variable Data Type Description

arr[ ] int Integer array of maximum size


150

limit int Integer variable to Store Actual


limit of arr[ ]

num int Integer variable to store the


number to search

start int Integer to store start index of arr[]

last int Integer to store last index of arr[]

x int Integer to store a number

i int Integer to run the loop

ptr int Integer to store a number

temp int Integer to store a number

p int Integer to store a number

mid int Integer to store mid value

n int Integer to store a number


Question 11:-

Design a class exchange to accept a sentence and interchange the first alphabet with the

last alphabet

for each word in the sentence with single letter word remaining unchanged. The words in the

input

sentence are separated by a single blank space and terminated by a full stop.

For example

Input: it is a warm day

Output: it is a marw yad.

Some of the data remember members and members functions are given below.

class name: exchange

data members/ instance variable

sent: store the sentence

rev: store the new sentence

size: to the length of the sentence.

members function:

exchange():default constructor.

void read sentence ():to accept the sentence.

void exfirstlast(): each words and interchange the first and the last alphabet of the word form

a new

sentence new using the change words.

void display(): display the originals sentence along with the new change sentence.

Specify the class exchange() given details of the constructor with void read sentence and

void display

along with the main function to create.


Algorithm:-
class Exchange
Step 1: start
Step: 2 instance variables sent, rev, of String type and size of int type is initialised
public static void main (String args[])
Step 1: start
Step 2: object of the class is created
Step 3: methods are called
Steps 4: end
Exchange ()
Step 1: start
Step 2: initialise the data members as their default values
Step 3: end
voiid readsentence ()
Step 1: start
Step 2: accept a sentence from user
Step 3: end
void exfirstlast ()
Step 1: start
Step 2: local variables s1, s2, s3 and s of string type are initialised
Step 3: extract each word and interchange the first and last alphabet of the word and form a
new..
sentence rev using the changed words
Step 4: end
void display ()
Step 1: start
Step 3: display the original sentence and the new changed sentence
Step 3: end
Class ended
import java.util.*;
class Exchange
{
String sent,rev;int size;
Exchange()
{
sent="";
rev="";
size=0;
}
void readsentence()
{
System.out.println("Enter a sentence");
sent=new Scanner(System.in).nextLine();
}
void exfirstlast()
{
String s1="",s2="",s3="",s;
if(".".indexOf(sent.charAt(sent.length()-1))<=-1)
{
System.out.println("Invalid input");
System.exit(0);
}
else
sent=sent.substring(0,sent.length()-1);
StringTokenizer st=new StringTokenizer(sent);
while(st.hasMoreTokens())
{
s=st.nextToken();
if(s.length()>1)
{
s1=s.substring(0,1);
s2=s.substring(s.length()-1);
s3=s.substring(1,s.length()-1);
rev+=s2+s2+s1+"";
}
else
rev+=s+"";
}
}
}
void display()
{
System.out.print("Original sentence\t:\t"+sent+".");
System.out.print("\nNew sentence\t:\t"+rev+".");
}
public static void main(String args[])
{
Exchange ob=new Exchange();
ob.readsentence();
ob.exfirstlast();
ob.display();
}//main close
}//class close
OUTPUT:-
Enter a sentence

It is a warm day.
Original sentence : it is a warm day.
New sentence : ti si a marw yad.
Variable Description Table:-

Variable Data Type Description

sent String String to store sentence

rev String String to store the new sentence

size int Integer to store the length of the


sentence

s1 String String to Store value

s2 String String to Store value

s3 String String to Store value

s String String to Store value


Question 14:-

A class Matrix contain a two-dimensional integer array of order m into n value possible for

both m and n

is 25. design a class Matrix to find the difference of the two Matrix the details of the members

of the

class are given below.

Class name: rearrange

Data members/instance variable

arr[][]: to store The Matrix element

m: integer to store the numbers of row

n: integer to store the number of columns

Member function

Rearrange (int mm,int nn):constructor to initialise the size of the matrix m=mm and n=nn

void fillarray(): to enter the element of the matrix

Rearrange SubMat(Matrix A):subtract the current object from the matrix of the parameter is

object and

return the resulting object

void display (): displays the matrix element.

specify the class Matrix given the details of the constructor (int, int), void fillarray (),

Rearrange

SubMat(Matrix) and void display(). define a main function to create an object and call the

function

accordingly to enable the task.


ALGORITHM:-
class Rearrange
public static void main (String args[])
Step 1: start
Step 2: input of the no. of rows and columns is taken
Step 3: if r<=25&& c<=25, objects of the class is created
Step 4: methods are called and displayed
Step 5: else print "Invalid Input!! Try again"
Step 6: end
Step 1: start
Step 2: a 2D array arr & instance variables m,n of int type are initialised
Rearrange (int mm, int nn)
Step 1: start
Step 2: initialize the size of the matrix m=mm & n=nn
Step 3: end
void fillarray ()
Step 1: start
Step 2: a local variable i of int type is initialised
Step 3: elements of the array are taken input
Step 4: end
Rearrange SubMat (Rearrange A)
Step 1: start
Step 2: local variables i and j of int type are initialised
Step 3: subtract the current object from the matrix of the parameter is object and return the
resulting object
Step 4: end
Void display()
Step 1: Start
Step 2: local variables i and j of int type are initialised
Step 3: display the matrix elements
Step 4: end
import java.util.*;
class Rearrange
{
int arr[][],m,n;
Rearrange(int mm, int nn)
{
m=mm;
n=nn;
arr=new int[m][n];
}
void fillarray()
{
for(int i=0;i<m;i++)
{
String st=new Scanner(System.in).nextLine();
StringTokenizer s=new StringTokenizer(st);
for(int j=0;j<n;j++)
{
arr[i][j]=Integer.parseInt(s.nextToken());
}
}
}
Rearrange SubMat(Rearrange A)
{
Rearrange d=new Rearrange(A.arr.length,this.arr.length);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
d.arr[i][j]=A.arr[i][j] - this.arr[i];
}
}
return d;
}
void display()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
public static void main(String args[])
{
System.out.print("Row\t:\t");
int r=new Scanner(System.in).nextInt();
System.out.print("\nColumn\t:\t");
int c=new Scanner(System.in).nextInt();
if(r<=25 && c<=25)
{
Rearrange a=new Rearrange(r,c);
Rearrange b=new Rearrange(r,c);
System.out.println("Enter elements for Matrix A");
a.fillarray();
System.out.println("Enter elements for Matrix B");
b.fillarray();
System.out.println("Matrix A");
a.display();
System.out.println("Matrix B");
b.display();
System.out.println("Difference");
a.SubMat(b).display();
}
else
{
System.out.println("Invalid input!!Try again.");
}
}//main close
}//class close
OUTPUT:-
Row : 3
Column : 3
Enter elements for Matrix A
12 17 13
15 14 11
19 16 18
Enter elements for Matrix B
24 20 22
30 32 31
45 40 50
Matrix A
12 17 13
15 14 11
19 16 18
Matrix B
24 20 22
30 32 31
45 40 50
Difference
12 3 9
15 18 20
26 24 32
Variable Description Table:-

Variable Data Type Description

arr[ ] int Integer to store the matrix


element

m int Integer to store number of row

n int Integer to store number of


column

mm int Integer to initialize m=mm

nn int Integer to initialize n=nn

i int Integer to run the loop

st String String to store value

j int Integer to run the loop

j int Integer to run the loop

r int Integer to store value

c int Integer to store value


Question 15:-

A class prime series defines a recursion function to find the prime number from a list of data.

The details

of the class are given below:

Class name :prime_series

Data members/instance variable:

limit: to store the limit of the integers

arr[]: to create an integer array (maximum size 150)

Member functions/methods:

prime_series():constructor to assign 0 to limit and arr[]

void Accept(): to accept the limit and input the integer in array arr[] up to the limit.

int IsPrime(int num,int j,int f) :to check n is prime or not, using Recursive Technique .If prime

then the

function return 1 otherwise 0.

void Displayprime(): to display prime numbers from the array[] by invoking in IsPrime up to

the given limit

Specific the class prime series given the details of the constructor white except in his prime

in an NP avoid display prime the main function need not to be written.


ALGORITHM:-
Step 1: start

Step 2: variable is declared of int data type

Step 3: object of the class is created

Step 4: methods are called

Step 5: end

prime_series()

Step 1: start

Step 2: data members 'limit' and 'arr[]' is assign to 0

Step 3: end

void accept ()

Step 1: start

Step 2: input the limit from the scanner

Step 3: input the integers in array 'arr' upto tht limit

Step 4: end

int IsPrime(int,int)

Step 1:start

Step 2: through a recursive technique check whether the input number is prime or not

Step 3: if prime then it will return 1 else 0

Step 4: end

void Displayprime ()

Step 1:start

Step 2: display the prime number from the array by invoking IsPrime

Step 3:upto the given limit

Step 4: end
import java.util.*;
class Prime_Series
{
Scanner sc=new Scanner(System.in);
int limit;
int arr[]=new int[150];
Prime_Series()
{
limit=0;
int i;
for(i=0;i<150;i++)
{
arr[i]=0;
}
}
void Accept()
{
int j;
System.out.println("ENTER THE NUMBER OF ELEMENTS TO STORE:");
limit=sc.nextInt();
System.out.println("ENTER THE ELEMENTS:");
for(j=0;j<limit;j++)
{
arr[j]=sc.nextInt();
}
}
int Isprime(int num,int j,int f)
{
if(j==num)
return (f);
else
{
if(num%j==0)
f=0;
return(Isprime(num,++j,f));
}
}
void PrintPrime()
{
int v;
for(int i=0;i<limit;i++)
{
v=Isprime(arr[i],2,1);
if(v==1)
System.out.println(“NUMBER”+arr[i]+”IS PRIME”):
}
}
OUTPUT:-
ENTER THE NUMBER OF ELEMENTS TO STORE:
7
ENTER THE ELEMENTS:
11
21
37
43
57
87
47
NUMBER 11 IS PRIME
NUMBER 37 IS PRIME
NUMBER 43 IS PRIME
NUMBER 47 IS PRIME
Variable Description Table:-

Variable Data Type Description

limit int Integer to store limit

arr[ ] int Integer to create an array

i int Integer to run the loop

j int Integer to run the loop

num int Function variable

j int Function variable

f int Function variable

v int Integer to store value

You might also like