Description:
The main purpose of this lab is to teach us how to create a program to analyze data that the user inputs. We had to use four function, asking for input of different numbers and then the code would have to put them in how they were entered, reverse order, and then inĀ ascendingĀ order. according to the lab the program had to have two menus first one asking user what kind of data they would like to input (string,double, or integer). second menu would ask how many numbers would the user like to input. once the user had entered all the number they would get a result like the one shown in the screen shot.
Code:
package lab8; import java.util.LinkedList; import java.util.Scanner; import java.util.Stack; import java.util.TreeSet; import java.util.Collections; public class lab8 { private static Scanner input; public static void main(String[] args) { input = new Scanner(System.in); Stack stack = new Stack(); LinkedList list1 = new LinkedList(); LinkedList list2 = new LinkedList(); TreeSet treeset = new TreeSet(); System.out.print("Choose the opration you would like to use (1 = String) (2 = Double) (3 = Integer) "); int link = 0; link = input.nextInt(); if (link == 1) { int x = 0; System.out.println("You have choosen the option String. "); System.out.print("How many strings would you like to use: "); int y = input.nextInt(); while(x < y) { System.out.print("Strings: "); String c = input.next(); list1.addFirst(c); stack.push(c); treeset.add(c); x++; } list2.addAll(list1); Collections.sort(list2); System.out.println("Strings in order they were entered: " + stack); System.out.println("Strings in Reversed Order: " + list1); System.out.println("Strings in Ascending Order: " + list2); } if (link == 2) { int x = 0; System.out.println("You have choosen the option Double. "); System.out.print("How many numbers would you like to use: "); int y = input.nextInt(); while(x < y) { System.out.print("Numbers: "); double c = input.nextDouble(); list1.addFirst(c); stack.push(c); treeset.add(c); x++; } list2.addAll(list1); Collections.sort(list2); System.out.println("Numbers in order they were entered: " + stack); System.out.println("Numbers in Reversed Order: " + list1); System.out.println("Numbers in Ascending Order: " + list2); } if (link == 3) { int x = 0; System.out.println("You have choosen the option Integer. "); System.out.print("How many numbers would you like to use: "); int y = input.nextInt(); while(x < y) { System.out.print("Numbers: "); Integer c = input.nextInt(); list1.addFirst(c); stack.push(c); treeset.add(c); x++; } list2.addAll(list1); Collections.sort(list2); System.out.println("Numbers in order they were entered: " + stack); System.out.println("Numbers in Reversed Order: " + list1); System.out.println("Numbers in Ascending Order: " + list2); } } }
Screenshot: