You are on page 1of 2

// Project #4 : Polymorphism

//
import java.util.Scanner;
import java.util.Arrays;
public class project4 {
public static void main(String[] args){
Object[] characterArray = null, doubleArray = null, exceptionArr
ay = null, floatArray = null, intArray = null, longArray = null, stringArray = n
ull;
int c = 0, d = 0, e = 0, f = 0, i = 0, l = 0, s = 0;
Scanner k = new Scanner(System.in);
String token = k.next();
while(token.toLowerCase().charAt(0) != 'q'){
switch(token.toLowerCase().charAt(0)){
case 'c' : if (c == 0)
characterArray = new Object[0];
characterArray = grow(characterArray);
characterArray[c] = k.next().charAt(0);
c++;
break;
case 'd' : if (d == 0)
doubleArray = new Object[0];
doubleArray = grow(doubleArray);
doubleArray[d] = k.nextDouble();
d++;
break;
case 'e' : if (e == 0)
exceptionArray = new Object[0];
exceptionArray = grow(exceptionArray);
exceptionArray[e] = new MyException(k.ne
xtLine()); // Made a custom Exception class to override toString() method
e++;
break;
case 'f' : if (f == 0)
floatArray = new Object[0];
floatArray = grow(floatArray);
floatArray[f] = k.nextFloat();
f++;
break;
case 'i' : if (i == 0)
intArray = new Object[0];
intArray = grow(intArray);
intArray[i] = k.nextInt();
i++;
break;
case 'l' : if (l == 0)
longArray = new Object[0];
longArray = grow(longArray);
longArray[l] = k.nextLong();
l++;
break;
case 's' : if (s == 0)
stringArray = new Object[0];
stringArray = grow(stringArray);
stringArray[s] = k.nextLine();
s++;
break;
}

token = k.next();
}
order(characterArray, c, "Character");
order(doubleArray, d, "Double");
order(exceptionArray, e, "Exception");
order(floatArray, f, "Float");
order(intArray, i, "Integer");
order(longArray, l, "Long");
order(stringArray, s, "String");
}
public static Object[] grow(Object[] objectArray){
Object[] temp = new Object[(objectArray.length + 1)];
for (int i = 0; i < objectArray.length; i++)
temp[i] = objectArray[i];
return objectArray = temp;
}
public static void order(Object[] objectArray, int n, String dataType){
if (objectArray[0] instanceof Character){
Object[] charToStringArray = new Object[n];
for (int i = 0; i < charToStringArray.length; i++)
charToStringArray[i] = "" + objectArray[i];
order(charToStringArray, n, dataType);
}
else if (objectArray[0] instanceof MyException){
Object[] exceptionToStringArray = new Object[n];
for (int i = 0; i < exceptionToStringArray.length; i++)
exceptionToStringArray[i] = objectArray[i].toStr
ing();
order(exceptionToStringArray, n, dataType);
}
else if (objectArray[0] instanceof String){
String[] newArray = Arrays.copyOf(objectArray, n, String
[].class);
Arrays.sort(newArray, String.CASE_INSENSITIVE_ORDER);
for (int i = 0; i < newArray.length; i++){
System.out.printf("%-15s:
%s\n", dataTyp
e, newArray[i].trim());
}
}
else if (objectArray[0] instanceof Float || objectArray[0] insta
nceof Double){
Arrays.sort(objectArray);
for (int i = 0; i < objectArray.length; i++){
System.out.printf("%-15s:%10.2f\n", dataType, ob
jectArray[i]);
}
}
else if (objectArray[0] instanceof Integer || objectArray[0] ins
tanceof Long){
Arrays.sort(objectArray);
for (int i = 0; i < objectArray.length; i++ )
System.out.printf("%-15s:%10d\n", dataType, obje
ctArray[i]);
}
}
}

You might also like