Algorithm for the JQ2 Program
Algorithm for main()
1. Start.
2. Create an object obj of class JQ2.
3. Call the input() method on obj.
4. Call the display() method on obj.
5. Call the check() method on obj.
6. Call the sum() method on obj.
7. Call the output() method on obj.
8. End.
Algorithm for JQ2() (Constructor)
1. Start.
2. Initialize integer variable M to 0.
3. Initialize integer variables sumld and sumrd to 0.
4. Initialize boolean variable sym to false.
5. End.
Algorithm for input()
1. Start.
2. Create a Scanner object to accept user input.
3. Display "Enter Value of 'M': " to the user.
4. Accept an integer value into M.
5. If M is less than or equal to 2 OR M is greater than or equal to
10, then:
a. Display "THE MATRIX SIZE IS OUT OF RANGE!".
b. Terminate the program.
6. Declare a 2D integer array A of size MxM.
7. Display "Enter Array Elements:".
8. Initialize integer i to 0.
9. Repeat steps 10 to 13 while i < M.
10.Initialize integer j to 0.
11.Repeat steps 12 while j < M.
12.Accept an integer into A[i][j].
13.Increment j by 1.
14.Increment i by 1.
15.End.
Algorithm for display()
1. Start.
2. Display "Original Matrix: ".
3. Initialize integer i to 0.
4. Repeat steps 5 to 8 while i < M.
5. Initialize integer j to 0.
6. Repeat steps 7 while j < M.
7. Display the value of A[i][j] followed by a space.
8. Increment j by 1.
9. Display a new line.
10.Increment i by 1.
11.End.
Algorithm for check()
1. Start.
2. Declare and initialize integer count to 0.
3. Initialize integer i to 0.
4. Repeat steps 5 to 10 while i < M.
5. Initialize integer j to 0.
6. Repeat steps 7 to 9 while j < M.
7. Store A[i][j] into ele1.
8. Store A[j][i] into ele2.
9. If ele1 is equal to ele2, then increment count by 1.
10.Increment j by 1.
11.Increment i by 1.
12.If count is equal to (M * M), then set sym to true.
13.End.
Algorithm for sum()
1. Start.
2. Initialize integer i to 0.
3. Repeat steps 4 to 9 while i < M.
4. Initialize integer j to 0.
5. Repeat steps 6 to 8 while j < M.
6. If i is equal to j, then add A[i][j] to sumld.
7. If (i + j) is equal to (M - 1), then add A[i][j] to sumrd.
8. Increment j by 1.
9. Increment i by 1.
10.End.
Algorithm for output()
1. Start.
2. If sym is true, then display "THE GIVEN MATRIX IS SYMMETRIC!".
3. If sym is false, then display "THE GIVEN MATRIX IS NOT
SYMMETRIC!".
4. Display "The Sum of the Left Diagonal: " followed by sumld.
5. Display "The Sum of the Right Diagonal: " followed by sumrd.
6. End.
Algorithm for the JQ3 Program
Algorithm for main()
1. Start.
2. Create an object obj of class JQ3.
3. Call the input() method on obj.
4. Call the convert() method on obj.
5. End.
Algorithm for JQ3() (Constructor)
1. Start.
2. Initialize integer variables hr and min to 0.
3. Initialize String variable timeinwords to an empty string "".
4. End.
Algorithm for input()
1. Start.
2. Create a Scanner object to accept user input.
3. Display "Input time in hours: " to the user.
4. Accept an integer value into hr.
5. Display "Input time in minutes: " to the user.
6. Accept an integer value into min.
7. If hr is greater than 12 OR hr is less than 1, then:
a. Display the invalid time in the format "hr:min invalid as per 12
hour format.".
b. Terminate the program.
8. End.
Algorithm for convert()
1. Start.
2. Declare a String array timewords and initialize it with the word
values of minutes of time.
3. If min is equal to 0, then:
a. Set timeinwords to timewords[hr] concatenated with "
o'clock".
b. Display the time in the format "hr:min timeinwords".
4. If min is equal to 15, then:
a. Set timeinwords to timewords[min] concatenated with "
past " and timewords[hr].
b. Display the time in the format "hr:min timeinwords".
5. If min is equal to 30, then:
a. Set timeinwords to timewords[min] concatenated with "
past " and timewords[hr].
b. Display the time in the format "hr:min timeinwords".
6. If min is equal to 45, then:
a. Set timeinwords to timewords[min] concatenated with " to "
and timewords[hr + 1].
b. Display the time in the format "hr:min timeinwords".
7. If min is NOT equal to 0 AND min is NOT equal to 15 AND min is
less than 30, then:
a. Set timeinwords to timewords[min] concatenated with "
minutes past " and timewords[hr].
b. Display the time in the format "hr:min timeinwords".
8. If min is NOT equal to 45 AND min is less than 60 AND min is
greater than 30, then:
a. Set timeinwords to timewords[60 - min] concatenated with "
minutes to " and timewords[hr + 1].
b. Display the time in the format "hr:min timeinwords".
9. End.
Algorithm for the JQ5 Program
Algorithm for main()
1. Start.
2. Create an object obj of class JQ5.
3. Call the accept() method on obj.
4. Call the check() method on obj.
5. End.
Algorithm for JQ5() (Constructor)
1. Start.
2. Initialize String variables s1 and s2 to empty strings "".
3. End.
Algorithm for accept()
1. Start.
2. Create a Scanner object to accept user input.
3. Display "Enter Sentence 1: " to the user.
4. Accept a line of text into s1.
5. Convert s1 to uppercase.
6. Display "Enter Sentence 2: " to the user.
7. Accept a line of text into s2.
8. Convert s2 to uppercase.
9. End.
Algorithm for check()
1. Start.
2. Declare a character array arr1 with a size equal to the length
of s1.
3. Declare a character array arr2 with a size equal to the length
of s2.
4. Initialize integer i to 0.
5. Repeat steps 6 to 7 while i < length of s1.
6. If the character at index i of s1 is not a space, then store this
character into arr1[i].
7. Increment i by 1.
8. Initialize integer i to 0.
9. Repeat steps 10 to 11 while i < length of s2.
10.If the character at index i of s2 is not a space, then store this
character into arr2[i].
11.Increment i by 1.
12.Declare and initialize integer count to 0.
13.Declare and initialize integer charlen to 0.
14.Declare and initialize integer len to 0.
15.If the length of s1 is greater than the length of s2, then set len
to the length of s1.
16.Else (if length of s2 is greater than or equal to length of s1),
then set len to the length of s2.
17.Initialize integer i to 0.
18.Repeat steps 19 to 23 while i < len - 1.
19.Increment charlen by 1.
20. Initialize integer j to 0.
21.Repeat steps 22 while j < len - 1.
22. If arr1[i] is equal to arr2[j], then increment count by 1 and
break from this inner loop (go to step 23).
23. Increment j by 1.
24. Increment i by 1.
25. Display "Original Sentences".
26. Display s1.
27. Display s2.
28. If count is equal to charlen, then display "These Sentences
are Anagrams!".
29. Else (if count is NOT equal to charlen), then display "These
Sentences are not Anagrams!".
30. End.
Algorithm for the NumDude Program
Algorithm for main()
1. Start.
2. Create an object obj of class NumDude.
3. Call the input() method on obj.
4. Call the sumDigits() method on obj.
5. Call the isDude() method on obj.
6. End.
Algorithm for NumDude() (Constructor)
1. Start.
2. Initialize integer variables num and sum to 0.
3. End.
Algorithm for input()
1. Start.
2. Create a Scanner object to accept user input.
3. Display "Enter Number: " to the user.
4. Accept an integer value into num.
5. End.
Algorithm for sumDigits()
1. Start.
2. Declare an integer variable cnum and initialize it with the value
of num.
3. Declare an integer variable dig.
4. Repeat steps 5 to 7 while cnum is NOT equal to 0.
5. Store the remainder of cnum divided by 10 into dig.
6. Add dig to sum.
7. Divide cnum by 10 and store the result back into cnum.
8. End.
Algorithm for isDude()
1. Start.
2. If the cube of sum (i.e., sum raised to the power of 3) is equal
to num, then:
a. Display num concatenated with " is a Dudeney Number!".
3. Else:
a. Display num concatenated with " is not a Dudeney Number!".
4. End.
Algorithm for the JQ7 Program
Algorithm for main()
1. Start.
2. Create an object obj of class JQ7.
3. Call the input() method on obj.
4. Call the reverse() method on obj.
5. Call the display() method on obj.
6. End.
Algorithm for JQ7() (Constructor)
1. Start.
2. Initialize String variables sen and revsen to empty strings "".
3. End.
Algorithm for input()
1. Start.
2. Create a Scanner object to accept user input.
3. Display "Enter Sentence: " to the user.
4. Accept a line of text into sen.
5. End.
Algorithm for reverse()
1. Start.
2. Create a StringTokenizer object using sen.
3. Declare a String array arr with a size equal to the number of
tokens in sen.
4. Initialize integer i to 0.
5. Repeat step 6 while i is less than the length of arr.
6. Store the next token from the StringTokenizer into arr[i].
7. Increment i by 1.
8. Initialize integer j to length of arr - 1.
9. Repeat step 10 while j is greater than or equal to 0.
10.Concatenate arr[j] and a space to revsen, and store the result
back into revsen.
11.Decrement j by 1.
12.End.
Algorithm for display()
1. Start.
2. Display "Original Sentence: " concatenated with sen.
3. Display "Reversed Sentence: " concatenated with revsen.
4. End.
Algorithm for the MatrixFun Program
Algorithm for main()
1. Start.
2. Create a Scanner object to accept user input.
3. Display "Enter Value of N: " to the user.
4. Accept an integer value into Nval.
5. Create an object obj of class MatrixFun, passing Nval to its
constructor.
6. Call the accept() method on obj.
7. Display "What elements would you like to see? \n1)Border
Elements \n2)Diagonal Elements" to the user.
8. Accept an integer value into choice.
9. If choice is equal to 1, then:
a. Call the border() method on obj.
10.If choice is equal to 2, then:
a. Call the diagonal() method on obj.
11.End.
Algorithm for MatrixFun(int N) (Constructor)
1. Start.
2. Assign the passed value of N to the class member this.N.
3. Declare a 2D integer array A of size NxN.
4. End.
Algorithm for accept()
1. Start.
2. Create a Scanner object to accept user input.
3. Display "Enter Array Elements:" to the user.
4. Initialize integer i to 0.
5. Repeat steps 6 to 8 while i < N.
6. Initialize integer j to 0.
7. Repeat step 8 while j < N.
8. Accept an integer into A[i][j].
9. Increment j by 1.
10.Increment i by 1.
11.End.
Algorithm for border()
1. Start.
2. Initialize integer i to 0.
3. Repeat steps 4 to 15 while i < N.
4. Initialize integer j to 0.
5. Repeat steps 6 to 14 while j < N.
6. If i is equal to 0, then:
a. Display A[i][j].
7. If j is equal to 0 OR j is equal to N - 1, then:
a. If i is NOT equal to 0 AND i is NOT equal to N - 1, then:
i. Display A[i][j].
8. If i is equal to N - 1, then:
a. Display A[i][j].
9. Increment j by 1.
10.Increment i by 1.
11.End.
Algorithm for diagonal()
1. Start.
2. Display "Diagonal Elements: ".
3. Initialize integer i to 0.
4. Repeat steps 5 to 9 while i < N.
5. Initialize integer j to 0.
6. Repeat steps 7 to 8 while j < N.
7. If i is equal to j, then:
a. Display A[i][j].
8. If (i + j) is equal to (N - 1), then:
a. Display A[i][j].
9. Increment j by 1.
10.Increment i by 1.
11.End.
Algorithm for the JQ10 Program
Algorithm for main()
1. Start.
2. Create a Scanner object to accept user input.
3. Declare an integer variable n.
4. Display "Enter Value of n: " to the user.
5. Accept an integer value into n.
6. Declare a 2D integer array arr of size nxn.
7. Display "Enter Array Values: ".
8. Initialize integer i to 0.
9. Repeat steps 10 to 12 while i < n.
10. Initialize integer j to 0.
11. Repeat step 12 while j < n.
12. Accept an integer into arr[i][j].
13. Increment j by 1.
14. Increment i by 1.
15. Declare and initialize integer l1 to arr[0][0].
16. Declare and initialize integer l1r to 0.
17. Declare and initialize integer l1c to 0.
18. Declare and initialize integer l2 to arr[0][0].
19. Declare and initialize integer l2r to 0.
20. Declare and initialize integer l2c to 0.
21. Initialize integer i to 0.
22. Repeat steps 23 to 29 while i < n.
23. Initialize integer j to 0.
24. Repeat steps 25 to 28 while j < n.
25. If arr[i][j] is greater than l1, then:
a. Set l1 to arr[i][j].
b. Set l1r to i.
c. Set l1c to j.
26. If arr[i][j] is greater than l2 AND arr[i][j] is less than l1, then:
a. Set l2 to arr[i][j].
b. Set l2r to i.
c. Set l2c to j.
27. Increment j by 1.
28. Increment i by 1.
29. Declare an integer array arrc of size n * n.
30. Declare and initialize integer k to 0.
31. Initialize integer i to 0.
32. Repeat steps 33 to 35 while i < n.
33. Initialize integer j to 0.
34. Repeat step 35 while j < n.
35. Set arrc[k] to arr[i][j].
36. Increment k by 1.
37. Increment j by 1.
38. Increment i by 1.
39. Declare an integer variable temp.
40. Initialize integer i to 0.
41. Repeat steps 42 to 47 while i < length of arrc - 1.
42. Initialize integer j to 0.
43. Repeat steps 44 to 46 while j < length of arrc - i - 1.
44. If arrc[j + 1] is greater than arrc[j], then:
a. Set temp to arrc[j].
b. Set arrc[j] to arrc[j + 1].
c. Set arrc[j + 1] to temp.
45. Increment j by 1.
46. Increment i by 1.
47. Set k to 0.
48. Initialize integer i to n - 1.
49. Repeat steps 50 to 52 while i >= 0.
50. Initialize integer j to n - 1.
51. Repeat step 52 while j >= 0.
52. Set arr[i][j] to arrc[k].
53. Increment k by 1.
54. Decrement j by 1.
55. Decrement i by 1.
56. Display "Sorted Array: ".
57. Initialize integer i to 0.
58. Repeat steps 59 to 62 while i < n.
59. Initialize integer j to 0.
60. Repeat step 61 while j < n.
61. Display arr[i][j] followed by a space.
62. Increment j by 1.
63. Display a new line.
64. Increment i by 1.
65. Display "The Largest Number: " concatenated with l1, "is in the
row " concatenated with (l1r + 1), and " and column " concatenated
with (l1c + 1).
66. Display "The Second Largest Number: " concatenated with l2, "is
in the row " concatenated with (l2r + 1), and " and column "
concatenated with (l2c + 1).
67. End.