LAB 8

Description:

For this lab we had to create a program to analyze data that first will display a menu asking the user to enter 2 things:

(1) the data type of the elements (string, double or integer) and
(2) how many elements to process.

Then the user enters the data and after finishing the program will analyze it by displaying the following:

1-    Display zall the elements in the order they were entered.
2-    Display all the elements in the reverse order they were entered.
3-    Display all the elements in ascending order.
4-    Display the elements only once (i.e. no repetitions).

 

 

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

public class lab8
{
	private static Scanner input;
	public static void main(String [] args)
	{

		    input = new Scanner(System.in);
		    Stack stack = new Stack();
		    LinkedList list1 = new LinkedList();
		    LinkedList list2 = new LinkedList();
		    TreeSet treeset = new TreeSet();

		System.out.print("Choose which Data : 1 (string) , 2 (double), 3( integer) ");
		int link = 0;
		link= input.nextInt();

		if (link == 1) {
			int x = 0;
			System.out.print("You have chosen String \n");
			System.out.print("how many elements do you wish to process? ");
			int y = input.nextInt();

			while (x  < y) {
				System.out.print("Enter Any 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 they were entered: %s\n ",stack);
			System.out.printf("(2) In the reverse order they were entered: %s\n ",list1);
			System.out.printf("(3) in ascending order: %s\n ",list2);
			System.out.printf("(4) Display the elements only once: %s\n ",treeset);
		}

		if (link ==2){

			int x = 0;
			System.out.print("You have chosen Double \n");
			System.out.print("how many elements do you wish to process? ");
			int y = input.nextInt();

			while (x  < y) {
				System.out.print("Enter Any 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 they were entered: %s\n ",stack);
			System.out.printf("(2) In the reverse order they were entered: %s\n ",list1);
			System.out.printf("(3) in ascending order: %s\n ",list2);
			System.out.printf("(4) Display the elements only once: %s\n ",treeset);
		}

		if (link == 3) {

			int x = 0;
			System.out.print("You have chosen Integer \n");
			System.out.print("how many elements do you wish to process? ");
			int y = input.nextInt();

			while (x  < y) {
				System.out.print("Enter Any 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 they were entered: %s\n ",stack);
			System.out.printf("(2) In the reverse order they were entered: %s\n ",list1);
			System.out.printf("(3) in ascending order: %s\n ",list2);
			System.out.printf("(4) Display the elements only once: %s\n ",treeset);
		}

}
}

OUTPUT: