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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. System.out.print(s + " ");
  41. System.out.flush();
  42. }
  43. public static int readInt (BufferedReader in) throws IOException {
  44. if (T==null) refresh(in);
  45. while (true) {
  46. try {
  47. return Integer.parseInt(T.nextToken());
  48. }
  49. catch (NoSuchElementException e1) {
  50. refresh (in);
  51. }
  52. catch (NumberFormatException e2) {
  53. System.out.println("Error in number, try again.");
  54. }
  55. }
  56. }
  57. public static char readChar (BufferedReader in) throws IOException {
  58. if (T==null) refresh(in);
  59. while (true) {
  60. try {
  61. return T.nextToken().trim().charAt(0);
  62. }
  63. catch (NoSuchElementException e1) {
  64. refresh (in);
  65. }
  66. }
  67. }
  68. public static double readDouble (BufferedReader in) throws IOException {
  69. if (T==null) refresh(in);
  70. while (true) {
  71. try {
  72. String item = T.nextToken();
  73. return Double.valueOf(item.trim()).doubleValue();
  74. }
  75. catch (NoSuchElementException e1) {
  76. refresh (in);
  77. }
  78. catch (NumberFormatException e2) {
  79. System.out.println("Error in number, try again.");
  80. }
  81. }
  82. }
  83. public static String readString (BufferedReader in) throws IOException {
  84. if (T==null) refresh (in);
  85. while (true) {
  86. try {
  87. return T.nextToken();
  88. }
  89. catch (NoSuchElementException e1) {
  90. refresh (in);
  91. }
  92. }
  93. }
  94. // Keep reading.
  95. private static void refresh (BufferedReader in) throws IOException {
  96. S = in.readLine ();
  97. if (S==null) throw new EOFException();
  98. T = new StringTokenizer (S);
  99. }
  100. // Write methods
  101. // -------------
  102. private static DecimalFormat N = new DecimalFormat();
  103. private static final String spaces = " ";
  104. public static String writeDouble (double number, int align, int frac) {
  105. N.setGroupingUsed(false);
  106. N.setMaximumFractionDigits(frac);
  107. N.setMinimumFractionDigits(frac);
  108. String num = N.format(number);
  109. if (num.length() < align)
  110. num = spaces.substring(0,align-num.length()) + num;
  111. return num;
  112. }
  113. // Okay to finish this lab, create a class that uses a few of the methods
  114. // here. Make sure it runs. Commit the changes and do a pull request.
  115. public static String writeInt (int number, int align) {
  116. N.setGroupingUsed(false);
  117. N.setMaximumFractionDigits(0);
  118. String num = N.format(number);
  119. if (num.length() < align)
  120. num = spaces.substring(0,align-num.length()) + num;
  121. return num;
  122. }
  123. }