Advertisement
Guest User

Prog7b

a guest
Apr 27th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. public class Prog7b {
  2.  
  3. // @SuppressWarnings({ "unchecked", "rawtypes" })
  4. public static void main(String[] args) {
  5.  
  6. if (args[0].matches("I|i")) {
  7. Integer[] array = new Integer[Integer.parseInt(args[1])];
  8. for (int i = 2; i < args.length - 1; i++) {
  9. array[i - 2] = new Integer(args[i]);
  10. }
  11. Integer key = new Integer(args[args.length - 1]);
  12. if (linearSearch(array, key) < 0) {
  13. System.out.println("Didnt find it");
  14. } else
  15. System.out.println("Found it at index: " + (linearSearch(array, key) - 1));
  16. System.out.println("The max of the array is: " + max(array));
  17. print(array);
  18. } else if (args[0].matches("S|s")) {
  19. String[] array = new String[Integer.parseInt(args[1])];
  20. for (int i = 2; i < args.length - 1; i++) {
  21. array[i - 2] = new String(args[i]);
  22. }
  23. String key = new String(args[args.length - 1]);
  24. if (linearSearch(array, key) < 0) {
  25. System.out.println("Didnt find it");
  26. } else
  27. System.out.println("Found it at index: " + (linearSearch(array, key) - 1));
  28. System.out.println("The max of the array is: " + max(array));
  29. print(array);
  30. } else {
  31. Double[] array = new Double[Integer.parseInt(args[1])];
  32. for (int i = 2; i < args.length - 1; i++) {
  33. array[i - 2] = new Double(args[i]);
  34. }
  35. Double key = new Double(args[args.length - 1]);
  36. if (linearSearch(array, key) < 0) {
  37. System.out.println("Didnt find it");
  38. } else
  39. System.out.println("Found it at index: " + (linearSearch(array, key) - 1));
  40. System.out.println("The max of the array is: " + max(array));
  41. print(array);
  42. }
  43. }
  44.  
  45. public static <E extends Comparable<E>> int linearSearch(E[] array, E key) {
  46. int index = 0;
  47. for (int i = 0; i < array.length; i++) {
  48. index++;
  49. if (array[i].equals(key)) {
  50. return index;
  51. }
  52. }
  53. return -1;
  54. }
  55.  
  56. public static <E extends Comparable<E>> E max(E[] list) {
  57. E max = list[0];
  58. for (int i = 1; i < list.length; i++) {
  59. if (max.compareTo(list[i]) < 0) {
  60. max = list[i];
  61. }
  62. }
  63. return max;
  64. }
  65.  
  66. private static <E> void print(E[] list) {
  67. System.out.print("[");
  68. for (int i = 0; i < list.length - 1; i++)
  69. System.out.print(list[i] + ", ");
  70. System.out.print(list[list.length - 1] + "]\n");
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement