Exercises from labs 2023-2024
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

343 rivejä
10 KiB

  1. import java.util.Calendar;
  2. import java.util.Scanner;
  3. import java.util.concurrent.ThreadLocalRandom;
  4. class runLabs {
  5. public static void main(String[] args) {
  6. new Lab4();
  7. new Lab5();
  8. new Lab6();
  9. }
  10. }
  11. class Section {
  12. private int _sectionNumber;
  13. public Section(int s) {
  14. this._sectionNumber = s;
  15. this.printSection();
  16. }
  17. public int getSection() {
  18. return _sectionNumber;
  19. }
  20. public void printSection() {
  21. String title = String.format("Starting section %s", this._sectionNumber);
  22. System.out.println("-".repeat(title.length()));
  23. System.out.println(String.format("%s", title));
  24. System.out.println("-".repeat(title.length()));
  25. }
  26. }
  27. class Lab {
  28. private int _labNumber;
  29. public Section currentSection;
  30. public Section getCurrentSection() {
  31. return currentSection;
  32. }
  33. public void setCurrentSection(Section currentSection) {
  34. this.currentSection = currentSection;
  35. }
  36. public static void printSection(String section) {
  37. String title = String.format("Starting section %s", section);
  38. System.out.println("-".repeat(title.length()));
  39. System.out.println(String.format("%s", title));
  40. System.out.println("-".repeat(title.length()));
  41. }
  42. public Lab(int lN) {
  43. String title = String.format("|Starting Lab %s|", lN);
  44. this._labNumber = lN;
  45. System.out.println("*".repeat(title.length()));
  46. System.out.println(title);
  47. System.out.println("*".repeat(title.length()));
  48. }
  49. public int getLabNumber() {
  50. return this._labNumber;
  51. }
  52. }
  53. class Lab4 extends Lab {
  54. public Lab4() {
  55. super(4);
  56. ex44();
  57. ex46();
  58. }
  59. // public static void main(String[] args) {
  60. // }
  61. public static void ex41() {
  62. String labSection = "4.1";
  63. // System.out.println(String.format("Starting section %s", labSection));
  64. printSection(labSection);
  65. int testYear1 = 1996; // true
  66. int testYear2 = 1997; // false
  67. int testYear3 = 2000; // true
  68. int testYear4 = 9332; // true
  69. int testYear5 = 1900; // false
  70. int randomYear = ThreadLocalRandom.current().nextInt(1, 9999 + 1);
  71. System.out.println(String.format("Testing %s", testYear1));
  72. System.out.println(String.format("%s", isLeap(testYear1)));
  73. System.out.println(String.format("Testing %s", testYear2));
  74. System.out.println(String.format("%s", isLeap(testYear2)));
  75. System.out.println(String.format("Testing %s", testYear3));
  76. System.out.println(String.format("%s", isLeap(testYear3)));
  77. System.out.println(String.format("Testing %s", testYear4));
  78. System.out.println(String.format("%s", isLeap(testYear4)));
  79. System.out.println(String.format("Testing %s", randomYear));
  80. System.out.println(String.format("%s", isLeap(randomYear)));
  81. System.out.println(String.format("Testing %s", testYear5));
  82. }
  83. public static void ex44() {
  84. String labSection = "4.4";
  85. printSection(labSection);
  86. // System.out.println("------------------------");
  87. // System.out.println("Starting lab section " + labSection);
  88. // System.out.println("------------------------");
  89. String month1 = "oct";
  90. int year1 = 2000;
  91. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  92. month1, year1, monthLen(month1, year1)));
  93. String month2 = "feb";
  94. int year2 = 2000;
  95. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  96. month2, year2, monthLen(month2, year2)));
  97. String month3 = "feb";
  98. int year3 = 1997;
  99. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  100. month3, year3, monthLen(month3, year3)));
  101. String month4 = "apr";
  102. int year4 = 2001;
  103. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  104. month4, year4, monthLen(month4, year4)));
  105. }
  106. /**
  107. * Lab section 4.5
  108. *
  109. * Write a program that can calculate how many whole years old someone is from
  110. * today’s date and their date of birth. You could store each date as three
  111. * integers (day,
  112. * month, year)
  113. */
  114. public static void ex46() {
  115. String labSection = "4.6";
  116. printSection(labSection);
  117. // calculate whole years' old to date
  118. Calendar today = Calendar.getInstance();
  119. System.out.println("Today:\t" + today.getTime());
  120. Calendar dob1 = Calendar.getInstance();
  121. dob1.set(1990, 1, 1);
  122. System.out.println(String.format(
  123. "DOB 1:\t%s (difference:\t%d years)",
  124. dob1.getTime(),
  125. yearDifference(today, dob1)));
  126. Calendar dob2 = Calendar.getInstance();
  127. dob2.set(1994, 12, 22);
  128. System.out.println(String.format(
  129. "DOB 2:\t%s (difference:\t%d years)",
  130. dob2.getTime(),
  131. yearDifference(today, dob2)));
  132. }
  133. public static boolean isLeap(int year) {
  134. if (year % 4 != 0) {
  135. return false;
  136. } else {
  137. if (year % 100 == 0) {
  138. if (year % 400 == 0) {
  139. return true;
  140. } else {
  141. return false;
  142. }
  143. }
  144. return true;
  145. }
  146. }
  147. public static int monthLen(String month, int year) {
  148. if (month == "feb") {
  149. if (isLeap(year)) {
  150. return 29;
  151. } else {
  152. return 28;
  153. }
  154. } else if (
  155. (month == "apr") ||
  156. (month == "jun") ||
  157. (month == "sep") ||
  158. (month == "nov")) {
  159. return 30;
  160. } else {
  161. return 31;
  162. }
  163. }
  164. public static int yearDifference(Calendar date1, Calendar date2)
  165. {
  166. return (int) Math.abs(date1.get(Calendar.YEAR) -
  167. date2.get(Calendar.YEAR)
  168. );
  169. }
  170. }
  171. /**
  172. * Keyboard input
  173. */
  174. class Lab5 extends Lab {
  175. public Lab5() {
  176. super(5);
  177. new five1();
  178. }
  179. class five1 extends Section {
  180. /**
  181. * We will now look at how we can get user input from the keyboard. We will
  182. * gloss over some
  183. * of the details here, but give you just the information you need to start
  184. * getting input and
  185. * processing it.
  186. */
  187. public five1() {
  188. super(1);
  189. System.out.println(String.format("TBC"));
  190. // Scanner keyboard = new Scanner(System.in);
  191. // System.out.println("Please enter your name, followed by the return key?");
  192. // String userEntry = keyboard.nextLine();
  193. // System.out.println("Hello " + userEntry);
  194. // System.out.println(String.format("%s", keyboard));
  195. }
  196. }
  197. }
  198. /**
  199. * Loops
  200. */
  201. class Lab6 extends Lab {
  202. public Lab6() {
  203. super(6);
  204. new six1();
  205. System.out.println(String.format("%n"));
  206. new six3();
  207. new six5();
  208. }
  209. /**
  210. * Times tables
  211. */
  212. class six1 extends Section {
  213. public six1() {
  214. super(1);
  215. int testNum = 6;
  216. System.out.println(String.format("Times table for %d:", testNum));
  217. writeTimesTable(7);
  218. }
  219. }
  220. class six3 extends Section {
  221. public six3() {
  222. super(3);
  223. int testNum = 13; // statistically the most prime of all numbers
  224. System.out.println(String.format("Testing primes..."));
  225. System.out.println(String.format("%d prime? %s", testNum, isPrime(testNum)));
  226. System.out.println(String.format("%d prime? %s", 23, isPrime(23)));
  227. System.out.println(String.format("%d prime? %s", 27, isPrime(27)));
  228. System.out.println(String.format("%d prime? %s", 28, isPrime(28)));
  229. System.out.println(String.format("%d prime? %s", 299, isPrime(299)));
  230. System.out.println(String.format("Generating primes..."));
  231. generatePrimes(1024);
  232. System.out.println(String.format("Done!%n"));
  233. }
  234. }
  235. class six5 extends Section {
  236. public six5() {
  237. super(5);
  238. System.out.println(String.format("TODO / NOT IMPLEMENTED until we get stdin working"));
  239. }
  240. }
  241. public void writeTimesTable(int num, int limit) {
  242. for (int i = 0; i < (limit + 1); i++) {
  243. System.out.printf("%d\t", num * i);
  244. }
  245. }
  246. public void writeTimesTable(int num) {
  247. int DEFAULT_LIMIT = 10;
  248. writeTimesTable(num, DEFAULT_LIMIT);
  249. }
  250. public void generatePrimes(int upperLimit) {
  251. // handle 2 as a special case
  252. System.out.println(String.format("2"));
  253. // for (int i = 3; i <= upperLimit; i += 2) {
  254. for (int i = 2; i <= upperLimit; i++) {
  255. // TODO: figure out smarter way of doing this, I'm sure primes must be at least 6 apart...
  256. // Something about 2n ± 1 ?
  257. // seive of Erasthotenes ..?
  258. if (isPrime(i)) {
  259. System.out.printf("%d\t", i);
  260. }
  261. }
  262. }
  263. /**
  264. * But is it prime?
  265. * @param num Integer to test for primality
  266. * @return if it's prime
  267. */
  268. public boolean isPrime(int num) {
  269. boolean DEBUG = false;
  270. if ((num == 2) || (num == 1)) {
  271. return true;
  272. }
  273. if (num % 2 == 0){
  274. return false;
  275. } else {
  276. int divisor = 3;
  277. double numSquareRoot = Math.sqrt((double)num);
  278. while (divisor <= numSquareRoot) {
  279. if (num % divisor == 0) {
  280. return false;
  281. }
  282. divisor += 2;
  283. if (DEBUG) {
  284. System.out.printf("%d\t", divisor);
  285. }
  286. }
  287. return true;
  288. }
  289. }
  290. }