|
| 1 | +import java.util.InputMismatchException; |
| 2 | +import java.util.Scanner; |
| 3 | +public class Java_Exceptions { |
| 4 | + public static void main(String[] args){ |
| 5 | + // TODO: example_1 |
| 6 | + Scanner scanner = new Scanner(System.in); |
| 7 | + try { |
| 8 | + System.out.print("Enter the first number: "); |
| 9 | + double number1 = scanner.nextDouble(); |
| 10 | + System.out.print("Enter the second number: "); |
| 11 | + double number2 = scanner.nextDouble(); |
| 12 | + div(number1, number2); |
| 13 | + mul(number1, number2); |
| 14 | + } catch (InputMismatchException e) { |
| 15 | + System.err.println("Error: " + e.getMessage()); |
| 16 | + } finally { |
| 17 | + scanner.close(); |
| 18 | + } |
| 19 | + // TODO: example_2 |
| 20 | + try { |
| 21 | + int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; |
| 22 | + System.out.println("Original Matrix:"); |
| 23 | + displayMatrix(matrix); |
| 24 | + transposeInPlace(matrix); |
| 25 | + System.out.println("In-Place Transposed Matrix:"); |
| 26 | + displayMatrix(matrix); |
| 27 | + } catch (IllegalArgumentException e) { |
| 28 | + System.err.println("Error: " + e.getMessage()); |
| 29 | + } |
| 30 | + // TODO: example_3 |
| 31 | + try { |
| 32 | + char[] characters = {'a', 'b', 'c', 'd', 'e'}; |
| 33 | + reverseArray(characters); |
| 34 | + System.out.println("Reversed Array:"); |
| 35 | + for (char ch : characters) { |
| 36 | + System.out.print(ch + " "); |
| 37 | + } |
| 38 | + } catch (IllegalArgumentException e) { |
| 39 | + System.err.println("Error: " + e.getMessage()); |
| 40 | + } |
| 41 | + // TODO: example_4 |
| 42 | + try { |
| 43 | + printPattern("triangle", 5); |
| 44 | + printPattern("inverted_triangle", 5); |
| 45 | + printPattern("pyramid", 5); |
| 46 | + // Uncomment the line below to test an invalid pattern type |
| 47 | + // printPattern("invalid_pattern", 5); |
| 48 | + } catch (IllegalArgumentException e) { |
| 49 | + System.err.println("IllegalArgumentException: " + e.getMessage()); |
| 50 | + } |
| 51 | + }// end of main method |
| 52 | + |
| 53 | + // TODO: example_1 |
| 54 | + public static void div(double number1, double number2) { |
| 55 | + try { |
| 56 | + double result = number1 / number2; |
| 57 | + System.out.println("Result of division: " + result); |
| 58 | + } catch (ArithmeticException e) { |
| 59 | + System.err.println("Error during division: " + e.getMessage()); |
| 60 | + } |
| 61 | + } |
| 62 | + public static void mul(double number1, double number2) { |
| 63 | + try { |
| 64 | + double result = number1 * number2; |
| 65 | + System.out.println("Result of multiplication: " + result); |
| 66 | + } catch (Exception e) { |
| 67 | + System.err.println("Error during multiplication: " + e.getMessage()); |
| 68 | + } |
| 69 | + } |
| 70 | + // TODO: example_2 |
| 71 | + // Transposing a 2D Array Without Creating a New Array using method |
| 72 | + public static void transposeInPlace(int[][] matrix) { |
| 73 | + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { |
| 74 | + throw new IllegalArgumentException("Matrix must be non-null and non-empty."); |
| 75 | + } |
| 76 | + for (int i = 0; i < matrix.length; i++) { |
| 77 | + for (int j = i + 1; j < matrix[i].length; j++) { |
| 78 | + int temp = matrix[i][j]; |
| 79 | + matrix[i][j] = matrix[j][i]; |
| 80 | + matrix[j][i] = temp; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + public static void displayMatrix(int[][] matrix) { |
| 85 | + for (int i = 0; i < matrix.length; i++) { |
| 86 | + for (int j = 0; j < matrix[i].length; j++) { |
| 87 | + System.out.print(matrix[i][j] + " "); |
| 88 | + } |
| 89 | + System.out.println(); |
| 90 | + } |
| 91 | + } |
| 92 | + // TODO: example_3 |
| 93 | + // Reversing an Array |
| 94 | + public static void reverseArray(char[] array) { |
| 95 | + if (array == null || array.length == 0) { |
| 96 | + throw new IllegalArgumentException("Array must be non-null and non-empty."); |
| 97 | + } |
| 98 | + int start = 0; |
| 99 | + int end = array.length - 1; |
| 100 | + while (start < end) { |
| 101 | + // Swap elements at start and end indices |
| 102 | + char temp = array[start]; |
| 103 | + array[start] = array[end]; |
| 104 | + array[end] = temp; |
| 105 | + // Move indices towards the center |
| 106 | + start++; |
| 107 | + end--; |
| 108 | + } |
| 109 | + } |
| 110 | + // TODO: example_4 |
| 111 | + // print patterns |
| 112 | + private static void printPattern(String patternType, int size) { |
| 113 | + if (size <= 0) { |
| 114 | + throw new IllegalArgumentException("Size must be a positive integer."); |
| 115 | + } |
| 116 | + switch (patternType.toLowerCase()) { |
| 117 | + case "triangle": |
| 118 | + printRightAngledTriangle(size); |
| 119 | + break; |
| 120 | + case "inverted_triangle": |
| 121 | + printInvertedRightAngledTriangle(size); |
| 122 | + break; |
| 123 | + case "pyramid": |
| 124 | + printPyramid(size); |
| 125 | + break; |
| 126 | + default: |
| 127 | + throw new IllegalArgumentException("Invalid pattern type: " + patternType); |
| 128 | + } |
| 129 | + } |
| 130 | + private static void printRightAngledTriangle(int size) { |
| 131 | + for (int i = 1; i <= size; i++) { |
| 132 | + for (int j = 1; j <= i; j++) { |
| 133 | + System.out.print("* "); |
| 134 | + } |
| 135 | + System.out.println(); |
| 136 | + } |
| 137 | + System.out.println(); |
| 138 | + } |
| 139 | + private static void printInvertedRightAngledTriangle(int size) { |
| 140 | + for (int i = size; i >= 1; i--) { |
| 141 | + for (int j = 1; j <= i; j++) { |
| 142 | + System.out.print("* "); |
| 143 | + } |
| 144 | + System.out.println(); |
| 145 | + } |
| 146 | + System.out.println(); |
| 147 | + } |
| 148 | + private static void printPyramid(int size) { |
| 149 | + for (int i = 1; i <= size; i++) { |
| 150 | + for (int j = 1; j <= size - i; j++) { |
| 151 | + System.out.print(" "); |
| 152 | + } |
| 153 | + for (int k = 1; k <= 2 * i - 1; k++) { |
| 154 | + System.out.print("* "); |
| 155 | + } |
| 156 | + System.out.println(); |
| 157 | + } |
| 158 | + System.out.println(); |
| 159 | + } |
| 160 | +} |
0 commit comments