This commit is contained in:
louiscklaw
2025-01-31 19:15:17 +08:00
parent 09adae8c8e
commit 6c60a73f30
1546 changed files with 286918 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
#### Question 1
| Exception class | IOException/ RuntimeException |
|--------------------------------|-------------------------------|
| ArithmeticException | RuntimeException |
| EOFException | IOException |
| FileNotFoundException | IOException |
| ArrayIndexOutOfBoundsException | RuntimeException |
| NumberFormatException | RuntimeException |
| InputMismatchException | RuntimeException |
| NUllPointerExpcetion | RuntimeExpcetion |
#### Question 2
(a) NullPointerException
(b) ArrayIndexOutOfBoundsException
(c) ArithmeticException(divide by zero)

View File

@@ -0,0 +1,50 @@
import javax.swing.JOptionPane;
public class InputParsing {
public static void main(String[] args) {
String input;
int num = 0;
int total = 0;
int average = 0;
int[] a = { 80, 60, 72, 85, 90 };
String output;
output = "The 5 marks are:";
for (int i = 0; i < 5; i++)
output += " " + a[i];
output += "\nAverage of how many numbers?";
input = JOptionPane.showInputDialog(output);
try {
System.out.println("Input length = " + input.length());
num = Integer.parseInt(input);
if (num == 0)
throw new ArithmeticException();
if (num == -1)
throw new NegativeNumberException();
total = 0;
for (int i = 0; i < num; i++)
total += a[i];
average = total / num;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("No more than 5 please!");
} catch (NumberFormatException e) {
System.out.println("Integer please!");
} catch (NullPointerException e) {
System.out.println("Input length = 0; cancelled");
} catch (NegativeNumberException e) {
System.out.println(e.getMessage());
} catch (ArithmeticException e) {
System.out.println("Don't input zero!");
} catch (RuntimeException e) {
System.out.println("Run time error!");
e.printStackTrace();
return;
} catch (Exception e) {
System.out.println("Something wrong!");
e.printStackTrace();
} finally {
System.out.println("Number = " + num);
}
System.out.println("Average over first " + num + " numbers = " + average);
}
}

View File

@@ -0,0 +1,66 @@
import javax.swing.JOptionPane;
public class InputParsing {
public static void main(String[] args) {
String input;
int[] a = { 80, 60, 72, 85, 90 };
String output;
output = "The 5 marks are:";
for (int i = 0; i < 5; i++)
output += " " + a[i];
output += "\nAverage of how many numbers?";
do {
input = JOptionPane.showInputDialog(output);
try {
parseInput(a, input);
} catch (Exception e) {
System.out.println("Number should be in 1 to 5!");
}
} while (JOptionPane.showConfirmDialog(null, "Enter again?", "elect an option",
JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION);
}
public static void parseInput(int[] a, String input) {
int num = 0;
int total = 0;
int average = 0;
try {
System.out.println("Input length = " + input.length());
num = Integer.parseInt(input);
if (num == 0)
throw new ArithmeticException();
if (num == -1)
throw new NegativeNumberException();
total = 0;
for (int i = 0; i < num; i++)
total += a[i];
average = total / num;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("No more than 5 please!");
throw e;
} catch (NumberFormatException e) {
System.out.println("Integer please!");
} catch (NullPointerException e) {
System.out.println("Input length = 0; cancelled");
} catch (NegativeNumberException e) {
System.out.println(e.getMessage());
throw e;
} catch (ArithmeticException e) {
System.out.println("Don't input zero!");
throw e;
} catch (RuntimeException e) {
System.out.println("Run time error!");
e.printStackTrace();
return;
} catch (Exception e) {
System.out.println("Something wrong!");
e.printStackTrace();
} finally {
System.out.println("Number = " + num);
}
System.out.println("Average over first " + num + " numbers = " + average);
}
}

View File

@@ -0,0 +1,5 @@
public class NegativeNumberException extends ArithmeticException {
public NegativeNumberException() {
super("No negative number please!");
}
}