package Reflection7DynamicLoading; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; /** * CPSC 501 * Code that will dynamically load a sort to use for list * Best example would be a sort in another jar included into classpath * * @author Jonathan Hudson */ public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); Random r = new Random(12345); List list = new ArrayList<>(); System.out.println("Enter an array size:"); int size = s.nextInt(); for (int i = 0; i < size; i++) { list.add(r.nextInt(size)); } s.nextLine(); System.out.println("Enter the sort class (including package path):"); String sort_type = s.nextLine(); try { Class sort_class = Class.forName(sort_type); Object obj = sort_class.newInstance(); Sort sort = (Sort) obj; sort.sort(list); System.out.println(list); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } }