P1-P5: rewrite to match current syllabus
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
43
P5.java
43
P5.java
@@ -1,20 +1,27 @@
|
||||
import java.util.function.Consumer;
|
||||
|
||||
class P5 {
|
||||
public static void main(String[] _args) {
|
||||
int sub[] = { 64, 91, 45 };
|
||||
P5 obj = new P5();
|
||||
int sum = 0;
|
||||
|
||||
for (int i = 0; i < sub.length; i++) {
|
||||
obj.grade(sub[i], i + 1);
|
||||
sum += sub[i];
|
||||
}
|
||||
|
||||
System.out.println("Overall Percentage: " + (float) sum / 3 + "%");
|
||||
}
|
||||
|
||||
void grade(int n, int s) {
|
||||
System.out.println( "Subject " + s + " grade: " + (char)( n >= 90 ? 'A' : n < 50 ? 'F' : 'A' + (9 - n / 10)));
|
||||
class MyException extends Exception {
|
||||
public MyException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
public class P5 {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
int result = divide(10, 0);
|
||||
System.out.println("Result: " + result);
|
||||
} catch (MyException e) {
|
||||
System.out.println("My Exception: " + e.getMessage());
|
||||
} catch (ArithmeticException e) {
|
||||
System.out.println("Arithmetic Exception: " + e.getMessage());
|
||||
} finally {
|
||||
System.out.println("Finally block executed.");
|
||||
}
|
||||
}
|
||||
|
||||
public static int divide(int num1, int num2) throws MyException {
|
||||
if (num2 == 0) {
|
||||
throw new MyException("Division by zero not allowed.");
|
||||
}
|
||||
return num1 / num2;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user