Lab 5

Lab description:

In this lab, I will have to write a program that give user three choices of data type to pick from: string, double, and integer. And the it will prompt user to inputs, user decide how many inputs it is going to have. Finally, make four analyze using arraylist, stack, priorityqueue, and hashset to give four outputs of a list what the user enters.

Code:

//Name: Guan Hong Chen
//Date: Nov. 18 2013
//CET 3640 Fall 2013
//Lab 5 (DataStructure.java)
import java.util.ArrayList;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.HashSet;
import java.util.Scanner;

public class DataStructure 
{
	public static void printHashSet(HashSet Set) 
	{
        System.out.print( "HashSet: " );
        Iterator iterator = Set.iterator();
        while (iterator.hasNext())
            System.out.print( iterator.next() + " " );
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		ArrayList List = new ArrayList();
		Stack doStack = new Stack();
		PriorityQueue Queue = new PriorityQueue();
		HashSet Set = new HashSet();

		int number;
		char selection;

		System.out.println("Menu:");
		System.out.println("(a) String");
		System.out.println("(b) Double");
		System.out.println("(c) Integer");
		System.out.printf("Please select one of the above data type of the elements: ");		
		selection = input.next().charAt(0);
		System.out.printf("Please enter the number of elements to process: ");
		number = input.nextInt();

		switch(selection)
		{
			case 'a': 

				System.out.printf("Please enter " + number + " strings: ");

				String[] stringArray = new String[number];
				for(int i = 0; i < number; i++)
				{
					stringArray[i] = input.next();
					List.add(stringArray[i]);
					doStack.push(stringArray[i]);
					Queue.offer(stringArray[i]);
					Set.add(stringArray[i]);
				}

				System.out.printf("Displaying all the elements in the order they were entered now (using ArrayList): ");
				for(int i = 0; i < List.size(); i++) 					System.out.print(List.get(i)+" "); 				 				System.out.printf("\nDisplaying all the elements in reversed order now(using Stack): "); 				while(!doStack.empty()) 					System.out.print(doStack.pop()+" "); 			 				System.out.printf("\nDisplaying all the elements in ascending order now(using PriorityQueue): " ); 				while(Queue.size() > 0)
				{
					System.out.print(Queue.peek()+" ");
					Queue.poll();
				}

				System.out.printf("\nDisplaying all the elements only once now(using HashSet): " );
				printHashSet(Set);

				break;

			case 'b':
				System.out.printf("Please enter " + number + " doubles: ");

				double[] doubleArray = new double[number];
				for(int i = 0; i < number; i++)
				{
					doubleArray[i] = input.nextDouble();
					List.add(doubleArray[i]);
					doStack.push(doubleArray[i]);
					Queue.offer(doubleArray[i]);
					Set.add(doubleArray[i]);
				}

				System.out.printf("Displaying all the elements in the order they were entered now (using ArrayList): ");
				for(int i = 0; i < List.size(); i++) 					System.out.print(List.get(i)+" "); 				 				System.out.printf("\nDisplaying all the elements in reversed order now(using Stack): "); 				while(!doStack.empty()) 					System.out.print(doStack.pop()+" "); 			 				System.out.printf("\nDisplaying all the elements in ascending order now(using PriorityQueue): " ); 				while(Queue.size() > 0)
				{
					System.out.print(Queue.peek()+" ");
					Queue.poll();
				}

				System.out.printf("\nDisplaying all the elements only once now(using HashSet): " );
				printHashSet(Set);

				break;

			case 'c':
				System.out.printf("Please enter " + number + " integers: ");
				int[] intArray = new int[number];
				for(int i = 0; i < number; i++)
				{
					intArray[i] = input.nextInt();
					List.add(intArray[i]);
					doStack.push(intArray[i]);
					Queue.offer(intArray[i]);
					Set.add(intArray[i]);
				}

				System.out.printf("Displaying all the elements in the order they were entered now (using ArrayList): ");
				for(int i = 0; i < List.size(); i++) 					System.out.print(List.get(i)+" "); 				 				System.out.printf("\nDisplaying all the elements in reversed order now(using Stack): "); 				while(!doStack.empty()) 					System.out.print(doStack.pop()+" "); 			 				System.out.printf("\nDisplaying all the elements in ascending order now(using PriorityQueue): " ); 				while(Queue.size() > 0)
				{
					System.out.print(Queue.peek()+" ");
					Queue.poll();
				}

				System.out.printf("\nDisplaying all the elements only once now(using HashSet): " );
				printHashSet(Set);

				break;

			default: 
				System.out.println("invalid input");
				break;
		}
	}

}

Screenshot:

Capture

 

Capture1

Capture2

Leave a Reply

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