Exercises from labs 2023-2024
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

471 rader
14 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 Exercise {
  12. private int _exerciseNumber;
  13. public Exercise(int s) {
  14. this._exerciseNumber = s;
  15. this.printExercise();
  16. }
  17. public void printExercise() {
  18. String title = String.format("Starting exercise %s", this._exerciseNumber);
  19. System.out.println("-".repeat(title.length()));
  20. System.out.println(String.format("%s", title));
  21. System.out.println("-".repeat(title.length()));
  22. }
  23. public int get_exerciseNumber() {
  24. return _exerciseNumber;
  25. }
  26. public void set_exerciseNumber(int _exerciseNumber) {
  27. this._exerciseNumber = _exerciseNumber;
  28. }
  29. }
  30. class Lab {
  31. private int _labNumber;
  32. public Exercise currentExercise;
  33. public Lab(int lN) {
  34. String title = String.format("|Starting Lab %s|", lN);
  35. this._labNumber = lN;
  36. System.out.println("*".repeat(title.length()));
  37. System.out.println(title);
  38. System.out.println("*".repeat(title.length()));
  39. }
  40. public Exercise getCurrentExercise() {
  41. return currentExercise;
  42. }
  43. public void setCurrentExercise(Exercise currentSection) {
  44. this.currentExercise = currentSection;
  45. }
  46. public static void printSection(String section) {
  47. String title = String.format("Starting exercise %s", section);
  48. System.out.println("-".repeat(title.length()));
  49. System.out.println(String.format("%s", title));
  50. System.out.println("-".repeat(title.length()));
  51. }
  52. public int getLabNumber() {
  53. return this._labNumber;
  54. }
  55. }
  56. class Lab4 extends Lab {
  57. public Lab4() {
  58. super(4);
  59. ex44();
  60. ex46();
  61. }
  62. // public static void main(String[] args) {
  63. // }
  64. public static void ex41() {
  65. String labSection = "4.1";
  66. // System.out.println(String.format("Starting section %s", labSection));
  67. printSection(labSection);
  68. int testYear1 = 1996; // true
  69. int testYear2 = 1997; // false
  70. int testYear3 = 2000; // true
  71. int testYear4 = 9332; // true
  72. int testYear5 = 1900; // false
  73. int randomYear = ThreadLocalRandom.current().nextInt(1, 9999 + 1);
  74. System.out.println(String.format("Testing %s", testYear1));
  75. System.out.println(String.format("%s", isLeap(testYear1)));
  76. System.out.println(String.format("Testing %s", testYear2));
  77. System.out.println(String.format("%s", isLeap(testYear2)));
  78. System.out.println(String.format("Testing %s", testYear3));
  79. System.out.println(String.format("%s", isLeap(testYear3)));
  80. System.out.println(String.format("Testing %s", testYear4));
  81. System.out.println(String.format("%s", isLeap(testYear4)));
  82. System.out.println(String.format("Testing %s", randomYear));
  83. System.out.println(String.format("%s", isLeap(randomYear)));
  84. System.out.println(String.format("Testing %s", testYear5));
  85. }
  86. public static void ex44() {
  87. String labSection = "4.4";
  88. printSection(labSection);
  89. // System.out.println("------------------------");
  90. // System.out.println("Starting lab section " + labSection);
  91. // System.out.println("------------------------");
  92. String month1 = "oct";
  93. int year1 = 2000;
  94. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  95. month1, year1, monthLen(month1, year1)));
  96. String month2 = "feb";
  97. int year2 = 2000;
  98. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  99. month2, year2, monthLen(month2, year2)));
  100. String month3 = "feb";
  101. int year3 = 1997;
  102. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  103. month3, year3, monthLen(month3, year3)));
  104. String month4 = "apr";
  105. int year4 = 2001;
  106. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  107. month4, year4, monthLen(month4, year4)));
  108. }
  109. /**
  110. * Lab section 4.5
  111. *
  112. * Write a program that can calculate how many whole years old someone is from
  113. * today’s date and their date of birth. You could store each date as three
  114. * integers (day,
  115. * month, year)
  116. */
  117. public static void ex46() {
  118. String labSection = "4.6";
  119. printSection(labSection);
  120. // calculate whole years' old to date
  121. Calendar today = Calendar.getInstance();
  122. System.out.println("Today:\t" + today.getTime());
  123. Calendar dob1 = Calendar.getInstance();
  124. dob1.set(1990, 1, 1);
  125. System.out.println(String.format(
  126. "DOB 1:\t%s (difference:\t%d years)",
  127. dob1.getTime(),
  128. yearDifference(today, dob1)));
  129. Calendar dob2 = Calendar.getInstance();
  130. dob2.set(1994, 12, 22);
  131. System.out.println(String.format(
  132. "DOB 2:\t%s (difference:\t%d years)",
  133. dob2.getTime(),
  134. yearDifference(today, dob2)));
  135. }
  136. public static boolean isLeap(int year) {
  137. if (year % 4 != 0) {
  138. return false;
  139. } else {
  140. if (year % 100 == 0) {
  141. if (year % 400 == 0) {
  142. return true;
  143. } else {
  144. return false;
  145. }
  146. }
  147. return true;
  148. }
  149. }
  150. public static int monthLen(String month, int year) {
  151. if (month == "feb") {
  152. if (isLeap(year)) {
  153. return 29;
  154. } else {
  155. return 28;
  156. }
  157. } else if (
  158. (month == "apr") ||
  159. (month == "jun") ||
  160. (month == "sep") ||
  161. (month == "nov")) {
  162. return 30;
  163. } else {
  164. return 31;
  165. }
  166. }
  167. public static int yearDifference(Calendar date1, Calendar date2)
  168. {
  169. return (int) Math.abs(date1.get(Calendar.YEAR) -
  170. date2.get(Calendar.YEAR)
  171. );
  172. }
  173. }
  174. /**
  175. * Keyboard input
  176. */
  177. class Lab5 extends Lab {
  178. public Lab5() {
  179. super(5);
  180. new five1();
  181. }
  182. class five1 extends Exercise {
  183. /**
  184. * We will now look at how we can get user input from the keyboard. We will
  185. * gloss over some
  186. * of the details here, but give you just the information you need to start
  187. * getting input and
  188. * processing it.
  189. */
  190. public five1() {
  191. super(1);
  192. System.out.println(String.format("TBC"));
  193. // Scanner keyboard = new Scanner(System.in);
  194. // System.out.println("Please enter your name, followed by the return key?");
  195. // String userEntry = keyboard.nextLine();
  196. // System.out.println("Hello " + userEntry);
  197. // System.out.println(String.format("%s", keyboard));
  198. }
  199. }
  200. }
  201. /**
  202. * Loops
  203. */
  204. class Lab6 extends Lab {
  205. public Lab6() {
  206. super(6);
  207. new six1();
  208. System.out.println(String.format("%n"));
  209. new six3();
  210. new six5();
  211. new six6();
  212. new six7();
  213. }
  214. /**
  215. * Times tables
  216. */
  217. class six1 extends Exercise {
  218. public six1() {
  219. super(1);
  220. int testNum = 6;
  221. System.out.println(String.format("Times table for %d:", testNum));
  222. writeTimesTable(7);
  223. }
  224. }
  225. class six3 extends Exercise {
  226. public six3() {
  227. super(3);
  228. int testNum = 13; // statistically the most prime of all numbers
  229. System.out.println(String.format("Testing primes..."));
  230. System.out.println(String.format("%d prime? %s", testNum, isPrime(testNum)));
  231. System.out.println(String.format("%d prime? %s", 23, isPrime(23)));
  232. System.out.println(String.format("%d prime? %s", 27, isPrime(27)));
  233. System.out.println(String.format("%d prime? %s", 28, isPrime(28)));
  234. System.out.println(String.format("%d prime? %s", 299, isPrime(299)));
  235. System.out.println(String.format("Generating primes..."));
  236. generatePrimes(1024);
  237. System.out.println(String.format("Done!%n"));
  238. }
  239. }
  240. class six5 extends Exercise {
  241. public six5() {
  242. super(5);
  243. System.out.println(String.format("TODO / NOT IMPLEMENTED until we get stdin working"));
  244. }
  245. }
  246. /**
  247. * Produce a number triangle
  248. *
  249. * eg:
  250. * 1
  251. * 2 2
  252. * 3 3 3
  253. * 4 4 4 4
  254. */
  255. class six6 extends Exercise {
  256. public six6() {
  257. super(6);
  258. numberTriangle(1);
  259. numberTriangle(4);
  260. numberTriangle(8);
  261. numberTriangle(29);
  262. }
  263. }
  264. class six7 extends Exercise {
  265. public six7() {
  266. super(7);
  267. describeDD(4.2, 1.6);
  268. describeDD(47.42, 7.9);
  269. }
  270. /** Simple exposition of double division
  271. * @param num1 Dividend
  272. * @param num2 Divisor
  273. */
  274. public void describeDD(double num1, double num2) {
  275. System.out.println(String.format("Double division of %.2f by %.2f:\t", num1, num2));
  276. DoubleDivisionResult result = doubleDivision(num1, num2);
  277. System.out.println(String.format("Quotient: %d (ie %.2f ✕ %d = %.2f)\tRemainder: %.2f",
  278. result.getQuotient(),
  279. num2,
  280. result.getQuotient(),
  281. (num2 * result.getQuotient()),
  282. result.getRemainder()));
  283. }
  284. }
  285. /* TOASK - is there a better way of returning multiple values in Java?
  286. à la python, ie: return (quotient, remainder)
  287. */
  288. /**
  289. * Wrapper class for returning results from double division
  290. */
  291. final class DoubleDivisionResult {
  292. private int quotient; // that's the number of times something goes into something else
  293. private double remainder; // that's the bit left over
  294. public DoubleDivisionResult(int quotient, double remainder) {
  295. this.quotient = quotient;
  296. this.remainder = remainder;
  297. }
  298. public int getQuotient() {
  299. return quotient;
  300. }
  301. public double getRemainder() {
  302. return remainder;
  303. }
  304. }
  305. /**
  306. * Perform 'double division'
  307. *
  308. * @param dividend Number to be divided
  309. * @param divisor Number to divide by
  310. * @return a {@link DoubleDivisionResult} with the (int) quotient and (double) remainder
  311. */
  312. public DoubleDivisionResult doubleDivision(double dividend, double divisor) {
  313. // example used is 4.2 and 1.6
  314. // should return 2 and 1.0
  315. final boolean DEBUG = false;
  316. double dQuotient = dividend / divisor;
  317. int iQuotient = (int)dQuotient;
  318. double remainder = dividend - (iQuotient * divisor);
  319. if (DEBUG) {
  320. System.out.println(String.format("float quotient: %.2f", dQuotient));
  321. System.out.println(String.format("int quotient: %d", iQuotient));
  322. // TOASK: is there a neater way to do this casting?
  323. }
  324. return new DoubleDivisionResult(iQuotient, remainder);
  325. }
  326. /**
  327. * @param num Size of triangle
  328. */
  329. public void numberTriangle(int num) {
  330. /* Observations:
  331. - for even-sized triangles, preceding number of spaces starts at n=rows and decrements
  332. - for even-sized triangles even numbers have odd spaces and vice versa
  333. */
  334. System.out.println(String.format(""));
  335. int spaces = num;
  336. for (int i = 1; i <= num; i++) {
  337. System.out.println(" ".repeat(spaces)
  338. + String.format("%s ", i).repeat(i)
  339. + " ".repeat(spaces-1)
  340. );
  341. spaces--;
  342. }
  343. System.out.println(String.format(""));
  344. }
  345. /**
  346. * @param num The number to write the times table for
  347. * @param limit How many multiples to show
  348. */
  349. public void writeTimesTable(int num, int limit) {
  350. for (int i = 0; i < (limit + 1); i++) {
  351. System.out.printf("%d\t", num * i);
  352. }
  353. }
  354. /* TOASK - overload example.
  355. Question: is there a better way of doing optional function parameters in java?
  356. */
  357. /**
  358. * Write times table for num (10 entries)
  359. * @param num Number to show times table for
  360. */
  361. public void writeTimesTable(int num) {
  362. int DEFAULT_LIMIT = 10;
  363. writeTimesTable(num, DEFAULT_LIMIT);
  364. }
  365. public void generatePrimes(int upperLimit) {
  366. // handle 2 as a special case
  367. System.out.println(String.format("2"));
  368. // for (int i = 3; i <= upperLimit; i += 2) {
  369. for (int i = 2; i <= upperLimit; i++) {
  370. // TODO: figure out smarter way of doing this, I'm sure primes must be at least 6 apart...
  371. // Something about 2n ± 1 ?
  372. // seive of Erasthotenes ..?
  373. if (isPrime(i)) {
  374. System.out.printf("%d\t", i);
  375. }
  376. }
  377. }
  378. /**
  379. * But is it prime?
  380. * @param num Integer to test for primality
  381. * @return if it's prime
  382. */
  383. public boolean isPrime(int num) {
  384. boolean DEBUG = false;
  385. if ((num == 2) || (num == 1)) {
  386. return true;
  387. }
  388. if (num % 2 == 0){
  389. return false;
  390. } else {
  391. int divisor = 3;
  392. double numSquareRoot = Math.sqrt((double)num);
  393. while (divisor <= numSquareRoot) {
  394. if (num % divisor == 0) {
  395. return false;
  396. }
  397. divisor += 2;
  398. if (DEBUG) {
  399. System.out.printf("%d\t", divisor);
  400. }
  401. }
  402. return true;
  403. }
  404. }
  405. }