Lab Report 5 – Description
I created a program that analyzes data displays a menu asking to choose three different data types of the elements (string, double or integer) and how many elements the user would like to process. The user will then enter their data and the program will analyze it and display the data in the order they were entered, reverse order they were entered, in ascending order, and only once (i.e. no repetitions). I used the appropriate data structure as instructed.
Code:
package LAB_5; import java.io.BufferedReader; import java.io.InputStreamReader; public class usersdataInput { public static String dataInput(String dataType){ BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); System.out.print(dataType); System.out.flush(); try { return stdin.readLine(); }catch (Exception e){ return "Error: "+e.getMessage(); } } } package LAB_5; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Scanner; import java.util.Set; import java.util.SortedSet; import java.util.Stack; import java.util.TreeSet; public class datastrings { String strng1 = usersdataInput.dataInput("Enter the amount of Elements you would like to process: \n"); int str = Integer.parseInt(strng1); String[] strg = new String[str]; public void pqueue(){ Queue myStringstk = new LinkedList(); for (int i = 0; i < str; i++) { Scanner input = new Scanner( System.in ); System.out.println("Enter the Elements: \n"); String capacity = input.next(); strg[i] = capacity; myStringstk.offer(capacity); } System.out.println("Displaying Elements you entered in order using Queue Structuring: "); while (myStringstk.size() > 0){ System.out.printf("%s", myStringstk.peek()); myStringstk.poll(); System.out.print(": "); } System.out.println(""); } public void stacks(){ Stack myStringstk = new Stack(); for (int i = 0; i < str; i++) { Scanner input = new Scanner( System.in ); System.out.println("Enter the Elements: \n"); String capacity = input.next(); strg[i] = capacity; myStringstk.push(capacity); } System.out.println("Displaying Elements you entered in reverse order using Stack Structuring: "); while (myStringstk.size() > 0){ System.out.printf("%s", myStringstk.pop()); System.out.print(": "); } System.out.println(""); } public void ascendinglist(){ for (int i = 0; i < str; i++) { Scanner input = new Scanner( System.in ); System.out.println("Enter the Elements: \n"); String capacity = input.next(); strg[i] = capacity; } SortedSet myStringstk = new TreeSet(Arrays.asList(strg)); System.out.print("Elements you entered Ascending order using TreeSet Structuring: \n"); printSet(myStringstk); System.out.println("\n"); } public void nonduplicates(){ for (int i = 0; i < str; i++) { Scanner input = new Scanner( System.in ); System.out.println("Enter the Elements: \n"); String capacity = input.next(); strg[i] = capacity; } List myStringstk = Arrays.asList(strg); System.out.println("Non-Duplicate Elements you entered using HashSet Structuring: "); printNonDuplicates(myStringstk); } private static void printSet(SortedSet myStr) { for (String s : myStr) { System.out.printf("%s: ", s); } } private static void printNonDuplicates(Collection myStg) { Set set = new HashSet(myStg); for (String t : set) { System.out.printf("%s: ", t); } } } package LAB_5; import java.util.Collection; import java.util.List; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Set; import java.util.SortedSet; import java.util.Stack; import java.util.TreeSet; public class datadoubles { String dble1 = usersdataInput.dataInput("Enter the amount of Elements you would like to process: \n"); int str = Integer.parseInt(dble1); Double[] strg = new Double[str]; public void pqueue(){ Queue myDoublestk = new LinkedList(); for (int i = 0; i < str; i++) { Scanner input = new Scanner( System.in ); System.out.println("Enter the Elements: \n"); Double capacity = input.nextDouble(); strg[i] = capacity; myDoublestk.offer(capacity); } System.out.println("Displaying Elements you entered in order using Queue Structuring: "); while (myDoublestk.size() > 0){ System.out.printf("%.1f", myDoublestk.peek()); myDoublestk.poll(); System.out.print(": "); } System.out.println(""); } public void stacks(){ Stack myDoublestk = new Stack(); for (int i = 0; i < str; i++) { Scanner input = new Scanner( System.in ); System.out.println("Enter the Elements: \n"); Double capacity = input.nextDouble(); strg[i] = capacity; myDoublestk.push(capacity); } System.out.println("Displaying Elements you entered in reverse order using Stack Structuring: "); while (myDoublestk.size() > 0){ System.out.printf("%.1f", myDoublestk.pop()); System.out.print(": "); } System.out.println(""); } public void ascendinglist(){ for (int i = 0; i < str; i++) { Scanner input = new Scanner( System.in ); System.out.println("Enter the Elements: \n"); Double capacity = input.nextDouble(); strg[i] = capacity; } SortedSet myDoublestk = new TreeSet(Arrays.asList(strg)); System.out.print("Elements you entered Ascending order using TreeSet Structuring: \n"); printSet(myDoublestk); System.out.println("\n"); } public void nonduplicates(){ for (int i = 0; i < str; i++) { Scanner input = new Scanner( System.in ); System.out.println("Enter the Elements: \n"); Double capacity = input.nextDouble(); strg[i] = capacity; } List myDoublestk = Arrays.asList(strg); System.out.println("Non-Duplicate Elements you entered using HashSet Structuring: "); printNonDuplicates(myDoublestk); } private static void printSet(SortedSet myDbl) { for (Double s : myDbl) { System.out.printf("%s: ", s); } } private static void printNonDuplicates(Collection myDble) { Set set = new HashSet(myDble); for (Double t : set) { System.out.printf("%s: ", t); } } } package LAB_5; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Queue; import java.util.Scanner; import java.util.Set; import java.util.SortedSet; import java.util.Stack; import java.util.TreeSet; public class dataintegers { String intgr1 = usersdataInput.dataInput("Enter the amount of Elements you would like to process: \n"); int str = Integer.parseInt(intgr1); Integer[] strg = new Integer[str]; public void pqueue(){ Queue myIntegerstk = new LinkedList(); for (int i = 0; i < str; i++) { Scanner input = new Scanner( System.in ); System.out.println("Enter the Elements: \n"); int capacity = input.nextInt(); strg[i] = capacity; myIntegerstk.offer(capacity); } System.out.println("Displaying Elements you entered in order using Queue Structuring: "); while (myIntegerstk.size() > 0){ System.out.printf("%d", myIntegerstk.peek()); myIntegerstk.poll(); System.out.print(": "); } System.out.println(""); } public void stacks(){ Stack myIntegerstk = new Stack(); for (int i = 0; i < str; i++) { Scanner input = new Scanner( System.in ); System.out.println("Enter the Elements: \n"); Integer capacity = input.nextInt(); strg[i] = capacity; myIntegerstk.push(capacity); } System.out.println("Displaying Elements you entered in reverse order using Stack Structuring: "); while (myIntegerstk.size() > 0){ System.out.printf("%d", myIntegerstk.pop()); System.out.print(": "); } System.out.println(""); } public void ascendinglist(){ for (int i = 0; i < str; i++) { Scanner input = new Scanner( System.in ); System.out.println("Enter the Elements: \n"); Integer capacity = input.nextInt(); strg[i] = capacity; } SortedSet myIntegerstk = new TreeSet(Arrays.asList(strg)); System.out.print("Elements you entered Ascending order using TreeSet Structuring: \n"); printSet(myIntegerstk); System.out.println("\n"); } public void nonduplicates(){ for (int i = 0; i < str; i++) { Scanner input = new Scanner( System.in ); System.out.println("Enter the Elements: \n"); Integer capacity = input.nextInt(); strg[i] = capacity; } List myIntegerstk = Arrays.asList(strg); System.out.println("Non-Duplicate Elements you entered using HashSet Structuring: "); printNonDuplicates(myIntegerstk); } private static void printSet(SortedSet myInt) { for (Integer s : myInt) { System.out.printf("%s: ", s); } } private static void printNonDuplicates(Collection myInts) { Set set = new HashSet(myInts); for (Integer t : set) { System.out.printf("%s: ", t); } } } package LAB_5; public class dataStructure { String Introduction; public dataStructure (String Program) { Introduction = Program; } public String getWELCOME() { return Introduction; } public void MSGDISPLAY() { System.out.printf("Welcome to Washington Sarmiento's Fifth Program\n%s!\n\n", getWELCOME()); } public static void main(String[] args) { dataStructure prgMessage = new dataStructure("CET3640f2013 LAB 5: Data Structures"); prgMessage.MSGDISPLAY(); String input1 = usersdataInput.dataInput("Enter a Data Type: 1-String, 2-Double, 3-Integer: \n"); int inputcase = Integer.parseInt(input1); switch (inputcase){ case 1: datastrings myStrings = new datastrings(); myStrings.pqueue(); myStrings.stacks(); myStrings.ascendinglist(); myStrings.nonduplicates(); break; case 2: datadoubles myDoubles = new datadoubles(); myDoubles.pqueue(); myDoubles.stacks(); myDoubles.ascendinglist(); myDoubles.nonduplicates(); break; case 3: dataintegers myIntegers = new dataintegers(); myIntegers.pqueue(); myIntegers.stacks(); myIntegers.ascendinglist(); myIntegers.nonduplicates(); break; default: break; } } }