|
| 1 | +/*AARTI RATHI |
| 2 | +My website - https://aartirathi17.herokuapp.com/ */ |
| 3 | + |
| 4 | +/*Problem Statement : |
| 5 | +Implement a generic program using any collection class to count the number of elements in a collection that have a specific property such as even numbers, odd number, prime number and palindromes. */ |
| 6 | + |
| 7 | +---------------------------------------------------------------------------------------------------------------- |
| 8 | + |
| 9 | +package assign7 |
| 10 | +import java.util.*; |
| 11 | +import java.lang.*; |
| 12 | +import java.io.*; |
| 13 | +class assign_7 |
| 14 | +{ |
| 15 | + static int count = 0; //COUNT VARIABLE |
| 16 | + |
| 17 | + //FUNCTION TO CHECK PALINDROME |
| 18 | + static void check_palindrome(String x) |
| 19 | + { |
| 20 | + StringBuilder s1 = new StringBuilder(x); |
| 21 | + if(x.equals(s1.reverse().toString())) |
| 22 | + { |
| 23 | + System.out.println(x+" is a Palindrome"); |
| 24 | + count += 1; //count the number of palindromes |
| 25 | +} |
| 26 | +else |
| 27 | +{ |
| 28 | + System.out.println(x+" is not a Palindrome"); |
| 29 | +} |
| 30 | +} |
| 31 | + //FUNCTION TO CHECK EVEN OR ODD |
| 32 | +static void even_odd(int x) |
| 33 | +{ |
| 34 | + if(x % 2 == 0) |
| 35 | + { |
| 36 | + System.out.println(x+" IS EVEN"); |
| 37 | + count += 1; //count the number of even numbers |
| 38 | +} |
| 39 | +else |
| 40 | +{ |
| 41 | + System.out.println(x+" IS ODD"); |
| 42 | +} |
| 43 | +} |
| 44 | + //FUNCION TO CHECK PRIME NUMBER |
| 45 | +static void prime(int x) |
| 46 | +{ |
| 47 | + boolean flag = false; |
| 48 | + for(int i = 2; i <= x/2; ++i) |
| 49 | + { |
| 50 | + if(x % i == 0) |
| 51 | + { |
| 52 | + flag = true; |
| 53 | + break; |
| 54 | + } |
| 55 | + } |
| 56 | + if (!flag) |
| 57 | + { |
| 58 | + System.out.println(x + " is a prime number."); |
| 59 | + count += 1; //count the number of prime numbers |
| 60 | +} |
| 61 | +else |
| 62 | +{ |
| 63 | + System.out.println(x + " is not a prime number."); |
| 64 | +} |
| 65 | +} |
| 66 | +//FUNCTION TO DECIDE WHICH FUNCTION TO CHECK |
| 67 | +static void check(int ch,int x) |
| 68 | +{ |
| 69 | + switch(ch) |
| 70 | + { |
| 71 | + case 1: |
| 72 | + even_odd(x); //call even_odd fucntion for number x |
| 73 | + break; |
| 74 | + case 2: |
| 75 | + prime(x); //call prime fucntion for number x |
| 76 | + break; |
| 77 | + default: |
| 78 | + System.out.println("ENTER CORRECT OPTION"); |
| 79 | +} |
| 80 | + } //FUNCTION FOR INTEGER ARRAY |
| 81 | + static void number_op() |
| 82 | + { |
| 83 | + int element,n,choice; |
| 84 | + |
| 85 | + Scanner sc = new Scanner(System.in); |
| 86 | + |
| 87 | + //ArrayList from Collection Interface |
| 88 | + //Integer type |
| 89 | + ArrayList<Integer> nums = new ArrayList<Integer>(); |
| 90 | + |
| 91 | + System.out.println("Enter the number of elements:"); |
| 92 | + n = sc.nextInt(); |
| 93 | + |
| 94 | + |
| 95 | + for(int i=0;i<n;i++) |
| 96 | + { |
| 97 | + System.out.println("Enter the element:"+(i+1)); |
| 98 | + element = sc.nextInt(); |
| 99 | + nums.add(element); //Add elements to the ArrayList |
| 100 | +} |
| 101 | +System.out.println("Enter the Operation to be performed:"); |
| 102 | +System.out.println("1. ODD or EVEN?"); |
| 103 | +System.out.println("2. CHECK PRIME ?"); |
| 104 | +choice = sc.nextInt(); |
| 105 | + |
| 106 | + Iterator itr = nums.iterator(); //Iterator from the COLLECTION interface |
| 107 | + count = 0; |
| 108 | + while(itr.hasNext()) //Loop till there are elements in the ArrayList |
| 109 | + { |
| 110 | + |
| 111 | + check(choice,(int)itr.next()); //call the check function for each element |
| 112 | +} |
| 113 | + |
| 114 | + //Give the Count |
| 115 | +if(choice == 1) |
| 116 | +{ |
| 117 | + System.out.println("The number of EVEN numbers is: "+ count); |
| 118 | + System.out.println("The number of ODD numbers is: "+ (nums.size()-count)); |
| 119 | +} |
| 120 | +else |
| 121 | +{ |
| 122 | + System.out.println("The number of PRIME numbers is: "+ count); |
| 123 | + System.out.println("The number of Non-PRIME numbers is: "+ (nums.size()-count)); |
| 124 | +} |
| 125 | +} |
| 126 | + //FUNCTION FOR STRING ARRAY |
| 127 | +static void string_op() |
| 128 | +{ |
| 129 | + int n; |
| 130 | + String word; |
| 131 | + |
| 132 | + //ArrayList from COLLECTION interface |
| 133 | + //String type |
| 134 | + ArrayList<String> words = new ArrayList<String>(); |
| 135 | + |
| 136 | + Scanner sc = new Scanner(System.in); |
| 137 | + System.out.println("Enter the number of strings?"); |
| 138 | + n = sc.nextInt(); |
| 139 | + |
| 140 | + for(int i=0;i<n;i++) |
| 141 | + { |
| 142 | + System.out.println("Enter string: "+(i+1)); |
| 143 | + word = sc.next(); |
| 144 | + words.add(word); //Add elements to the ArrayList |
| 145 | +} |
| 146 | +count = 0; |
| 147 | + for(String w:words) //Loop the ArrayList |
| 148 | + { |
| 149 | + check_palindrome(w); |
| 150 | + } |
| 151 | + |
| 152 | + System.out.println("The number of PALINDROMES is: "+ count); |
| 153 | +} |
| 154 | +public static void main(String[] args) |
| 155 | +{ |
| 156 | + Scanner sc = new Scanner(System.in); |
| 157 | + int ch; |
| 158 | + //Choose the type of List needed |
| 159 | + |
| 160 | + |
| 161 | + do |
| 162 | + { |
| 163 | + System.out.println("[1] String"); |
| 164 | + System.out.println("[2] Integer"); |
| 165 | + System.out.println("Choose Type: (enter 0 to exit)"); |
| 166 | + ch = sc.nextInt(); |
| 167 | + |
| 168 | + if(ch==1) |
| 169 | + { |
| 170 | + string_op(); //Calls String arraylist |
| 171 | +} |
| 172 | + |
| 173 | +else if(ch == 2) |
| 174 | +{ |
| 175 | + number_op(); //Calls Interger arraylist |
| 176 | +} |
| 177 | +else |
| 178 | +{ |
| 179 | + System.out.print("\nINVALID INPUT!!"); |
| 180 | +} |
| 181 | +} |
| 182 | +while(ch!=0); |
| 183 | +System.out.print("\nOPERATION COMPLETED!!! Thank you :) "); |
| 184 | + |
| 185 | +} |
| 186 | + |
| 187 | +} |
0 commit comments