java file best
TRANSCRIPT
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 1/53
SACRED HEART CONVENT HR. SEC.
SCHOOL
2014-2015
Computer
Project
F!e
Su"mtte# To$Su"mtte#%&$
Mr.Sanjay Chauhan Harshita Goyal
XII-B
1
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 2/53
RTIFI AT
This is to certify that Harshita Goyal of XII-B has
completed this project under my guidance and
supervision.She has taken proper care and shon utmost
sincerity in the completion of this project.It is the token of
her hard ork.
It is further certi!ed that this project is up to my
e"pectations and as per the guidelines issued #y IS$.
Mr.Sanjay Chauhan
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 3/53
A KNOWL DG M NT
By the grace of God I am very much pleased at the
completion of this project !le%and it is my foremost duty
to e"press my sincere thanks and gratitude to all those
ho have spared their valua#le time and gave me their
cooperation to complete this project !le.
&very successful ork re'uiresinspiration%cooperation and assistance %the same as the
e"perience ith this project ork.
So I thank my $omputer teacher %(r.Sanjay $hauhan
for his guidance and supervision.
I also give my hearty thanks to my parents ho
encouraged me and for their moral support hich ena#led
me to complete this project !le.
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 4/53
Question 1:
.Write a program to print the pseudoarithmetic sequence of numbers .You are given a
sequence of N integers, which are called as pseudoarithmetic sequences (sequences that are in
arithmetic progression).Sequence of N integers :2,5,6,8,9,12. We observe that 2+12=5+9=6+8=14.
The sum of the above sequence can be calculated as 14*3=42.
For sequence containing an odd numbers of elements the rule is to double the middle element, for
example 2,5,7,9,12=2+12=5+9=7+7=14 .
14*3=42 [middle element =7]
A class Pseudoarithmetic determines whether a given sequence is a pseudoarithmetic sequence.
The details of the class are given below:
Class name : Pseudoarithmetic
Data members/
Instance variables:
n : to store the size of the sequence
a [ ] : integer array to store the sequence of numbers
ans ,flag : store the status
sum : store the sum of sequence of numbers
r : store the sum of the two numbers
Member functions:
Pseudoarithmetic (): default constructor
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 5/53
Void accept (int nn) : to assign nn to n and to create an integer array . Fill in the elements of the
array
boolean check(): return true if the sequence is pseudoarithmetic
sequence otherwise returns false.
Specify the class Pseudoarithmetic , giving the details of the constructor( ),void accept (int) and
boolean check( ).Also define a main ( ) function to create an object and call the member functions
accordingly to enable the task.
Solution:
import java.io.*;
class Pseudoarithmetic
{
int n,a[ ],ans, fag,sum,r;
public Pseudoarithmetic( )
{
nansfagsumr!;
"
void accept(int nn) thro#s $%&'ception
{
int i;
nnn;
ane# int[n];
ueredeader brne# $nput+treameader(+stem.in));
-or(i!;in;i//)
{
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 6/53
+stem.out.println(0&nter a number1);
a[i]$nteger.parse$nt (br.read2ine());
"
"
boolean chec3( )
{
int i,j;
ra[!]/a[n45];
i-(n67!)
{
ans5;
"
-or( i!,jn45;in87;i//,j44)
{
sum a[i]/a[j];
i-(sum9r)
{
fag5;
brea3;
"
"
i- (ans!)
{
sum 7* a[n87];
i-(sum9r)
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 7/53
{
fag5;
"
"
i- (fago)
{
return(true);
"
else
{
return (-alse);
"
"
public static void main(+tring args [ ] ) thro#s $%&'ception
{
pseudoarithmetic obne# Pseudoarithmetic ( );
ob.accept(:);
boolean 'ob.chec3( );
i- ('true)
{
+stem.out.println(0pseudoarithmetic seuence1);
"
else
{
+stem.out.println(0not a pseudoarithmetic seuence1);
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 8/53
" "
"
Output:
Enter a number : 2 5 6 8 10 9 14
pseudoarithmetic sequence
Question 2:
A class Mixer has been defined 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 elements 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 elements of the array in ascending order without
any duplicates
Mixer mix( Mixer A) : to merge the current object array elements with the
parameterized array elements and return the resultant objectvoid display() : to display the elements of the array
Specify the classMixer, giving details of theconstructor(int),void accept(),Mixer
mix(Mixer) and void display(). Define themain() function to create an object and call the function
accordingly to enable the task.
Solution:
import java.io.*;class <i'er
{ int arr[]; int n; static ueredeader br ne# ueredeader(ne# $nput+treameader(+st <i'er(int nn) { n nn; arr ne# int[n];
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 9/53
"void accept()thro#s $%&'ception
{+stem.out.println(=>n* $nput the ?rra *>n=);
-or(int i!; in; i//)
{ +stem.out.print(=&nter &lement [=/(i/5)/=] @ =); arr[i] $nteger.parse$nt(br.read2ine()); " +stem.out.println(); "
<i'er mi'(<i'er ?) { int siAe this.arr.length / ?.arr.length;
<i'er ne# <i'er(siAe);
int ' !; -or(int i!; isiAe; i//) { i-(i?.arr.length) .arr[i] ?.arr[i]; else { .arr[i] this.arr[']; '//; " "
int temp!; -or(int i!; isiAe45; i//) { -or(int ji/5; jsiAe; j//) { i-(.arr[i]B.arr[j]) { temp .arr[i]; .arr[i] .arr[j]; .arr[j] temp; "
" " return ; " void displa() { -or(int i!; in; i//) {
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 10/53
+stem.out.print(arr[i] / = =); " +stem.out.println(); "
public static void main(+tring args[])thro#s $%&'ception { +stem.out.print(=&nter siAe o- the 5st arra @ =); int p $nteger.parse$nt(br.read2ine()); <i'er obj5 ne# <i'er(p); obj5.accept();
+stem.out.print(=&nter siAe o- the 7nd arra @ =); int $nteger.parse$nt(br.read2ine()); <i'er obj7 ne# <i'er(); obj7.accept();
<i'er objC obj7.mi'(obj5);+stem.out.print(=Dhe 5st ?rra is @ =);
obj5.displa();
+stem.out.print(=Dhe 7nd ?rra is @ =); obj7.displa();
+stem.out.print(=Dhe <erged ?rra is @ =); objC.displa(); "
"
Output:
Enter size of the 1st array : 5
* Input the Array *
Enter Element [1] : 11
Enter Element [2] : 13
Enter Element [3] : 17
Enter Element [4] : 24
Enter Element [5] : 33
Enter size of the 2nd array : 3
* Input the Array *
Enter Element [1] : 5
Enter Element [2] : 19
Enter Element [3] : 30
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 11/53
The 1st Array is : 11 13 17 24 33
The 2nd Array is : 5 19 30
The Merged Array is : 5 11 13 17 19 24 30 33
Question 3:
Write a program to accept a sentence which may be terminated by either ‘.’ ‘?’ or ‘!’ only. Any other
character may be ignored. The words may be separated by more than one blank space and are in
UPPER CASE.
Perform the following tasks:
(a) Accept the sentence and reduce all the extra blank space between two words to
a single blank space.
(b) Accept a word from the user which is part of the sentence along with its
position number and delete the word and display the sentence.
Test your program with the sample data and some random data:
Example 1
INPUT: A MORNING WALK IS A IS BLESSING FOR THE WHOLE DAY.
WORD TO BE DELETED: IS
WORD POSITION IN THE SENTENCE: 6
OUTPUT: A MORNING WALK IS A BLESSING FOR THE WHOLE DAY.
Example 2
INPUT: AS YOU SOW, SO SO YOU REAP.
WORD TO BE DELETED: SO
WORD POSITION IN THE SENTENCE: 4
OUTPUT: AS YOU SOW, SO YOU REAP.
Example 3
INPUT: STUDY WELL ##.
OUTPUT: INVALID INPUT.
Solution:
import java.io.*;class emoveEord
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 12/53
{public static void main(+tring args[])thro#s $%&'ception
{ ueredeader brne# ueredeader (ne# $nput+treameader (+stem.in)
+stem.out.print(=&nter an sentence @ =); 88 $nputting the sentence +tring s br.read2ine(); s s.toFpperGase(); 88 Gonverting the sentence into Fpper Gase int l s.length(); +tring ans==; 88 +tring variable to store the Hnal result char last s.char?t(l45); 88 &'tracting the last character 8* Ghec3ing #hether the sentence ends #ith I.I, IJI or a I9I or not *8 i-(last I.I KK last IJI KK last I9I) { +tring #ord[]s.split(=[.J9 ]/=); 88 +aving the #ords in an arra using split() int c #ord.length; 88 Linding the number o- #ords
+stem.out.print(=&nter the #ord to delete @ =); +tring del br.read2ine(); del del.toFpperGase(); +stem.out.print(=&nter the #ord position in the sentence @ =); int ' $nteger.parse$nt(br.read2ine()); i-('5 KK 'Bc) 88 Ghec3ing #hether integer inputted is acceptable or not { +stem.out.println(=+orr9 Dhe #ord position entered is out o- range=); " else {
-or(int i!; ic; i//) { 8* +3ipping i- the #ord to delete and the position matches *8 i-(#ord[i].euals(del)true MM i '45) continue; ans ans / #ord[i] / = =; " +stem.out.print(=%utput @ =/ans.trim()/last); " " else
{ +stem.out.println(=$nvalid $nput. &nd a sentence #ith either I.I, IJI or I9I=); " ""
Output:
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 13/53
1. Enter any sentence : A MORNING WALK IS A IS BLESSING FOR THE WHOLE DAY.
Enter the word to delete : IS
Enter the word position is the sentence : 6
Output : A MORNING WALK IS A BLESSING FOR THE WHOLE DAY.
Question 4:
Write a program to input a word from the user and remove the consecutive repeated characters by
replacing the sequence of repeated characters by its single occurrence.
Example:
INPUT-Heeeiiiissggoiinggg
OUTPUT – Heisgoing
Solution:
import java.io.*;class emoveepGhar{ public static void main(+tring args[])thro#s $%&'ception { ueredeader brne# ueredeader(ne# $nput+treameader(+stem.in)); +stem.out.print(=&nter an #ord@ =); 88 $nputting the #ord +tring s br.read2ine();
s s / = =; 88 ?dding a space at the end o- the #ord
int ls.length(); 88 Linding the length o- the #ord +tring ans==; 88 Nariable to store the Hnal result char ch5,ch7;
-or(int i!; il45; i//) { ch5s.char?t(i); 88 &'tracting the Hrst character
ch7s.char?t(i/5); 88 &'tracting the ne't character88 ?dding the Hrst e'tracted character to the result i- the current and the ne't char
are dierent
i-(ch59ch7) { ans ans / ch5; " " +stem.out.println(=Eord a-ter removing repeated characters =/ans); 88 Prin res
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 14/53
""
Output:
Enter any word: Jaaavvvvvvvvaaaaaaaaaaa
Word after removing repeated characters = Java
Question 5:
Given a time in numbers we can convert it into words. For example :
5 : 00 —— five o’clock
5 : 10 —— ten minutes past five
5 : 15 —— quarter past five
5 : 30 —— half past five5 : 40 —— twenty minutes to six
5 : 45 —— quarter to six
5 : 47 —— thirteen minutes to six
Write a program which first inputs two integers, the first between 1 and 12 (both inclusive)
and second between 0 and 59 (both inclusive) and then prints out the time they represent, in words.
Your program should follow the format of the examples above.
SAMPLE DATA :
1. INPUT :
TIME : 3,0
OUTPUT : 3 : 00 Three o’ clock
2.INPUT :
TIME : 7,29
OUTPUT : 7 : 29 Twenty nine minutes past seven
3.INPUT :
TIME : 6,34
OUTPUT : 6 : 34 Twenty six minutes to seven
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 15/53
4.INPUT :
TIME : 12,1
OUTPUT : 12 : 01 One minute past Twelve
5.INPUT :
TIME : 12,45
OUTPUT : 12 : 45 Quarter to One
6.INPUT :
TIME : 10,59
OUTPUT : 10 : 59 One minute to Eleven
7.INPUT :
TIME : 14,60
OUTPUT : Incorrect Input
Test your program for the data values given in the examples above and some random data.
Solution:
import java.io.*;public class Dime$nEords{
public static void main(+tring args[])thro#s $%&'ception {
ueredeader brne# ueredeader(ne# $nput+treameader(+stem.in)); 8* $nputting hours and minutes *8 +stem.out.print(=&nter Oours @ =); int h$nteger.parse$nt(br.read2ine()); +stem.out.print(=&nter <inutes @ =); int m$nteger.parse$nt(br.read2ine());
i-((hB5 MM h57) MM (mB! MM m:)) 88 chec3ing #hether given input { or not 8* creating an arra containing numbers -rom 547 in #ords * +tring #ords[]{==, =%ne=, =D#o=, =Dhree=, =Lour=, =Live=, =+i'=,=+even=, =&ight =Den=, =&leven=,=D#elve=,=Dhirteen=,=Lourteen=,=Li-teen=,=+i'teen=,=+eventeen=, =&ighteen=,=Qineteen=,=D#ent=,=D#ent one=, =D#ent t#o=, =D#ent three=, =D
-our=, =D#ent Hve=,=D#ent si'=,=D#ent seven=,=D#ent eight=, =D#ent nine= 8* Dhe belo# code is -or Hnding #hether to print the #ord IminuteI or IminutesI
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 16/53
+tring plu, a; i-(m 5) plu =<inute=; else plu =<inutes=;
i-(h57) a #ords[5]; 88storing IoneI #hen hour is 57 else a #ords[h/5];
8* Dhe belo# code chec3s minutes and accordingl prints the time in #ords using +stem.out.print(=%utput @ =/h/=@=/m/= 44444 =); 88printing the given time in n i-(m!) +stem.out.println(#ords[h]/= %I cloc3=); else i-(m5:) +stem.out.println(=Ruarter past =/#ords[h]); else i-(mC!)
+stem.out.println(=Oal- past =/#ords[h]); else i-(mS:) +stem.out.println(=Ruarter to =/a); else i-(mC!) 88 condition -or minutes bet#een 547 +stem.out.println(#ords[m]/= =/plu/= past =/#ords[h]); else 88 condition -or minutes bet#een C54: +stem.out.println(#ords[T!4m]/= =/plu/= to =/a); " 88end o- outer i-
else +stem.out.println(=$nvalid $nput 9=); 88printing error message -or illegal inpu
""
Output:
Example 1:
Enter Hours : 12
Enter Minutes : 39
Output : 12:39 —– Twenty one Minutes to One
Example 2:
Enter Hours : 12
Enter Minutes : 39
Output : 12:39 —– Twenty one Minutes to One
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 17/53
Question 6:
The input in this question will consist of a number of lines of English text consisting of the letters of
the English alphabets, the punctuation marks (‘) apostrophe, (.) full stop, (,) comma , (;) semicolon ,
(:) colon and white space. Write a program to print the words of the input in reverse order without any
punctuation marks other than blanks. Individual words (i.e. characters of every word) are not
reversed .For example, Consider the following input text:
INPUT:
Enter number of sentences: 2
Enter the sentences:
This is a sample piece of text to illustrate this question
if you are smart you will solve this right.
OUTPUT: right this solve will you smart are you if question this illustrate to text of piece sample a is
this
Test your program for the following data and some random data:
Sample Input :
Enter number of sentences: 1
Enter the text:
Do not judge a book by its cover.
Sample Output: Cover its by book a judge not do
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 18/53
Solution:
import java.io.*;import java.util.*;class +entU<ergeUev{ public static void main(+tring args[])thro#s $%&'ception { ueredeader brne# ueredeader(ne# $nput+treameader(+stem.in)); +stem.out.print(=&nter the number o- sentences@ =);
int n $nteger.parse$nt(br.read2ine()); +tring s ==;
-or(int i5; in; i//) { +stem.out.print(=&nter +entence =/i/=@ =); s s / br.read2ine(); 88inputting multiple sentences and joining them 88 " +tringDo3eniAer strne# +tringDo3eniAer(s,= I.,;@9J=); int cstr.countDo3ens(); +tring #==, rev==; -or(int i5; ic; i//) { # str.ne'tDo3en(); 88e'tracting one #ord at a time
rev #/= =/rev; 88joining the e'tracted #ords in reverse order "
+stem.out.println(=%utput@ =/rev); ""
Output:
Enter the number of sentences: 2
Enter Sentence 1: Emotions, controlled and directed to work, is character.
Enter Sentence 2: By Swami Vivekananda.
Output: Vivekananda Swami By character is work to directed and controlled Emotions
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 19/53
Question 7:
A classRecursion has been defined to find the Fibonacci series upto a limit. Some of the members
of the class are given below:
Class Name : Recursion
Data Members/instance variables : a, b, c, limit (all integers)
Member functions/methods :
Recursion() : constructor to assign a,b,c with appropriate values.
void input() : to accept the limit of the series.
int fib(int n) : to return thenth Fibonacci term using recursive technique.
void genearate_fibseries() : to generate the Fibonacci series upto the given limit.
Specify the classRecursion giving details of theconstructor,int fib() ,void generate_fibseries().
You may assume other functions are written for you and you need not write the main function.
Solution:
import java.io.*;class ecursion{ static ueredeader brne# ueredeader(ne# $nput+treameader(+stem.i int a,b,c,limit;ecursion() 88Gonstructor { a!; b5; c!; limit!; " void input()thro#s $%&'ception 88Lunction to input the limit {
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 20/53
+stem.out.print(=&nter the limit @ =); limit$nteger.parse$nt(br.read2ine()); " int Hb(int n) 88ecursive -unction generating the InthI term o- Libonacci +eries
{ i-(n5) return a; else i-(n7) return b; else return (Hb(n45)/Hb(n47)); " void generateUHbseries() 88Lunction generating all the Libonacci +eries no.s upto InI {
+stem.out.println(=Dhe Libonacci +eries is@=); -or(int i5;ilimit;i//) { cHb(i); +stem.out.print(c/= =); " " public static void main(+tring args[])thro#s $%&'ception { ecursion obne# ecursion();
ob.input();ob.generateUHbseries();
""
Output:
Enter the limit : 11
The Fibonacci Series is:
0 1 1 2 3 5 8 13 21 34 55
Enter the limit : 20
The Fibonacci Series is:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 21/53
Question 8:
Write a program to declare a square matrix A[ ] [ ] of order (M x M) where ‘M’ is the number of rows
and the number of columns such that M must be greater than 2 and less than 10. Accept the value of
M as user input. Display an appropriate message for an invalid input. Allow the user to input integers
into this matrix. Perform the following tasks:
(a) Display the original matrix.
(b) Check if the given matrix is Symmetric or not.
A square matrix is said to be Symmetric, if the element of the ith row and jth column is equal to
the element of the jth row and ith column.
(c) Find the sum of the elements of left diagonal and the sum of the elements of right diagonal
of the matrix and display them.
Test your program with the sample data and some random data:
Example 1
INPUT : M = 3
1 2 3
2 4 5
3 5 6
OUTPUT :
ORIGINAL MATRIX
1 2 3
2 4 5
3 5 6
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 22/53
THE GIVEN MATRIX IS SYMMETRIC
The sum of the left diagonal = 11
The sum of the right diagonal = 10
Example 2
INPUT : M = 22
OUTPUT : THE MATRIX SIZE IS OUT OF RANGE
Solution:
import java.io.*;
class +metric<atri'
{
public static void main(+tring args[])thro#s $%&'ception
{
ueredeader brne# ueredeader(ne# $nput+treameader(+stem.in));
+stem.out.print(=&nter the number o- elements @ =);
int m$nteger.parse$nt(br.read2ine());
int ?[][]ne# int[m][m];
i-(mB7 MM m5!) 88 Ghec3ing -or valid input o- ro#s and columns siAe
{
+stem.out.println(=n$nputting the elements in the <atri'@ n=);
-or(int i!;im;i//)
{
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 23/53
-or(int j!;jm;j//)
{
+stem.out.print(=&nter the elements @ =);
?[i][j]$nteger.parse$nt(br.read2ine());
"
"
+stem.out.println(=nDhe %riginal <atri' is @ =);
-or(int i!;im;i//)
{
-or(int j!;jm;j//)
{
+stem.out.print(?[i][j]/=>t=);
"
+stem.out.println();
" 8* Ghec3ing #hether the matri' is smmetric or not *8
int fag !;
-or(int i!;im;i//)
{
-or(int j!;jm;j//)
{
i-(?[i][j] 9 ?[j][i])
{
fag 5; 88 +etting fag 5 #hen elements do not match
brea3;
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 24/53
"
"
"
i-(fag 5)
+stem.out.println(=nDhe given <atri' is Qot +mmetric=);
else
+stem.out.println(=nDhe given <atri' is +mmetric=);
int ld !, rd !; 8* Linding sum o- the diagonals *8
-or(int i!;im;i//)
{
-or(int j!;jm;j//)
{
i-(i j) 88 Gondition -or the le-t diagonal
{
ld ld / ?[i][j];
"
i-((i/j) (m45)) 88 Gondition -or the right diagonal
{
rd rd / ?[i][j];
"
"
"
+stem.out.println(=Dhe sum o- the le-t diagonal =/ld);
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 25/53
+stem.out.println(=Dhe sum o- the right diagonal =/rd);
"
else
+stem.out.println(=Dhe <atri' +iAe is %ut %- ange=);
"
"
Output:
Enter the number of elements:3
Inputting the elements in the matrix:
Enter the elements : 1
Enter the elements : 2
Enter the elements : 3
Enter the elements : 4
Enter the elements : 5
Enter the elements : 6
The Original Matrix Is:
1 2 3
2 4 5
3 5 6
The given Matrix is Symmetric
The sum of the left diagonal =11
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 26/53
The sum of the right diagonal =10
Question 9:
Write a Program in Java to fill a square matrix of size ‘n*n” in a spiral fashion (from the inside) with
natural numbers from 1 to n*n, taking ‘n’ as input.
For example: if n = 5, then n*n = 25, hence the array will be filled as given below.
Note: Don’t be confused as to how the filling will start from the center. Just treat the above program
as (see the difference in the direction of the arrows):
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 27/53
So, you see, this question is nothing but the same circular matrix which we did earlier (See: Java
Program to print Circular (Spiral) Matrix). The only change is that here the filling of numbers is from
‘n*n’ to 1 every time decreasing by one. In the earlier program it was from 1 to ‘n*n’ every time
increasing by one.
So slight changes in our earlier program will result into this new spiral matrix. The changes will be:
• Start ‘k’ from ‘n*n’ and not 1
• Change the condition in while loop to while(k >= 1)
• Decrease the value of ‘k’ every time as ‘k- -‘
Solution:
import java.io.*;
class Gircular <atri'
{
public static void main(+tring args[])thro#s $%&'ception
{
ueredeader brne# ueredeader(ne# $nput+treameader(+stem.in
+stem.out.print(=&nter the number o- elements @ =);
int n$nteger.parse$nt(br.read2ine());
int ?[][]ne# int[n][n];
int 3n*n, c5!, c7n45, r5!, r7n45;
#hile(3B5)
{
-or(int ic5;ic7;i//)
{
?[r5][i]344;
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 28/53
"
-or(int jr5/5;jr7;j//)
{
?[j][c7]344;
"
-or(int ic745;iBc5;i44)
{
?[r7][i]344;
"
-or(int jr745;jBr5/5;j44)
{
?[j][c5]344;
"
c5//;
c744;
r5//;
r744;
"
+stem.out.println(=Dhe Gircular <atri' is@=);
-or(int i!;in;i//)
{
-or(int j!;jn;j//)
{
+stem.out.print(?[i][j]/ =>t=);
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 29/53
"
+stem.out.println();
"
"
"
Output:
Enter the number of elements:5
The Circular Matrix is:
25 24 23 22 21
10 9 8 7 20
11 2 1 6 19
12 3 4 5 18
13 14 15 16 17
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 30/53
Question 10:
Write a Program in Java to input a 2-D array of size ‘m*n’ and print its boundary (border) elements.
For example:
Solution:
import java.io.*;
class oundarU&lement
{
public static void main(+tring args[])thro#s $%&'ception
{
int i,j,m,n;
ueredeader brne# ueredeader(ne# $nput+treameader(+stem.in));
+stem.out.print(=&nter the no. o- ro#s@ =); 88$nputting the number o- ro#s
m$nteger.parse$nt(br.read2ine());
+stem.out.print(=&nter the no. o- columns@ =); 88$nputting the number o- colum
n$nteger.parse$nt(br.read2ine());
int ?[][]ne# int[m][n]; 88Greating the arra
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 31/53
-or(i!;im;i//) 8* $nputting the arra *8
{
-or(j!;jn;j//)
{
+stem.out.print(=&nter the elements@ =);
?[i][j]$nteger.parse$nt(br.read2ine());
"
"
+stem.out.println(=Dhe oundar &lements are@=);
-or(i!;im;i//)
{
-or(j!;jn;j//)
{
i-(i! KK j! KK i m45 KK j n45) 88condition -or accessing boundar
+stem.out.print(?[i][j]/=>t=);
else
+stem.out.print(= >t=);
"
+stem.out.println();
"
"
"
Output:
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 32/53
Enter the no. of rows: 4
Enter the no. of columns: 3
Enter the elements : 1
Enter the elements : 2
Enter the elements : 3
Enter the elements : 4
Enter the elements : 5
Enter the elements : 6
Enter the elements : 7
Enter the elements : 8
Enter the elements : 9
Enter the elements : 10
Enter the elements : 11
Enter the elements : 12
The Boundary Elements are :
1 2 3
4 6
7 9
10 11 12
Question 11:
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 33/53
Write a program to input a number. Count and print the frequency of each digit present in that
number. The output should be given as:
Sample Input: 44514621
Sample Output:
=====================
Digit Frequency
=====================
1 2
2 1
4 3
5 1
6 1
Solution:
import java.io.*;class VigitULre{ public static void main(+tring args[])thro#s $%&'ception { ueredeader brne# ueredeader(ne# $nput+treameader(+stem.in)); +stem.out.print(=&nter an number @ =); int n $nteger.parse$nt(br.read2ine());
int -re[]ne# int[5!]; 88arra -or storing -reuenc o- all digits
-or(int i!; i5!; i//) { -re[i]!; 88intialiAing the count o- ever digit #ith I!I "
8* Lreuenc o- digit I!I is stored in -re[!], -reuenc o- I5I in -re[5] and so o
+stem.out.println(=%utput@=); +stem.out.println(==); 88this is just -or stling output +stem.out.println(=Vigit>tLreuenc=); +stem.out.println(==);
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 34/53
int d; #hile(nB!) { dn65!; 88e'tracting digit -rom the end -re[d]//; 88increasing the -reuenc o- that digit.
nn85!; "
-or(int i!; i5!; i//) { i-(-re[i]9!) 88printing onl those digits #hose count is not I!I +stem.out.println(= =/i/=>t =/-re[i]); " " "
Output:
Enter any number : 937825770
Output:
=====================
Digit Frequency
=====================
0 1
2 1
3 1
5 1
7 3
8 1
9 1
Question 12:
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 35/53
Write a program in Java to find the Roman equivalent of any Decimal number entered by the user.
[The number entered should be within the Range 1-3999]. The Roman numerals follow this basic
pattern,
1000 = M, 900 = CM, 500 = D, 400 = CD, 100 = C, 90 = XC, 50 = L, 40 = XL, 10 = X, 9 = IX, 5 = V, 4
= IV, 1 = I
The symbols “I”, “X”, “C”, and “M” can be repeated three times in succession, but no more. i.e. 234
can be represented as CCXXXIV, but 244 cannot be written as CCXXXXIV [Since we cannot repeat
X more than 3 times successively].
(They may only appear more than three times if they appear non-sequentially, such as XXXIX.) “D”,
“L”, and “V” can never be repeated.
Solution:
import java.io.*;class Vec7oman {public static void main(+tring args[]) thro#s $%&'ception{ueredeader brne# ueredeader(ne# $nput+treameader(+stem.in));+stem.out.print(=&nter a Qumber @ =);
int num$nteger.parse$nt(br.read2ine()); 88accepting decimal number i-(numB! MM numS!!!) 88chec3ing #hether the no. entered is #ithin the range [5{ 8*+aving the oman o- the thousand, hundred, ten and units place o- a decimal n
+tring thou[]{==,=<=,=<<=,=<<<=";+tring hund[]{==,=G=,=GG=,=GGG=,=GV=,=V=,=VG=,=VGG=,=VGGG=,=G<=";+tring ten[]{==,=W=,=WW=,=WWW=,=W2=,=2=,=2W=,=2WW=,=2WWW=,=WG=";+tring unit[]{==,=$=,=$$=,=$$$=,=$N=,=N=,=N$=,=N$$=,=N$$$=,=$W="; int thnum85!!!; 8*Linding the digits in the thousand, hundred, ten and units place
int h(num85!!)65!;int t(num85!)65!;int unum65!; +stem.out.println(=oman &uivalent =/thou[th]/hund[h]/ten[t]/unit[u]);" else+stem.out.println(=nXou entered a number out o- ange.nPlease enter a number in
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 36/53
range [54C]=);""
Output:
Enter a Number : 3482
Roman Equivalent = MMMCDLXXXII
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 37/53
Question 13:
Write a program to swap two numbers without using any third variable
Solution:
import java.io.*;class +#apping { public static void main(+tring args[])thro#s $%&'ception { ueredeader brne# ueredeader(ne# $nput+treameader(+stem.in int a,b; +stem.out.print(=&nter the 5st no@ =); a$nteger.parse$nt(br.read2ine()); +stem.out.print(=&nter the 7nd no@ =);
b$nteger.parse$nt(br.read2ine()); +stem.out.println(=4444444444444444444444444444444=); +stem.out.println(=Dhe numbers be-ore s#apping are=); +stem.out.println(=a =/a); +stem.out.println(=b =/b); aa/b; 88eginning o- +#apping ba4b; aa4b; 88&nd o- +#apping +stem.out.println(=4444444444444444444444444444444=); +stem.out.println(=Dhe numbers a-ter s#apping are=); +stem.out.println(=a =/a);
+stem.out.println(=b =/b); " "
Output:
Enter the 1st no: 25
Enter the 2nd no: 13
The numbers before swapping are
a = 25
b = 13
The numbers after swapping are
a = 13
b = 25
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 38/53
Question 14:
Write a Program in Java to input a Date in ddmmyyyy 8-digit format and print it in:
1) dd/mm/yyyy format
2) dd, month name, yyyy format
Input: 01011943
Output:
01/10/1943
1 January, 1943
Solution:
import java.io.*;class VateUVV<<XX{
public static void main(+tring args[])thro#s $%&'ception {
ueredeader brne# ueredeader(ne# $nput+treameader(+stem.in)); int l, , d, m; +tring dd, mm, ; int ma'das[]{!,C5,7Y,C5,C!,C5,C!,C5,C5,C!,C5,C!,C5";
+tring month[]{ ==, =Zanuar=, =Lebruar=, =<arch=, =?pril=, =<a=, =Zune=, =Zu
=?ugust=, =+eptember=, =%ctober=, =Qovember=, =Vecember= "; +stem.out.print(=&nter an date in Y digits (ddmm) -ormat@ =); +tring date br.read2ine(); 88inputting the date in +tring -ormat l date.length(); 88Hnding number o- digits in the given input i-(lY) 88per-orming the tas3 onl #hen number o- digits is Y { dd date.substring(!,7); 88e'tracting the da in +tring -ormat mm date.substring(7,S); 88e'tracting the month in +tring -ormat date.substring(S); 88e'tracting the ear in +tring -ormat d $nteger.parse$nt(dd); 88da in $nteger -ormat m $nteger.parse$nt(mm); 88month in $nteger -ormat
$nteger.parse$nt(); 88ear in $nteger -ormat i-((6S!!!) KK ((65!!9!)MM(6S!))) 88 condition -or leap ear { ma'das[7]7; "
i-(m! KK mB57 KK d! KK dBma'das[m] KK ! KK B)+stem.out.println(=Dhe da, month or ear are outside acceptable limit=);
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 39/53
"
else { +stem.out.println(=Vate in dd8mm8 -ormat =/dd/=8=/mm/=8=/
+stem.out.print(=Vate in dd, month name, -ormat =/dd/= =/mo =/); " "
else +stem.out.println(=Erong $nput=); ""
Output:
1)Enter any date in 8 digits (ddmmyyyy) format: 02052013
Date in dd/mm/yyyy format = 02/05/2013
Date in dd, month name, yyyy format = 02 May, 2013
2)Enter any date in 8 digits (ddmmyyyy) format: 12111963
Date in dd/mm/yyyy format = 12/11/1963
Date in dd, month name, yyyy format = 12 November, 1963
3)Enter any date in 8 digits (ddmmyyyy) format: 252013
Wrong Input
4)Enter any date in 8 digits (ddmmyyyy) format: 29022013
The day, month or year are outside acceptable limit
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 40/53
Question 15:
Write a program to check whether a given number is palindrome or not.
Solution:
class palindrome
{
public static void main(String args[])
{
int num=989, n=num,q=0,dig,rn=0;
while(num>0)
{
q= num/10;
dig=num%10;
rn=rn*10+dig;
num=q;
}
System.out.println(“Number is Palindrome”);
else
System.out.println(“ Number is not Palindrome”);
System.out.println(“Number is ”+n);
}
}
OUTPUT:
Number is Palindrome
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 41/53
Number is 989
Question 16 :
Write a program to generate Fibonacci series.
For example:1,1,2,3,5,8,13.
Solution:
import java.io.*;
class Libonacci
{
public static void main(+tring args[])thro#s $%&'ception
{
ueredeader brne# ueredeader (ne#
$nput+treameader(+stem.in));
+stem.out.print(0&nter the number o- terms@1);
int n$nteger.parse$nt(br.read2ine());
int a!b5,c!,';
#hile (cn)
{
+stem.out.println(0 0/a);
'a/b;
ab;
b';
c//;
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 42/53
"
"
"
Output:
Enter the number of terms: 1 1 2 3 5 8 13 21
Question 17
Write a program to take any word of length less than ten from the user and then print the number of
vowels present in the word.
Solution:
import java.io.*;
class length
{
public static void main(String args[])throws IOException
{
String str= “ “;
int i, count=0;
char c;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Do
{
if(str.length()>=10)
System.out.print(“Word Length must be less than 10.ReEnter”);
else
System.out.print(“Enter the word :”);
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 43/53
str=br.readLine();
}
while(str.length()>=10)
str =str. toLowerCase();
for(i=0;i<str.length();i++)
{
c=str.charAt(i);
If(c==’a’||c==’e’||c==’I’||c==’o’||c==’u’)
{
count++;
}
}
System.out.println(“The number of vowels are”+count);
}
}
Output:
Enter the word ; Harshita
The number of vowels are : 3
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 44/53
Question 18:
Write a program to accept a sentence as input. The words in the string are to be separated by a
blank. Each word must be in upper case. The sentence is terminated by either “.”,”!” or “?”. Perform
the following tasks:
(i) Obtain thelength of the sentence.(measured in words)(ii) Arrange the sentence in alphabetical order of the words.
Test your program with the sample data and some random data:
Example 1:
INPUT:NECESSITY IS THE MOTHER OF INVENTION.
OUTPUT:
Length: 6
Rearranged Sentence:
INVENTION IS MOTHER NECESSITY OF THE
Example 2:
INPUT: BE GOOD TO OTHERS.
OUTPUT:
Length: 4
Rearranged Sentence: BE GOOD OTHERS TO
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 45/53
Solution:
import java.io.*;class Eords$n+entence{
public void ta3e() thro#s $%&'ception { ueredeader brne# ueredeader(ne#$nput+treameader(+stem.in)); +tring str, #ords[], st311; int i,j,c!,fag, len; char ch; #hile(true) { fag!; +stem.out.println(0&nter the sentence@1);
strbr.read2ine(); len str.length(); #ordsne# +tring[len]; -or(i!;i { i-(Gharacter.is2o#erGase(str.char?t(i))) { fag5; brea3; " "
i- (fag!) brea3; else +stem.out.println(0&nter the sentence again #ith all uppercaseletters1); " i!; #hile(ilen) { chstr.char?t(i); i-(ch \KK ch. KK chJ KK ch9)
{ #ords[c]st3; c//; i//; st311; " else {
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 46/53
st3/ch; i//; " " -or(i!;ic;i//)
{ -or(j!;jc45;j//) { i-((#ords[j].compareDo(#ords[j/5]))B!) { st3#ords[j]; #ords[j]#ords[j/5]; #ords[j/5]st3; " " "
+stem.out.println(02ength 0/c); +stem.out.println(0>nearranged +entence@>n1); -or(i!;ic;i//) +stem.out.print(#ords[i]/1 0); " public static void main(+tring args[]) thro#s $%&'ception { Eords$n+entence obne# Eords$n+entence(); ob.ta3e(); "}
Output:
&nter the sentence@ BE GOOD TO OTHERSLength: 4
Rearranged Sentence: BE GOOD OTHERS TO
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 47/53
Question 19:
Write a program to declare a matrix A [][] of order (MXN) where ‘M’ is the numer of rows and
‘N’ is the numer of columns such that oth M and N must e greater than ! and less than !"#
Allow the user to input integers into this matrix# $erform the following tas%s on the matrix&
'ispla the input matrix
ind the maximum and minimum *alue in the matrix and displa them along with their position#
+ort the elements of the matrix in ascending order using an standard sorting techni,ue and
rearrange them in the matrix#
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 48/53
-utput the rearranged matrix#
Test your program with the sample data and some random data:
Example 1.
INPUT:
M./
N.0
1ntered *alues& 234353/36!3"3037383/39360
OUTPUT:
-riginal matrix&
2 4 5 /
6! " 0 7
8 / 9 60
:argest Numer& 5
;ow& "
<olumn& !
+mallest Numer& 60
;ow.!
<olumn./
;earranged matrix&
60 6! " 8
/ / 0 7
9 4 2 5
Solution :
import =a*a#io#>?
class Matrix
@
pulic *oid ta%e()throws 1xception
@
uffered;eader r.new uffered;eader(new Bnput+tream;eader(+stem#in))?
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 49/53
int arr[][]3m3n?
int g3h3i3=3max3min3maxr3maxc3minr3minc?
while(true)
@
+stem#out#println(CDn1nter the numer of rows &E)?
m.Bnteger#parseBnt(r#read:ine())?+stem#out#println(CDn1nter the numer of columns&E)?
n.Bnteger#parseBnt(r#read:ine())?
if(mF! GG nF! GG mH!" GG nH!")
+stem#out#println(CDn1nter the numer of rows and columnsE)?
else
rea%?
I
arr.new int[m][n]?
for(i."?iFm? iJJ)
@
for(=."?=Fn?=JJ)@
+stem#out#println(CDn1nter Kalue&E)?
arr[i][=].Bnteger#parseBnt(r#read:ine())?
I
I
max.arr["]["]?
min.arr["]["]?
maxr."?
minr."?
maxc."?
minc."?
for(i."?iFm?iJJ)
@
for(=."?=Fn?=JJ)
@
if(arr[i][=]Hmax)
@
max.arr[i][=]?
maxr.i?
maxc.=?
I
else if(arr[i][=]F min)
@
minr.i?
minc.=?
min.arr[i][=]?
I
I
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 50/53
I
+stem#out#println(CDn-riginal MatrixDnE)?
for(i."?iFm?iJJ)
@
for(=."?=Fn?=JJ)
@+stem#out#print(arr[i][=]JE C)?
I
+stem#out#println()?
I
+stem#out#println(CDnLargest Number.EJmax)?
+stem#out#println(CDn;ow.EJmaxr)?
+stem#out#println(CDn<olumn.EJmaxc)?
+stem#out#println(CDn Smallest Number.EJmin)?
+stem#out#println(CDn;ow.EJminr)?
+stem#out#println(CDn<olumn.EJminc)?
for(g."?gFm?gJJ)
@
for(h."?hFn?hJJ)
@
for(i."?iFm?iJJ)
@
for(=."?=Fn?=JJ)
@
if(arr[g][h]F arr[i][=])
@
min.arr[g][h]?arr[g][h].arr[i][=]?
arr[i][=].min?
I
I
I
I
I
+stem#out#println(CDn;earranged matrixDnE)?
for(i."?iFm?iJJ)
@
for(=."?=Fn?=JJ)@
+stem#out#print(arr[i][=]JE C)?
I
+stem#out#println()?
I
I
pulic static *oid main(+tring args[]) throws 1xception
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 51/53
@
Matrix o.new Matrix()?
o#ta%e()?
I
I
OUTPUT:
Original matrix:
8 7 9 3
-2 0 4 5
1 3 6 -4
Largest Number: 9
Row: 0
Column: 2
Smallest Number: -4
Row=2
Column=3
Rearranged matrix:
-4 -2 0 1
3 3 4 5
6 7 8 9
Question 20:
Write a Program to sort the array using Exchange Selection Sort.
Solution:
8/9/2019 Java File Best
http://slidepdf.com/reader/full/java-file-best 52/53
class selection sort
{
public static void main (+tring args[])thro#s $%&'ception
{
int i, j ,small, posn,temp;
-or(i!;ia.length();i//)
{
small a[i];
posni;
-or(ji/5;ja.length()45;j//)
{
i-(a[j],small)
{
small a[j];
posn j;
"
"
tempa[i];
a[i]a[posn];
a[posn]temp;
"
+stem.out.println(0?rra in ascending order1);
-or(i!;ia.length;i//)
+stem.out.println(a[i]);
"