-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerics_Array.java
161 lines (132 loc) · 4.17 KB
/
Generics_Array.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.util.*;
//template class T
class Template<T> {
// method to check palindrome of string or integer
void isPalindrome(T[] t) {
int pal = 0;
for (int i = 0; i < t.length; i++) {
// create string buffer for string temp
String temp = t[i].toString();
StringBuffer s = new StringBuffer(temp);
// compare string to its reverse
if (t[i].equals(s.reverse().toString())) {
System.out.println(t[i] + " is a PALINDROME");
pal += 1; // increment no of palindrome
} else {
System.out.println(t[i] + " is a NOT A PALINDROME");
}
}
// ANALYSIS
System.out.println("\n-------------PALINDROME ANALYSIS-------------");
System.out.println("Total palindrome strings are:" + pal);
System.out.println("Total non-palindrome strings are:" + (t.length - pal));
System.out.println("---------------------------------------------");
}
// method to check odd or even
void OddEven(T[] t) {
int even = 0;
for (int i = 0; i < t.length; i++) {
// even condition
if ((int) t[i] % 2 == 0) {
System.out.println(t[i] + " is EVEN");
even++; // increment no of even numbers
} else {
System.out.println(t[i] + " is ODD");
}
}
// ANALYSIS
System.out.println("\n------------- ODD-EVEN ANALYSIS-------------");
System.out.println("Total even numbers:" + even);
System.out.println("Total odd numbers:" + (t.length - even));
System.out.println("---------------------------------------------");
}
// method to check prime or composite
void isPrime(T[] t) {
boolean flag = false;
int prime = 0;
for (int i = 0; i < t.length; i++) {
flag = false;
// composite number condition
for (int j = 2; j < (int) t[i]; j++) {
if ((int) t[i] % j == 0) {
flag = true;
break;
}
}
if (!flag) {
System.out.println(t[i] + " is PRIME");
prime++; // increment no of prime numbers
} else {
System.out.println(t[i] + " is COMPOSITE");
}
}
// ANALYSIS
System.out.println("\n------------- PRIME-COMPOSITE ANALYSIS-------------");
System.out.println("Total prime numbers:" + prime);
System.out.println("Total composite numbers:" + (t.length - prime));
System.out.println("---------------------------------------------");
}
}// class end
public class Generics_Array {
public static void main(String[] args) {
// TODO Auto-generated method stub
while (true) {
System.out.println("\nEnter the type for which you want yo use template?");
System.out.println("1)STRING \n2)INTEGER \n3)EXIT");
Scanner sc = new Scanner(System.in);
System.out.println("\nEnter your choice:");
int opn = sc.nextInt();
switch (opn) {
case 1:
Template<String> T1 = new Template<String>(); // STRING template
System.out.println("Enter the number of strings:");
int no_strings = sc.nextInt();
// input for strings
String s[] = new String[no_strings];
for (int i = 0; i < no_strings; i++) {
System.out.print("Enter the string " + (i + 1) + "::");
s[i] = sc.next();
}
T1.isPalindrome(s);
break;
case 2:
Template<Integer> T2 = new Template<Integer>(); // INTEGER template
System.out.println("Enter the number of integrs:");
int no_int = sc.nextInt();
// input for integers
Integer x[] = new Integer[no_int];
for (int j = 0; j < no_int; j++) {
System.out.print("Enter the integer " + (j + 1) + "::");
x[j] = sc.nextInt();
}
boolean flag = true;
int ch = 0;
do {
System.out.println("\nEnter the operation \n1.ODD-EVEN\n2.PRIME-COMPOSITE ");
System.out.println("Enter the choice:");
ch = sc.nextInt();
if (ch == 1) {
T2.OddEven(x); // odd-even method call
}
if (ch == 2) {
T2.isPrime(x); // prime-composite method call
flag = false; // to go out of do loop
}
} while (flag == true); // end do
break;
case 3:
System.out.println("Are you sure you want to exit? \n1.YES\n2.NO");
int p = sc.nextInt();
if (p == 1) {
System.out.println("THANK YOU!!!"); // exit the program
return;
} else {
break; // go for options
}
default:
System.out.println("Enter valid choice!!"); // for invalid choice
break;
}
}
}
}// end function