some code samples, various examples of simple modeling ideas and some minor algorithms.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4. public class TextIO {
  5. public TextIO () {};
  6. /* The All New Famous Text class by J M Bishop Aug 1996
  7. * revised for Java 1.1 by Alwyn Moolman Aug 1997
  8. * revised for efficiency by J M Bishop Dec 1997
  9. *
  10. * Provides simple input from the keyboard and files.
  11. * Now also has simple output formatting methods
  12. * and file opening facilities.
  13. *
  14. * public static void prompt (String s)
  15. * public static int readInt (BufferedReader in)
  16. * public static double readDouble (BufferedReader in)
  17. * public static String readString (BufferedReader in)
  18. * public static char readChar (BufferedReader in)
  19. * public static String writeInt (int number, int align)
  20. * public static String writeDouble
  21. (double number, int align, int frac)
  22. * public static BufferedReader open (InputStream in)
  23. * public static BufferedReader open (String filename)
  24. * public static PrintWriter create (String filename)
  25. */
  26. private static StringTokenizer T;
  27. private static String S;
  28. public static BufferedReader open (InputStream in) {
  29. return new BufferedReader(new InputStreamReader(in));
  30. }
  31. public static BufferedReader open (String filename)
  32. throws FileNotFoundException {
  33. return new BufferedReader (new FileReader (filename));
  34. }
  35. public static PrintWriter create
  36. (String filename) throws IOException {
  37. return new PrintWriter (new FileWriter (filename));
  38. }
  39. public static void prompt (String s) {
  40. // String z = new String ("fire");
  41. // StringBuilder builder = new StringBuilder();
  42. // builder.append(s);
  43. // builder.append(z);
  44. // String result = builder.toString();
  45. System.out.print(s + " ");
  46. System.out.flush();
  47. }
  48. public static int readInt (BufferedReader in) throws IOException {
  49. if (T==null) refresh(in);
  50. while (true) {
  51. try {
  52. return Integer.parseInt(T.nextToken());
  53. }
  54. catch (NoSuchElementException e1) {
  55. refresh (in);
  56. }
  57. catch (NumberFormatException e2) {
  58. System.out.println("Error in number, try again.");
  59. }
  60. }
  61. }
  62. public static char readChar (BufferedReader in) throws IOException {
  63. if (T==null) refresh(in);
  64. while (true) {
  65. try {
  66. return T.nextToken().trim().charAt(0);
  67. }
  68. catch (NoSuchElementException e1) {
  69. refresh (in);
  70. }
  71. }
  72. }
  73. public static double readDouble (BufferedReader in) throws IOException {
  74. if (T==null) refresh(in);
  75. while (true) {
  76. try {
  77. String item = T.nextToken();
  78. return Double.valueOf(item.trim()).doubleValue();
  79. }
  80. catch (NoSuchElementException e1) {
  81. refresh (in);
  82. }
  83. catch (NumberFormatException e2) {
  84. System.out.println("Error in number, try again.");
  85. }
  86. }
  87. }
  88. public static String readString (BufferedReader in) throws IOException {
  89. if (T==null) refresh (in);
  90. while (true) {
  91. try {
  92. return T.nextToken();
  93. }
  94. catch (NoSuchElementException e1) {
  95. refresh (in);
  96. }
  97. }
  98. }
  99. // Keep reading.
  100. private static void refresh (BufferedReader in) throws IOException {
  101. S = in.readLine ();
  102. if (S==null) throw new EOFException();
  103. T = new StringTokenizer (S);
  104. }
  105. // Write methods
  106. // -------------
  107. private static DecimalFormat N = new DecimalFormat();
  108. private static final String spaces = " ";
  109. public static String writeDouble (double number, int align, int frac) {
  110. N.setGroupingUsed(false);
  111. N.setMaximumFractionDigits(frac);
  112. N.setMinimumFractionDigits(frac);
  113. String num = N.format(number);
  114. if (num.length() < align)
  115. num = spaces.substring(0,align-num.length()) + num;
  116. return num;
  117. }
  118. // Okay to finish this lab, create a class that uses a few of the methods
  119. // here. Make sure it runs. Commit the changes and do a pull request.
  120. public static String writeInt (int number, int align) {
  121. N.setGroupingUsed(false);
  122. N.setMaximumFractionDigits(0);
  123. String num = N.format(number);
  124. if (num.length() < align)
  125. num = spaces.substring(0,align-num.length()) + num;
  126. return num;
  127. }
  128. }