Lab Description:
For this lab, we will create a program to analyze data that first will display a menu asking the user to choose their data type, number of elements, then have the user enter their desired elements. After entering all their data, the program will analyze it by displaying the following:
- Display all the elements in the order they were entered.
- Display all the elements in the reverse order they were entered.
- Display all the elements in ascending order.
- Display the elements only once.
Code:
DataStructure.java
package Lab5; import java.util.Scanner; public class DataStructure { public static void main(String[] args) { Scanner input = new Scanner(System.in); StringDataType myString = new StringDataType(); DoubleDataType myDouble = new DoubleDataType(); IntegerDataType myInteger = new IntegerDataType(); System.out.println("Choose desired Data Type:"); System.out.println("(1) String"); System.out.println("(2) Double"); System.out.println("(3) Integer"); System.out.print("Enter number of choice: "); int choice = input.nextInt(); switch(choice) { case 1: myString.setStrings(); break; case 2: myDouble.setDoubles(); break; case 3: myInteger.setIntegers(); break; default: System.out.println("Invalid entry!!!"); break; } } }
StringDataType.java
package Lab5; import java.util.HashSet; import java.util.Scanner; import java.util.Queue; import java.util.LinkedList; import java.util.Stack; import java.util.ArrayList; import java.util.Collections; import java.util.Set; public class StringDataType { public void setStrings() { Scanner input = new Scanner(System.in); System.out.print("Enter number of elements: "); int numberOfElements = input.nextInt(); int stackCount = numberOfElements; Queue<String> stringQueue = new LinkedList<String>(); Stack<String> stringStack = new Stack<String>(); ArrayList<String> stringList = new ArrayList<String>(); Set<String> stringSet = new HashSet<String>(); int count = 1; do { System.out.printf("Enter Element #%d : ",count); String stringInput = input.next(); stringQueue.add(stringInput); stringStack.push(stringInput); stringList.add(stringInput); stringSet.add(stringInput); count++; numberOfElements--; }while(numberOfElements != 0); System.out.println(); System.out.println("Elements in order using Queue:"); for(String display:stringQueue) { System.out.println(display); } System.out.println("------------------------------------------"); System.out.println("Elements in reverse using Stack:"); for(int i = stackCount;i != 0;i--) { System.out.println(stringStack.pop()); } Collections.sort(stringList); System.out.println("------------------------------------------"); System.out.println("Elements sorted using Collections.sort on ArrayList:"); for(String display:stringList) { System.out.println(display); } System.out.println("------------------------------------------"); System.out.println("Elements without duplicates using Set:"); for(String display:stringSet) { System.out.println(display); } } }
DoubleDateType.java
package Lab5; import java.util.HashSet; import java.util.Scanner; import java.util.Queue; import java.util.LinkedList; import java.util.Stack; import java.util.ArrayList; import java.util.Collections; import java.util.Set; public class DoubleDataType { public void setDoubles() { Scanner input = new Scanner(System.in); System.out.print("Enter number of elements: "); int numberOfElements = input.nextInt(); int stackCount = numberOfElements; Queue<Double> doubleQueue = new LinkedList<Double>(); Stack<Double> doubleStack = new Stack<Double>(); ArrayList<Double> doubleList = new ArrayList<Double>(); Set<Double> doubleSet = new HashSet<Double>(); int count = 1; do { System.out.printf("Enter Element #%d : ",count); Double doubleInput = input.nextDouble(); doubleQueue.add(doubleInput); doubleStack.push(doubleInput); doubleList.add(doubleInput); doubleSet.add(doubleInput); count++; numberOfElements--; }while(numberOfElements != 0); System.out.println(); System.out.println("Elements in order using Queue:"); for(Double display:doubleQueue) { System.out.println(display); } System.out.println("------------------------------------------"); System.out.println("Elements in reverse using Stack:"); for(int i = stackCount;i != 0;i--) { System.out.println(doubleStack.pop()); } Collections.sort(doubleList); System.out.println("------------------------------------------"); System.out.println("Elements sorted using Collections.sort on ArrayList:"); for(Double display:doubleList) { System.out.println(display); } System.out.println("------------------------------------------"); System.out.println("Elements without duplicates using Set:"); for(Double display:doubleSet) { System.out.println(display); } } }
IntegerDataType.java
package Lab5; import java.util.HashSet; import java.util.Scanner; import java.util.Queue; import java.util.LinkedList; import java.util.Stack; import java.util.ArrayList; import java.util.Collections; import java.util.Set; public class IntegerDataType { public void setIntegers() { Scanner input = new Scanner(System.in); System.out.print("Enter number of elements: "); int numberOfElements = input.nextInt(); int stackCount = numberOfElements; Queue<Integer> intQueue = new LinkedList<Integer>(); Stack<Integer> intStack = new Stack<Integer>(); ArrayList<Integer> intList = new ArrayList<Integer>(); Set<Integer> intSet = new HashSet<Integer>(); int count = 1; do { System.out.printf("Enter Element #%d : ",count); int intInput = input.nextInt(); intQueue.add(intInput); intStack.push(intInput); intList.add(intInput); intSet.add(intInput); count++; numberOfElements--; }while(numberOfElements != 0); System.out.println(); System.out.println("Elements in order using Queue:"); for(int display:intQueue) { System.out.println(display); } System.out.println("------------------------------------------"); System.out.println("Elements in reverse using Stack:"); for(int i = stackCount;i != 0;i--) { System.out.println(intStack.pop()); } Collections.sort(intList); System.out.println("------------------------------------------"); System.out.println("Elements sorted using Collections.sort on ArrayList:"); for(int display:intList) { System.out.println(display); } System.out.println("------------------------------------------"); System.out.println("Elements without duplicates using Set:"); for(int display:intSet) { System.out.println(display); } } }