Exercises from labs 2023-2024
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

865 řádky
29 KiB

  1. import java.util.Calendar;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4. import java.util.concurrent.ThreadLocalRandom;
  5. class runLabs {
  6. public static void main(String[] args) {
  7. // new Lab4();
  8. new Lab5();
  9. // new Lab6();
  10. // new Lab7();
  11. }
  12. }
  13. class Exercise {
  14. private int _exerciseNumber;
  15. public Exercise(int s) {
  16. this._exerciseNumber = s;
  17. this.printExercise();
  18. }
  19. public void printExercise() {
  20. String title = String.format("Starting exercise %s", this._exerciseNumber);
  21. System.out.println("-".repeat(title.length()));
  22. System.out.println(String.format("%s", title));
  23. System.out.println("-".repeat(title.length()));
  24. }
  25. public int get_exerciseNumber() {
  26. return _exerciseNumber;
  27. }
  28. public void set_exerciseNumber(int _exerciseNumber) {
  29. this._exerciseNumber = _exerciseNumber;
  30. }
  31. }
  32. class Lab {
  33. private int _labNumber;
  34. public Exercise currentExercise;
  35. public Lab(int lN) {
  36. String title = String.format("|Starting Lab %s|", lN);
  37. this._labNumber = lN;
  38. System.out.println("*".repeat(title.length()));
  39. System.out.println(title);
  40. System.out.println("*".repeat(title.length()));
  41. }
  42. public Exercise getCurrentExercise() {
  43. return currentExercise;
  44. }
  45. public void setCurrentExercise(Exercise currentSection) {
  46. this.currentExercise = currentSection;
  47. }
  48. public static void printSection(String section) {
  49. String title = String.format("Starting exercise %s", section);
  50. System.out.println("-".repeat(title.length()));
  51. System.out.println(String.format("%s", title));
  52. System.out.println("-".repeat(title.length()));
  53. }
  54. public int getLabNumber() {
  55. return this._labNumber;
  56. }
  57. }
  58. class Lab4 extends Lab {
  59. public Lab4() {
  60. super(4);
  61. // ex44();
  62. ex46();
  63. }
  64. // public static void main(String[] args) {
  65. // }
  66. public static void ex41() {
  67. String labSection = "4.1";
  68. // System.out.println(String.format("Starting section %s", labSection));
  69. printSection(labSection);
  70. int testYear1 = 1996; // true
  71. int testYear2 = 1997; // false
  72. int testYear3 = 2000; // true
  73. int testYear4 = 9332; // true
  74. int testYear5 = 1900; // false
  75. int randomYear = ThreadLocalRandom.current().nextInt(1, 9999 + 1);
  76. System.out.println(String.format("Testing %s", testYear1));
  77. System.out.println(String.format("%s", isLeap(testYear1)));
  78. System.out.println(String.format("Testing %s", testYear2));
  79. System.out.println(String.format("%s", isLeap(testYear2)));
  80. System.out.println(String.format("Testing %s", testYear3));
  81. System.out.println(String.format("%s", isLeap(testYear3)));
  82. System.out.println(String.format("Testing %s", testYear4));
  83. System.out.println(String.format("%s", isLeap(testYear4)));
  84. System.out.println(String.format("Testing %s", randomYear));
  85. System.out.println(String.format("%s", isLeap(randomYear)));
  86. System.out.println(String.format("Testing %s", testYear5));
  87. }
  88. public static void ex44() {
  89. String labSection = "4.4";
  90. printSection(labSection);
  91. // System.out.println("------------------------");
  92. // System.out.println("Starting lab section " + labSection);
  93. // System.out.println("------------------------");
  94. String month1 = "oct";
  95. int year1 = 2000;
  96. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  97. month1, year1, monthLen(month1, year1)));
  98. String month2 = "feb";
  99. int year2 = 2000;
  100. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  101. month2, year2, monthLen(month2, year2)));
  102. String month3 = "feb";
  103. int year3 = 1997;
  104. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  105. month3, year3, monthLen(month3, year3)));
  106. String month4 = "apr";
  107. int year4 = 2001;
  108. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  109. month4, year4, monthLen(month4, year4)));
  110. }
  111. /**
  112. * Lab section 4.5
  113. *
  114. * Write a program that can calculate how many whole years old someone is from
  115. * today’s date and their date of birth. You could store each date as three
  116. * integers (day, month, year)
  117. */
  118. public static void ex46() {
  119. String labSection = "4.6";
  120. printSection(labSection);
  121. // calculate whole years' old to date
  122. Calendar today = Calendar.getInstance();
  123. System.out.println("Today:\t" + today.getTime());
  124. Calendar dob1 = Calendar.getInstance();
  125. dob1.set(1990, 1, 1);
  126. System.out.println(String.format(
  127. "DOB 1:\t%s (difference:\t%d years)",
  128. dob1.getTime(),
  129. yearsOld(today, dob1)));
  130. Calendar dob2 = Calendar.getInstance();
  131. dob2.set(1994, 11, 22);
  132. System.out.println(String.format(
  133. "DOB 2:\t%s (difference:\t%d years)",
  134. dob2.getTime(),
  135. yearsOld(today, dob2)));
  136. Calendar dob3 = Calendar.getInstance();
  137. dob3.set(2000, 8, 19); // 23 years old
  138. System.out.println(String.format(
  139. "DOB 3:\t%s (difference:\t%d years)",
  140. dob3.getTime(),
  141. yearsOld(today, dob3)));
  142. Calendar dob4 = Calendar.getInstance();
  143. dob4.set(2000, 8, 20); // 23 years old
  144. System.out.println(String.format(
  145. "DOB 4:\t%s (difference:\t%d years)",
  146. dob4.getTime(),
  147. yearsOld(today, dob4)));
  148. Calendar dob5 = Calendar.getInstance();
  149. dob5.set(2000, 8, 21); // 22 years old
  150. System.out.println(String.format(
  151. "DOB 5:\t%s (difference:\t%d years)",
  152. dob5.getTime(),
  153. yearsOld(today, dob5)));
  154. }
  155. public static boolean isLeap(int year) {
  156. if (year % 4 != 0) {
  157. return false;
  158. } else {
  159. if (year % 100 == 0) {
  160. if (year % 400 == 0) {
  161. return true;
  162. } else {
  163. return false;
  164. }
  165. }
  166. return true;
  167. }
  168. }
  169. public static int monthLen(String month, int year) {
  170. if (month == "feb") {
  171. if (isLeap(year)) {
  172. return 29;
  173. } else {
  174. return 28;
  175. }
  176. } else if (
  177. (month == "apr") ||
  178. (month == "jun") ||
  179. (month == "sep") ||
  180. (month == "nov")) {
  181. return 30;
  182. } else {
  183. return 31;
  184. }
  185. }
  186. public static int yearsOld(Calendar laterDate, Calendar earlierDate)
  187. {
  188. int birthdayModifier = 0; // we assume they've had their birthday
  189. // we could probably do this with one if, checking DAY_OF_YEARs,
  190. // but since problem spec mentioned months and days we should really
  191. // pay attention to those
  192. if (earlierDate.get(Calendar.MONTH) > laterDate.get(Calendar.MONTH)) {
  193. // ie 8 (August) < 9 (September)
  194. birthdayModifier = -1; // not had their birthday yet so take 1 year off difference
  195. } else if (earlierDate.get(Calendar.MONTH) == laterDate.get(Calendar.MONTH)) {
  196. if (earlierDate.get(Calendar.DAY_OF_MONTH) > laterDate.get(Calendar.DAY_OF_MONTH)) {
  197. birthdayModifier = -1;
  198. }
  199. }
  200. return (int) Math.abs(laterDate.get(Calendar.YEAR) -
  201. earlierDate.get(Calendar.YEAR) +
  202. birthdayModifier
  203. );
  204. }
  205. }
  206. /**
  207. * Keyboard input
  208. */
  209. class Lab5 extends Lab {
  210. public Lab5() {
  211. super(5);
  212. // new five1();
  213. // new five5();
  214. // new five6();
  215. new five7();
  216. }
  217. class five1 extends Exercise {
  218. /**
  219. * We will now look at how we can get user input from the keyboard. We will
  220. * gloss over some
  221. * of the details here, but give you just the information you need to start
  222. * getting input and
  223. * processing it.
  224. */
  225. public five1() {
  226. super(1);
  227. Scanner keyboard = new Scanner(System.in);
  228. System.out.println("Please enter your name, followed by the return key?");
  229. String userEntry = keyboard.nextLine();
  230. System.out.println("Hello " + userEntry);
  231. System.out.println(String.format("%s", keyboard));
  232. System.out.println("Please enter your age");
  233. int userAge = keyboard.nextInt();
  234. // keyboard.nextLine();
  235. System.out.println("You are " + userAge + " years old");
  236. System.out.println("What is your favourite colour?");
  237. String col = keyboard.nextLine();
  238. }
  239. }
  240. /**
  241. * Prompt for input and summarise a circle
  242. *
  243. */
  244. class five5 extends Exercise {
  245. public five5() {
  246. super(5);
  247. userCircles();
  248. }
  249. public void userCircles() {
  250. Scanner kbIn = new Scanner(System.in);
  251. System.out.println(String.format("Please supply a radius, that I might describe you a circle"));
  252. double radius = kbIn.nextDouble();
  253. kbIn.nextLine();
  254. summariseCircle(radius);
  255. kbIn.close();
  256. }
  257. public void summariseCircle(double radius) {
  258. System.out.println(String.format("A circle with radius %.2f has circumference %.2f and radius %.2f",
  259. radius, compCircumference(radius), compArea(radius)));
  260. }
  261. public double compCircumference(double radius) {
  262. return (2 * Math.PI * radius);
  263. }
  264. public double compArea(double radius) {
  265. return (Math.PI * Math.pow(radius, 2));
  266. }
  267. }
  268. /**
  269. * {@link Scanner} understanding - actually process input
  270. *
  271. */
  272. class five6 extends Exercise {
  273. public five6() {
  274. super(6);
  275. getNameAndAge();
  276. }
  277. public void getNameAndAge() {
  278. String userName;
  279. int userAge;
  280. Scanner kbIn = new Scanner(System.in);
  281. System.out.println(String.format("Please enter your name and age separated by a space"));
  282. String userInput = kbIn.nextLine();
  283. Scanner inputOps = new Scanner(userInput);
  284. userName = inputOps.next();
  285. // TOASK -- is this what 'add an extra line that fixes this problem' was going for?
  286. while (!inputOps.hasNextInt()) {
  287. userName += " " + inputOps.next();
  288. }
  289. if (inputOps.hasNextInt()) {
  290. userAge = inputOps.nextInt();
  291. } else {
  292. userAge = 0;
  293. }
  294. System.out.println(String.format("Your name is %s and your age is %d",
  295. userName, userAge));
  296. kbIn.close();
  297. inputOps.close();
  298. }
  299. }
  300. /**
  301. * Get DOB from user input and calculate age using {@link Lab4.ex46()}
  302. *
  303. */
  304. class five7 extends Exercise {
  305. public five7() {
  306. super(7);
  307. showUserAge();
  308. }
  309. public void showUserAge() {
  310. Scanner userInput = new Scanner(System.in);
  311. System.out.println(String.format("Please input your DOB"));
  312. System.out.println(String.format("input format is YYYY M D"));
  313. String inputString = userInput.nextLine();
  314. Scanner processInput = new Scanner(inputString);
  315. int dobYear = processInput.nextInt();
  316. int dobMonth = processInput.nextInt();
  317. int dobDay = processInput.nextInt();
  318. // input sanitisation could be nice...
  319. Calendar userDOB = Calendar.getInstance();
  320. // recall months are zero-indexed!
  321. userDOB.set(dobYear, dobMonth-1, dobDay);
  322. Calendar today = Calendar.getInstance();
  323. System.out.println(String.format("Today:\t%s", today.getTime()));
  324. System.out.println(String.format(
  325. "DOB :\t%s (difference: %d years)",
  326. userDOB.getTime(),
  327. Lab4.yearsOld(today, userDOB)));
  328. userInput.close();
  329. processInput.close();
  330. }
  331. }
  332. }
  333. /**
  334. * Loops
  335. */
  336. class Lab6 extends Lab {
  337. public Lab6() {
  338. super(6);
  339. new six1();
  340. System.out.println(String.format("%n"));
  341. new six3();
  342. new six6();
  343. new six7();
  344. new six8();
  345. new six9();
  346. new six5();
  347. }
  348. /**
  349. * Times tables
  350. */
  351. class six1 extends Exercise {
  352. public six1() {
  353. super(1);
  354. int testNum = 6;
  355. System.out.println(String.format("Times table for %d:", testNum));
  356. writeTimesTable(7);
  357. }
  358. }
  359. class six3 extends Exercise {
  360. public six3() {
  361. super(3);
  362. int testNum = 13; // statistically the most prime of all numbers
  363. System.out.println(String.format("Testing primes..."));
  364. System.out.println(String.format("%d prime? %s", testNum, isPrime(testNum)));
  365. System.out.println(String.format("%d prime? %s", 23, isPrime(23)));
  366. System.out.println(String.format("%d prime? %s", 27, isPrime(27)));
  367. System.out.println(String.format("%d prime? %s", 28, isPrime(28)));
  368. System.out.println(String.format("%d prime? %s", 299, isPrime(299)));
  369. System.out.println(String.format("Generating primes..."));
  370. generatePrimes(1024);
  371. System.out.println(String.format("Done!%n"));
  372. }
  373. }
  374. /**
  375. * Number guessing game
  376. * Random number between 0 and 99, user guesses and is told higher / lower / correct
  377. */
  378. class six5 extends Exercise {
  379. private Random random = new Random();
  380. public six5() {
  381. super(5);
  382. System.out.println(String.format("TODO: get stdin working to user can play"));
  383. numberGuessingGame();
  384. // System.out.println(String.format("Testing randomBetween..."));
  385. // int max = 100;
  386. // int min = 50;
  387. // for (int i=0; i<125; i++) {
  388. // System.out.printf("%d ", randomBetween(min, max));
  389. // }
  390. // System.out.println(String.format("done."));
  391. }
  392. public void numberGuessingGame() {
  393. final boolean SUPER_ADVANCED_AI = true;
  394. final int HIGHEST = 99;
  395. final int MAX_GUESSES = 10;
  396. int chosenNumber = random.nextInt(HIGHEST+1);
  397. int guess = random.nextInt(HIGHEST+1);
  398. // super advanced AI section
  399. int maxPossible = HIGHEST+1;
  400. int minPossible = 0;
  401. String guesses = "guess"; // for correct plurals
  402. System.out.printf("Guessing...\t");
  403. for (int i = 0; i < MAX_GUESSES; i++ ) {
  404. System.out.printf("%d", guess);
  405. if (guess == chosenNumber) {
  406. System.out.println(String.format("\nYou guessed right after %d %s! It was indeed %d",
  407. i+1, guesses, chosenNumber));
  408. return;
  409. }
  410. guesses = "guesses"; // didn't get it on the first try
  411. if (guess < chosenNumber) {
  412. System.out.printf("↑\t");
  413. if (guess > minPossible) { minPossible = guess; }
  414. } else {
  415. System.out.printf("↓\t");
  416. if (guess < maxPossible) {
  417. maxPossible = guess;
  418. }
  419. }
  420. // The better approach is to guess halfway between minPossible and maxPossible
  421. // but that's kinda boring and robotic
  422. if (SUPER_ADVANCED_AI) {
  423. guess = randomBetween(minPossible, maxPossible);
  424. }
  425. // TODO change the behaviour so it doesn't guess the same number in a row
  426. }
  427. System.out.println(String.format("\nThe answer was: %d. Better luck next time!", chosenNumber));
  428. }
  429. public int randomBetween(int min, int max) {
  430. if (min < 0) { min = 0; }
  431. if (max == 0) { return 0; }
  432. // System.out.printf(" random.nextInt(%d - %d) + %d ", max, min, min);
  433. return (random.nextInt(max - min) + min);
  434. }
  435. }
  436. /**
  437. * Produce a number triangle
  438. *
  439. * eg:
  440. * 1
  441. * 2 2
  442. * 3 3 3
  443. * 4 4 4 4
  444. */
  445. class six6 extends Exercise {
  446. public six6() {
  447. super(6);
  448. numberTriangle(1);
  449. numberTriangle(4);
  450. numberTriangle(8);
  451. numberTriangle(29);
  452. }
  453. }
  454. class six7 extends Exercise {
  455. public six7() {
  456. super(7);
  457. describeDD(4.2, 1.6);
  458. describeDD(47.42, 7.9);
  459. }
  460. /** Simple exposition of double division
  461. * @param num1 Dividend
  462. * @param num2 Divisor
  463. */
  464. public void describeDD(double num1, double num2) {
  465. System.out.println(String.format("Double division of %.2f by %.2f:\t", num1, num2));
  466. DoubleDivisionResult result = doubleDivision(num1, num2);
  467. System.out.println(String.format("Quotient: %d (ie %.2f ✕ %d = %.2f)\tRemainder: %.2f",
  468. result.getQuotient(),
  469. num2,
  470. result.getQuotient(),
  471. (num2 * result.getQuotient()),
  472. result.getRemainder()));
  473. }
  474. }
  475. /**
  476. * Exercise 8 - String pad with specified character
  477. */
  478. class six8 extends Exercise {
  479. public six8() {
  480. super(8);
  481. describeStringPadding("NoNeed", 4, '!');
  482. describeStringPadding("Simon", 10, '@');
  483. describeStringPadding("Zaphod Beeblebrox", 42, '?');
  484. // describeStringPadding("Unicode", 10, '🤯'); aww
  485. }
  486. /**
  487. * Print for display what we're padding and the result
  488. *
  489. * @param toPad The string to pad eg "Simon"
  490. * @param length Desired length
  491. * @param padChar Character to pad with
  492. */
  493. public void describeStringPadding(String toPad, int paddedLength, char padChar) {
  494. System.out.println(String.format("Padding '%s' to length %d using padding character: %s",
  495. toPad, paddedLength, padChar));
  496. System.out.println(stringLPad(toPad, paddedLength, padChar));
  497. }
  498. /**
  499. * (Left-)pad a string with the specific character
  500. *
  501. * @param toPad The string to pad eg "Simon"
  502. * @param length Desired length
  503. * @param padChar Character to pad with
  504. * @return The padded result
  505. */
  506. public static String stringLPad(String toPad, int paddedLength, char padChar) {
  507. // early out - return unmodified
  508. if (toPad.length() >= paddedLength) {
  509. return toPad;
  510. }
  511. // note that we could do this with String.repeat(), but since this is in the 'Loops'
  512. // part of the lab, we should probably do this with a loop
  513. // String paddedString = String.valueOf(padChar) + toPad;
  514. String paddedString = toPad;
  515. while (paddedString.length() < paddedLength) {
  516. paddedString = String.valueOf(padChar) + paddedString;
  517. }
  518. return paddedString;
  519. }
  520. }
  521. class six9 extends Exercise {
  522. private Random random = new Random(); // initialise once
  523. // see https://stackoverflow.com/a/35277291, to pick a random member of an enum, first convert it to an array
  524. private Throw[] throwsArray = Throw.values(); // can't call it 'throws' for obvious reasons
  525. final Throw randomThrow() {
  526. return throwsArray[random.nextInt(throwsArray.length)];
  527. }
  528. public six9() {
  529. super(9);
  530. System.out.println(String.format("TODO: Take input when we have stdin working"));
  531. Throw player1 = Throw.ROCK; // "Good ol' rock, nothing beats that"
  532. Throw player2 = randomThrow();
  533. playRockPaperScissors(player1, player2);
  534. player2 = randomThrow();
  535. playRockPaperScissors(player1, player2);
  536. player2 = randomThrow();
  537. playRockPaperScissors(player1, player2);
  538. }
  539. }
  540. public void playRockPaperScissors (Throw player1, Throw player2) {
  541. // slight semantic overload with throwing an error but I think that's the term
  542. // for plays in RPS
  543. String result;
  544. // early out - TOASK why this doesn't work when it works in a REPL!!
  545. if (player1 == player2) {
  546. result = "a draw";
  547. } else {
  548. switch (player1) {
  549. case ROCK:
  550. if (player2 == Throw.SCISSORS) { result = "R>S Player 1 wins"; }
  551. else { result = "R<P Player 2 wins"; } // must be paper
  552. break;
  553. case PAPER:
  554. if (player2 == Throw.SCISSORS) { result = "P<S Player 2 wins"; }
  555. else { result = "P>R Player 1 wins"; } // must be rock
  556. break;
  557. case SCISSORS:
  558. if (player2 == Throw.ROCK) { result = "S<R Player 2 wins"; }
  559. else { result = "S>P Player 1 wins"; } // must be paper
  560. break;
  561. default:
  562. result = "undefined behaviour";
  563. break;
  564. }
  565. }
  566. writeRPSPlay(player1, player2, result);
  567. }
  568. public String getThrowName(Throw thrown) {
  569. // This could probably be done in the enum itself
  570. switch (thrown) {
  571. case ROCK: return six8.stringLPad("rock", 8, ' '); // pad to 'scissors' length so everything stays lined-up
  572. case PAPER: return six8.stringLPad("paper", 8, ' ');
  573. case SCISSORS: return "scissors";
  574. default: return "uh?";
  575. }
  576. }
  577. public void writeRPSPlay (Throw player1, Throw player2, String result) {
  578. System.out.printf("Player 1 throws... %s\t\t", getThrowName(player1));
  579. System.out.printf("Player 2 throws... %s\t\t", getThrowName(player2));
  580. System.out.println(String.format("The result is...\t%s", result));
  581. }
  582. enum Throw {
  583. ROCK,
  584. PAPER,
  585. SCISSORS
  586. }
  587. /* TOASK - is there a better way of returning multiple values in Java?
  588. à la python, ie: return (quotient, remainder)
  589. */
  590. /**
  591. * Wrapper class for returning results from double division
  592. */
  593. final class DoubleDivisionResult {
  594. private int quotient; // that's the number of times something goes into something else
  595. private double remainder; // that's the bit left over
  596. public DoubleDivisionResult(int quotient, double remainder) {
  597. this.quotient = quotient;
  598. this.remainder = remainder;
  599. }
  600. public int getQuotient() {
  601. return quotient;
  602. }
  603. public double getRemainder() {
  604. return remainder;
  605. }
  606. }
  607. /**
  608. * Perform 'double division'
  609. *
  610. * @param dividend Number to be divided
  611. * @param divisor Number to divide by
  612. * @return a {@link DoubleDivisionResult} with the (int) quotient and (double) remainder
  613. */
  614. public DoubleDivisionResult doubleDivision(double dividend, double divisor) {
  615. // example used is 4.2 and 1.6
  616. // should return 2 and 1.0
  617. final boolean DEBUG = false;
  618. double dQuotient = dividend / divisor;
  619. int iQuotient = (int)dQuotient;
  620. double remainder = dividend - (iQuotient * divisor);
  621. if (DEBUG) {
  622. System.out.println(String.format("float quotient: %.2f", dQuotient));
  623. System.out.println(String.format("int quotient: %d", iQuotient));
  624. // TOASK: is there a neater way to do this casting?
  625. }
  626. return new DoubleDivisionResult(iQuotient, remainder);
  627. }
  628. /**
  629. * @param num Size of triangle
  630. */
  631. public void numberTriangle(int num) {
  632. /* Observations:
  633. - for even-sized triangles, preceding number of spaces starts at n=rows and decrements
  634. - for even-sized triangles even numbers have odd spaces and vice versa
  635. TODO: handle multi-digit numbers
  636. */
  637. System.out.println(String.format(""));
  638. int spaces = num;
  639. for (int i = 1; i <= num; i++) {
  640. System.out.println(" ".repeat(spaces)
  641. + String.format("%s ", i).repeat(i)
  642. + " ".repeat(spaces-1)
  643. );
  644. spaces--;
  645. }
  646. System.out.println(String.format(""));
  647. }
  648. /**
  649. * @param num The number to write the times table for
  650. * @param limit How many multiples to show
  651. */
  652. public void writeTimesTable(int num, int limit) {
  653. for (int i = 0; i < (limit + 1); i++) {
  654. System.out.printf("%d\t", num * i);
  655. }
  656. }
  657. /* TOASK - overload example.
  658. Question: is there a better way of doing optional function parameters in java?
  659. */
  660. /**
  661. * Write times table for num (10 entries)
  662. * @param num Number to show times table for
  663. */
  664. public void writeTimesTable(int num) {
  665. int DEFAULT_LIMIT = 10;
  666. writeTimesTable(num, DEFAULT_LIMIT);
  667. }
  668. public void generatePrimes(int upperLimit) {
  669. // handle 2 as a special case
  670. System.out.println(String.format("2"));
  671. // for (int i = 3; i <= upperLimit; i += 2) {
  672. for (int i = 2; i <= upperLimit; i++) {
  673. // TODO: figure out smarter way of doing this, I'm sure primes must be at least 6 apart...
  674. // Something about 2n ± 1 ?
  675. // seive of Erasthotenes ..?
  676. if (isPrime(i)) {
  677. System.out.printf("%d\t", i);
  678. }
  679. }
  680. }
  681. /**
  682. * But is it prime?
  683. * @param num Integer to test for primality
  684. * @return if it's prime
  685. */
  686. public boolean isPrime(int num) {
  687. boolean DEBUG = false;
  688. if ((num == 2) || (num == 1)) {
  689. return true;
  690. }
  691. if (num % 2 == 0){
  692. return false;
  693. } else {
  694. int divisor = 3;
  695. double numSquareRoot = Math.sqrt((double)num);
  696. while (divisor <= numSquareRoot) {
  697. if (num % divisor == 0) {
  698. return false;
  699. }
  700. divisor += 2;
  701. if (DEBUG) {
  702. System.out.printf("%d\t", divisor);
  703. }
  704. }
  705. return true;
  706. }
  707. }
  708. }
  709. /**
  710. * String formatting
  711. *
  712. */
  713. class Lab7 extends Lab {
  714. public Lab7() {
  715. super(7);
  716. writeTimesTable(8.24579, 10);
  717. writeTimesTable(93.12279, 22);
  718. writeTimesTable(193.12279, 22);
  719. writeTimesTable(13.9, 122);
  720. }
  721. /**
  722. * @param num The number to write the times table for (0 <= num <= 99)
  723. * @param limit How many multiples to show (0 <= limit <= 99)
  724. */
  725. public void writeTimesTable(double num, int limit) {
  726. final double MINNUM = 0;
  727. final double MAXNUM = 99;
  728. final int MINLIMIT = 0;
  729. final int MAXLIMIT = 99;
  730. if ((num < MINNUM) || (num > MAXNUM)) {
  731. System.err.println(String.format("Please specify a number between %.2f and %.2f (supplied: %.2f)",
  732. MINNUM, MAXNUM, num));
  733. return;
  734. }
  735. if ((limit < MINLIMIT) || (limit > MAXLIMIT)) {
  736. System.err.println(String.format("Please specify a limit between %d and %d (supplied: %d)",
  737. MINLIMIT, MAXLIMIT, limit));
  738. return;
  739. }
  740. for (int i = 0; i < (limit + 1); i++) {
  741. // output specification:
  742. // ~~.~~~ x ~~ = ~~~~~.~~~
  743. System.out.printf("%2.3f x %02d = %5.3f%n", num, i, (num*i));
  744. }
  745. }
  746. }