Exercises from labs 2023-2024
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

209 líneas
6.0 KiB

  1. import java.util.Calendar;
  2. import java.util.concurrent.ThreadLocalRandom;
  3. class runLabs {
  4. public static void main(String[] args) {
  5. new Lab4();
  6. new Lab5();
  7. }
  8. }
  9. class Section {
  10. private int _sectionNumber;
  11. public Section(int s) {
  12. this._sectionNumber = s;
  13. }
  14. public int getSection() {
  15. return _sectionNumber;
  16. }
  17. }
  18. class Lab {
  19. private int _labNumber;
  20. public Section currentSection;
  21. public Section getCurrentSection() {
  22. return currentSection;
  23. }
  24. public void setCurrentSection(Section currentSection) {
  25. this.currentSection = currentSection;
  26. }
  27. public static void printSection(String section) {
  28. String title = String.format("Starting section %s", section);
  29. System.out.println("-".repeat(title.length()));
  30. System.out.println(String.format("%s", title));
  31. System.out.println("-".repeat(title.length()));
  32. }
  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 int getLabNumber() {
  41. return this._labNumber;
  42. }
  43. }
  44. class Lab4 extends Lab {
  45. public Lab4() {
  46. super(4);
  47. ex44();
  48. ex46();
  49. }
  50. // public static void main(String[] args) {
  51. // }
  52. public static void ex41() {
  53. String labSection = "4.1";
  54. // System.out.println(String.format("Starting section %s", labSection));
  55. printSection(labSection);
  56. int testYear1 = 1996; // true
  57. int testYear2 = 1997; // false
  58. int testYear3 = 2000; // true
  59. int testYear4 = 9332; // true
  60. int testYear5 = 1900; // false
  61. int randomYear = ThreadLocalRandom.current().nextInt(1, 9999 + 1);
  62. System.out.println(String.format("Testing %s", testYear1));
  63. System.out.println(String.format("%s", isLeap(testYear1)));
  64. System.out.println(String.format("Testing %s", testYear2));
  65. System.out.println(String.format("%s", isLeap(testYear2)));
  66. System.out.println(String.format("Testing %s", testYear3));
  67. System.out.println(String.format("%s", isLeap(testYear3)));
  68. System.out.println(String.format("Testing %s", testYear4));
  69. System.out.println(String.format("%s", isLeap(testYear4)));
  70. System.out.println(String.format("Testing %s", randomYear));
  71. System.out.println(String.format("%s", isLeap(randomYear)));
  72. System.out.println(String.format("Testing %s", testYear5));
  73. }
  74. public static void ex44() {
  75. String labSection = "4.4";
  76. printSection(labSection);
  77. // System.out.println("------------------------");
  78. // System.out.println("Starting lab section " + labSection);
  79. // System.out.println("------------------------");
  80. String month1 = "oct";
  81. int year1 = 2000;
  82. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  83. month1, year1, monthLen(month1, year1)));
  84. String month2 = "feb";
  85. int year2 = 2000;
  86. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  87. month2, year2, monthLen(month2, year2)));
  88. String month3 = "feb";
  89. int year3 = 1997;
  90. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  91. month3, year3, monthLen(month3, year3)));
  92. String month4 = "apr";
  93. int year4 = 2001;
  94. System.out.println(String.format("Month: %s\tYear: %d\tDays in month: %d",
  95. month4, year4, monthLen(month4, year4)));
  96. }
  97. /**
  98. * Lab section 4.5
  99. *
  100. * Write a program that can calculate how many whole years old someone is from
  101. * today’s date and their date of birth. You could store each date as three
  102. * integers (day,
  103. * month, year)
  104. */
  105. public static void ex46() {
  106. String labSection = "4.6";
  107. printSection(labSection);
  108. // calculate whole years' old to date
  109. Calendar today = Calendar.getInstance();
  110. System.out.println("Today:\t" + today.getTime());
  111. Calendar dob1 = Calendar.getInstance();
  112. dob1.set(1990, 1, 1);
  113. System.out.println(String.format(
  114. "DOB 1:\t%s (difference:\t%d years)",
  115. dob1.getTime(),
  116. yearDifference(today, dob1)));
  117. Calendar dob2 = Calendar.getInstance();
  118. dob2.set(1994, 12, 22);
  119. System.out.println(String.format(
  120. "DOB 2:\t%s (difference:\t%d years)",
  121. dob2.getTime(),
  122. yearDifference(today, dob2)));
  123. }
  124. public static boolean isLeap(int year) {
  125. if (year % 4 != 0) {
  126. return false;
  127. } else {
  128. if (year % 100 == 0) {
  129. if (year % 400 == 0) {
  130. return true;
  131. } else {
  132. return false;
  133. }
  134. }
  135. return true;
  136. }
  137. }
  138. public static int monthLen(String month, int year) {
  139. if (month == "feb") {
  140. if (isLeap(year)) {
  141. return 29;
  142. } else {
  143. return 28;
  144. }
  145. } else if (
  146. (month == "apr") ||
  147. (month == "jun") ||
  148. (month == "sep") ||
  149. (month == "nov")) {
  150. return 30;
  151. } else {
  152. return 31;
  153. }
  154. }
  155. public static int yearDifference(Calendar date1, Calendar date2)
  156. {
  157. return (int) Math.abs(date1.get(Calendar.YEAR) -
  158. date2.get(Calendar.YEAR)
  159. );
  160. }
  161. }
  162. class Lab5 extends Lab {
  163. public Lab5() {
  164. super(5);
  165. System.out.println(String.format("hi"));
  166. }
  167. }