Lab 5

Description

Create a program to let user choose type of elements to input and choose how many to process. After enter the elements, it should display the elements as they were entered, reverse the order of entries, sorted the entries in ascending order, and display unique entries (no duplicates).

Codes

Lab5App.java

package Lab5;

import java.util.*;

/**
 * 1-	Display all 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).
 */
public class Lab5App {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int inputType, inputCounter;
		String inString;
		int inInt;
		double inDouble;


		do {
			System.out.print("MENU:\n1. string\n2. double\n3. integer\n0. Quit\n" +
					"Enter input data type: ");
			inputType = in.nextInt();
			if (inputType == 0)
				break;
			System.out.print("How many elements to process: ");
			inputCounter = in.nextInt();
			in.nextLine(); // fix the empty line

			switch (inputType) {
				case 1:
					ArrayList stringItems = new ArrayList();
					Stack stackString = new Stack();
					Set setString = new TreeSet();
					for (int i = 0; i < inputCounter; i++) {
						System.out.print("Enter element #" + i + ":");
						inString = in.nextLine();
						stringItems.add(inString);
						stackString.push(inString);
						setString.add(inString);
					}

					// display
					System.out.println("Order they were entered: ");
					for (int i = 0; i < inputCounter; i++) {
						System.out.printf("Item #%d = %s\n", i, stringItems.get(i));
					}

					System.out.println("Reversed order: ");
					for (int i = 0; i < inputCounter; i++) {
						System.out.println("Item #" + i + " = " + stackString.pop());
					}
					Collections.sort(stringItems);
					System.out.printf("\nSorted array elements:\n%s\n\n", stringItems);

					// display unique entry
					System.out.println("Unique entry: \n" + setString);
					break;
				case 2:
					ArrayList doubleItems = new ArrayList();
					Stack doubleStack = new Stack();
					Set doubleSet = new TreeSet();

					for (int i = 0; i < inputCounter; i++) {
						System.out.print("Enter element #" + i + ":");
						inDouble = in.nextDouble();
						doubleItems.add(inDouble);
						doubleStack.push(inDouble);
						doubleSet.add(inDouble);
					}

					System.out.println("Order they were entered: ");
					for (int i = 0; i = 0; i--) {
						System.out.println("Item #" + i + " = " + doubleStack.pop());
					}
					// Sorted
					Collections.sort(doubleItems);
					System.out.printf("\nSorted array elements:\n%s\n\n", doubleItems);

					// display unique entry
					System.out.println("Unique entry: \n" + doubleSet);
					break;
				default:
					ArrayList intItems = new ArrayList();
					Stack intStack = new Stack();
					Set intSet = new TreeSet();

					for (int i = 0; i < inputCounter; i++) {
						System.out.print("Enter element #" + i + ":");
						inInt = in.nextInt();
						intItems.add(inInt);
						intStack.push(inInt);
						intSet.add(inInt);
					}

					System.out.println("Order they were entered: ");
					for (int i = 0; i = 0; i--) {
						System.out.println("Item #" + i + " = " + intStack.pop());
					}

					Collections.sort(intItems);
					System.out.printf("\nSorted array elements:\n%s\n\n", intItems);

					System.out.println("Unique entry: \n" + intSet);
			}
			System.out.println(); // display empty line
		} while (inputType != 0);
	}
}

Screenshots

Leave a Reply

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