Lab Description
The purpose of this lab experiment is to create a program to display a menu asking the user to enter 2 things: (1) the data type of the elements (string, double or integer) and (2) amount of elements will be processed. Using the scanner, will let program read the inputs that user entered to be analyze and after the program being compiled and tested the results should displaying the following:
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).
Program Codes
package lab5; import java.util.Collections; import java.util.Scanner; import java.util.Stack; import java.util.TreeSet; import java.util.HashSet; import java.util.LinkedList; public class lab5 { public static void main(String[] args) { // TODO Auto-generated method stub int choice; Scanner input = new Scanner(System.in); HashSet Set = new HashSet(); Stack Stack = new Stack(); TreeSet treeset = new TreeSet(); LinkedList Rev = new LinkedList(); LinkedList Asc = new LinkedList(); System.out.println("Enter Type of elements"); System.out.println(" 1) Strings"); System.out.println(" 2) Doubles"); System.out.println(" 3) Integers\n"); choice = input.nextInt(); if(choice ==1){ int y = 0; System.out.print("Amount of elements you want to process: \n"); int z = input.nextInt(); while(y < z) { String X = input.next(); Rev.addFirst(X); Stack.push(X); treeset.add(X); Set.add(X); y++; } } else if(choice == 2){ int y = 0; System.out.print("Amount of elements you want to process: "); int z = input.nextInt(); while(y < z) { Double X = input.nextDouble(); Rev.addFirst(X); Stack.push(X); treeset.add(X); Set.add(X); y++; } } else if(choice == 3){ int y = 0; System.out.print("Amount of elements you want to process: "); int z = input.nextInt(); while(y < z) { Integer X = input.nextInt(); Rev.addFirst(X); Stack.push(X); treeset.add(X); Set.add(X); y++; } } Asc.addAll(Rev); Collections.sort(Asc); System.out.println("Elements in Order they were Entered:" + Stack); System.out.println("Elements in Reversed Order:" + Rev); System.out.println("Elements in Ascending Order:" + Asc); System.out.println("Elements without Repeating:" + Set); } }