Skip to content

Categories:

Lab 5

In this lab we are asked to create a program that will accomplish the following two things:

1. Ask the user the type of element (String, Double, or Integer)

2. Ask the user how many elements they want to enter.

When the user is finished entering the data, it will :

Display all the elements in the order they were entered.
Display all the elements in the reverse order they were entered.
Display all the elements in ascending order.
Display the elements only once (i.e. no repetitions).

Program

import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.TreeSet;

public class Program {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Scanner input = new Scanner(System.in);
		String dataType = null;
		int numberOfData = 0;
		
		int[] intArray = null;
		String[] stringArray = null;
		double[] doubleArray = null; 
		
		System.out.println("Application menu");
		System.out.println("----------------");
		System.out.println("Select the data type as follows...");
		System.out.println("s - String");
		System.out.println("d - double");
		System.out.println("i - integer");
		System.out.println("q - quit");
		
		while(true) {
			System.out.print("Enter the data type that you are going to enter : ");
			dataType = input.nextLine().toLowerCase().trim();
			if(dataType.equals("s") || dataType.equals("d") || dataType.equals("i")) {
				break;
			}else if(dataType.equals("q")){
				System.out.println("System is exiting");
				System.exit(0);
			}else {
				System.out.println("Incorrect input... please enter a valid input...");
			}
		}
		while(true){
			System.out.print("Enter the number of data that you are going to use : ");
			try{
				numberOfData = input.nextInt();
				if(dataType.equals("s")) {
					stringArray = new String[numberOfData];
				}else if(dataType.equals("i")) {
					intArray = new int[numberOfData];
				}else if(dataType.equals("d")) {
					doubleArray = new double[numberOfData];
				}
				break;
			}catch(InputMismatchException e) {
				System.out.println("Incorrect data...Please enter an integer...");
			}
		}
		
		if(dataType.equals("s")) {
			for(int i = 0; i < numberOfData; i++) {
				System.out.print("Enter your data number " + (i+1) + " :");
				stringArray[i] = input.nextLine();
			}
		}else if(dataType.equals("d")) {
			for(int i = 0; i < numberOfData; i++) {
				System.out.print("Enter your data number " + (i+1) + " :");
				try{
					doubleArray[i] = input.nextDouble();
				}catch(InputMismatchException e) {
					System.out.print("You have entered a wrong input... please enter a double...");
					i--;
				}
			}
		}else if(dataType.equals("i")) {
			for(int i = 0; i < numberOfData; i++) {
				System.out.print("Enter your data number " + (i+1) + " :");
				try{
					intArray[i] = input.nextInt();
				}catch(InputMismatchException e) {
					System.out.print("You have entered a wrong input... please enter an integer...");
					i--;
				}
			}
		}
		
		System.out.println("Printing row data...");
		if(stringArray != null) {
			for(int i = 0; i < numberOfData; i++) {
				System.out.print(stringArray[i] + " ");
			}
			System.out.println();
		}else if(intArray != null) {
			for(int i = 0; i < numberOfData; i++) {
				System.out.print(intArray[i]+ " ");
			}
			System.out.println();
		}else if(doubleArray != null) {
			for(int i = 0; i = 0; i--) {
				System.out.print(stringArray[i]+ " ");
			}
			System.out.println();
		}else if(intArray != null) {
			for(int i = numberOfData-1; i >= 0; i--) {
				System.out.print(intArray[i]+ " ");
			}
			System.out.println();
		}else if(doubleArray != null) {
			for(int i = numberOfData-1; i >= 0; i--) {
				System.out.print(doubleArray[i]+ " ");
			}
			System.out.println();
		}
		System.out.println("Used the same list and reversed the data...");
		
		System.out.println("Printing Sorted list...");
		if(stringArray != null) {
			Arrays.sort(stringArray);
			for(int i = 0; i < numberOfData; i++) {
				System.out.print(stringArray[i] + " ");
			}
			System.out.println();
		}else if(intArray != null) {
			Arrays.sort(intArray);
			for(int i = 0; i < numberOfData; i++) {
				System.out.print(intArray[i]+ " ");
			}
			System.out.println();
		}else if(doubleArray != null) {
			Arrays.sort(doubleArray);
			for(int i = 0; i < numberOfData; i++) {
				System.out.print(doubleArray[i]+ " ");
			}
			System.out.println();
		}
		System.out.println("Used arrays.sort() method to sort the array");
		
		System.out.println("Avoid duplicates...");
		if(stringArray != null) {
			TreeSetset=new TreeSet();
			for(int i = 0; i < numberOfData; i++) {
				set.add(stringArray[i]);
			}
			System.out.println(set);
		}else if(intArray != null) {
			TreeSetset=new TreeSet();
			for(int i = 0; i < numberOfData; i++) {
				set.add(intArray[i]);
			}
			System.out.println(set);
		}else if(doubleArray != null) {
			TreeSetset=new TreeSet();
			for(int i = 0; i < numberOfData; i++) {
				set.add(doubleArray[i]);
			}
			System.out.println(set);
		}
		System.out.println("Used tree set to avoid duplicates...");
		System.out.println("End of the program...");
	}
}

Screenshots

lab5screenshot

lab5screenshot

lab5screenshot


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.