Write a program that perform exception by programming the Employee class through Generics and by using an ArrayList.
Program / Input:
myException Class:
Generic Class:
Test Class:
Program / Input:
myException Class:
1 2 3 4 5 6 | public class myException extends Exception { myException(String s) { super(s); } } |
Generic Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | package Generic_or_ExceptionHandling; import java.util.Comparator; public class Generic_class<u, v> implements Comparator<Generic_class>, Comparable<Generic_class> { private u id; private v name; public Generic_class() { } public Generic_class(u id, v num) { this.id = id; this.name = num; } public u getId() { return id; } public void setId(u id) { this.id = id; } public v getNum() { return name; } public void setNum(v num) { this.name = num; } @Override //implements by Comparator<Generic_class> public int compare(Generic_class o1, Generic_class o2) { int a1 = (int) o1.id; int a2 = (int) o2.id; return a1 - a2; } // implements Comparable<Generic_class> @Override public int compareTo(Generic_class o) { String a1 = this.name.toString(); String a2 = o.getNum().toString(); int i = a1.compareTo(a2); return i; } public void checkType() throws myException { Boolean b = id instanceof Integer; if (b) { System.out.println("true!!! its string"); } else { throw new myException("Its not String"); } } } |
Test Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | package Generic_or_ExceptionHandling; import java.util.ArrayList; import java.util.Collections; public class test { public static void main(String[] args) throws myException { ArrayList<Generic_class<Integer, String>> arr = new ArrayList<>(); // Generic_class<Integer, String> gc0 = new Generic_class<>(2, "Ahmed"); // Generic_class<Integer, String> gc1 = new Generic_class<>(5, "Nida"); // Generic_class<Integer, String> gc2 = new Generic_class<>(1, "Maria"); // Generic_class<Integer, String> gc3 = new Generic_class<>(3, "Anaya"); // Generic_class<Integer, String> gc4 = new Generic_class<>(4, "Ali"); arr.add(new Generic_class<>(2, "Ahmed")); arr.add(new Generic_class<>(5, "Nida")); arr.add(new Generic_class<>(1, "Maria")); arr.add(new Generic_class<>(3, "Anaya")); arr.add(new Generic_class<>(4, "Ali")); // compareTo Collections.sort(arr); System.out.println("Name \t ID"); for (int i = 0; i < arr.size(); i++) { System.out.println(arr.get(i).getNum() + " " + arr.get(i).getId()); } System.out.println(); System.out.println(); // compare() Collections.sort(arr, new Generic_class()); for (int i = 0; i < arr.size(); i++) { System.out.println(arr.get(i).getNum() + " " + arr.get(i).getId()); } System.out.println("Check its generic class or not???"); Generic_class g = new Generic_class(1, "1"); g.checkType(); // Generic_class obj = new Generic_class("sada", 44); // // ArrayList<Generic_class<String, Integer>> aw = new ArrayList<>(); // try { // obj.CheckType1(); // } catch (Exception e) { // System.out.println(e.getMessage()); // // } } } |
Result/Run:
Name ID Ahmed 2 Ali 4 Anaya 3 Maria 1 Nida 5 Maria 1 Ahmed 2 Anaya 3 Ali 4 Nida 5 Check its generic class or not??? true!!! its string
No comments:
Post a Comment