Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // task 1
- public class FirstT {
- public static void main(String[] args) {
- String[][] array = {
- {"1", "2", "3", "4"},
- {"5", "6", "7", "8"},
- {"9", "10", "11", "12"},
- {"13", "14", "15", "16", "17"}
- };
- try {
- int result = sumArrayElements(array);
- System.out.println("Сумма всех элементов массива: " + result);
- } catch (MyArraySizeException | MyArrayDataException e) {
- System.out.println("Ошибка: " + e.getMessage());
- }
- }
- public static int sumArrayElements(String[][] array) throws MyArraySizeException, MyArrayDataException {
- if (array.length != 4) {
- throw new MyArraySizeException("Размер массива должен быть 4x4");
- }
- for (String[] row : array) {
- if (row.length != 4) {
- throw new MyArraySizeException("Размер массива должен быть 4x4");
- }
- }
- int sum = 0;
- for (int i = 0; i < array.length; i++) {
- for (int j = 0; j < array[i].length; j++) {
- try {
- sum += Integer.parseInt(array[i][j]);
- } catch (NumberFormatException ex) {
- throw new MyArrayDataException("Неверные данные в ячейке [" + i + "][" + j + "]");
- }
- }
- }
- return sum;
- }
- }
- class MyArraySizeException extends Exception {
- public MyArraySizeException(String message) {
- super(message);
- }
- }
- class MyArrayDataException extends Exception {
- public MyArrayDataException(String message) {
- super(message);
- }
- }
- //------------------------------------------------------------------------
- // task 2
- public class SecondT {
- public static void main(String[] args) {
- System.out.println(getWorkingHoursLeft(DayOfWeek.TUESDAY));
- }
- public static int getWorkingHoursLeft(DayOfWeek day) {
- int totalWorkingHours = 0;
- for (DayOfWeek d : DayOfWeek.values()) {
- if (d.ordinal() >= day.ordinal()) {
- totalWorkingHours += d.getWorkingHours();
- }
- }
- return totalWorkingHours;
- }
- public enum DayOfWeek {
- MONDAY(8),
- TUESDAY(8),
- WEDNESDAY(8),
- THURSDAY(8),
- FRIDAY(8),
- SATURDAY(0),
- SUNDAY(0);
- private final int workingHours;
- DayOfWeek(int workingHours) {
- this.workingHours = workingHours;
- }
- public int getWorkingHours() {
- return workingHours;
- }
- }
- }
- //----------------------------------------------------
- // task 3
- import java.util.Arrays;
- public class Main {
- static final int size = 10000000;
- static final int h = size / 2;
- static float[] arr = new float[size];
- static float[] a1 = new float[h];
- static float[] a2 = new float[h];
- public static void main(String[] args) {
- Arrays.fill(arr, 1);
- Arrays.fill(a1, 1);
- Arrays.fill(a2, 1);
- long start = System.currentTimeMillis();
- calculateArray();
- System.out.println("Single thread: " + (System.currentTimeMillis() - start));
- Arrays.fill(arr, 1);
- start = System.currentTimeMillis();
- calculateArrayInTwoThreads();
- System.out.println("Two threads: " + (System.currentTimeMillis() - start));
- }
- public static void calculateArray() {
- for (int i = 0; i < size; i++) {
- arr[i] = (float)(arr[i] * Math.sin(0.2f + i / 5) * Math.cos(0.2f + i / 5) * Math.cos(0.4f + i / 2));
- }
- }
- public static void calculateArrayInTwoThreads() {
- System.arraycopy(arr, 0, a1, 0, h);
- System.arraycopy(arr, h, a2, 0, h);
- Thread t1 = new Thread(() -> {
- for (int i = 0; i < h; i++) {
- a1[i] = (float)(a1[i] * Math.sin(0.2f + i / 5) * Math.cos(0.2f + i / 5) * Math.cos(0.4f + i / 2));
- }
- });
- Thread t2 = new Thread(() -> {
- for (int i = 0; i < h; i++) {
- a2[i] = (float)(a2[i] * Math.sin(0.2f + i / 5) * Math.cos(0.2f + i / 5) * Math.cos(0.4f + i / 2));
- }
- });
- t1.start();
- t2.start();
- try {
- t1.join();
- t2.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.arraycopy(a1, 0, arr, 0, h);
- System.arraycopy(a2, 0, arr, h, h);
- }
- }
- //------------------------------------------------------------------------------
- // task "variant"
- public class Var_task {
- float num1;
- float num2;
- float multi() throws Exception {
- if (Float.isInfinite(num1 * num2)) {
- throw new Exception("overflow!!!");
- }
- else{
- return(num1 * num2);
- }
- }
- public Var_task(float num1, float num2) {
- this.num1 = num1;
- this.num2 = num2;
- }
- }
- class Do{
- public static void main(String[] args) throws Exception {
- Var_task var1 = new Var_task(1e38f, 1e37f);
- //Var_task var1 = new Var_task(22f, 32.5f);
- try {
- System.out.println("answer " + var1.multi());
- }
- catch (Exception e){
- System.out.println("overflow");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement