“DATA STRUCTURE”
LAB DESCRIPTION:
Java has powerful package of data structure, which can perform the wide range of functions ans tasks. Java API provide many predefined data structure as ArrayList, Set, Stack, LinkedList, Queue, Vector and Map. The collection in data structure can hold reference in other object. In addition, the data structure can be use to display, add, remove, sort, or reverse the order of elements in the array of data.
The Program will ask user to pick the data type of element to analyze. And then the user will choose how many elements they want to analyze. After their inputs, the program will display the inputs and using data structure to change the order of inputs. The program will reverse inputs, showing them in ascending order and display unique values.
CODE:
“Order.java”
import java.util.*; public class Order { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); //Asking for choice from user System.out.println("What data type do you want to impliment?"); System.out.println("String (1); Double (2); Integer (3)"); int choice = keyboard.nextInt(); //Asking for elements to analyze System.out.println("How many element do you want to process?"); int element = keyboard.nextInt(); keyboard.nextLine(); //String analyze if(choice==1) { System.out.println("Enter the elements:"); //Data Structure String[] StringArray = new String[element]; Queue myQ=new LinkedList(); Stack StringStack = new Stack(); Set uniques = new HashSet(); //Adding elements from array for(int i=0;i<element;i++) { StringArray[i]=keyboard.nextLine(); myQ.add(StringArray[i]); StringStack.push(StringArray[i]); } List StringList = Arrays.asList(StringArray); //Reverse input Collections.reverse(StringList); //Sort input Collections.sort(StringStack); //Get Unique Value uniques.addAll(StringStack); //Print out the result System.out.println("The order of input is: "+myQ); System.out.println("The reverse order of elements:"+StringList); System.out.println("The ascending order of elements:"+StringStack); System.out.println("The uniques of elements:"+uniques); } //double analyze else if (choice==2) { System.out.println("Enter the elements:"); double[] DoubleArray = new double[element]; //Data Structure Queue DoubleQ=new LinkedList(); Stack DoubleStack = new Stack(); Set DoublueUniques = new HashSet(); List DoubleList = new ArrayList(); //Adding elements from array for(int i=0;i<element;i++) { DoubleArray[i]=keyboard.nextDouble(); DoubleQ.add(DoubleArray[i]); DoubleStack.push(DoubleArray[i]); DoubleList.add(DoubleArray[i]); } //Sort input Collections.sort(DoubleStack); //Reverse input Collections.reverse(DoubleList); //Get Unique Value DoublueUniques.addAll(DoubleStack); //Print out the result System.out.println("The order of input is: "+DoubleQ); System.out.println("The reverse order of elements:"+DoubleList); System.out.println("The ascending order of elements:"+DoubleStack); System.out.println("The uniques of elements:"+DoublueUniques); } //integer analyze else if (choice==3) { System.out.println("Enter the elements:"); int[] IntegerArray = new int[element]; //Data Structure Queue IntegerQ=new LinkedList(); Stack IntegerStack = new Stack(); Set IntegerUniques = new HashSet(); List IntegerList = new ArrayList(); //Adding elements from array for(int i=0;i<element;i++) { IntegerArray[i]=keyboard.nextInt(); IntegerQ.add(IntegerArray[i]); IntegerStack.push(IntegerArray[i]); IntegerList.add(IntegerArray[i]); } //Sort input Collections.sort(IntegerStack); //Reverse input Collections.reverse(IntegerList); //Get Unique Value IntegerUniques.addAll(IntegerStack); //Print out the result System.out.println("The order of input is: "+IntegerQ); System.out.println("The reverse order of elements:"+IntegerList); System.out.println("The ascending order of elements:"+IntegerStack); System.out.println("The uniques of elements:"+IntegerUniques); } } } SCREENSHOT: