update,
This commit is contained in:
16
_resources/it114105/itp4510/Lab02/Lab2.1/q3/Divide.java
Normal file
16
_resources/it114105/itp4510/Lab02/Lab2.1/q3/Divide.java
Normal file
@@ -0,0 +1,16 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class Divide {
|
||||
public static void main(String[] args) {
|
||||
int a, n;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
try{
|
||||
n = (int) (Math.random() * 10);
|
||||
a = 100 / n;
|
||||
System.out.println("n:" + n + ", a:" + a);
|
||||
}catch(ArithmeticException e){
|
||||
System.out.println("Division by Zero");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
|
||||
public class ReadTextFile {
|
||||
public static void main(String[] args) {
|
||||
String filename = "";
|
||||
String line;
|
||||
try {
|
||||
filename = args[0];
|
||||
Scanner fin = new Scanner(new File(filename));
|
||||
while (fin.hasNextLine()) {
|
||||
line = fin.nextLine();
|
||||
System.out.println(line);
|
||||
}
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
System.out.println("Usage: java ReadTextFile <filename>");
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("Failed to open file " + filename);
|
||||
}
|
||||
}
|
||||
}
|
@@ -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)
|
@@ -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);
|
||||
|
||||
}
|
||||
}
|
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
public class NegativeNumberException extends ArithmeticException {
|
||||
public NegativeNumberException() {
|
||||
super("No negative number please!");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user