Lab Description:
We are to create a program that ask that ask the user 2 things:
1. ask the user the type of element (String, Double, or Integer)
2. ask the user how many elements they want to enter.
When the user finish entering the data it will display :
- Display all the elements in the order they were entered.
- Display all the elements in the reverse order they were entered.
- Display all the elements in ascending order.
- Display the elements only once (i.e. no repetitions).
Code:
import java.util.*; public class element { private static Scanner scanner; public static void main(String[] args) { scanner = new Scanner(System.in); double Number,select; int counter = 0, num; String numb; ArrayList list = new ArrayList(); Stack stack = new Stack(); Queue queue = new PriorityQueue(); TreeSet setList = new TreeSet(); System.out.println("Please select the data type of the elements: (Enter 1 for double, Enter 2 for interger, Enter 3 string)"); select = scanner.nextDouble(); if(select == 1){ System.out.println("Please enter the number of element you wish to enter: "); select = scanner.nextDouble(); while (counter < select){ System.out.println("Please enter a number: "); Number = scanner.nextDouble(); list.add(Number); stack.push(Number); queue.offer(Number); setList.add(Number); counter++; } System.out.println("\n numbers in the order entered are as follow: "); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } System.out.println("\n\n The numbers in reverse order as entered as follow: "); while (!stack.empty()) { System.out.print(stack.peek() + " "); stack.pop(); } System.out.println("\n\nThe numbers in ascending order is as follow: "); while (queue.size() > 0) { System.out.print(queue.peek() + " "); queue.poll(); } System.out.println("\n\nThe numbers in ascending order with no duplicates is as follow: "); while (!setList.isEmpty()) { System.out.print(setList.first() + " "); setList.remove(setList.first()); } } else if(select == 2){ System.out.println("Please enter the number of element you wish to enter: "); num = scanner.nextInt(); counter = 0; while (counter < num){ System.out.println("Please enter a number: "); num = scanner.nextInt(); list.add(num); stack.push(num); queue.offer(num); setList.add(num); counter++; } System.out.println("\n numbers in the order entered are as follow: "); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } System.out.println("\n\n The numbers in reverse order as entered as follow: "); while (!stack.empty()) { System.out.print(stack.peek() + " "); stack.pop(); } System.out.println("\n\nThe numbers in ascending order is as follow: "); while (queue.size() > 0) { System.out.print(queue.peek() + " "); queue.poll(); } System.out.println("\n\nThe numbers in ascending order with no duplicates is as follow: "); while (!setList.isEmpty()) { System.out.print(setList.first() + " "); setList.remove(setList.first()); } } else { System.out.println("Please enter the number of element you wish to enter: "); num = scanner.nextInt(); counter = 0; while (counter < num){ System.out.println("Please enter a number: "); numb = scanner.next(); list.add(numb); stack.push(numb); queue.offer(numb); setList.add(numb); counter++; } System.out.println("\n numbers in the order entered are as follow: "); for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } System.out.println("\n\n The numbers in reverse order as entered as follow: "); while (!stack.empty()) { System.out.print(stack.peek() + " "); stack.pop(); } System.out.println("\n\nThe numbers in ascending order is as follow: "); while (queue.size() > 0) { System.out.print(queue.peek() + " "); queue.poll(); } System.out.println("\n\nThe numbers in ascending order with no duplicates is as follow: "); while (!setList.isEmpty()) { System.out.print(setList.first() + " "); setList.remove(setList.first()); } } } }