Lab 5

Lab 5: Data Structures

Description:

In this lab, we were required to analyze different data types using the data structures that we learned, such as List, TreeSet, and Collections. They were used to relate the different data types to one another, so that the user could access them. The way that they were accessed was through a menu, where the user was asked to choose between 3 data types: string, double, and integer. When a choice was made, the choice would be processed through an if else loop, where according to the data type, the number of elements to be processed was asked for, and then each one was entered.

Once each element was entered, they were then related to one another through the different data structures. The stack structure ordered them by entry, the list structure reversed the order of the entries, and the TreeSet was configurable by either descending or ascending values, depending on the data type.  For string it would have been by letter, for double and integer by number.

Code:

package lab5;
	import java.util.LinkedList;
	import java.util.Scanner;
	import java.util.Stack;
	import java.util.TreeSet;
	import java.util.Collections;

public class lab5 {
		private static Scanner input;
		public static void main(String [] args){
			//Stack- LIFO --> organizes data into a stack, where data can be push / pop
			//LinkedList- permits all list operations and elements (lets you have access to all data types)
			//TreeSet- elements are displayed naturally (repeating included)
			//Collections- organizes data into a collection --> to be sorted.
			    input = new Scanner(System.in);
			    Stack stack = new Stack();
			    LinkedList list1 = new LinkedList();
			    LinkedList list2 = new LinkedList();
			    TreeSet treeset = new TreeSet();

			    //menu for choosing a data type
			System.out.print("Pick type of data to input : \n 1: string \n 2: double \n 3: integer ");
			int linkList = 0;
			linkList= input.nextInt();

			//if else loops for each option (option 1)
			if (linkList == 1) {
				int x = 0;
				System.out.print("You picked: String \n");
				System.out.print("Number of elements to process? ");
				int y = input.nextInt();

				//list - displays elements
				//stack.push- orders elements in certain way
				//tree.set- displays non repeating elements
				while (x  < y) {
					System.out.print("Enter a string: ");
					String c = input.next();
	                list1.addFirst(c);
					stack.push(c);
					treeset.add(c);
					x++;

				}

				list2.addAll(list1);
		        Collections.sort(list2);

		    	System.out.printf("(1) Order entered: %s\n ",stack);
				System.out.printf("(2) Reverse order entered: %s\n ",list1);
				System.out.printf("(3) Ascending order: %s\n ",list2);
				System.out.printf("(4) Display elements once: %s\n ",treeset);
			}
			//if else loops for each option (option 2)
			if (linkList ==2){

				int x = 0;
				System.out.print("You picked: Double \n");
				System.out.print("Number of elements to process? ");
				int y = input.nextInt();

				while (x  < y) {
					System.out.print("Enter a Double:  ");
					double c = input.nextDouble();
		            list1.addFirst(c);
		            stack.push(c);
		            treeset.add(c);
		            x++; 
			}
				list2.addAll(list1);
		        Collections.sort(list2);

		    	System.out.printf("(1) Order entered: %s\n ",stack);
				System.out.printf("(2) Reverse order entered: %s\n ",list1);
				System.out.printf("(3) Ascending order: %s\n ",list2);
				System.out.printf("(4) Display elements once: %s\n ",treeset);
			}
			//if else loops for each option (option 3)
			if (linkList == 3) {

				int x = 0;
				System.out.print("You picked: Integer \n");
				System.out.print("Number of elements to process? ");
				int y = input.nextInt();

				while (x  < y) {
					System.out.print("Enter an Integer: ");
					double c = input.nextDouble();
		            list1.addFirst(c);
		            stack.push(c);
		            treeset.add(c);
		            x++; 
			}
				list2.addAll(list1);
		        Collections.sort(list2);

		    	System.out.printf("(1) Order entered: %s\n ",stack);
				System.out.printf("(2) Reverse order entered: %s\n ",list1);
				System.out.printf("(3) Ascending order: %s\n ",list2);
				System.out.printf("(4) Display elements once: %s\n ",treeset);
			}

	}
}

Screenshot:

LAB 5 CET3640

Leave a Reply

Your email address will not be published. Required fields are marked *