firstsubroutines.java.xhtml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * <https://y.st./>
  4. * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org./licenses/>.
  18. **/
  19. $xhtml = array(
  20. 'title' => 'firstsubroutines.java',
  21. 'subtitle' => 'Written in <span title="Programming 1">CS 1102</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalised on 2017-05-03',
  22. 'copyright year' => '2017',
  23. 'body' => <<<END
  24. <h2>Source code:</h2>
  25. <blockquote>
  26. <pre><code>/*
  27. * Copyright (C) 2017 Alex Yst
  28. *
  29. * This program is free software: you can redistribute it and/or modify
  30. * it under the terms of the GNU General Public License as published by
  31. * the Free Software Foundation, either version 3 of the License, or
  32. * (at your option) any later version.
  33. *
  34. * This program is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU General Public License
  40. * along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;.
  41. */
  42. package firstsubroutines;
  43. import java.util.Scanner;
  44. /**
  45. * Alex Yst&apos;s CS 1102 (Programming 1) Unit 4 submission
  46. *
  47. * This class was programmed to fill the requirements on the Unit 4 programming
  48. * assignment in University of the People&apos;s CS 1102 (Programming 1) course.
  49. *
  50. * @author Alex Yst
  51. */
  52. public class firstsubroutines {
  53. /**
  54. * This method reverses the string passed to it as an argument, and thus the
  55. * name of this method.
  56. *
  57. * @param str The string to reverse
  58. * @return The reversed string
  59. */
  60. public static String reverse(String str) {
  61. String reverse = &quot;&quot;;
  62. for (int i = str.length() - 1; i &gt;= 0; i--) {
  63. reverse = reverse + str.charAt(i);
  64. }
  65. return reverse;
  66. }
  67. /**
  68. * This method removes all non-letters and converts all letters to lower case.
  69. *
  70. * In computer science, the conversion of strings to a unified for like this,
  71. * in which all equivilant strings are converted to an identical form, is
  72. * called normalization, and thus the name of this method.
  73. *
  74. * @param str A string to be normalized
  75. * @return The normalized string
  76. */
  77. public static String normalize(String str) {
  78. str = str.toLowerCase();
  79. int end = str.length() -1;
  80. String normalized = &quot;&quot;;
  81. for (int i = 0; i &lt;= end; i++) {
  82. char ch = str.charAt(i);
  83. if(ch &gt;= &apos;a&apos; &amp;&amp; ch &lt;= &apos;z&apos;) {
  84. normalized = normalized + ch;
  85. }
  86. }
  87. return normalized;
  88. }
  89. /**
  90. * This is just the main method. In Java, the main() method is called. Code
  91. * isn&apos;t simply included outside all method definitions when it needs to be
  92. * run.
  93. *
  94. * @param arguments This parameter is unused.
  95. */
  96. public static void main(String[] arguments) {
  97. Boolean loop = true;
  98. Scanner scanner = new Scanner(System.in);
  99. String forward;
  100. String reverse;
  101. String compare;
  102. System.out.println(&quot;Please enter a string to be checked, or enter an empty string to abort:&quot;);
  103. String input = scanner.nextLine();
  104. while(!input.equals(&quot;&quot;)) {
  105. forward = normalize(input);
  106. reverse = reverse(forward);
  107. System.out.println(&quot;stripped: &quot;+forward);
  108. System.out.println(&quot;reversed: &quot;+reverse);
  109. if(forward.equals(reverse)) {
  110. compare = &quot;IS&quot;;
  111. } else {
  112. compare = &quot;is NOT&quot;;
  113. }
  114. System.out.println(&quot;This &quot;+compare+&quot; a palindrome.&quot;);
  115. System.out.println(&quot;Please enter a string to be checked, or enter an empty string to abort:&quot;);
  116. input = scanner.nextLine();
  117. }
  118. }
  119. }</code></pre>
  120. </blockquote>
  121. <h2>Output:</h2>
  122. <blockquote>
  123. <pre>run:
  124. Please enter a string to be checked, or enter an empty string to abort:
  125. Hello World!
  126. stripped: helloworld
  127. reversed: dlrowolleh
  128. This is NOT a palindrome.
  129. Please enter a string to be checked, or enter an empty string to abort:
  130. Campus motto: Bottoms up, Mac!
  131. stripped: campusmottobottomsupmac
  132. reversed: campusmottobottomsupmac
  133. This IS a palindrome.
  134. Please enter a string to be checked, or enter an empty string to abort:
  135. BUILD SUCCESSFUL (total time: 21 seconds)</pre>
  136. </blockquote>
  137. END
  138. );