Lab 5

Lab Description:

In Lab 5, we are learning how to use different data types to achieve certain goal like giving some elements and display it in the order they were entered, display in the reverse order they were entered, display in ascending order of the elements, and display the elements only once (without duplicate). In order to achieve these goals, we will be using 4 different data types, which are ArrayList for “the order they were entered”, Stack for “the reverse order they were entered”, PriorityQueue for “ascending order of the elements”, and finally Set for “elements without duplicate”.

Code:

//Create By Kawa Tsang
package Lab05;
import java.util.*;

public class Lab05_1 {

    public static void main(String[] args) {
        char elementType;        //declared variables to use
        String tempString = null;
        double tempDouble = 0;
        int tempInt = 0;
        int numberNeed = 0;

        Scanner input = new Scanner(System.in);        //Create a new object scanner
        System.out.println("Welcome to My program, Please enter the following things:");    //Start the program
        System.out.println("(1) The data type of the elements (string, double or interger): ");    //Display the menu that asking the user to enter 2 things
        System.out.println("(2) How many elements you want to enter: ");
        System.out.println("Now, please type in the elements type(s for String, d for Double, i for Integer):");    //Ask the user to enter the first thing
        elementType = input.next().charAt(0);
        System.out.println("Now, please enter how many elements you want to enter:");    //Ask the user how many elements they want to enter
        numberNeed = input.nextInt();

        if ( elementType == 's' || elementType == 'S' ){    //Check the element type
            ArrayList<String> stringList = new ArrayList<String>();        //Create the right array list type
            Stack<String> stringStack = new Stack<String>();
            PriorityQueue<String> stringQueue = new PriorityQueue<String>();
            HashSet<String> stringHS = new HashSet<String>();

            for (int i = 1; i <= numberNeed; i++){    //Loop the user inputting the elements and save it into the array list
                System.out.println("Now, please enter the " + i + " element:");
                tempString = input.next();
                stringList.add(tempString);
                stringStack.add(tempString);
                stringQueue.add(tempString);
                stringHS.add(tempString);
            }

            System.out.print("All the elements in the order they were entered (Array List used):" );    //Show the elements in the order they were entered using Array List
            printArraylist(stringList);
            System.out.print("All the elements in the reverse order they were entered (Stack used):" );     //Show the elements in reverse order they were entered using Stack
            printStack(stringStack);
            System.out.print("All the elements in the ascending order (Queue used):");        //Show the elements in ascending order using Priority Queue
            printQueue(stringQueue);
            System.out.println("All the elements without duplicates (HashSet used):" + stringHS );        //Show the elements without duplicates using Set
            input.close();    //Close the scanner
        }
        else if (elementType == 'd' || elementType == 'D' ){    //Check the element type
            ArrayList<Double> doubleList = new ArrayList<Double>();        //Create the right array list type
            Stack<Double> doubleStack = new Stack<Double>();
            PriorityQueue<Double> doubleQueue = new PriorityQueue<Double>();
            HashSet<Double> doubleHS = new HashSet<Double>();

            for (int i = 1; i <= numberNeed; i++){    //Loop the user inputting the elements and save it into the array list
                System.out.println("Now, please enter the " + i + " element:");
                tempDouble = input.nextDouble();
                doubleList.add(tempDouble);
                doubleStack.add(tempDouble);
                doubleQueue.add(tempDouble);
                doubleHS.add(tempDouble);
            }

            System.out.print("All the elements in the order they were entered (Array List used):" );    //Show the elements in the order they were entered using Array List
            printArraylist(doubleList);
            System.out.print("All the elements in the reverse order they were entered (Stack used):" );        //Show the elements in reverse order they were entered using Stack
            printStack(doubleStack);
            System.out.print("All the elements in the ascending order (Queue used):");        //Show the elements in ascending order using Priority Queue
            printQueue(doubleQueue);
            System.out.println("All the elements without duplicates (HashSet used):" + doubleHS );        //Show the elements without duplicates using Set
            input.close();    //Close the scanner
        }
        else if (elementType == 'i' || elementType == 'I'){    //Check the element type
            ArrayList<Integer> integerList = new ArrayList<Integer>();        //Create the right array list type
            Stack<Integer> integerStack = new Stack<Integer>();
            PriorityQueue<Integer> integerQueue = new PriorityQueue<Integer>();
            HashSet<Integer> integerHS = new HashSet<Integer>();

            for (int i = 1; i <= numberNeed; i++){    //Loop the user inputting the elements and save it into the array list
                System.out.println("Now, please enter the " + i + " element:");
                tempInt = input.nextInt();
                integerList.add(tempInt);
                integerStack.add(tempInt);
                integerQueue.add(tempInt);
                integerHS.add(tempInt);
            }

            System.out.print("All the elements in the order they were entered (Array List used):");        //Show the elements in the order they were entered using Array List
            printArraylist(integerList);
            System.out.print("All the elements in the reverse order they were entered (Stack used):");        //Show the elements in reverse order they were entered using Stack
            printStack(integerStack);
            System.out.print("All the elements in the ascending order (Queue used):");        //Show the elements in ascending order using Priority Queue
            printQueue(integerQueue);
            System.out.println("All the elements without duplicates (HashSet used):" + integerHS );        //Show the elements without duplicates using Set
            input.close();    //Close the scanner        
        }
        else{
            System.out.println("Unsupported type, System exit!!");
            input.close();    //Close the scanner
            System.exit(0); //Exit system
        }
    }

    private static void printArraylist(ArrayList<?> s) {    //print the array list elements one by one
        int i=0;
        System.out.print("[");
        while(i != s.size() )
        {
            System.out.print(s.get(i));
            i++;
            if(i < s.size())
            {
                System.out.print(",");
                System.out.print(" ");
            }
        }
        System.out.println("]");
    }

    private static void printStack(Stack<?> s) {    //print the stack elements one by one
        System.out.print("[");
        while(!s.isEmpty())
        {
            System.out.print(s.pop());
            if(s.size() != 0)
            {
                System.out.print(",");
                System.out.print(" ");
            }
        }
        System.out.println("]");
    }

    private static void printQueue(Queue<?> s) {    //print the queue elements one by one
        System.out.print("[");
        while(s.size() != 0)
        {
            System.out.print(s.poll());
            if(s.size() != 0)
            {
                System.out.print(",");
                System.out.print(" ");
            }
        }
        System.out.println("]");
    }
}

Screenshot:

d i s

Leave a Reply

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