Lab 8
Lab Description: In this lab, I had to create a program to analyze data that a user input. First i would ask the user to say what kind of elements they will give and then they will input their elements. The program will organize their elements for them.
CODE:
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); System.out.print("Type of Elements of Analyze (1 = String, 2 = Double, 3 = Integer) "); int link = 0; link = input.nextInt(); if (link == 1) { int a = 0; System.out.println("You have selected String. "); System.out.print("Enter Amount of Strings: "); int b = input.nextInt(); Stackstack = new Stack (); LinkedList list1 = new LinkedList (); LinkedList list2 = new LinkedList (); TreeSet treeset = new TreeSet (); while(a < b) { System.out.print("Strings: "); String c = input.next(); list1.addFirst(c); stack.push(c); treeset.add(c); a++; } list2.addAll(list1); Collections.sort(list2); System.out.println("Strings Entered in Order: " + stack); System.out.println("Strings Entered in Reversed Order: " + list1); System.out.println("Strings Entered in Ascending Order: " + list2); } if (link == 2) { int a = 0; System.out.println("You have selected Double. "); System.out.print("Enter Amount of Numbers: "); int b = input.nextInt(); Stack stack = new Stack (); LinkedList list1 = new LinkedList (); LinkedList list2 = new LinkedList (); TreeSet treeset = new TreeSet (); while(a < b) { System.out.print("Numbers: "); double c = input.nextDouble(); list1.addFirst(c); stack.push(c); treeset.add(c); a++; } list2.addAll(list1); Collections.sort(list2); System.out.println("Numbers Entered in Order: " + stack); System.out.println("Numbers Entered in Reversed Order: " + list1); System.out.println("Numbers Entered in Ascending Order: " + list2); } if (link == 3) { int a = 0; System.out.println("You have selected Integer. "); System.out.print("Enter the Amount of Numbers: "); int b = input.nextInt(); Stack stack = new Stack (); LinkedList list1 = new LinkedList (); LinkedList list2 = new LinkedList (); TreeSet treeset = new TreeSet (); while(a < b) { System.out.print("Numbers: "); Integer c = input.nextInt(); list1.addFirst(c); stack.push(c); treeset.add(c); a++; } list2.addAll(list1); Collections.sort(list2); System.out.println("Numbers Entered in Order: " + stack); System.out.println("Numbers Entered in Reversed Order: " + list1); System.out.println("Numbers Entered in Ascending Order: " + list2); } } }