Sie sind auf Seite 1von 91

Introduction

Java, world’s most used and the simplest high-level


programming language still known. Developed in
2008, is Turn
still to
the only language competing with C++.
open >
It is used everywhere…from Androids to parking lot,
from scientific computers to restaurant computers,
everywhere. Java is integrated to our daily life, in a
way that we don’t even know we are using it. Java
does almost everything we do on our phones,
computers, ATMs and all the other electronic devices
that we use and that use software. Features like
being robust, platform independent, and access-
specific, easy and above all being an object-oriented
programming language makes it one of the best in
the world

1
QUESTION 1
Create an application to enter a number 𝑛 and
check whether it is a 𝑆𝑚𝑖𝑡ℎ 𝑛𝑢𝑚𝑏𝑒𝑟 or not.
Smith number: A number is said to be Smith number
if
𝑺𝒖𝒎 𝒐𝒇 𝒅𝒊𝒈𝒊𝒕𝒔 𝒐𝒇 𝑷𝑹𝑰𝑴𝑬 𝑭𝑨𝑪𝑻𝑶𝑹𝑺 𝒊𝒔 𝑬𝑸𝑼𝑨𝑳 𝒕𝒐 𝑺𝒖𝒎 𝒐𝒇 𝒕
𝒅𝒊𝒈𝒊𝒕𝒔 𝒐𝒇 𝒕𝒉𝒆 𝑵𝑼𝑴𝑩𝑬𝑹 𝑰𝑻𝑺𝑬𝑳𝑭.

2
Algorithm
Step 1. START
Step 2. n is taken as INPUT and i is INITIALISED
with 2; c, sp, sd and copy2 are with 0
Step 3. copy is INITIALISED with n
Step 4. j is INITIALISED with 1
Step 5. IF i MOD j EQUALS 0 THEN c is
INCREMENTED by 1
Step 6. j is INCREMENTED by 1
Step 7. IF j is LESSER than or EQUALS i THEN
GOTO Step 5
Step 8. IF c EQUALS 2 THEN GOTO Step 9 ELSE
GOTO Step 16
Step 9. IF copy MOD i EQUALS 0 THEN GOTO
Step 10 ELSE GOTO Step 16
Step 10. copy2 is re-INITIALISED with i
Step 11. IF copy2 NOT equals 0 THEN GOTO
Step 12 ELSE GOTO Step 14
Step 12. sp is INCREMENTED by copy2 MOD 10,
copy2 STORES copy2 DIVIDED 10

3
Step 13. GOTO Step 11
Step 14. copy STORES copy DIVIDED i
Step 15. GOTO Step 9
Step 16. c is re-INITIALISED with 0
Step 17. i is INCREMENTED by 1
Step 18. IF i is LESSER than n THEN GOTO Step
4
Step 19. IF n NOT equals 0 THEN GOTO Step 20
ELSE GOTO Step 22
Step 20. sd is INCREMENTED by n MOD 10, n
STORES n DIVIDED 10
Step 21. GOTO Step 19
Step 22. IF sp EQUALS sd THEN PRINT “Smith
number”
Step 23. END

4
Code
import java.util.*;
public class PROgramSmith_PROject1
{
//Class created to check for Smith number
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n,i,j,c=0,sp=0,sd=0,copy=0,copy2=0;
System.out.print("Enter number to be checked
:\t");
n=in.nextInt();//Input number
copy=n;
for(i=2;i<n;i++)
{
for(j=1;j<=i;j++)
if(i%j==0)
c++;
if(c==2)
{

5
while(copy%i==0)//Check for repeating
prime factors
{
copy2=i;
while(copy2!=0)
{
sp+=(copy2%10);//Sum of digits of
prime factors
copy2/=10;
}
copy/=i;
}
}
c=0;
}
while(n!=0)
{
sd+=(n%10);//Sum of digits
n/=10;
}
if(sp==sd)
System.out.println("This is a SMITH number");
else

6
System.out.println("This is NOT a SMITH
NUMBER");
}
}

7
Variable listing
Variable Data type Description
n int Input variable.
Inputs the
number to be
checked for.
i int Loop variable.
j int Loop variable.
c int Counter variable.
Counts the
number of factors.
sp int Storage variable.
Stores the sum of
prime factors.
sd int Storage variable.
Stores the sum of
digits.
copy int Storage variable.
Stores a copy of
the number.
copy 2 int Storage variable.
Stores a copy of i.

8
Output

9
Question 2
Create an application to input two numbers
𝑚 and 𝑛 and find out the 𝑼𝒏𝒊𝒒𝒖𝒆 𝑫𝒊𝒈𝒊𝒕 𝒏𝒖𝒎𝒃𝒆𝒓𝒔
in the range 𝑚 to 𝑛 along with the number of
numbers obtained.
Unique Digit number: A number is said to be
Unique Digit number if
𝑨 𝑷𝑶𝑺𝑰𝑻𝑰𝑽𝑬 𝒊𝒏𝒕𝒆𝒈𝒆𝒓 𝑾𝑰𝑻𝑯𝑶𝑼𝑻 𝒍𝒆𝒂𝒅𝒊𝒏𝒈 𝒁𝑬𝑹𝑶𝑬𝑺 𝒂𝒏
𝑫𝑼𝑷𝑳𝑰𝑪𝑨𝑻𝑬 𝑫𝑰𝑮𝑰𝑻.

10
Algorithm
Step 1. START
Step 2. m and n are taken as input;copy1,
copy2,count and countn are INITIALISED with 0
Step 3. IF m is LESSER than n AND m is GREATER
than 0 AND n is GREATER than 0 THEN GOTO
Step 4 ELSE GOTO Step 20
Step 4. i is INITIALISED with m
Step 5. copy1 is INITIALISED with i
Step 6. IF copy1 NOT equals 0 THEN GOTO Step 7
ELSE GOTO Step 16
Step 7. count is re-INITIALISED with 0 and copy2
with i
Step 8. IF copy2 NOT equals 0 THEN GOTO Step 9
ELSE GOTO Step 13
Step 9. IF copy2 MOD 10 EQUALS copy1 MOD 10
THEN count is INCREMENTED by 1
Step 10. IF count is GREATER than or EQUALS 2
THEN GOTO Step 13
Step 11. copy2 stores copy2 DIVIDED 10

11
Step 12. GOTO Step 8
Step 13. IF count is GREATER than or EQUALS 2
THEN GOTO Step 16
Step 14. copy1 stores copy1 DIVIDED 10
Step 15. GOTO Step 6
Step 16. IF count EQUALS 1 THEN PRINT i and
countn is INCREMENTED by 1
Step 17. i is INCREMENTED by 1
Step 18. IF i is LESSER than or EQUALS n THEN
GOTO Step 5
Step 19. PRINT countn
Step 20. END

12
Code
import java.util.*;
public class PROgramUnique_PROject2
{
//Class created to find UNIQUE DIGIT NUMBERS
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int m,n,i,copy1=0,copy2=0,count=0,countn=0;
System.out.print("Enter lower limit:\t");
m=in.nextInt();
System.out.print("Enter upper limit:\t");
n=in.nextInt();
System.out.println("The Unique Digit numbers
between "+m+" and "+n+" are:");
if(m<n && m>0 &&n>0)
{
for(i=m;i<=n;i++)
{
copy1=i;
while(copy1!=0)
{
count=0;

13
copy2=i;
while(copy2!=0)
{
if((copy2%10)==(copy1%10))
count++;
if(count>=2)
break;
copy2/=10;
}
if(count>=2)
break;
copy1/=10;
}
if(count==1)
{
System.out.println(i);
countn++;
}
}
System.out.println("There are "+countn+"
numbers");
}
else

14
System.out.println("TECHNICAL ERROR
DETECTED");
}}

15
Variable listing
Variable Datatype Description
m int Input variable.
Inputs lower limit.

n int Input variable.


Inputs upper limit.

i int Loop variable


copy1 int Storage variable.
Stores a copy of i.

copy2 int Storage variable.


Stores a copy of i.

count int Counter variable.


Counts number of
digits alike.
countn int Counter variable.
Counts number of
Unique Digit
Numbers.

16
Output

17
Question 3
Create an application to declare a square matrix of
order 𝑚 × 𝑚 | 𝑚 > 2 and 𝑚 < 10. Display the
original matrix and
1. Rotate the matrix 90° 𝐶𝐿𝑂𝐶𝐾𝑊𝐼𝑆𝐸.
2. Sum of the 4 corners of the matrix.

18
Algorithm
Step 1. START
Step 2. m is taken as INPUT
Step 3. IF m is GREATER than 2 AND m is LESSER
than 10 THEN GOTO Step 4 ELSE GOTO Step 30
Step 4. matrix[][] is DECLARED as int[m][m]
and rmatrix[][] also as int[m][m]
Step 5. i is INITIALISED with 0
Step 6. j is INITIALISED with 0
Step 7. matrix[i][j] is taken as INPUT
Step 8. j is INCREMENTED by 1
Step 9. IF j is LESSER than m THEN GOTO Step 7
Step 10. i is INCREMENTED by 1
Step 11. IF i is LESSER than m THEN GOTO Step
6
Step 12. i is INITIALISED with 0
Step 13. c is re-INITIALISED with 0
Step 14. j is INITIALISED with m-1
Step 15. rmatrix[i][c] STORES matrix[j][i] and
c is INCREMENTED by 1

19
Step 16. j is DECREMENTED by 1
Step 17. IF j is GREATER than or EQUALS 0
THEN GOTO Step 15
Step 18. i is INCREMENTED by 1
Step 19. IF i is LESSER than m THEN GOTO Step
13
Step 20. c STORES matrix[0][0]+matrix[0][(m-
1)]+matrix[(m-1)][0]+matrix[(m-1)][(m-1)]
Step 21. i is INITIALISED with 0
Step 22. j is INITIALISED with 0
Step 23. PRINT rmatrix[i][j]
Step 24. j is INCREMENTED by 1
Step 25. IF j is LESSER than m THEN GOTO Step
23
Step 26. PRINT new line
Step 27. i is INCREMENTED by 1
Step 28. IF i is LESSER than m THEN GOTO Step
22
Step 29. PRINT c
Step 30. END

20
Code
import java.util.*;
public class PROgramMatrix_PROject3
{
//Class created to perform MATRIX operations
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int m,i,j,c;
System.out.print("Enter size of the matrix:\t");
m=in.nextInt();//Size of the array
if(m>2 && m<10)
{
int matrix[][]=new int[m][m];
int rmatrix[][]=new int[m][m];
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print("Enter data at("+(i+1)+"
, "+(j+1)+"):\t");
matrix[i][j]=in.nextInt();//Array input
}

21
}
for(i=0;i<m;i++)
{
c=0;
for(j=m-1;j>=0;j--)
{
rmatrix[i][c]=matrix[j][i];//Array
rotated
c++;
}
}
c=matrix[0][0]+matrix[0][(m-
1)]+matrix[(m-1)][0]+matrix[(m-1)][(m-1)];//Sum of
corner elements
System.out.println("Original matrix:");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(matrix[i][j]+" ");
if(i==m/2)
System.out.print("<---90\u00B0 clockwise
rotated---> ");
else
System.out.print(" ");

22
for(j=0;j<m;j++)
System.out.print(rmatrix[i][j]+" ");
System.out.println();
}
System.out.print("Sum of the corner
elements:\t");
System.out.println(c);
}
else
System.out.println("WRONG PARAMETERS
ENTERED!");
}
}

23
Variable listing
Variable Datatype Description
m int Input variable.
Inputs size of the
matrix.
i int Loop variable.
j int Loop variable.
c int Counter as well
as storage
variable.
matrix[][] int Matrix of size m.
Acts as input
variable. This is
the array to be
rotated.
rmatrix[][] int Matrix of size m.
Acts as storage
variable. Stores
rotated
matrix[][].

24
output

25
question 4
Create an application to accept the size of a String
array 𝒏 | 𝟏 < 𝒏 < 𝟏𝟎. Display an appropriate
message for invalid input. Fill in the array with
sentences row-wise. Change the sentences of the odd
numbered rows with
𝒂𝒏 𝒆𝒏𝒄𝒓𝒚𝒑𝒕𝒊𝒐𝒏 𝒐𝒇 𝟐 𝒄𝒉𝒂𝒓𝒂𝒄𝒕𝒆𝒓𝒔 𝒂𝒉𝒆𝒂𝒅. Spaces will
remain unchanged. 𝑅𝑒𝑣𝑒𝑟𝑠𝑒 each word of the
sentences for even numbered rows.

26
Algorithm
Step 1. START
Step 2. n is taken as INPUT and s and rev is
INITIALISED with “”
Step 3. IF n is GREATER than 1 AND n is LESSER
than 10 THEN GOTO Step 4 ELSE GOTO Step 38
Step 4. arr[ ][ ] is DECLARED as String[n+1][1]
Step 5. i is INITIALISED with 0
Step 6. arr[i][0] is taken as INPUT
Step 7. i is INCREMENTED by 1
Step 8. IF i is LESSER than n+1 THEN GOTO Step 6
Step 9. i is INITIALISED with 1
Step 10. IF i MOD 2 not EQUALS 0 THEN GOTO
Step 11 ELSE GOTO Step 18
Step 11. j is INITIALISED with 0
Step 12. c STORES (arr[i][0]).charAt(j)
Step 13. IF c is NOT equals ‘ ‘ THEN c STORES
(char)((int)c+2) and c is CONCATENATED with s
ELSE c is CONCATENATED with s
Step 14. j is INCREMENTED by 1

27
Step 15. IF j is LESSER than length of arr[i][0]
THEN GOTO Step 11
Step 16. arr[i][0] is re-INITIALISED with s
Step 17. s is re-INITIALISED with “”; GOTO Step
32
Step 18. arr[i][0] STORES arr[i][0]
CONCATENATED with “ ”
Step 19. j is INITIALISED with 0
Step 20. c STORES (arr[i][0]).charAt(j)
Step 21. IF c EQUALS ‘ ‘ THEN GOTO Step 22
ELSE s is CONCATENATED with c and GOTO
Step 28
Step 22. k is INITIALISED with 0
Step 23. rev STORES s.charAt(k)
CONCATENATED with rev
Step 24. k is INCREMENTED by 1
Step 25. IF k is LESSER than length of s THEN
GOTO Step 23
Step 26. rev STORES “ “ CONCATENATED with
rev
Step 27. s is re-INITIALISED with “”

28
Step 28. j is INCREMENTED by 1
Step 29. IF j is LESSER than length of arr[i][0]
THEN GOTO Step 20
Step 30. arr[i][0] is re-INITIALISED with
rev.trim()
Step 31. rev is re-INITIALISED with “”
Step 32. i is INCREMENTED by 1
Step 33. IF i is LESSER than n+1 THEN GOTO
Step 10
Step 34. i is INITIALISED with 0
Step 35. PRINT arr[i][0] in new line
Step 36. i is INCREMENTED by 1
Step 37. IF i is LESSER than n+1 THEN GOTO
Step 34
Step 38. END

29
Code
import java.util.*;
public class PROgramEncode_PROject4
{
//Class created to ENCODE sets of sentences
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n,i,j,k;
char c;
String s="",rev="";
System.out.print("Enter number of
sentences:\t");
n=in.nextInt();//Size input
if(n>1 && n<10)
{
String arr[][]=new String[n+1][1];
for(i=0;i<(n+1);i++)
{
if(i!=0)
System.out.print("Enter #"+i+"
sentence:\t");
arr[i][0]=in.nextLine();//Array input

30
}
for(i=1;i<(n+1);i++)
{
if((i%2)!=0)
{
for(j=0;j<(arr[i][0]).length();j++)
{
c=(arr[i][0]).charAt(j);
if(c!=' ')
{
c=(char)((int)c+2);//Encode process
s=s+c;
}
else
s=s+c;
}
arr[i][0]=s;
s="";
}
else
{
arr[i][0]=arr[i][0]+" ";
for(j=0;j<(arr[i][0]).length();j++)
{

31
c=(arr[i][0]).charAt(j);
if(c==' ')
{
//Word reverse
for(k=0;k<s.length();k++)
rev=s.charAt(k)+rev;
rev=" "+rev;
s="";
}
else
s=s+c;
}
arr[i][0]=rev.trim();
rev="";
}
}
System.out.println("Encoded array:");
for(i=0;i<(n+1);i++)
System.out.println(arr[i][0]);
}
else
System.out.println("PARAMETERS ERROR!");
}
}

32
Variable listing
Variable Datatype Description
n int Input variable.
Inputs size of the
array.
i int Loop variable.
j int Loop variable.
k int Loop variable.
c char Storage variable.
Stores the each
character of the
sentence
temporarily.
s String Storage variable.
Stores newly
formed sentence
temporarily.
rev String Storage variable.
Stores the reverse
of sentence.
arr[][] String String array to
store the sentences.

33
output

34
question 5
Create an application to enter a number 𝒏 and find
the value of Mobius function by the given value.
Mobius function 𝑴(𝒏) is defined as
𝑀(𝑛)
1, 𝒏=𝟏
0, any prime factors of 𝑛
={
is contained in 𝑛 more than once
−1𝑝 , 𝑛 is a product of 𝑝 distinct prime factors
eg.,
𝑴(𝟕𝟖) = −𝟏
𝟕𝟖 = 𝟐 × 𝟑 × 𝟏𝟑
𝑴(𝟏𝟐) = 𝟎
𝟏𝟐 = 𝟐 × 𝟐 × 𝟑
𝑴(𝟏) = 𝟏

35
Algorithm
Step 1. START
Step 2. n is taken INPUT; result, count and p is
INITIALISED with 0 and prod with 1
Step 3. IF n is GREATER than 0 THEN GOTO Step
4 ELSE GOTO Step 19
Step 4. IF n EQUALS 1 THEN result is re-
INITIALISED with 1 and GOTO Step 15 ELSE GOTO
Step 5
Step 5. i is INITIALISED with 1
Step 6. IF n MOD i EQUALS 0 THEN GOTO Step 7
ELSE GOTO Step 14
Step 7. j is INITIALISED with 1
Step 8. IF i MOD j EQUALS 0 THEN count is
INCREMENTED by 1
Step 9. j is INCREMENTED by 1
Step 10. IF j is LESSER than or EQUALS i THEN
GOTO Step 8
Step 11. IF count EQUALS 2 THEN GOTO Step 12
ELSE GOTO Step 14

36
Step 12. prod STORES prod MULTIPLIED i
Step 13. p is INCREMENTED by 1
Step 14. count is re-INITIALISED with 0
Step 15. i is INCREMENTED by 1
Step 16. IF i is LESSER than n THEN GOTO Step
6
Step 17. IF prod EQUALS n THEN result is re-
INITIALISED with (int)Math.pow((-1),p) ELSE
result is re-INITIALISED with 0
Step 18. PRINT result
Step 19. END

37
Code
import java.util.*;
public class PROgramMobius_PROject5
{
//Class created to find the result of MOBIUS
FUNCTION
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n,result=0,i,j,count=0,prod=1,p=0;
System.out.print("Enter number:\t");
n=in.nextInt();
if(n>0)
{
if(n==1)//Check for 1 as input
result=1;
else
{
for(i=1;i<=n;i++)
{
if(n%i==0)
{
for(j=1;j<=i;j++)

38
if(i%j==0)
count++;
if(count==2)//Prime factors check
{
prod*=i;//Prime factors multiplied
p++;//Count for prime factors
}
}
count=0;
}
if(prod==n)//Check for duplicate prime
factors
result=(int)Math.pow((-1),p);
else
result=0;
}
System.out.println("Mobius function M("+n+")
= "+result);
}
else
System.out.println("Incorrect parameters");
}
}

39
Variable listing
Variable Datatype Description
n int Input variable.
Inputs the number.
result int Storage variable.
Stores the result of
the function.
i int Loop variable.
j int Loop variable.
count int Counter variable.
Counts the number
of factors.
prod int Storage variable.
Stores the product
of prime factors.
p int Counter variable.
Counts number of
prime factors.

40
Output

41
Question 6
Create an application to convert an entered VALID
time 𝑖𝑛 𝑤𝑜𝑟𝑑𝑠.

42
Algorithm
Step 1. START
Step 2. h and m are taken as INPUT; time is
INITIALISED with “”. Array hrs[] is STORED with
{“One","Two","Three","Four","Five","Six","Seven",
"Eight","Nine","Ten","Eleven","Twelve"} and
array min[] with
{"One","Two","Three","Four","Five","Six","Seven",
"Eight","Nine","Ten","Eleven","Twelve","Thirteen
","Fourteen","Fifteen","Sixteen","Seventeen","Eig
hteen","Nineteen","Twenty"}
Step 3. IF h is GREATER than or EQUALS 0 AND h
is LESSER than or EQUALS 23 AND m is GREATER
than or EQUALS 0 AND m is LESSER than or
EQUALS 59 THEN GOTO Step 4 ELSE GOTO Step
17
Step 4. IF h EQUALS 0 THEN p STORES 11 ELSE IF
h is GREATER than 12 p STORES 13 ELSE p STORES
1

43
Step 5. IF m is LESSER than or EQUALS 30 THEN
GOTO Step 6 ELSE GOTO Step 7
Step 6. IF h not EQUALS 0 THEN time STORES
hrs[h-p] ELSE time STORES hrs[p] GOTO Step 8
Step 7. IF h is NOT EQUALS 0 THEN time STORES
hrs[h-p+1] ELSE time STORES hrs[0]
Step 8. IF m EQUALS 15 OR m EQUALS 45 THEN
GOTO Step 9 ELSE GOTO Step 10
Step 9. IF m EQUALS 15 THEN time STORES
“Quarter past “ CONCATENATED with time ELSE
time STORES “Quarter to “ CONCATENATED
with time; GOTO Step16
Step 10. IF m EQUALS 30 THEN time STORES
“Half past “ CONCATENATED with time and
GOTO Step ELSE GOTO Step 11
Step 11. IF (m is GREATER than or EQUALS 1
AND m is LESSER than or EQUALS 20) OR (m is
GREATER than or EQUALS 40 AND m is LESSER
than or EQUALS 59) THEN GOTO Step 12 ELSE
GOTO Step 13

44
Step 12. IF m is LESSER than 30 THEN time
STORES min[(m-1)] and “ minutes past “
CONCATENATED with time ELSE time STORES
min[(59-m)] and “ minutes to “
CONCATENATED with time; GOTO Step 16
Step 13. IF (m is GREATER than or EQUALS 21
AND m is LESSER than or EQUALS 29) OR (m is
GREATER than or EQUALS 31 AND m is LESSER
than or EQUALS 39) THEN GOTO Step 14 ELSE
GOTO Step 15
Step 14. IF m is LESSER than 30 THEN time
STORES min[19] and min[(m MOD 10)-1] and “
minutes past “ CONCATENATED with time ELSE
time STORES min[19] and min[(m MOD 10)-1]
and “ minutes to “ CONCATENATED with time;
GOTO Step 16
Step 15. time is CONCATENATED with “ o’
clock”
Step 16. PRINT time
Step 17. END

45
Code
import java.util.*;
public class PROgramTime_PROject6
{
//Class created to convert time to WORDS
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String
hrs[]={"One","Two","Three","Four","Five","Six","Seve
n","Eight","Nine","Ten","Eleven","Twelve"};
String
min[]={"One","Two","Three","Four","Five","Six","Sev
en","Eight","Nine","Ten","Eleven","Twelve","Thirteen
","Fourteen","Fifteen","Sixteen","Seventeen","Eighte
en","Nineteen","Twenty"};
int h,m,p;
String time="";
System.out.print("Enter hours(24h format): ");
h=in.nextInt();//Hour input
System.out.print("Enter minutes: ");
m=in.nextInt();//Minutes input
if(h>=0 && h<=23 && m>=0 && m<=59)

46
{
//Hours check
if(h==0)
p=11;
else if(h>12)
p=13;
else
p=1;
if(m<=30)
{
if(h!=0)
time=hrs[h-p];
else
time=hrs[p];
}
else
{
if(h!=0)
time=hrs[h-p+1];
else
time=hrs[0];
}
//Minutes check
if(m==15 || m==45)

47
{
if(m==15)
time="Quarter past "+time;
else
time="Quarter to "+time;
}
else if(m==30)
time="Half past "+time;
else if((m>=1 && m<=20)||(m>=40 && m<=59))
{
if(m<30)
time=min[(m-1)]+" minutes past "+time;
else
time=min[(59-m)]+" minutes to "+time;
}
else if((m>=21 && m<=29)||(m>=31 && m<=39))
{
if(m<30)
time=min[19]+" "+min[(m%10)-1]+" minutes
past "+time;
else
time=min[19]+" "+min[(m%10)-1]+" minutes
to "+time;
}

48
else
time=time+" o' clock";
System.out.println(h+"."+m+" ---In Words--->
"+time);
}
else
System.out.println("NOT A TIME FIGURAL!");
}
}

49
Variable listing
Variable Datatype Description
hrs[] String String array stores
possible words for
hours.
min[] String String array stores
possible words for
minutes.
h int Input variable.
Inputs hours.
m int Input variable.
Inputs minutes.
p int Counter variable.
Counts the suitable
index number.
time String Storage variable.
Stores the time in
words.

50
Output

51
Question 7
Create an application to 𝑎𝑑𝑑 to input times by
𝒐𝒃𝒋𝒆𝒄𝒕 𝒑𝒂𝒔𝒔𝒊𝒏𝒈.
Class name: Time
Data members: 𝒕[ ] (: Array to store time)
Member functions: 𝑻𝒊𝒎𝒆( ) (: Non parameterized
constructor to store default values of array as 0)
𝑻𝒊𝒎𝒆(𝒊𝒏𝒕 𝒉𝒉, 𝒊𝒏𝒕 𝒎𝒎) (: Parameterized constructor
to store hh as hours and mm as minutes)
𝑻𝒊𝒎𝒆 𝒔𝒖𝒎𝑻𝒊𝒎𝒆(𝑻𝒊𝒎𝒆 𝑨) (: Function to find the
sum of the times stored as current object and formal
object)
𝒗𝒐𝒊𝒅 𝒅𝒊𝒔𝒑𝒍𝒂𝒚( ) (: Displays all the ‘times’)
𝒎𝒂𝒊𝒏( ) function is needed to be written with
proper object passing.

52
Algorithm
Step 1. START
Step 2. Array A[] is declared as local variable
int[2]
Constructor Adder() INITIALISES both elements with
0
Function void readTime():
Step 1. i is INITIALISED with 0
Step 2. A[i] is taken as INPUT
Step 3. i is INCREMENTED by 1
Step 4. IF i is LESSER than 2 THEN GOTO Step 2
Function void addTime(Adder x, Adder y):
Step 1. A[0] corresponding to current calling
object STORES the SUM of A[0] corresponding to
objects x and y
Step 2. IF SUM of A[1] corresponding to object x
and y is GREATER than or EQUALS 60 THEN
GOTO Step 3 ELSE GOTO Step 5

53
Step 3. A[1] corresponding to current calling
object STORES the SUM of A[1] corresponding to
objects x and y SUBTRACTED by 60
Step 4. A[0] corresponding to current calling
object is INCREMENTED by 1
Step 5. A[1] corresponding to current calling
object STORES the SUM of A[1] corresponding to
objects x and y
Function void dispTime():
Step 1. PRINT A[0] and A[1] of current calling
object
main() function:
Step 1. Three objects m, n and s are CREATED as
new Adder()
Step 2. m and n CALLS Function void readTime()
Step 3. s CALLS Function void addTime(m, n)
Step 4. m, n and s CALLS Function void
dispTime()

54
Code
import java.util.*;
public class Adder
{
//Class created to perform SUMMATION on time
figurals
int A[]=new int[2];
//Constructor initialises the array to 0
Adder()
{
A[0]=0;
A[1]=0;
}
void readTime()
{
Scanner in=new Scanner(System.in);
int i;
System.out.println("Enter time:");
for(i=0;i<2;i++)
A[i]=in.nextInt();
}
void addTime(Adder x,Adder y)
{

55
this.A[0]=x.A[0]+y.A[0];//Adds hours
//Adds seconds
if((x.A[1]+y.A[1])>=60)
{
this.A[1]=x.A[1]+y.A[1]-60;
this.A[0]=this.A[0]+1;
}
else
this.A[1]=x.A[1]+y.A[1];
}
void dispTime()
{
int i;
System.out.println(this.A[0]+"\u00B0
"+this.A[1]+"'");
}
public static void main(String args[])
{
//Objects created
Adder m=new Adder();
Adder n=new Adder();
Adder s=new Adder();
m.readTime();
n.readTime();

56
s.addTime(m,n);
System.out.print("Time A: ");
m.dispTime();
System.out.print("Time B: ");
n.dispTime();
System.out.print("----------------------\nTime
Sum: ");
s.dispTime();
}
}

57
Variable listing
Variable Datatype Description
A[] int Integer array stores
time.
x Adder Formal parameter
for passed object.
y Adder Formal parameter
for passed object.
i int Loop variable.
m Adder Actual parameter.
Inputs #1 time.
n Adder Actual parameter.
Inputs #2 time.
s Adder Calling object. Calls
addTime().

58
Output

59
Question 8
Create an application to 𝑑𝑒𝑐𝑜𝑑𝑒 encrypted message.
The process of encryption is as:
𝑬𝒂𝒄𝒉 𝒄𝒉𝒂𝒓𝒂𝒄𝒕𝒆𝒓(𝒄𝒐𝒎𝒑𝒓𝒊𝒔𝒊𝒏𝒈 𝒐𝒏𝒍𝒚 𝒍𝒆𝒕𝒕𝒆𝒓𝒔 𝒂𝒏𝒅 𝒘𝒉𝒊𝒕𝒆
𝒊𝒔 𝒄𝒐𝒏𝒗𝒆𝒓𝒕𝒆𝒅 𝒕𝒐 𝒊𝒕𝒔 𝑨𝑺𝑪𝑰𝑰 𝒆𝒒𝒖𝒊𝒗𝒂𝒍𝒆𝒏𝒕. 𝑻𝒉𝒆𝒚 𝒂𝒓𝒆 𝒄𝒐𝒏𝒄
𝒂𝒏𝒅 𝒕𝒉𝒆𝒏 𝒓𝒆𝒗𝒆𝒓𝒔𝒆𝒅.

60
Algorithm
Step 1. START
Step 2. code is taken as INPUT; s and msg are
INITIALISED with “”
Step 3. i is INITIALISED with (length of code)-1
Step 4. s is CONCATENATED with code.charAt(i)
and p STORES s CONVERTED to integer
Step 5. IF p EQUALS 32 OR (p is GREATER than or
EQUALS 65 AND p is LESSER than or EQUALS 91)
OR (p is GREATER than or EQUALS 97 AND p is
LESSER than or EQUALS 122) THEN msg is
CONCATENATED with p CONVERTED to
character and s is re-INITIALISED with ””
Step 6. i is DECREMENTED by 1
Step 7. IF i is GREATER than or EQUALS 0 THEN
GOTO Step 4
Step 8. PRINT msg
Step 9. END

61
Code
import java.util.*;
public class PROgramDecode_PROject8
{
//Class created to DECODE a secret message
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String code,s="",msg="";
int i,p;
System.out.print("Enter code:\t");
code=in.nextLine();
for(i=code.length()-1;i>=0;i--)
{
s=s+code.charAt(i);//Creating sets of digits
p=Integer.valueOf(s);
if(p==32||(p>=65 && p<=91)||(p>=97 &&
p<=122))//Checking if it is valid set
{
msg+=(char)p;
s="";
}
}

62
System.out.println("Decoded message:\t"+msg);
}}

63
Variable listing
Variable Datatype Description
code String Input variable.
Inputs code to be
decoded.
s String Storage variable.
Stores sets of digits.
msg String Storage variable.
Stores the decoded
message.
i int Input variable.
Inputs hour for the
first array.
p int Storage variable.
Stores the converted
string in integer.

64
Output

65
Question 9
Create an application to convert all words in a
sentence into 𝑝𝑎𝑙𝑖𝑛𝑑𝑟𝑜𝑚𝑒𝑠. The rule for making
palindromes is as:
𝑻𝒉𝒆 𝒘𝒐𝒓𝒅 𝒊𝒔 𝒄𝒐𝒏𝒄𝒂𝒕𝒆𝒏𝒂𝒕𝒆𝒅 𝒘𝒊𝒕𝒉 𝒕𝒉𝒆 𝒓𝒆𝒗𝒆𝒓𝒔𝒆 𝒐𝒇 𝒊𝒕𝒔𝒆𝒍
𝒇𝒐𝒓 𝒕𝒉𝒆 𝒍𝒂𝒔𝒕 𝒍𝒆𝒕𝒕𝒆𝒓.
𝒆𝒈. : 𝑨𝑩 𝒄𝒉𝒂𝒏𝒈𝒆𝒔 𝒕𝒐 𝑨𝑩𝑨.
𝑯𝒐𝒘𝒆𝒗𝒆𝒓 𝒊𝒇 𝒕𝒉𝒆𝒓𝒆 𝒂𝒓𝒆 𝒓𝒆𝒑𝒆𝒂𝒕𝒊𝒏𝒈 𝒍𝒂𝒔𝒕 𝒍𝒆𝒕𝒕𝒆𝒓𝒔, 𝒕𝒉𝒆 𝒘𝒐
𝒄𝒐𝒏𝒄𝒂𝒕𝒆𝒏𝒂𝒕𝒆𝒅 𝒘𝒊𝒕𝒉 𝒕𝒉𝒆 𝒓𝒆𝒗𝒆𝒓𝒔𝒆 𝒐𝒇 𝒕𝒉𝒆 𝒑𝒂𝒓𝒕 𝒐𝒇 𝒕𝒉𝒆 𝒘
𝒑𝒓𝒆𝒄𝒆𝒅𝒊𝒏𝒈 𝒕𝒉𝒆 𝒍𝒆𝒕𝒕𝒆𝒓𝒔.
𝒆𝒈. : 𝑨𝑩𝑩 𝒄𝒉𝒂𝒏𝒈𝒆𝒔 𝒕𝒐 𝑨𝑩𝑩𝑨 𝒂𝒏𝒅 𝒏𝒐𝒕 𝑨𝑩𝑩𝑩𝑨

66
Algorithm
Step 1. START
Step 2. s is taken as INPUT; n and w are
INITIALISED with “” and flag with false
Step 3. s is CONCATENATED with “ “
Step 4. i is INITIALISED with 0
Step 5. IF s.charAt(i) EQUALS ‘ ‘ THEN GOTO Step
6 ELSE GOTO Step 18
Step 6. j is INITIALISED with (length of w)-1
Step 7. IF w.charAt(j) EQUALS w.charAt(j-1)
THEN flag is re-INITIALISED with false ELSE flag
is re-INITIALISED with true
Step 8. IF flag EQUALS true THEN GOTO Step 11
Step 9. j is DECREMENTED by 1
Step 10. IF j is GREATER than or EQUALS 1
THEN GOTO Step 7
Step 11. n is INCREMENTED by w
Step 12. k is INITIALISED with j-1
Step 13. n is CONCATENATED with
w.charAt(k)

67
Step 14. k is DECREMENTED by 1
Step 15. IF k is GREATER than or EQUALS 0
THEN GOTO Step 13
Step 16. flag is re-INITIALISED with false and w
with “”
Step 17. n is CONCATENATED with ” “;GOTO
Step 19
Step 18. w is CONCATENATED s.charAt(i)
Step 19. i is INCREMENTED by 1
Step 20. IF i is LESSER than length of s THEN
GOTO Step 5
Step 21. PRINT n
Step 22. END

68
Code
import java.util.*;
public class PROgramPalindrome_PROject9
{
//Class created to to construct a PALINDROMIC
sentence
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String s,n="",w="";
boolean flag=false;
int i,j,k;
System.out.print("Enter sentence:\t");
s=in.nextLine();
s=s+" ";
for(i=0;i<s.length();i++)
{
if(s.charAt(i)==' ')//Word break
{
//Check for repeating last letters
for(j=w.length()-1;j>=1;j--)
{
if(w.charAt(j)==w.charAt(j-1))

69
flag=false;
else
flag=true;
if(flag==true)
break;
}
//Palindrome construction
n=n+w;
for(k=j-1;k>=0;k--)
n=n+w.charAt(k);
flag=false;
n=n+" ";
w="";
}
else
w=w+s.charAt(i);//Word construction
}
System.out.println("Constructed palindromic
sentence:\n"+n);
}
}

70
Variable listing
Variable Datatype Description
s String Input variable.
Inputs sentence to
be converted.
n String Storage variable.
Stores palindromic
sentence.
w String Storage variable.
Stores constructed
normal word.
flag boolean Decisive variable.
Decides whether j is
to be terminated.
i int Loop variable.
j int Loop variable.
k int Loop variable.

71
Output

72
Question 10
Create an application to find the
𝑯𝒂𝒎𝒎𝒊𝒏𝒈 𝒅𝒊𝒔𝒕𝒂𝒏𝒄𝒆 between two numbers.
𝑯𝒂𝒎𝒎𝒊𝒏𝒈 𝒅𝒊𝒔𝒕𝒂𝒏𝒄𝒆 𝒊𝒔 𝒕𝒉𝒆 𝒏𝒖𝒎𝒃𝒆𝒓 𝒐𝒇 𝒅𝒊𝒈𝒊𝒕𝒔 𝒂𝒍𝒊𝒌𝒆 𝒇
𝒕𝒉𝒆 𝒍𝒆𝒇𝒕 𝒉𝒂𝒏𝒅 𝒔𝒊𝒅𝒆𝒔 𝒐𝒇 𝒕𝒉𝒆 𝒃𝒊𝒏𝒂𝒓𝒚 𝒆𝒒𝒖𝒊𝒗𝒂𝒍𝒆𝒏𝒕𝒔 𝒐𝒇 𝒕𝒘
𝒏𝒖𝒎𝒃𝒆𝒓𝒔.
𝒆𝒈. : 𝑩𝒊𝒏𝒂𝒓𝒚 𝒆𝒒𝒖𝒊𝒗𝒂𝒍𝒆𝒏𝒕 𝒐𝒇 𝟗 ∶ 𝟏𝟎𝟎𝟏
𝑩𝒊𝒏𝒂𝒓𝒚 𝒆𝒒𝒖𝒊𝒗𝒂𝒍𝒆𝒏𝒕 𝒐𝒇 𝟓 ∶ 𝟎𝟏𝟎𝟏
𝑯𝒂𝒎𝒎𝒊𝒏𝒈 𝒅𝒊𝒔𝒕𝒂𝒏𝒄𝒆 𝒃𝒆𝒕𝒘𝒆𝒆𝒏 𝟓 𝒂𝒏𝒅 𝟗 𝒊𝒔 𝟐.

73
Algorithm
Step 1. START
Function int bin(int a):
Step 1. binary and c are INITIALISED with 0
Step 2. IF a is NOT equals 0 THEN GOTO Step 3
ELSE GOTO Step 5
Step 3. IF a MOD 2 EQUALS 1 THEN binary is
INCREMENTED by 10 raised to the POWER c and
c is INCREMENTED ELSE c is INCREMENTED by 1
Step 4. a STORES a DIVIDED 2;GOTO Step 2
Step 5. RETURN binary to main()
main() function:
Step 1. m and n are taken as INPUT; c is
INITIALISED with 0
Step 2. Object ob is CREATED as
PROgramHamming_PROject10()
Step 3. bm STORES the return value from bin() as
passed parameter m and bn STORES the return
value from bin() as passed parameter n

74
Step 4. IF MAX(bm,bn) is NOT equals 0 THEN
GOTO Step 5 ELSE GOTO Step 7
Step 5. IF bm MOD 10 EQUALS bn MOD 10 THEN
c is INCREMENTED by 1 ELSE GOTO Step 7
Step 6. bm STORES bm DIVIDED 10 and bn
STORES bn DIVIDED 10; GOTO Step 4
Step 7. PRINT c
Step 8. END

75
Code
import java.util.*;
public class PROgramHamming_PROject10
{
//Class created to find the HAMMING distance
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int m,n,bm,bn,c=0;
System.out.print("Enter first number:\t");
m=in.nextInt();
System.out.print("Enter second number:\t");
n=in.nextInt();
PROgramHamming_PROject10 ob=new
PROgramHamming_PROject10();
//Calling function to convert to binary
bm=ob.bin(m);
bn=ob.bin(n);
System.out.println("Bin("+m+"): "+bm);
System.out.println("Bin("+n+"): "+bn);
while(Math.max(bm,bn)!=0)
{
//Checking for alike

76
if((bm%10)==(bn%10))
c++;
else
break;
bm/=10;
bn/=10;
}
System.out.println("-------------------------
\n"+"Hamming distance: "+c);
}
int bin(int a)
{
int binary=0,c=0;
//Convert to binary
while(a!=0)
{
if(a%2==1)
{
binary=(int)Math.pow(10,c)+binary;
c++;
}
else
c++;
a/=2;

77
}
return binary;
}
}

78
Variable listing
Variable Datatype Description
m int Input variable.
Inputs first number.
n int Input variable.
Inputs second
number.
bm int Storage variable.
Stores binary form
of m.
bn int Storage variable.
Stores binary form
of n.
c int Counter variable.
Counts the number
of digits alike from
l.h.s.
a int Formal parameter.
binary int Storage variable.
Stores the binary
form of a and
returns it to main()

79
function.
c int Counter variable in
bin() function.
Counts nmber of
digits.

80
Output

81
Question 11
Create an application to construct the
𝒓𝒊𝒈𝒉𝒕 𝒎𝒊𝒓𝒓𝒐𝒓 𝒎𝒂𝒕𝒓𝒊𝒙 of the input square matrix.
Size of the matrix to be input by the user.
eg.: Input size is 3
1 23
Input matrix is [4 56]
7 89
3 2 1
Right mirror matrix is [6 5 4]
9 8 7

82
Algorithm
Step 1. START
Step 2. m is taken as INPUT
Step 3. a[][] matrix is DECLARED as int[m][m]
Step 4. i is INITIALISED with 0
Step 5. j is INITIALISED with 0
Step 6. a[i][j] is taken as INPUT
Step 7. j is INCREMENTED by 1
Step 8. IF j is LESSER than m THEN GOTO Step 6
Step 9. i is INCREMENTED by 1
Step 10. IF i is LESSER than m THEN GOTO Step
5
Step 11. i is INITIALISED with 0
Step 12. j is INITIALISED with m-1
Step 13. PRINT a[i][j]
Step 14. j is DECREMENTED by 1
Step 15. IF j is GREATER than or EQUALS 0
THEN GOTO Step 13
Step 16. PRINT new line
Step 17. i is INCREMENTED by 1

83
Step 18. IF i is LESSER than m THEN GOTO Step
12
Step 19. END

84
Code
import java.util.*;
public class PROgramMirror_PROject11
{
//Class created to construct the MIRROR matrix
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int m,i,j;
System.out.print("Enter size:\t");
m=in.nextInt();
int a[][]=new int[m][m];
//Array input
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
System.out.print("Enter element at
("+(i+1)+","+(j+1)+"):\t");
a[i][j]=in.nextInt();
}
}

85
System.out.println("Right reflection:");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
System.out.print(a[i][j]+" ");
System.out.print("| ");
for(j=m-1;j>=0;j--)
System.out.print(a[i][j]+" ");
System.out.println();
}
}
}

86
Variable listing
Variable Datatype Description
m int Input variable.
Inputs size of the
array.
i int Loop variable.
j int Loop variable.
a[][] int Integer matrix to be
reflected.

87
Output

88
CONCLUSION
Java might be one of the most used programming
languages today but it still has a lot many flaws.
And this is why this is one of the favorite attacks of
hackers and also viruses. Ranging from Facebook to
Whatsapp, all are prone to hacker attacks. There
come many Android apps which contain malware
that could damage to the phone. Such faulty gets
JAVA at times. JAVA developers are still sorting out
the problem. We could hope a lot more advanced
features as well as security such that JAVA truly
becomes the best programming language.

89
BIBLIOGRAPHY
COMPUTER SCIENCE with JAVA
Google images

90
91

Das könnte Ihnen auch gefallen