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

computer project A ROLL NO 34

This document is a computer practical file by Avneet Kaur from St. Thomas School for the session 2021-22. It includes various Java programs demonstrating concepts such as Smith numbers, Special numbers, Fascinating numbers, Disarium numbers, and more, along with variable descriptions and example inputs/outputs. The document also contains acknowledgments expressing gratitude to teachers and peers for their support.

Uploaded by

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

computer project A ROLL NO 34

This document is a computer practical file by Avneet Kaur from St. Thomas School for the session 2021-22. It includes various Java programs demonstrating concepts such as Smith numbers, Special numbers, Fascinating numbers, Disarium numbers, and more, along with variable descriptions and example inputs/outputs. The document also contains acknowledgments expressing gratitude to teachers and peers for their support.

Uploaded by

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

St .

thomas
School

NAME :- AVNEET kaur


CLASS :- 12 a
ROLL :- 34
NO.
SESSION :- 2021-22
GUIDED BY :-
Mr.Rohit Trivedi

COMPUTER PRACTICLE FILE 2020-21


ACKNOWLEDGEMENT
I AVNEET KAUR OF CLASS 12-A FINDS MYSELF VERY
FORTUNATE TO GET AN OPPORTUNITY TO WORK ON THIS
ASSIGNMENT. I WOULD LIKE TO EXTEND MY HEART-FELT
GRATITUDE TO MY SUBJECT TEACHER MR. ROHIT TRIVEDI
WHOSE HELP AND GUIDANCE HAS HELPED ME TO MAKE
MY PROJECT VIRTUOUS. I ALSO EXPRESS MY SINCERE
THANKS TO OUR PRINCIPAL REV. FATHER PAUL REGO FOR
HIS COMPLETE SUPPORT BECAUSE OF WHICH MY PROJECT IS
IN AN IMPRESSIVE SHAPE.
I WOULD LIKE TO EXTEND MY HEART-FELT GRATITUDE TO
MY PARENTS ELDERS AND MY DEAR FRIENDS. THEIR
SUGGESTION GRANTED LUXURIOUS AND DISTINCT DESIGN
TO MY PROJECT.
INTRODUCTION
Java is an object-oriented, cross platform, multi-
purpose programming language produced by Sun Microsystems. First
released in 1995, it was developed to be a machine independent web
technology. It was based on C and C++ syntax to make it easy for
programmers from those communities to learn. Since then, it has earned
a prominent place in the world of computer programming.
Java has many characteristics that have contributed to its popularity:

 Platform independence - Many languages are compatible with only


one platform. Java was specifically designed so that it would run on
any computer, regardless if it was running Windows, Linux, Mac,
Unix or any of the other operating systems.
 Simple and easy to use - Java's creators tried to design it so code
could be written efficiently and easily.
 Multi-functional - Java can produce many applications from
command-line programs to applets to Swing windows (basically,
sophisticated graphical user interfaces).
Java does have some drawbacks. Since it has automated garbage
collection, it can tend to use more memory than other similar languages.
There are often implementation differences on different platforms,
which have led to Java being described as a "write once, test
everywhere" system. Lastly, since it uses an abstract "virtual machine", a
generic Java program doesn't have access to the Native API's on a
system directly. None of these issues are fatal, but it can mean that Java
isn't an appropriate choice for a particular piece of software.
PROGRAM 1: Write a program to input a number and
find out whether it is a Smith number or not. A Smith
Number is a composite number whose sum of digits is equal
to the sum of digits is equal to the sum of digits in its prime
factorization.

import java.util.*;
class Smith
{
static int sum(int n)
{
int s,i,d;
s=0;
for(i=n;i!=0;i=i/10)
{ d=i
%10;
s=s+d;
}
return s;
}
public static void main()
{
Scanner sn=new Scanner(System.in);
int num,s,s1,p,n,cp,i;
System.out.println("enter a number");
num=sn.nextInt();
s=sum(num);
cp=num;
s1=0;
for(i=2;cp!=1;i++)
{
while(cp%i==0)
{
s1=s1+sum(i);
cp=cp/i;
}
}
if(s==s1)
System.out.println(num+" is smith number");
else
System.out.println(num+" is not smith number");
}
}
VARIBALE DESCRIPTION :

VARIABLE NAME DATA TYPE DESCRIPTION

n int to store the number entered by user

s int to store the sum of digits of the number

sum int to store the sum of its prime factors

a int to store the final value of the SumOfDigits()

b int to store the final value from SumOfPrimeFactor()

i int to store the numbers from 2 to n

INPUT : 22

OUTPUT : 22 is a Smith Number.

INPUT : 27

OUTPUT : 27 is Smith Number.


PROGRAM 2: Write a program to input a number and check
whether it is a Special number or not. number is said to be special
number when the sum of factorial of its digits is equal to the
number itself. Example- 145 is a Special Number as 1!+4!+5!=145.

class Special
{
public static void main(int n)
{
int copy=n;int s=0; while(n>0)
{
int r=n%10;int p=1;
for(int j=r;j>=1;j--)
{
p=p*j;
}
s=s+p;
n/=10;
}
if(copy==s)
System.out.println(copy+" is a special number"); else
System.out.println(copy+" is not a special number");
}
}
VARIABLE DESCRIPTION:

VARIABLE DATA DESCRIPTION


NAME TYPE
N int To store the number entered by the user
S int To store the sum of the
P int To store the product of the remainder’s factorial
J int To execute the loop from r to 1
copy int To store the copy of the number entered by the user

INPUT: 145

OUTPUT: 145 is a Special number


PROGRAM 3: Write a program to input a number and check
whether it is a Fascinating number or not. Fascinating Number:
When a number( 3 digits or more ) is multiplied by 2 and 3, and
when both these products are concatenated with the original number,
then it results in all digits from 1 to 9 present exactly once. There
could be any number of zeros and are ignored.
import java .util.*;

class Fascinating

public static void main (String args[])

Scanner sc=new Scanner(System.in);

System.out.println("Enter a Number.");

int n=sc.nextInt();

int a=n*1;

int b=n*2;

int c=n*3;

String s1=Integer.toString(a);

String

s2=Integer.toString(b);

String s3=Integer.toString(c);

String s=s1+s2+s3;

int l=s.length();

int d=0;

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

char h=s.charAt(i);
for (int j=i+1;j<=(l-1);j++)

char m=s.charAt(j);

if(h==m)

d++;

if(d==2)

break;

if(d==2)

System.out.println("It is not Fascinating number");

else

System.out.println("It is a Fascinating Number.");

}}

VARIABLE DESCRIPTION :

VARIABLE DATA DESCRIPTION


NAME TYPE
s String To store the number in string form
n int To store the number converted from string
m1 int To store the number multiplied by 2
m2 int To store the number multiplied by three
t1 String To store the m1 as a string
t2 String To store the m2 as a string
t String To concatnate t1 and t2
ch char To store one character of t
ch1 char To store the consecutive character of ch
a boolean To check if ch is not equal to ch1
Input: 192

Output: It is a fascinating number


PROGRAM 4: Write a program to input a number and check whether it is a
Disarium number or not through recursion. A number will be called Disarium
if the sum of its digits powered with their respective position is equal with the
number itself.

class disarium

int n,l; disarium(int x)

n=x;

l=(""+n).length();

int f1(int n)

if(n<10)

return (int)Math.pow(n,1); else

return (int)Math.pow(n%10,l--)+f1(n/10);

public static void main(int x)

disarium obj=new disarium(x); if(x==obj.f1(x))

System.out.println(x+" is a disarium number"); else


System.out.println(x+" is not a disarium number");

VARIABLE DESCRIPTION :

VARIABLE DATA TYPE DESCRIPTION


NAME
n int To store the number
x int To store the number
l int To store number’s length

Input: 135.

Output : 135 is a Disarium number


PROGRAM 5: A class Series_Sum is designed to calculate sum of
the following series using recursion
. [SUM=x2/1!+x4/3!+x6/5!+xn/(n-1)!].
import java.util.*;
class series_sum
{
public static void main(String args[])
{

Scanner sc=new Scanner(System.in);


System.out.println("enter you number");
int x=sc.nextInt();
System.out.println("enter your power");
int n=sc.nextInt();
series_sum obj= new series_sum();
double ans= obj.sumseries(x,n);
System.out.println(ans+" is the sum of series");
}
double sumseries(int x, int n)
{ if(n>0)

return (double)Math.pow(x,n)/fact(n- 1)+sumseries(x,n-2);


else return 0;
}
int fact(int n)
{

if(n==1) return 1; else


return n*fact(n-1);
}
}
VARIABLE DESCRIPTION :

VARIABLE NAME DATA TYPE DESCRIPTION


x int To store the input
number by the
user

n int To store the power


Number
PROGRAM 6: A positive natural number can be
represented as follows: 9 = 2 + 3 + 4 , 4 + 5
Write a program to input a number ‘n’ and bring out
consecutive natural numbers which when added give ‘n’.

import java.util.*;
class consecutive_sum
{
static void Cons(int n)
{
int start = 1;
int end = (n + 1) / 2;
while (start < end)
{
int sum = 0;
for (int i = start; i <= end; i++)
{
sum = sum + i;
if (sum == n)
{
for (int j = start; j <= i; j++)
System.out.print(j + " ");
System.out.println();
break;
}
if (sum > n)
break;
}
sum = 0;
start++;
}
}

public static void main (String[] args)


{
Scanner sc=new Scanner(System.in);
System.out.println("enter your number");
int n =sc.nextInt();
Cons(n);
}
}
VARIBLE DESCRIPTION:
VARIABLE DATA DESCRIPTION
NAME TYPE
n int To store the number
start int To store the starting point of numbers
end int To store the ending points of the number
sum int To store the sum of consecutive
numbers

INPUT : 27
OUTPUT : 2 + 3 + 4 + 5 + 6 + 7=27
8 + 9 + 10=27
PROGRAM 7 : Write a program to input a number and check
whether it is a composite magic number or not. Composite number:
A composite number is a number that has more than two factors. For
example: 10 Factors are: 1, 2, 5, 10. Magic number: A magic
number is a number in which the eventual sum of the digits is equal
to 1.

class composite_magic
{
public static void main(int n)
{
int c=0;int copy=n;
for(int i=1; i<=n;i++)
{
if (n%i==0) c++;
}
if(c>2)
{ int s;
while(n>1)
{ s=0;
while(n>0)
{int r=n%10; s=s+r; n/=10;} n=s;
}
if(n==1)
System.out.println(copy+" is a composite_magic");
else
System.out.println(copy+" is not a composite_magic number");
}
else
System.out.println( copy+" is not a composite_magic");
}
}
VARIABLE DESCRIPTION:

VARIABLE NAME DATA TYPE DESCRIPION


N int To store the number
C int To count the number of digits
copy int To keep a copy of the original number
R int To store the extracted digit of the number
S int To sum the digits of the number

INPUT : 28

OUTPUT : 28 is a Composite magic number


PROGRAM 8: Write a program to demonstrate call by value.

class Cbv

int x=10;

int y=20;

public static void main (String args[])

Cbv obj=new Cbv();

System.out.println(obj.x+" "+obj.y);

obj.swap(obj.x,obj.y);

System.out.println(obj.x+" "+obj.y);

public void swap(int a,int b)

int temp=0;

temp=a;

a=b;

b=temp;

System.out.println(a+" "+b);

}
VARIABLE DESCRIPTION :

VARIABLE DATA TYPE DESCRIPTION


NAME
a int To store the first number
b int To store the second number
x int To store the inputted first number
y int To store the second inputted number
z int To store the sum of both the numbers
PROGRAM 9 : Write a program to demonstrate call by reference.

class Cbr
{
int x=10;
int y=20;
public static void main (String args[])
{
Cbr obj=new Cbr();
System.out.println(obj.x+" "+obj.y);
obj.swap(obj);
System.out.println(obj.x+" "+obj.y);
}
public void swap(Cbr obj1)
{
int temp=0;
temp=obj1.x;
obj1.x=obj1.y;
obj1.y=temp;
System.out.println(obj1.x+" "+obj1.y);
}
}
VARIABLE DESCRIPTION :
VARIABLE DATA DESCRIPTION
NAME TYPE
t int To store first initial
a int To store elements in array
i int To execute the loop
PROGRAM 10 : A Perfect number is a positive integer, sum of its
proper divisor is equal to its own value. Input a number and check
whether it is Perfect or not through recursion.

import java.util.*;
class Perfect
{ int num;
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter your number"); int n=sc.nextInt();
Perfect obj=new Perfect(n); obj.check();
}
Perfect(int n)
{num=n;}
int SumOfFactors(int i)
{
if(i<num&&num%i==0)
return i+SumOfFactors(i+1);
else if(i<num&&num%i!=0)
return 0+SumOfFactors(i+1);
else
return 0;
}
void check()
{
if(num==SumOfFactors(1))
System.out.println(num+" is Perfect number");
else
System.out.println(num+" is not perfect number");
}
}
VARIABLE DESCRIPTION:

VARIABLE DATA DESCRIPTION


NAME TYPE
n int To store the number
num int To store the number as a class variable
i int To store the factors of the nummber

Input: 15
Output: 15 is not Perfect number
PROGRAM 11 : A happy number is one in which eventual sum of
square of all the digits is 1. Input a number and check whether it is
a happy number or not through recursion.
import java.util.*;
class Happy
{
int num;
public static void main(String args[])
{ Scanner sc=new Scanner(System.in); System.out.println("enter your
numer"); int n=sc.nextInt();
while(n!=1&&n!=4)
{
n=SumOfDigits(n);
}
if(n==1)
System.out.println("It is happy number");
else
System.out.println("It is not a happy number");
}
static int SumOfDigits(int n)
{
int sum=0;
while(n>0)
{
sum+=(int)Math.pow(n%10,2);
n=n/10;
}
return sum;
}
}
VARIABLE DESCRIPTION:

VARIABLE DATA DESCRIPTION


NAME TYPE
n int To store the number
sum int To the sum of the squares of each
digit
num int To store the number

Input: 19
Output: It is a
HappyNumber
Explanation:
1^2 + 9^2 =82

8^2 + 2^2 =68

6^2+8^2 =100

1^2 + 0^2 + 0^2 = 1


PROGRAM 12 : Write a program for a palindrome string
using recursion.

import java.util.*;
public class Palindrome
{
public static boolean checkPalindrome(String str)
{
if(str.length() == 0 || str.length() == 1)
return true;
if(str.charAt(0) == str.charAt(str.length() - 1))
return checkPalindrome(str.substring(1, str.length() - 1));
return false;
}
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a string : ");
String strInput = sc.nextLine();
if(checkPalindrome(strInput))
{
System.out.println(strInput + " is palindrome");
}
else
{
System.out.println(strInput + " is not a palindrome");
}
sc.close();
}
}
VARIABLE DESCRIPTION :

VARIABL NAME DATA TYPE DESCRIPTION


Str String To store the string
strInput String To store the string at
the time of input

INPUT : “ARORA”
OUTPUT : ARORA is a palindrome
PROGRAM 13 : A class contain a two dimensional integer
array of order M×N where maximum value of both M & N
are 5. Design a class to shuffle the matrix. The first row
becomes the last and the 2nd row becomes the 1st and so
on.

import java.io.*;
class Shift{
private int mat[][];
private int m;
private int n;
public Shift(int mm, int nn){
m = mm;
n = nn;
if(m > 5)
m = 5;
if(n > 5)
n = 5;
mat = new int[m][n];
}
public void input()throws IOException{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.println("Enter matrix elements:");
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
mat[i][j] = Integer.parseInt(br.readLine());
}
public void cyclic(Shift P){
int s = m - 1;
int t = 0;
for(int i = 0; i < n; i++)
this.mat[s][t++] = P.mat[0][i];
s = 0;
for(int i = 1; i < m; i++){
t = 0;
for(int j = 0; j < n; j++)
this.mat[s][t++] = P.mat[i][j];
s++;
}
}
public void display()
{ for(int i = 0; i < m; i+
+){
for(int j = 0; j < n; j++)
System.out.print(mat[i][j] + "\t");
System.out.println();
}
}
public static void main(String args[])
throws IOException{
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.print("Number of rows: ");
int rows = Integer.parseInt(br.readLine());
System.out.print("Number of columns: ");
int cols = Integer.parseInt(br.readLine());
Shift obj1 = new Shift(rows, cols);
Shift obj2 = new Shift(rows, cols);
obj1.input();
System.out.println("ORIGINAL
MATRIX:"); obj1.display();
obj2.cyclic(obj1);
System.out.println("SHUFFLED
MATRIX:"); obj2.display();
}
}
VARIABLE DESCRIPTION :

VARIABLE DATA DESCRIPTION


NAME TYPE
i Int To execute the loop M times
j Int To execute the loop N times
M Int To store the number of rows
N Int To store the number of columns
t Int To store the swap value
s Int To store the last number of row
arr int To store the elements in an array

INPUT :
Number of rows: 2
Number of columns: 1
Enter matrix elements:
12
34
OUTPUT :
ORIGINAL MATRIX:
12
34
SHUFFLED MATRIX:
34
12
PROGRAM 14: Write a program to print an inputted 8
digit number in date format.

import java.util.*;
class Date_8
{
public static void main( String args[] )
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter your date in eight digit format");
String s= sc.nextLine();
int dd=Integer.parseInt(s.substring(0,2));
int mm=Integer.parseInt(s.substring(2,4));
int yy=Integer.parseInt(s.substring(4,8));
int d[]={31,28,31,30,31,30,31,31,30,31,30,31};
int d1[]={31,29,31,30,31,30,31,31,30,31,30,31};
String
mname[]={"January","February","March","April","May","June","Jul
y","August"
,"September","October","November","December"};
if(yy%4==0||yy%100==0||yy%400==0)
{
if(d1[mm-1]>=dd)
{
System.out.println(dd+"th "+mname[mm-1]+","+yy);
System.out.println("Valid Date");
}
else
System.out.println("Invalid Date");
}
else
{
if(d[mm-1]>=dd)
{
System.out.println(dd+"th "+mname[mm-1]+","+yy);
System.out.println("Valid Date");
}
else
System.out.println("Invalid Date");
}
}
}
VARIABLE DESCRIPTION :

VARIABLE DATA DESCRIPTION


NAME TYPE
s String To store the input date
dd Int To store the first two initials
mm Int To store second two initials
yy Int To store last four initials
d Int To store the non leap year dates
d1 Int To store the leap year dates
mname String To store the name of months
INPUT : 20102003
OUTPUT : 20OCTOBER2003
PROGRAM 15: Write a program to check whether given
number is evil number or not. An evil number is a non-
negative number that has an even number of 1s in its
binary expansion.

class evil
{
public static void main(int n)
{
String w="";
int count=1;
while(n!=1)
{
int r=n%2; w=w+r; n/=2;
}
for(int i=0;i<w.length();i++)
{
char ch=w.charAt(i);
if(ch=='1')
count++;
}
if(count%2==0)

System.out.println("evil number");
else
System.out.println("Odious Number(not an evil
number)");
}
}
VARIABLE DESCRIPTION :

VARIABLE DATA DESCRIPTION


NAME TYPE
n int To store the number entered by user
w String To store the binary expansion of
number
count int To count the number of 1’s in string
r int To store the remainder when divided
by 2
i int To execute the loop
ch char To extract each character from string

Input : 3
Output : Evil Number
Explanation: Binary
expansion of 3 is 11, the
number of 1s in this is 2
i.e. even.
Input : 16
Output : Odious Number(not
an evil number) Explanation:
Binary expansion of 16 =
10000, having number of 1s
=1 i.e. odd.
PROGRAM 16: Write a program to accept a string and
print the letters of given string in reverse order
separated by ‘-'.

class reverse
{
public static void main(String s)
{ String w="";
int p=0;
for(int i=0;i<s.length();i++)
{
char ch=s.charAt(i);
if(ch==' ')
{
w=s.substring(p,i);
System.out.print(w.charAt(w.length()-
1)+w.substring(1,w.length()-1)+w.charAt(0)+" ");
p=i;
}
}
}
}
VARIABLE DESCRIPTION :

VARIABLE DATA DESCRIPTION


NAME TYPE
w String To store each word of string
s String To store the input string
k String To store the reversed string
ch char To store each character of String
i Int To execute the loop to extract letters
j int To execute the loop to reverse the
string

INPUT : “M-A-H-A-T-M-A- G-A-N-D-H-I”


OUTPUT : A-M-T-A-H-A-M- I-H-D-N-A-G
PROGRAM 17 : Write a program to enter a string and
exchange the 1st and the last letter of each word.

import java.util.*;
class exchange_first_to_last
{
public static void main(String Args[])
{
int m, l, i, q=0;
char a, z, x;
String t;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Sentence");
String s=sc.nextLine();
s=s+" ";
l=s.length();
for(i=0;i<l;i++)
{
String k;
a=s.charAt(i);
if(a==' ')
{
t=s.substring(q,i);
m=t.length();
if(m>1)
{
z=t.charAt(0);
x=t.charAt(m-1);
t=t.substring(1,m-1);
k=x+t+z;
}
else
k=t;
q=i+1;
System.out.print(" "+k);
}
}
}
}
VARIABLE DESCRIPTION :

VARIABLE NAME DATA TYPE DESCRIPTION


s String To store the input
String by the user
w String To break the string
accordingly
p int To store the index of
variable i
i int To execute the loop
ch char To extract each
character from the
string

INPUT : COMPUTER
OUTPUT: ROMPUTEC
PROGRAM 18 : Write a program to input number of
weekdays(1 to 7) and translate it to its equivalent name of
the day of the week.

class weeks
{
public static void main(int w)
{
String weekname[] =
{"Monday","Tuesday","Wednesday","Thursday","Friday",
"Saturday", "Sunday"};
System.out.println(weekname[w-1]);
}
}

VARIABLE DESCRIPTION :

VARIABLE NAME DATA TYPE DESCRIPTION


w int To store the input
number by the user
weekname String To store the name of
weeks

INPUT : 05

OUTPUT : FRIDAY
PROGRAM 19 : A super class Worker has been defined
to share the details of a worker.
Define a subclass Wages to compute the monthly
wages for the worker. The details of both the classes
are given below:
Class name: Worker Data members:
Name: To store the name of the worker
Basic: to store the basic pay in decimal Member
functions:
Worker (): parameterized constructor to assign values
to the instance variable.
void display(): to display the
worker details. Class name :
Wages

Data members:

hrs: to store the hours worked


rate: to store the rate per hour

Wage: store the overall wage of


the worker Member function:

Wages(): parameterized constructor to assign the


value to the instance variable of both the classes.
double overtime(): calculate and return the
overtime amount as (hrs × rate)
void display(): calculate the wage using the formula
wage = overtime amount + Basic pay and display
along with other details.
Specify the class Worker giving details of the
constructor() and void display(). Using the concept of
inheritance specify the class Wages giving details of
the constructor(), double overtime() and void
display(). The main() function needs not be written.

class Worker
{
String name; double basic;
Worker(String s,double b)
{
name=s; basic=b;
}
void display()
{ System.out.println("Name of the worker : "+name);
System.out.println("Basic pay of the worker : "+basic);
}
}
class Wages extends Worker
{ int hrs;
double rate; double wage;
Wages( String s,double b,int h,double r)
{
super(s,b); rate=r; hrs=h; wage=0.0;
}
double overtime()
{
return (hrs*rate);
}
void display()
{
super.display();
wage= overtime()+basic; System.out.println("Total Wage given =
"+wage);
}
}

VARIABLE DESCRIPTION :

VARIABLE DATA DESCRIPTION


NAME TYPE
name String To store the name of the worker
basic double To store the basic pay of the worker
hrs int To store the number of hours of work

wage double To store the total wage given to the


worker
rate double To store the rate of the worker’s
payment
PROGRAM 20 : 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.
Some of the members of the class are given
below: Class name: WordPile
Data members/instance variables:
ch[]: character array to hold the character elements.
capacity: integer variable to store the maximum
capacity. top: to point to the index of the topmost
element.

Methods/Member functions:

WordPile(int cap): constructor to initialize the data member capacity


= cap, top = -1 and create the WordPile.
void pushChar(char v): adds the character to the top of WordPile
if possible, otherwise output a message “WordPile is full” char
popChar(): returns the deleted character from the top of the
WordPile if possible, otherwise it returns ‘\\’.
Write a program with class Wordpile giving details of a
constructor, void pushchar(char) to add the character to top, char
popchar() to return the deleted character from the top of the
Wordpile.

class WordPile
{
char ch[];
int capacity, top;
WordPile(int cap)
{
ch=new char[cap]; capacity=cap; top=-1;
}
void pushChar(char v)
{
if(top>=capacity)
System.out.println("WordPile is full");
else
ch[++top]=v;
}
char popChar()
{
if(top<0)
return
'\\'; else
return ch[top--];
}
}
VARIABLE DESCRIPTION :

VARIABLE DATA DESCRIPTION


NAME TYPE
ch int To store all the elements in an array
capacity int To store the capacity of the array
top int To store the top position of the data
cap int To store the capacity of the array
CONCLUSION
AFTER COMLETING THIS PROJECT I CONSIDER
MYSELF TO BE PRIVILEGED TO GET
OPPORTUNITY TO MAKE THE PROJECT ON THE
GIVEN TOPIC AND THIS PROVED VERY
INTRESTING TO ME. IT WAS AN IMMENSE
PLEASURE TO WORK ON THIS VALUEABLE
ASSIGNMENT. IT HAS ENHANCED MY
KNOWLEDGE WHICH HAS PROVED VERY
USEFUL AND FRUITFUL TO ME. I HOPE YOU
HAVE ENJOYED GOING THROUGH MY PROJECT
AND APPRECIATED MY SINCERE EFFORTS. YOUR
SUGGESTIONS ARE MOST IMPORTANT FOR
FURTHER IMPROVEMENT.

You might also like