Lab5(Data Structures)

Lab Description:
The main objective of this lab is to create a data structure that asks the user to choose from three options that are double, string, and integer. It then should display an output that shows in ascending order, reverse order, no repetition, and in order they were entered. For this, I had to import Stack, treeset, List, Linkedlist, and Collection from the API. I used the if and else and than for each specific instance changes the scanner to double, string, and integer. I than used the while command and increment with the push, add, and addfirst. Also you have to know that when you are entering numbers, make sure that the input is actually translated into a double, string, and integer by using the “nextDouble”, “nextInt”, and “nextLine”.

Code:


import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;
import java.util.TreeSet;
import java.util.List;
import java.util.Collections;

public class DataStructure {
	/**
	 * @param args
	 */
	private static Scanner scanner;

	public static void main(String[] args) {
		double Number, select;
		scanner = new Scanner(System.in);
		System.out
		.println("Enter Type of elements:\n 1)double\n 2)interger\n 3)string");
		select = scanner.nextDouble();

		if (select == 1) {
			// TODO Auto-generated method stub
			Scanner input = new Scanner(System.in);
			Stack s = new Stack();
			LinkedList r = new LinkedList();
			LinkedList r2 = new LinkedList();
			TreeSet d = new TreeSet();

			double b = 0;
			double f = 0;

			System.out.print("How many Numbers would you like to enter?");
			f = scanner.nextDouble();

			while (b < f) {
				System.out.print("Please Enter those Numbers");
				double num = scanner.nextDouble();

				r.addFirst(num);
				s.push(num);
				d.add(num);

				b++;
			}

			r2.addAll(r);
			Collections.sort(r2);
			System.out.printf("numbers in the order they were entered:%s\n", s);
			System.out.printf("numbers in reversed Order:%s\n", r);
			System.out.printf("numbers in ascending order:%s\n", r2);
			System.out.printf("no repetition number entered:%s\n", d);

		}

		if (select == 2) {
			// TODO Auto-generated method stub
			Scanner input = new Scanner(System.in);
			Stack s = new Stack();
			LinkedList r = new LinkedList();
			LinkedList r2 = new LinkedList();
			TreeSet d = new TreeSet();

			int b = 0;
			int f = 0;

			System.out.print("How many Numbers would you like to enter?");
			f = input.nextInt();

			while (b < f) {
				System.out.print("Please Enter those Numbers");
				Integer num = input.nextInt();

				r.addFirst(num);
				s.push(num);
				d.add(num);

				b++;
			}

			r2.addAll(r);
			Collections.sort(r2);
			System.out.printf("numbers in the order they were entered:%s\n", s);
			System.out.printf("numbers in reversed Order:%s\n", r);
			System.out.printf("numbers in ascending order:%s\n", r2);
			System.out.printf("no repetition number entered:%s\n", d);

		}

		if (select == 3) {
			// TODO Auto-generated method stub
			Scanner input = new Scanner(System.in);
			Stack s = new Stack();
			LinkedList r = new LinkedList();
			LinkedList r2 = new LinkedList();
			TreeSet d = new TreeSet();

			int b = 0;
			double f = 0;

			System.out.print("How many Numbers would you like to enter?");
			f = scanner.nextDouble();

			while (b < f) {
				System.out.print("Please Enter those Numbers");
				Integer num = input.nextInt();

				r.addFirst(num);
				s.push(num);
				d.add(num);

				b++;
			}

			r2.addAll(r);
			Collections.sort(r2);
			System.out.printf("numbers in the order they were entered:%s\n", s);
			System.out.printf("numbers in reversed Order:%s\n", r);
			System.out.printf("numbers in ascending order:%s\n", r2);
			System.out.printf("no repetition number entered:%s\n", d);

		}
	}
}

Screenshots:
datastruc

Leave a Reply