A City Tech OpenLab ePortfolio

LAB 5 – DATA STRUCTURES

LAB 5 – Data Structures

Lab Description:

In this lab, we will create a small program to analyze different data with the use of different data structures depending on the desired purpose. It will begin by asking the user if it wants to enter a String, Double, or Integer data type and how many elements it wants to analyze. It will then begin analyzing the data by displaying it in 1) Order the were entered, 2) In the reverse order they were entered, 3) In ascending order, and 4) displaying the elements only once with no repetitions. Each will have to implement a different data structure as it seems appropriate to get the desired result and mention which data structure was used on the output of each particular case.

 

Code:
Movable() Interface

import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Stack;
import java.util.TreeSet;
public class Elements {

	public static void main(String[] args) {
		int dSelection = 0, nElements = 0, counter, nInteger;
		double nDouble;
		boolean fValid = false;
		String word;
		Scanner input = new Scanner(System.in);
		boolean valid1 = true;

	do{	
		do{
			valid1 = true;
			System.out.println("Data type?");
			System.out.println("(1)String (2)Double (3)Integer (4) Exit");
			try {
				dSelection = input.nextInt();
				while(dSelection < 1 || dSelection > 4){
					System.out.println("Invalid range of input");
					throw new InputMismatchException();
					}
				if(dSelection == 4){
					break;
				}
				}catch(InputMismatchException e) {
					System.out.println("Value must be an integer");
					input.nextLine();
					valid1 = false;
				}

		}while(!valid1);

		if (dSelection == 4){
			fValid = true;
			break;
		}	

		boolean valid2 = true;
			do{
				valid2 = true;
				System.out.println("How many elements would you like to process?");
				try{
					nElements = input.nextInt();
					while (nElements < 1) {
						System.out.print("Value must be positive");
						throw new InputMismatchException();
					}
					}catch(InputMismatchException e) {
						System.out.println("Value must be an Integer");
						input.nextLine(); //clear input value
						valid2 = false;
					}

			}while(!valid2);
			input.nextLine();		

			//String Selection code//

				if (dSelection == 1){

				/*  String array initialization*/
				String[] sArray = new String[nElements];
				ArrayList sArrayList = new ArrayList();
				Stack rStack = new Stack ();
				TreeSet sTreeSet = new TreeSet();

				for(counter = 0; counter < nElements; counter++) {
					System.out.println("Enter string: ");
					word = input.nextLine();
					sArray[counter] = word;
					rStack.push(word);
					sArrayList.add(word);
					sTreeSet.add(word);
				}

				//Element Print statements
				System.out.println("Elements in order: (Using ArrayList)");
				for(String item : sArrayList){
					System.out.printf(" %s", item);
				}
				System.out.println();
				System.out.println("Elements in reverse order: (Using Stack)");
				for(counter = 0; counter < nElements; counter++) {
					System.out.printf(" %s", rStack.pop());
				}
				System.out.println();
				System.out.println("Elements in ascending order: (Using Array sort)");
				Arrays.sort(sArray);
				for(String item : sArray){
					System.out.printf(" %s", item);
				}
				System.out.println();
				System.out.println("Elements with no repetitions: (Using TreeSet)");
				for(String item : sTreeSet){
					System.out.printf(" %s", item);
				}
			}

			//Double Selection code

			else if (dSelection == 2){

				/*  Double array initialization*/
				Double[] sArray = new Double[nElements];
				ArrayList sArrayList = new ArrayList();
				Stack rStack = new Stack ();
				TreeSet sTreeSet = new TreeSet();

				for(counter = 0; counter < nElements; counter++) {
					System.out.println("Enter Double: ");
					nDouble = input.nextDouble();
					sArray[counter] = nDouble;
					rStack.push(nDouble);
					sArrayList.add(nDouble);
					sTreeSet.add(nDouble);
				}

				//Element Print statements
				System.out.println("Elements in order: (Using ArrayList)");
				for(Double item : sArrayList){
					System.out.printf(" %s", item);
				}
				System.out.println();
				System.out.println("Elements in reverse order: (Using Stack)");
				for(counter = 0; counter < nElements; counter++) {
					System.out.printf(" %s", rStack.pop());
				}
				System.out.println();
				System.out.println("Elements in ascending order: (Using Array sort)");
				Arrays.sort(sArray);
				for(Double item : sArray){
					System.out.printf(" %s", item);
				}
				System.out.println();
				System.out.println("Elements with no repetitions: (Using TreeSet)");
				for(Double item : sTreeSet){
					System.out.printf(" %s", item);
				}
			}

			//Integer Selection code

			else if (dSelection == 3){

				/*  Integer array initialization*/
				Integer[] sArray = new Integer[nElements];
				ArrayList sArrayList = new ArrayList();
				Stack rStack = new Stack ();
				TreeSet sTreeSet = new TreeSet();

				for(counter = 0; counter < nElements; counter++) {
					System.out.println("Enter Integer: ");
					nInteger = input.nextInt();
					sArray[counter] = nInteger;
					rStack.push(nInteger);
					sArrayList.add(nInteger);
					sTreeSet.add(nInteger);
				}

				//Element Print statements
				System.out.println("Elements in order: (Using ArrayList)");
				for(Integer item : sArrayList){
					System.out.printf(" %s", item);
				}
				System.out.println();
				System.out.println("Elements in reverse order: (Using Stack)");
				for(counter = 0; counter < nElements; counter++) {
					System.out.printf(" %s", rStack.pop());
				}
				System.out.println();
				System.out.println("Elements in ascending order: (Using Array sort)");
				Arrays.sort(sArray);
				for(Integer item : sArray){
					System.out.printf(" %s", item);
				}
				System.out.println();
				System.out.println("Elements with no repetitions: (Using TreeSet)");
				for(Integer item : sTreeSet){
					System.out.printf(" %s", item);
				}
			}

			System.out.println();
			boolean valid3;		//Boolean used for do...while loop to catch exception in repeat simulation question.
			do {
				try {
					valid3 = true;
					System.out.println("Again? (Yes = 1 / No = 2)");
					int again = input.nextInt();

					/*  If selection statement to test for end of simulation or repeat*/
		            if (again == 1)			
		            	fValid = true;
		            else if (again == 2)
						fValid = false;
		            else if (again != 1 | again != 2 )
		                throw new InputMismatchException();
					}
					catch ( InputMismatchException nfe ) {		//Valid Input Check
						System.out.println( "Not a matching input" );
						valid3 = false;
						input.nextLine();	
						}

				}while (!valid3);	//End of do..while loop used for try/catch exception in end or repeat question

	}while (fValid);		//End of do...while loop used to Start Over Program

	System.out.println("Thank you!");

	}
}

Screenshot:
screenshot

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *