0% found this document useful (0 votes)
2 views7 pages

Java Generics Demo

Uploaded by

carismamarkyuan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Java Generics Demo

Uploaded by

carismamarkyuan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Mark Yuan P.

Carisma BSCS – 1A

The Code:

import [Link].*;

import [Link].*;

import [Link];

import [Link];

import [Link].*;

import [Link].*;

import [Link];

import [Link];

public class JavaGenericsDemo {

public static <T> T[] convert(int[] primitiveArray, Class<T> clazz) {

if (primitiveArray == null) {

throw new IllegalArgumentException("Input array cannot be null");

}
if (clazz == null) {

throw new IllegalArgumentException("Class type cannot be null");

@SuppressWarnings("unchecked")

T[] result = (T[]) [Link](clazz, [Link]);

for (int i = 0; i < [Link]; i++) {

result[i] = [Link]((Object) primitiveArray[i]);

return result;

}
public static double Average(int[] a) {

int Sum = 0;

for (Integer n : a) {

Sum += n;

return (Sum / [Link]);

public static void main(String[] args) throws IOException, InterruptedException {

BufferedReader bf = new BufferedReader(new InputStreamReader([Link]));

//Gene<Integer> i = new Gene<>();

String text, nums[], names[], charRegEx, nameRegEx;

int noel;

boolean repeat = false;

//char charArr[];

new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();

try {

do {

[Link]("Type at least seven (7) integers separated by punctuation


marks or common value separators : \n\t");

text = [Link]();

nums = [Link]("[,.;: \\|\\?\\<\\>\\(\\)\\[\\]\\{\\}/&]+");

noel = [Link];

[Link]([Link](nums));

[Link]("\tNumber of Elements is %d\n", noel);

if (noel < 7) {

[Link]("The integers you entered are fewer than the minimum


requirement.\n");
repeat = true;

} else {
repeat = false;

} while (repeat);

int[] arr = new int[noel];

for (int cnt = 0; cnt < noel; cnt++) {

try {

arr[cnt] = [Link](nums[cnt]);

} catch (NumberFormatException e) {

[Link]("%s is not a valid number!", nums[cnt]);

Integer[] b = convert(arr, [Link]);

[Link]("\n\tTheir Sorted Version :");

sort_generics(b);

[Link]("\tTheir Average :%.2f\n\n", Average(arr));

} catch (IndexOutOfBoundsException e) {

[Link]("Error has been found: %s\n\n", [Link]());

try {
do {

[Link]("Type exactly 10 of your classmates by their first names


separated by pipes : \n\t");

text = [Link]();

names = [Link]("[ \\|]+");

nameRegEx = "[A-Z a-z.\\|]+";

[Link]([Link](names));

[Link]("\tNumber of Elements is %d\n", [Link]);

if ([Link] != 10 || ![Link](nameRegEx)) {

repeat = true;
[Link]("Names entered aren't exactly 10 or names contain
foreign characters ...\n");

} else {

repeat = false;

} while (repeat);

[Link]("\n\tTheir Sorted Version : ");


sort_generics(names);

} catch (IndexOutOfBoundsException e) {

[Link]("Error has been found: %s\n\n", [Link]());

try {

do {

[Link]("Type exactly a dozen consonants in the English alphabet


with no space or separator between :\n\t");

text = [Link]().trim();

charRegEx = "[^AEIOUaeiou]{12}";

if (![Link](charRegEx)) {

[Link]("You didn't follow the instruction. Try again ...\n");

repeat = true;

} else {

repeat = false;

} while (repeat);

noel = [Link]();
Character[] charArr = new Character[noel];

for (int i = 0; i < noel; i++) {

charArr[i] = [Link](i);
}
[Link]("\nThe Consonants you entered are :" +
[Link](charArr));

[Link]("\n\tTheir Sorted Version : ");

sort_generics(charArr);

} catch (IndexOutOfBoundsException e) {

[Link]("Error has been found: %s\n\n",[Link]());

}
[Link]("[Link]", [Link]);

[Link]("[Link]", [Link]);

[Link]("[Link]", [Link]);

[Link]("[Link]", new Font("Alegreya


SC",[Link],16));

[Link]("[Link]", new Font("Rockwell


Condensed",[Link],16));

[Link]("[Link]", [Link]);

[Link]("[Link]", new Font("Rockwell


Condensed",[Link],16));
[Link]("[Link]", new Font("Rockwell
Condensed",[Link],16));

[Link]("[Link]", [Link]);

Object[] options = {"Clueless","Rising","Sinking","Neither"};

Object clicked = [Link](null,"How are array elements


were sorted ...","Your
Observation",JOptionPane.QUESTION_MESSAGE,null,options,options[0]);

[Link]("\nYou clicked: %s\n",clicked);

public static <T extends Comparable<T>> void sort_generics(T[] a) {

int x, y;

T temp;

for (x = 0; x < [Link] - 1; x++) {


for (y = x + 1; y < [Link]; y++) {

if (a[x].compareTo(a[y]) < 0) {

temp = a[x];

a[x] = a[y];

a[y] = temp;

for (T i : a) {

[Link](" " + i);

[Link]("\n");

The Output:

You might also like