Lab Description
In this lab, we are given a set of instruction to build a program that will analyze data from a user. This program will first display a menu asking the user the following things: The type of data type (String, Integer, and Double) and How many elements does the user want to process. After that the program will analyze the data the user had entered and display 4 types of information. The program will display the elements in the order which the user had entered them, it will display the reverse order that they were entered, it will also tell us the ascending order, and finally display all the elements once.
****************************************************************************************************************
Data_Structure.java
package Lab5; import java.util.Scanner; public class Data_Structure { public static void main(String args[]) { Scanner input = new Scanner(System.in); StringDataType myString = new StringDataType(); DoubleDateType myDouble = new DoubleDateType(); IntergerDataType myInteger = new IntergerDataType(); System.out.println("Choose desired Data Type:"); System.out.println("1. String"); System.out.println("2. Double"); System.out.println("3. Int"); System.out.print("Choose a number of elements u want: "); 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("What u have entered is wrong Please try again at next run!!!"); break; } input.close(); } }
**********************************************************************************************************
StringTypeData.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 stringQueue = new LinkedList(); Stack stringStack = new Stack(); ArrayList stringList = new ArrayList(); Set stringSet = new HashSet(); 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); } input.close(); } }
******************************************************************************
DoubleDataType.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 DoubleDateType { public void setDoubles() { Scanner input = new Scanner(System.in); System.out.print("Enter # of elements: "); int numberOfElements = input.nextInt(); int stackCount = numberOfElements; Queue doubleQueue = new LinkedList(); Stack doubleStack = new Stack(); ArrayList doubleList = new ArrayList(); Set doubleSet = new HashSet(); 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); } input.close(); } }
****************************************************************************************************
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 IntergerDataType { public void setIntegers() { Scanner input = new Scanner(System.in); System.out.print("Enter #elements: "); int numberOfElements = input.nextInt(); int stackCount = numberOfElements; Queue intQueue = new LinkedList(); Stack intStack = new Stack(); ArrayList intList = new ArrayList(); Set intSet = new HashSet(); 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); } input.close(); } }
*************************************************************************************************
Output (1)
****************************************************************************************************
Output(2)
*****************************************************************************************************
OUTPUT(3)