A City Tech OpenLab ePortfolio

Lab 5

Lab Description:
In this lab, we are asked to create a program that are able to analyze data. First we needed to make a menu that ask the user to pick one of the elements type(string, double or integer). After that it should prompt and ask the user how many elements the user want to process. After getting the date from the user, the program should be able to analyze by displaying the data in the order they were entered, in the reverse order they were entered, in ascending order, and display the elements only once. Lastly, we needed to include which data structure we used for each particular case.

Code:

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 {

	private static Scanner input;

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

		do{	
			do{
				try {
					valid1 = true;
					System.out.println("Data type?");
					System.out.println("(1)String (2)Double (3)Integer (4) Exit");
					dSelection = input.nextInt();

					if (dSelection = 4)
						valid1 = true;
					else if (dSelection  4){
						throw new NumberFormatException();
					}

				}catch (InputMismatchException nfe) {		// Number Check Exception
					System.out.println( "Invalid input. Please try again using only number." ); 
					valid1 = false;
					input.next();
				}
				catch (NumberFormatException nfe) {			// Range check exception
					System.out.println("Number is out of range. Please try again.");
					valid1 = false;
					input.nextLine();
				}

			}while(!valid1);

			if (dSelection == 4){
				System.out.println("Thank you for using this App. Hope you have fun with it. :)");
				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)
					{
						System.out.println("Thank you for using this App. Hope you have fun with it. :)");
						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

	}
}	

Screenshot:
Lab 5

Leave a Reply

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