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.

377 rivejä
11 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. new six6();
  209. }
  210. /**
  211. * Times tables
  212. */
  213. class six1 extends Section {
  214. public six1() {
  215. super(1);
  216. int testNum = 6;
  217. System.out.println(String.format("Times table for %d:", testNum));
  218. writeTimesTable(7);
  219. }
  220. }
  221. class six3 extends Section {
  222. public six3() {
  223. super(3);
  224. int testNum = 13; // statistically the most prime of all numbers
  225. System.out.println(String.format("Testing primes..."));
  226. System.out.println(String.format("%d prime? %s", testNum, isPrime(testNum)));
  227. System.out.println(String.format("%d prime? %s", 23, isPrime(23)));
  228. System.out.println(String.format("%d prime? %s", 27, isPrime(27)));
  229. System.out.println(String.format("%d prime? %s", 28, isPrime(28)));
  230. System.out.println(String.format("%d prime? %s", 299, isPrime(299)));
  231. System.out.println(String.format("Generating primes..."));
  232. generatePrimes(1024);
  233. System.out.println(String.format("Done!%n"));
  234. }
  235. }
  236. class six5 extends Section {
  237. public six5() {
  238. super(5);
  239. System.out.println(String.format("TODO / NOT IMPLEMENTED until we get stdin working"));
  240. }
  241. }
  242. /**
  243. * Produce a number triangle
  244. *
  245. * eg:
  246. * 1
  247. * 2 2
  248. * 3 3 3
  249. * 4 4 4 4
  250. */
  251. class six6 extends Section {
  252. public six6() {
  253. super(6);
  254. numberTriangle(8);
  255. }
  256. }
  257. /**
  258. * @param num Size of triangle
  259. */
  260. public void numberTriangle(int num) {
  261. /* Observations:
  262. - for even-sized triangles, preceding number of spaces starts at n=rows and decrements
  263. - for even-sized triangles even numbers have odd spaces and vice versa
  264. */
  265. int spaces = num;
  266. for (int i = 1; i <= num; i++) {
  267. System.out.println(" ".repeat(spaces)
  268. + String.format("%s ", i).repeat(i)
  269. + " ".repeat(spaces-1)
  270. );
  271. spaces--;
  272. }
  273. }
  274. public void writeTimesTable(int num, int limit) {
  275. for (int i = 0; i < (limit + 1); i++) {
  276. System.out.printf("%d\t", num * i);
  277. }
  278. }
  279. public void writeTimesTable(int num) {
  280. int DEFAULT_LIMIT = 10;
  281. writeTimesTable(num, DEFAULT_LIMIT);
  282. }
  283. public void generatePrimes(int upperLimit) {
  284. // handle 2 as a special case
  285. System.out.println(String.format("2"));
  286. // for (int i = 3; i <= upperLimit; i += 2) {
  287. for (int i = 2; i <= upperLimit; i++) {
  288. // TODO: figure out smarter way of doing this, I'm sure primes must be at least 6 apart...
  289. // Something about 2n ± 1 ?
  290. // seive of Erasthotenes ..?
  291. if (isPrime(i)) {
  292. System.out.printf("%d\t", i);
  293. }
  294. }
  295. }
  296. /**
  297. * But is it prime?
  298. * @param num Integer to test for primality
  299. * @return if it's prime
  300. */
  301. public boolean isPrime(int num) {
  302. boolean DEBUG = false;
  303. if ((num == 2) || (num == 1)) {
  304. return true;
  305. }
  306. if (num % 2 == 0){
  307. return false;
  308. } else {
  309. int divisor = 3;
  310. double numSquareRoot = Math.sqrt((double)num);
  311. while (divisor <= numSquareRoot) {
  312. if (num % divisor == 0) {
  313. return false;
  314. }
  315. divisor += 2;
  316. if (DEBUG) {
  317. System.out.printf("%d\t", divisor);
  318. }
  319. }
  320. return true;
  321. }
  322. }
  323. }