Sie sind auf Seite 1von 36

COMPUTER SCIENCE ASSIGNMENT

QUESTION 1
Design a programme to accept a day number (between 1 and 366), year (in 4 digit) from the user to
generate and display the corresponding date. Also accept 'N' (1<=N<=100) from the user to compute
and display the future date corresponding to 'N' days after the generated date. Display an error
message if the value of the day number, year and N are not within the limit or not according to the
conditions specified.
Test your programme for the following data and some random data.
Example 1:
InputDAY NUMBER
YEAR
DATE AFTER (N)
Output20 TH AUGUST 2008
DATE AFTER 17 DAYS
Example 2:
InputDAY NUMBER
YEAR
DATE AFTER (N)
Output25 TH DECEMBER 2008
DATE AFTER 45 DAYS

233
2008
17

6TH SEPTEMBER 2008

360
2008
45

8TH FEBRUARY 2009

COMPUTER SCIENCE ASSIGNMENT

ALGORITHM
Algorithm for main() method
1.
2.
3.
4.
5.
6.

Start
Take day as input from console and store it in int variable d1
Take year as input from console and store it in int variable y1
Take N as input from console and store it in int variable n
Call the function Date as: obj.Date(d1,y1)
Check if n>=1 and n<=100, if yes then,
Assign d1=d1+n
Check if (y1%400==0) or (y%4==0) and (y%100!=0), if yes then,
Assign y1=y1+1
Assign d1=d1-366
Call the function Date as: obj.Date(d1,y1)
Else check if (y1%400!=0) and (y1%4!==0), if yes then,
Assign y1=y1+1
Assign d1=d1-365
Call the function Date as: obj.Date(d1,y1)
Else
Call the function Date as: obj.Date(d1,y1)
7. Else
Print "OUT OF RANGE"
8. End of main() method
Algorithm for function Date(int d, int y)
1. Initialise an int array as:
a[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
2. Check if (y1%400==0) or (y%4==0) and (y%100!=0), if yes then,
Assign a[1]=29
3. Initialise a String array as:
m[]={"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE","JULY","AUGUST",
"SEPTEMBER","OCTEBER","NOVEMBER","DECEMBER"}
4. Initialise two int variables as i=0 and j=0
5. Initialise a String variable as month=""
6. Repeat steps 7 to 9 till d>a[i]
7. Assign d=da[i]
8. Increase value of i by 1
9. Assign j=i
10. Repeat step 11 for i=0 till i<11
11. Check if j==I, if yes then
Assign month=m[i]
12. Print d, month and y
13. End of function

COMPUTER SCIENCE ASSIGNMENT

SOURCE CODE
import java.io.*;
class QUESTION1
{
void DATE(int d,int y)
{
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
if((y%400==0) || (Y%4==0) && (Y%100!=0))
a[1]=29;
String m[]={"JANUARY","FEBRUARY","MARCH","APRIL","MAY","JUNE",
"JULY","AUGUST","SEPTEMBER","OCTEBER","NOVEMBER","DECEMBER"};
int i=0,j=0;
String month="";
while(d>a[i])
{
d=d-a[i];
i++;
j=i;
}
for(i=0;i<=11;i++)
{
if(j==i)
month=m[i];
}
System.out.println(d+"TH"+"\t"+month+" "+y);
}
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("DAY NUMBER\t");
int d1=Integer.parseInt(br.readLine());
System.out.print("YEAR \t\t");
int y1=Integer.parseInt(br.readLine());
System.out.print("DATE AFTER(N)\t");
int n=Integer.parseInt(br.readLine());
System.out.println("\n");
QUESTION1 obj=new QUESTION1();
obj.DATE(d1,y1);
if(n>=1 && n<=100)
{
d1=d1+n;
if(((y%400==0) || (Y%4==0) && (Y%100!=0)) && d1>366)
{
y1+=1;
d1-=366;

COMPUTER SCIENCE ASSIGNMENT


System.out.print("DATE AFTER "+n+" DAYS ");
obj.DATE(d1,y1);
}
else if(y1%4!=0 && Y1%400!=0 && d1>365)
{
y1+=1;
d1-=365;
System.out.print("DATE AFTER "+n+" DAYS ");
obj.DATE(d1,y1);
}
else
{
System.out.print("DATE AFTER "+n+" DAYS ");
obj.DATE(d1,y1);
}
}
else
System.out.println("OUT OF RANGE");
}
}

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION

NAME
d
y
a[]
m[]
i
j
d1
y1
n

DATA TYPE
int
Int
int array
String array
int
int
int
Int
int

DESCRIPTION
As an argument for the function Date()
As an argument for the function Date()
An array to store the number of days of the months of a year
An array to store the name of the months of a year
To run a loop
To store the month number
To store the inputted day number
To store the inputted year
To store the inputted N

COMPUTER SCIENCE ASSIGNMENT

OUTPUT IN THE TERMINAL WINDOW

COMPUTER SCIENCE ASSIGNMENT

QUESTION 2
Write a programme to declare a matrix A[][] of order (m X n) where m is the number of rows and n is
the number of columns such that both m and n must be greater than 2 and less than 20. Allow the
user to input positive integer into these matrix. Perform the following tasks on the matrix:
(a) Sort the elements of the outer row and column in ascending order using any standard sorting
technique and arrange them in array.
(b) Calculate the sum of the outer row and column elements.
(c) Output the original matrix and the rearranged matrix.
Example:
Inputm=3
n=3
Original matrix:

Rearranged matrix:

1
8
6

7
2
3

4
5
9

1
9
8

3
2
7

4
5
6

Sum=43

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.

Start
Initialise an int variable m with inputted number of rows
Initialise an int variable n with inputted number of columns
If m<=2 or m>=20 or n<=2 or n>=20 then print "OUT OF RANGE" and exit from program
Declare an int array A[][] with dimensions (m X n) as A[][]=new int[m][n]
Initialise two int variable i=0 and j=0
Repeat steps 7 to 10 till i<m
Repeat steps 8 and 9 till j<n
Take input from console and store it in A[i][j]
Increase value of j by 1
Increase value of I by 1
Assign j=0
Repeat steps 14 to 17 till i<m
Repeat step 15 and 16 till j<n
Print A[i][j]+"\t"
Increase value of j by 1
Increase value of I by 1
Assign j=0
Initialise an int variable with number of boundary elements of matrix A[][] as:
no=(m*2)+(n*2)4
Declare an int array e[] with dimension 'no' as e[]=new int[no]
Initialise two int variable k=0 and t=0
Assign i=0
Repeat steps24 to 26 from j=0 till j<n
Assign e[k]=A[i][j]
Increase value of k by 1
Increase value of j by 1
Assign i=m1
Repeat steps 29 to 31 from j=0 till j<n
Assign e[k]=A[i][j]
Increase value of k by 1
Increase value of j by 1
Assign j=0
Repeat steps 34 to from i=1 till i<m1
Assign e[k]=A[i][j]
Increase value of k by 1
Increase value of i by 1
Assign j=n1
Repeat steps 39 to 41 from i=1 till i<m1
Assign e[k]=A[i][j]
Increase value of k by 1
Increase value of i by 1
Repeat steps 43 to 46 from i=0 till i<k
Repeat steps 44 to 45 from j=0 till j<k
Check if e[j]>e[j+1], if yes then

COMPUTER SCIENCE ASSIGNMENT

45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.

Assign t=e[j]
Assign e[j]=e[j+1]
Assign e[j+1]=t
Increase value of j by 1
Increase value of I by 1
Initialise int variable sum=0
Repeat steps 49 and 50 from i=0 till i<k
Assign sum=sum + e[k]
Increase value of i by 1
Assign i=0, k=0
Repeat steps 53 and 54 from j=0 till j<n
Assign A[i][j]=e[k]
Increase value of k and j by 1
Assign j=n1
Repeat steps 57 and 58 from i=12 till i<m
Assign A[i][j]=e[k]
Increase value of k and i by 1
Assign i=m1
Repeat 61 to 63 from j=n2 till j=0
Assign A[i][j]=e[k]
Increase value of k by 1
Decrease value of j by 1
Assign j=0
Repeat steps 66 to 68 from i=m2 till i=0
Assign A[i][j]=e[k]
Increase value of k by 1
Decrease value of i by 1
Repeat steps 70 to 73 from i=0 till i<m
Repeat step 71 and 72 from j=0 till j<n
Print A[i][j]+ "\t"
Increase value of j by 1
Increase value of i by 1
End

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class QUESTION2
{
public static void main(String args[])throws IOException
{
int m, n, i, j;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter m: ");
m=Integer.parseInt(br.readLine());
System.out.print("Enter n: ");
n=Integer.parseInt(br.readLine());
if(m<=2 || m>=20 || n<=2 || n>=20)
{
System.out.println("OUT OF RANGE");
System.exit(0);
}
int A[][]=new int[m][n];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print("Enter element ["+i+"]["+j+"]:");
A[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("\n");
System.out.println("ORIGINAL MATRIX");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
int no=(m*2)+(n*2)-4;
int e[]=new int[no],k=0,t=0;
i=0;
for(j=0;j<n;j++)
{
e[k]=A[i][j];
k++;
}
i=m-1;

10

10

COMPUTER SCIENCE ASSIGNMENT


for(j=0;j<n;j++)
{
e[k]=A[i][j];
k++;
}
j=0;
for(i=1;i<m-1;i++)
{
e[k]=A[i][j];
k++;
}
j=n-1;
for(i=1;i<m-1;i++)
{
e[k]=A[i][j];
k++;
}
for(i=0;i<k;i++)
{
for(j=0;j<k-i-1;j++)
{
if(e[j]>e[j+1])
{
t=e[j];
e[j]=e[j+1];
e[j+1]=t;
}
}
}
int sum=0;
for(i=0;i<k;i++)
{
sum+=e[i];
}
i=0;
k=0;
for(j=0;j<n;j++)
{
A[i][j]=e[k];
k++;
}
j=n-1;
for(i=1;i<m;i++)
{
A[i][j]=e[k];
k++;

11

11

COMPUTER SCIENCE ASSIGNMENT


}
i=m-1;
for(j=n-2;j>=0;j--)
{
A[i][j]=e[k];
k++;
}
j=0;
for(i=m-2;i>0;i--)
{
A[i][j]=e[k];
k++;
}
System.out.println("\n");
System.out.println("REARRANGED MATRIX");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("SUM= "+sum);
}
}

12

12

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
m
n
i
j
A[][]
no
e[]
k
t
sum

DATA TYPE
int
int
int
int
int array
int
int array
int
int
int

DESCRIPTION
To store the number of rows
To store the number of columns
To run loops
To run loops
To store the elements inputted by user
To store the number of boundary elements
To store the boundary elements
To store the number of boundary elements
To use as a temporary variable during sorting
To store the sum of the boundary element

13

13

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

14

14

COMPUTER SCIENCE ASSIGNMENT

15

QUESTION 3
Read a single sentence which terminates with a full stop (.). The words are to be separated with a
single blank space and are in lower case. Arrange the words contained in the sentence according to
the length of the words in ascending order. If two words are of same length then the word occurring
first in the input sentence should come first. For both input and output, the sentence must begin in
upper case.
Test your programme for following data and some random data.
Input 1: The lines are printed in reverse order.
Output 1: In the are lines order printed reverse.
Input 2: Print the sentence in ascending order.
Output 2: In the print order sentence ascending.
Input 3: I love my country.
Output 3: I my love country.

15

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

11.
12.
13.
14.
15.
16.
17.
18.

Start
Take input from console
Convert it into lower case
Eliminate last character of the inputted string
Split the string using split() function and store it in a String array a[]
Initialise int l with the size of a[] as l=a.length
Declare String variable temp, int variable i and j
Repeat steps 9 to 12 from i=0 till i<l
Repeat steps 10 and 11 from j=0 till j<(li1)
If length of a[i] > length of a[i+1]
Assign temp=a[i]
Assign a[i]=a[i+1]
Assign a[i+1]=temp
Increase value of j
Increase value of i
Print first word of rearranged string with its first character changed to upper case
Repeat steps 15 and 16 from i=0 till i<(l1)
Print a[i]+" "
Increase value of i by 1
Print last word of rearranged string with full stop (.) at end as a[l1]+ "."
End

16

16

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class QUESTION3
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a sentence:");
String s1=(br.readLine()).toLowerCase();
s1=s1.substring(0,s1.length()-1);
String a[]=s1.split(" ");
int l=a.length;
String temp;
int i,j;
for(i=0;i<l;i++)
{
for(j=0;j<(l-i-1);j++)
{
if(a[j].length()>a[j+1].length())
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.print((Character.toUpperCase(a[0].charAt(0)))+a[0].substring(1)+" ");
for(i=1;i<l-1;i++)
System.out.print(a[i]+" ");
System.out.println(a[l-1]+".");
}
}

17

17

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE
NAME
s1
a[]
l
temp
i
j

DATA TYPE
String
String array
int
int
int
int

DESCRIPTION
To store the inputted string
To store the words of the string
To store the number of words
Temporary variable to be used during sorting
To run a loop
To run a loop

18

18

COMPUTER SCIENCE ASSIGNMENT

OUTPUT IN THE TERMINAL WINDOW

19

19

COMPUTER SCIENCE ASSIGNMENT

20

QUESTION 4
A bank intends to design a program to display the denomination of an input amount, up to 5 digits.
The available denomination with the bank are of rupees 1000, 500, 100, 50, 20, 10, 5, 2 and 1.
Design a program to accept the amount from the user and display the break-up in descending order
of denomination, (i.e., preference should be given to the highest denomination available) along with
the total number of notes. [Note: Only the denomination used should be displayed]. Also print the
amount in words according to the digits.
Example 1:
INPUT- 14856
OUTPUTONE FOUR EIGHT FIVE SIX
DENOMINATORS:
1000X14=14000
500X1=500
100X3=300
50X1=50
5X1=5
1X1=1
TOTAL NUMBER OF NOTES: 21
Example 2:
INPUT- 6043
OUTPUTSIX ZERO FOUR THREE
DENOMINATORS:
1000X6=6000
20X2=40
2X1=2
1X1=1
TOTAL NUMBER OF NOTES: 10
Example 3:
INPUT- 235001
OUTPUTInput out of range.

20

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
1. Start
2. Take input from console and store it in int variable amount
3. Check if amount>99999, if yes then
Print "INVALID AMOUNT"
Exit from program
4. Initialise int amt=amount, rev=0
5. Repeat steps 6 to 8 from i=0 till i<length of amt
6. Initialise char ch with the character at I in amt
7. Check ch with digits 0 to 9 and print the corresponding digit in words
8. Increase value of i by 1
9. Initialise an int array den[] with the available denominations as:
den[]={1000, 500, 100, 50, 20, 10, 5, 2, 1}
10. Initialise two int variable i=0 and tot=0
11. Repeat steps 12 to 15 till amount not equals 0
12. Assign rev=amount/den[i]
13. If rev not equals 0, then
Print (den[i]+ "X" +rev + "=" rev*den[i])
Assign tot=tot+rev
14. Assign amount=amount%den[i]
15. Increase value of i by 1
16. Print ("Total number of notes: "+tot)
17. End

21

21

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class QESTION4
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int rev=0, amount, amt;
System.out.print("Enter the amount:");
String a=br.readLine();
amount=Integer.parseInt(a);
if(amount>99999)
{
System.out.println("INVALID AMOUNT");
System.exit(0);
}
amt=amount;
for(int i=0;i<a.length();i++)
{
char ch=a.charAt(i);
switch(ch)
{
case '0':
System.out.print("ZERO");
break;
case '1':
System.out.print("ONE");
break;
case '2':
System.out.print("TWO");
break;
case '3':
System.out.print("THREE");
break;
case '4':
System.out.print("FOUR");
break;
case '5':
System.out.print("FIVE");
break;
case '6':
System.out.print("SIX");
break;
case '7':
System.out.print("SEVEN");
break;

22

22

COMPUTER SCIENCE ASSIGNMENT


case '8':
System.out.print("EIGHT");
break;
case '9':
System.out.print("NINE");
}
System.out.print(" ");
}
int den[]={1000,500,100,50,20,10,5,2,1};
int i=0,tot=0;
System.out.println();
System.out.println("\nDENOMINATION:\n");
while(amount!=0)
{
rev=amount/den[i];
if(rev!=0)
{
System.out.print("\t\t"+den[i]+"\tX\t"+rev+"\t=\t"+rev*den[i]);
System.out.println();
tot+=rev;
}
amount=amount%den[i];
i++;
}
System.out.println("\t\tTOTAL\t\t\t=\t"+a);
System.out.println();
System.out.println("TOTAL NUMBER OF NOTES\t=\t"+tot);
}
}

23

23

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
rev
amount
amt
a
ch
den[]
i
tot

DATA TYPE
int
int
int
String
char
int array
int
int

DESCRIPTION
To store the number of notes of a particular denomination
To store the inputted amount
Dummy variable
To store the inputted amount
To store the extracted digits of the inputted amount
To store the available denominations
To run a loop
To store the total number of notes

24

24

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

25

25

COMPUTER SCIENCE ASSIGNMENT

26

QUESTION 5
A positive whole number 'n' having 'd' number of digits is squared and split into two pieces, a righthand piece that has 'd' digits and a left-hand piece that has remaining 'd' or 'd-1' digits. If the sum of
the two pieces is equal to the number, then 'n' is a Kaprekar number. The first few Kaprekar numbers
are: 9, 45, 297 . . . . . . . . .
Example 1:
9
92=81, right-hand piece of 81=1 and left-hand piece of 81=8
Sum=1+8=9, i.e. equal to the number.
Example 2:
45
452=2025, right-hand piece of 2025=25 and left-hand piece of 2025=20
Sum=25+20=45, i.e. equal to the number.
Example 3:
297
2972=88209, right-hand piece of 88209=209 and left-hand piece of 88209=88
Sum=209+88=297, i.e. equal to the number.
Given two positive integers p and q, where p<q, write a programme to determine how many Kaprekar
numbers are there in the range between p and q (both inclusive) and output them.
The input contains two positive integers p and q. assume p<5000 and q<5000. You are to output the
number of Kaprekar numbers in the specified range along with their values in the format specified
below:
SAMPLE DATA:
InputP=1
Q=1000
OutputTHE KAPREKAR NUMBERS ARE:1, 9, 45, 55, 99, 297, 703, 999
FREQUENCY OF KAPREKAR NUMBERS IS: 8

26

COMPUTER SCIENCE ASSIGNMENT


Algorithm
1.
2.
3.
4.
5.

6.
7.
8.
9.
10.
11.
12.

13.
14.

Start
Take p as input from console and store bit in int variable p
Take q as input from console and store bit in int variable q
Initialise two int variable as i=0 and c=0
Check if p>5000 or q>5000 or p>q, if yes then
Print "OUT OF RANGE"
Else
Goto step 6
Repeat steps 7 to 12 for i=p till i=q
Assign int len with the length of i as len=(""+i).length()
Assign long sq=i*i
Assign long k=sq%(10^len)
Assign long s=sq/(10^len)
Assign long newn=k+s
Check if newn=I, if yes then
Print i
Increase value of c by 1
Print c
End

27

27

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class QUESTION5
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("p= ");
int p=Integer.parseInt(br.readLine());
System.out.print("q= ");
int q=Integer.parseInt(br.readLine());
int i=0,c=0;
if(p>5000 || q>5000 || p>q)
System.out.println("OUT OF RANGE");
else
{
System.out.println("THE KAPREKAR NUMBERS ARE:-");
for(i=p;i<=q;i++)
{
int len=(""+i).length();
long sq=i*i;
long r=sq%(long)(Math.pow(10,len));
long l=sq/(long)(Math.pow(10,len));
long newn=(r+l);
if (newn==i)
{
System.out.print(i+",");
c++;
}
}
System.out.println("\n"+"FREQUENCY OF KAPREKAR NUMBBERS IS: "+c);
}
}
}

28

28

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
p
q
i
c
len
sq
r
l
newn

DATA TYPE
int
int
int
int
long
long
long
long
long

DESCRIPTION
To store the lower limit
To store the upper limit
To run a loop
Counter to count the number of Kaprekar numbers
To store the length of each number
To store the square of each number
To store the right hand piece of the square of the number
To store the left hand piece of the square of the number
To store the new number created by adding r and l

29

29

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

30

30

COMPUTER SCIENCE ASSIGNMENT

31

QUESTION 6
Input a paragraph containing 'n' number of sentences where (1=<n<4). The words are to be separated
with a single blank space and are in UPPERCASE. A sentence may be terminated either with a full stop
'. ', or a question mark '? ' only. Any other character may be ignored. Perform the following operations:
(a) Accept the number of sentences. If the number of sentences exceeds the limit, an appropriate
error message must be displayed.
(b) Find the number of words in the whole paragraph.
(c) Display the words in ascending order of their frequency. Words with same frequency may
appear in any order.
Example 1:
InputEnter number of sentences.
1
Enter sentences.
TO BE OR NOT TO BE.
OutputTotal number of words: 6
WORD
FREQUENCY
OR
1
NOT
1
TO
2
BE
2
Example 2:
InputEnter number of sentences.
3
Enter sentences.
THIS IS A STRING PROGRAM. IS THIS EASY? YES IT IS.
OutputTotal number of words: 11
WORD
FREQUENCY
A
1
STRING
1
PROGRAM
1
EASY
1
YES
1
IT
1
THIS
2
IS
3
Example 3:
InputEnter number of sentences.
5

OutputInvalid entry

31

COMPUTER SCIENCE ASSIGNMENT


ALGORITHM
1. Start
2. Take the number of sentences as input from console and store it in int variable n
3. Check if n<1 or n>=4, if yes then
Print "Invalid entry"
Exit from program
4. Take the sentences as input from console and store it in a String variable s
5. Convert s to UPPERCASE
6. Initialize int i=0, j=0, t=0
7. Initialize String temp=""
8. Replace full stop (.) and question mark (?) in the sentences with a blank space (" ")
9. Split the words using split() function and store them in a String array a[]
10. Find the number of words and store it in int l as :
l=a.length
11. Declare an int array c[] of size l
12. Print ("Total number of words: "+l)
13. Repeat steps 14 to 18 from i=0 till i<l
14. Assign c[i]=1
15. Repeat steps 16 and 17 from j=i+1 till j<l
16. Check if a[i] equals a[j], if yes then,
Increase value of c[i] by 1
Assign a[j]= ""
17. Increase value of j
18. Increase value of i
19. Repeat steps 20 to 24 from i=0 till i<l
20. Check if a[i] not equals "" , then go to 21 else go to 24
21. Repeat steps22 and 23 from j=0 till j<l
22. Check if c[i]<c[j], if yes then ,
Assign t=c[i]
Assign c[i]=c[j]
Assign c[j]=t
Assign temp=a[i]
Assign a[i]=a[j]
Assign a[j]=temp
23. Increase value of j by 1
24. Increase value of I by 1
25. Repeat steps 26 and 27 from i=0 till i<l
26. Check if a[i] not equals "", then,
Print (a[i]+ "\t\t"+c[i])
27. Increase value of I by 1
28. End

32

32

COMPUTER SCIENCE ASSIGNMENT


SOURCE CODE
import java.io.*;
class QUESTION6
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter number of sentences.");
int n=Integer.parseInt(br.readLine());
if(n<1 || n>=4)
{
System.out.println("Invalid entry");
System.exit(0);
}
else
{
System.out.println("Enter the sentence(s).");
String s=(br.readLine()).toUpperCase();
int i=0,j=0,t;
String temp;
s=s.replace('.',' ');
s=s.replace('?',' ');
String a[]=s.split(" ");
int l=a.length;
int c[]=new int[l];
System.out.println("\nTotal number of words: "+l);
System.out.println("WORD"+"\t\t"+"FREQUENCY");
for(i=0;i<l;i++)
{
c[i]=1;
for(j=i+1;j<l;j++)
{
if(a[i].equals(a[j]))
{
c[i]++;
a[j]="";
}
}
}
for(i=0;i<l;i++)
{
if(a[i].equals(""))
{}
else
{
for(j=0;j<l;j++)

33

33

COMPUTER SCIENCE ASSIGNMENT


{
if(c[i]<c[j])
{
t=c[i];
c[i]=c[j];
c[j]=t;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
for(i=0;i<l;i++)
{
if(a[i].equals(""))
{}
else
{
System.out.println(a[i]+"\t\t\t"+c[i]);
}
}
}
}
}

34

34

COMPUTER SCIENCE ASSIGNMENT


DATA VARIABLE DESCRIPTION TABLE

NAME
n
s
i
j
t
temp
a[]
l
c[]

DATA TYPE
int
String
int
int
int
String
String array
int
int array

DESCRIPTION
To store the number of sentences
To store the sentences
To run a loop
To run a loop
Temporary variable used during sorting
Temporary variable used during sorting
Array to store the words
To store the number of words
Array to store the length of each word

35

35

COMPUTER SCIENCE ASSIGNMENT


OUTPUT IN THE TERMINAL WINDOW

36

36

Das könnte Ihnen auch gefallen