Lab 5
Description: This is a java application to demonstrate the use of data structures. The user inputs data and the program displays the data back in unsorted, sorted, reversed, and set form. A LinkedList is used for the unsorted and sorted forms. The reverse form is implemented with a Stack and the set form (which is also sorted) is implemented with a TreeSet.
Code:
package jwarren.lab5; import java.util.Collections; import java.util.LinkedList; import java.util.Scanner; import java.util.Stack; import java.util.TreeSet; public class DataStructuresDemoApp { @SuppressWarnings("unchecked") public static void main(String args[]) { Scanner in = new Scanner(System.in); LinkedList<?> list; Stack<?> stack; TreeSet<?> tree; int menuSelection = 0; int elementCount = 0; String dataType; boolean error = true; String str; Integer intVal; Double realVal; System.out.println("What type of data do you wish to process?"); System.out.println("1 - Text"); System.out.println("2 - Integers"); System.out.println("3 - Real Numbers"); System.out.print(">"); while (error) { try { menuSelection = in.nextInt(); if (menuSelection < 1 || menuSelection > 3) { System.err.println("Please enter a value between 1 and 3"); error = true; } else { error = false; } } catch (Exception e) { System.err.println("Please enter a numerical value"); in.next(); } } switch (menuSelection) { case 1: dataType = "word"; list = new LinkedList(); stack = new Stack(); tree = new TreeSet(); break; case 2: dataType = "integer"; list = new LinkedList(); stack = new Stack(); tree = new TreeSet(); break; default: dataType = "number"; list = new LinkedList(); stack = new Stack(); tree = new TreeSet(); } System.out.println("How many " + dataType + "s will you enter?"); error = true; while (error) { try { elementCount = in.nextInt(); if (elementCount < 0) { System.err.println("Please enter a non-negative value"); error = true; } else { error = false; } } catch (Exception e) { System.err.println("Please enter a numerical value"); in.next(); error = true; } } for (int i = 0; i < elementCount; i++) { System.out.print("Enter " + dataType + " #" + (i + 1) + ":"); error = true; while (error) { try { if (menuSelection == 1) { str = in.next(); ((LinkedList) list).add(str); ((Stack) stack).push(str); ((TreeSet) tree).add(str); } else if (menuSelection == 2) { intVal = in.nextInt(); ((LinkedList) list).add(intVal); ((Stack) stack).push(intVal); ((TreeSet) tree).add(intVal); } else if (menuSelection == 3) { realVal = in.nextDouble(); ((LinkedList) list).add(realVal); ((Stack) stack).push(realVal); ((TreeSet) tree).add(realVal); } error = false; } catch (Exception e) { System.err.println("Invalid input for type: " + dataType); in.next(); error = true; } } } // Print container PrintCollections(menuSelection, list, stack, tree); in.close(); } @SuppressWarnings("unchecked") private static void PrintCollections(int choice, LinkedList<?> list, Stack<?> stack, TreeSet<?> tree) { String str; Integer intVal; Double realVal; if (choice == 1) { System.out.print("Printing collection using list: "); for (String s : (LinkedList) list) { System.out.print(s + " "); } System.out.println(); System.out.print("Printing collection using stack: "); int size = ((Stack) stack).size(); for (int i = 0; i < size; i++) { str = ((Stack) stack).pop(); System.out.print(str + " "); } System.out.println(); System.out.print("Printing collection using sorted linked list: "); Collections.sort(((LinkedList)list)); for(String s : (LinkedList)list){ System.out.print(s + " "); } System.out.println(); System.out.print("Printing collection using tree: "); for (String s : (TreeSet) tree) { System.out.print(s + " "); } } else if (choice == 2) { System.out.print("Printing collection using list: "); for (Integer i : (LinkedList) list) { System.out.print(i + " "); } System.out.println(); System.out.print("Printing collection using stack: "); int size = ((Stack) stack).size(); for (int i = 0; i < size; i++) { intVal = ((Stack) stack).pop(); System.out.print(intVal + " "); } System.out.println(); System.out.print("Printing collection using sorted linked list: "); Collections.sort(((LinkedList)list)); for(Integer i : (LinkedList)list){ System.out.print(i + " "); } System.out.println(); System.out.print("Printing collection using tree: "); for (Integer i : (TreeSet) tree) { System.out.print(i + " "); } } else if (choice == 3) { System.out.print("Printing collection using list: "); for (Double d : (LinkedList) list) { System.out.print(d + " "); } System.out.println(); System.out.print("Printing collection using stack: "); int size = ((Stack) stack).size(); for (int i = 0; i < size; i++) { realVal = ((Stack) stack).pop(); System.out.print(realVal + " "); } System.out.println(); System.out.print("Printing collection using sorted linked list: "); Collections.sort(((LinkedList)list)); for(Double d : (LinkedList)list){ System.out.print(d + " "); } System.out.println(); System.out.print("Printing collection using tree: "); for (Double d : (TreeSet) tree) { System.out.print(d + " "); } } } }
Screenshots:
Leave a Reply