Advertisement
Teammasik

laba6_OOP

Nov 28th, 2023
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. // task 1
  2.  
  3. public class FirstT {
  4.  
  5. public static void main(String[] args) {
  6. String[][] array = {
  7. {"1", "2", "3", "4"},
  8. {"5", "6", "7", "8"},
  9. {"9", "10", "11", "12"},
  10. {"13", "14", "15", "16", "17"}
  11. };
  12.  
  13. try {
  14. int result = sumArrayElements(array);
  15. System.out.println("Сумма всех элементов массива: " + result);
  16. } catch (MyArraySizeException | MyArrayDataException e) {
  17. System.out.println("Ошибка: " + e.getMessage());
  18. }
  19. }
  20.  
  21. public static int sumArrayElements(String[][] array) throws MyArraySizeException, MyArrayDataException {
  22. if (array.length != 4) {
  23. throw new MyArraySizeException("Размер массива должен быть 4x4");
  24. }
  25.  
  26. for (String[] row : array) {
  27. if (row.length != 4) {
  28. throw new MyArraySizeException("Размер массива должен быть 4x4");
  29. }
  30. }
  31.  
  32. int sum = 0;
  33. for (int i = 0; i < array.length; i++) {
  34. for (int j = 0; j < array[i].length; j++) {
  35. try {
  36. sum += Integer.parseInt(array[i][j]);
  37. } catch (NumberFormatException ex) {
  38. throw new MyArrayDataException("Неверные данные в ячейке [" + i + "][" + j + "]");
  39. }
  40. }
  41. }
  42. return sum;
  43. }
  44. }
  45.  
  46. class MyArraySizeException extends Exception {
  47. public MyArraySizeException(String message) {
  48. super(message);
  49. }
  50. }
  51.  
  52. class MyArrayDataException extends Exception {
  53. public MyArrayDataException(String message) {
  54. super(message);
  55. }
  56. }
  57.  
  58.  
  59.  
  60. //------------------------------------------------------------------------
  61. // task 2
  62.  
  63. public class SecondT {
  64. public static void main(String[] args) {
  65. System.out.println(getWorkingHoursLeft(DayOfWeek.TUESDAY));
  66. }
  67.  
  68. public static int getWorkingHoursLeft(DayOfWeek day) {
  69. int totalWorkingHours = 0;
  70. for (DayOfWeek d : DayOfWeek.values()) {
  71. if (d.ordinal() >= day.ordinal()) {
  72. totalWorkingHours += d.getWorkingHours();
  73. }
  74. }
  75. return totalWorkingHours;
  76. }
  77.  
  78. public enum DayOfWeek {
  79. MONDAY(8),
  80. TUESDAY(8),
  81. WEDNESDAY(8),
  82. THURSDAY(8),
  83. FRIDAY(8),
  84. SATURDAY(0),
  85. SUNDAY(0);
  86.  
  87. private final int workingHours;
  88.  
  89. DayOfWeek(int workingHours) {
  90. this.workingHours = workingHours;
  91. }
  92.  
  93. public int getWorkingHours() {
  94. return workingHours;
  95. }
  96. }
  97. }
  98.  
  99. //----------------------------------------------------
  100. // task 3
  101. import java.util.Arrays;
  102.  
  103. public class Main {
  104. static final int size = 10000000;
  105. static final int h = size / 2;
  106. static float[] arr = new float[size];
  107. static float[] a1 = new float[h];
  108. static float[] a2 = new float[h];
  109.  
  110. public static void main(String[] args) {
  111. Arrays.fill(arr, 1);
  112. Arrays.fill(a1, 1);
  113. Arrays.fill(a2, 1);
  114.  
  115. long start = System.currentTimeMillis();
  116. calculateArray();
  117. System.out.println("Single thread: " + (System.currentTimeMillis() - start));
  118.  
  119. Arrays.fill(arr, 1);
  120. start = System.currentTimeMillis();
  121. calculateArrayInTwoThreads();
  122. System.out.println("Two threads: " + (System.currentTimeMillis() - start));
  123. }
  124.  
  125. public static void calculateArray() {
  126. for (int i = 0; i < size; i++) {
  127. arr[i] = (float)(arr[i] * Math.sin(0.2f + i / 5) * Math.cos(0.2f + i / 5) * Math.cos(0.4f + i / 2));
  128. }
  129. }
  130.  
  131. public static void calculateArrayInTwoThreads() {
  132. System.arraycopy(arr, 0, a1, 0, h);
  133. System.arraycopy(arr, h, a2, 0, h);
  134.  
  135. Thread t1 = new Thread(() -> {
  136. for (int i = 0; i < h; i++) {
  137. a1[i] = (float)(a1[i] * Math.sin(0.2f + i / 5) * Math.cos(0.2f + i / 5) * Math.cos(0.4f + i / 2));
  138. }
  139. });
  140.  
  141. Thread t2 = new Thread(() -> {
  142. for (int i = 0; i < h; i++) {
  143. a2[i] = (float)(a2[i] * Math.sin(0.2f + i / 5) * Math.cos(0.2f + i / 5) * Math.cos(0.4f + i / 2));
  144. }
  145. });
  146.  
  147. t1.start();
  148. t2.start();
  149.  
  150. try {
  151. t1.join();
  152. t2.join();
  153. } catch (InterruptedException e) {
  154. e.printStackTrace();
  155. }
  156.  
  157. System.arraycopy(a1, 0, arr, 0, h);
  158. System.arraycopy(a2, 0, arr, h, h);
  159. }
  160. }
  161.  
  162. //------------------------------------------------------------------------------
  163. // task "variant"
  164.  
  165. public class Var_task {
  166. float num1;
  167. float num2;
  168.  
  169. float multi() throws Exception {
  170. if (Float.isInfinite(num1 * num2)) {
  171. throw new Exception("overflow!!!");
  172. }
  173. else{
  174. return(num1 * num2);
  175. }
  176. }
  177.  
  178. public Var_task(float num1, float num2) {
  179. this.num1 = num1;
  180. this.num2 = num2;
  181. }
  182. }
  183.  
  184. class Do{
  185. public static void main(String[] args) throws Exception {
  186. Var_task var1 = new Var_task(1e38f, 1e37f);
  187. //Var_task var1 = new Var_task(22f, 32.5f);
  188.  
  189. try {
  190. System.out.println("answer " + var1.multi());
  191. }
  192. catch (Exception e){
  193. System.out.println("overflow");
  194. }
  195.  
  196. }
  197. }
  198.  
  199.  
  200.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement